Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Public static fields | Properties | Public Member Functions | List of all members
Loyc.Collections.Impl.InternalList< T > Struct Template Reference

A compact auto-enlarging array structure that is intended to be used within other data structures. It should only be used internally in "private" or "protected" members of low-level code. More...


Source file:
Inheritance diagram for Loyc.Collections.Impl.InternalList< T >:
Loyc.Collections.IListAndListSource< T > Loyc.Collections.IListRangeMethods< T > Loyc.ICloneable< out T > Loyc.Collections.IListSource< out T > Loyc.Collections.ICollectionAndReadOnly< T > Loyc.Collections.IAddRange< T > Loyc.Collections.ICount Loyc.Collections.IIsEmpty

Remarks

A compact auto-enlarging array structure that is intended to be used within other data structures. It should only be used internally in "private" or "protected" members of low-level code.

An article about this class is available.

InternalList is a struct, not a class, in order to save memory; and for maximum performance, it asserts rather than throwing an exception when an incorrect array index is used. Besides that, it has an InternalArray property that provides access to the internal array. For all these reasons one should not expose it in a public API, and it should only be used when performance trumps all other concerns.

Passing this structure by value is dangerous because changes to a copy of the structure may or may not be reflected in the original list. It's best not to pass it around at all, but if you must pass it, pass it by reference.

Also, do not use the default contructor. Always specify an initial capacity or copy InternalList.Empty so that _array gets a value. This is required because methods such as Add(), Insert() and Resize() assume _array is not null.

InternalList has one nice thing that List(of T) lacks: a Resize method and an equivalent Count setter. Which dork at Microsoft decided no one should be allowed to set the list length directly? This type also provides a handy Last property and a Pop method to respectively get or remove the last item.

Finally, alongside InternalList(T), the static class InternalList comes with some static methods (CopyToNewArray, Insert, RemoveAt, Move) to help manage raw arrays. You might want to use these in a data structure implementation even if you choose not to use InternalList(T) instances.

Public static fields

static readonly T[] EmptyArray = new T[0]
 
static readonly InternalList< T > Empty = new InternalList<T>(0)
 

Properties

int Count [get, set]
 
bool IsEmpty [get]
 
int Capacity [get, set]
 Gets or sets the array length. More...
 
this[int index] [get, set]
 
this[int index, T defaultValue] [get]
 
First [get, set]
 
Last [get, set]
 
bool IsReadOnly [get]
 
T[] InternalArray [get]
 
- Properties inherited from Loyc.Collections.ICount
int Count [get]
 Gets the number of items in the collection. More...
 
- Properties inherited from Loyc.Collections.IIsEmpty
bool IsEmpty [get]
 

Public Member Functions

 InternalList (int capacity)
 
 InternalList (T[] array, int count)
 
 InternalList (IEnumerable< T > items)
 
 InternalList (IEnumerator< T > items)
 
void AutoRaiseCapacity (int more, int capacityLimit)
 
void Resize (int newSize)
 Makes the list larger or smaller, depending on whether newSize is larger or smaller than Count. More...
 
void Resize (int newSize, bool allowReduceCapacity)
 
void Add (T item)
 
void AddRange (IEnumerator< T > items)
 
void Insert (int index, T item)
 
void InsertRange (int index, ICollectionAndReadOnly< T > items)
 
void InsertRange (int index, IReadOnlyCollection< T > items)
 
void InsertRange (int index, ICollection< T > items)
 
void InsertRange (int index, IEnumerable< T > e)
 
void AddRange (IReadOnlyCollection< T > items)
 
void AddRange (ICollection< T > items)
 
void AddRange (IEnumerable< T > e)
 
void AddRange (ICollectionAndReadOnly< T > items)
 
void Clear ()
 Clears the list and frees the memory used by the list. Can also be used to initialize a list whose constructor was never called. More...
 
void RemoveAt (int index)
 
void RemoveRange (int index, int count)
 
void Pop ()
 
InternalList< T > Clone ()
 Makes a copy of the list with the same capacity More...
 
InternalList< T > CloneAndTrim ()
 Makes a copy of the list with Capacity = Count More...
 
T[] ToArray ()
 Makes a copy of the list, as an array More...
 
int BinarySearch (T lookFor)
 
int BinarySearch (T lookFor, Comparer< T > comp)
 
int BinarySearch (T lookFor, Comparer< T > comp, bool lowerBound)
 
int BinarySearch< K > (K lookFor, Func< T, K, int > func, bool lowerBound)
 
void Move (int from, int to)
 Slides the array entry at [from] forward or backward in the list, until it reaches [to]. More...
 
int IndexOf (T item)
 
int IndexOf (T item, int index)
 
bool Contains (T item)
 
void CopyTo (T[] array, int arrayIndex)
 
bool Remove (T item)
 
System.Collections.IEnumerator
System.Collections.IEnumerable. 
GetEnumerator ()
 
IEnumerator< T > GetEnumerator ()
 
TryGet (int index, out bool fail)
 Gets the item at the specified index, and does not throw an exception on failure. More...
 
void Sort (Comparison< T > comp)
 
void Sort (int index, int count, Comparison< T > comp)
 
IRange< T > IListSource< T >. Slice (int start, int count)
 Returns a sub-range of this list. More...
 
Slice_< T > Slice (int start, int count)
 Returns a sub-range of this list. More...
 
InternalList< T > CopySection (int start, int subcount)
 

Member Function Documentation

Clears the list and frees the memory used by the list. Can also be used to initialize a list whose constructor was never called.

Makes a copy of the list with the same capacity

Implements Loyc.ICloneable< out T >.

InternalList<T> Loyc.Collections.Impl.InternalList< T >.CloneAndTrim ( )
inline

Makes a copy of the list with Capacity = Count

void Loyc.Collections.Impl.InternalList< T >.Move ( int  from,
int  to 
)
inline

Slides the array entry at [from] forward or backward in the list, until it reaches [to].

For example, if a list of integers is [0, 1, 2, 3, 4, 5] then Move(4,1) produces the following result: [0, 4, 1, 2, 3, 5].

void Loyc.Collections.Impl.InternalList< T >.Resize ( int  newSize)
inline

Makes the list larger or smaller, depending on whether newSize is larger or smaller than Count.

Parameters
allowReduceCapacityIf this is true, and the new size is smaller than one quarter the current Capacity, the array is reallocated to a smaller size. If this parameter is false, the array is never reallocated when shrinking the list.
newSizeNew value of Count. If the Count increases, copies of default(T) are added to the end of the the list; otherwise items are removed from the end of the list.

References Loyc.Collections.Impl.InternalList< T >.Resize().

Referenced by Loyc.Collections.Impl.InternalList< T >.Resize().

IRange<T> IListSource<T>. Loyc.Collections.Impl.InternalList< T >.Slice ( int  start,
int  count 
)
inline

Returns a sub-range of this list.

Parameters
startThe new range will start at this index in the current list (this location will be index [0] in the new range).
countThe desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list.
Returns
Returns a sub-range of this range.
Exceptions
ArgumentExceptionThe start index was below zero.

The (start, count) range is allowed to be invalid, as long as start is zero or above.

  • If count is below zero, or if start is above the original Count, the Count of the new slice is set to zero.
  • if (start + count) is above the original Count, the Count of the new slice is reduced to this.Count - start. Implementation note: do not compute (start + count) because it may overflow. Instead, test whether (count > this.Count - start).

Most collections should use the following implementation:

IRange<T> IListSource<T>.Slice(int start, int count) { return Slice(start, count); }
public Slice_<T> Slice(int start, int count) { return new Slice_<T>(this, start, count); }

Implements Loyc.Collections.IListSource< out T >.

Slice_<T> Loyc.Collections.Impl.InternalList< T >.Slice ( int  start,
int  count 
)
inline

Returns a sub-range of this list.

Parameters
startThe new range will start at this index in the current list (this location will be index [0] in the new range).
countThe desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list.
Returns
Returns a sub-range of this range.
Exceptions
ArgumentExceptionThe start index was below zero.

The (start, count) range is allowed to be invalid, as long as start is zero or above.

  • If count is below zero, or if start is above the original Count, the Count of the new slice is set to zero.
  • if (start + count) is above the original Count, the Count of the new slice is reduced to this.Count - start. Implementation note: do not compute (start + count) because it may overflow. Instead, test whether (count > this.Count - start).

Most collections should use the following implementation:

IRange<T> IListSource<T>.Slice(int start, int count) { return Slice(start, count); }
public Slice_<T> Slice(int start, int count) { return new Slice_<T>(this, start, count); }

Implements Loyc.Collections.IListSource< out T >.

T [] Loyc.Collections.Impl.InternalList< T >.ToArray ( )
inline

Makes a copy of the list, as an array

T Loyc.Collections.Impl.InternalList< T >.TryGet ( int  index,
out bool  fail 
)
inline

Gets the item at the specified index, and does not throw an exception on failure.

Parameters
indexAn index in the range 0 to Count-1.
failA flag that is set on failure.
Returns
The element at the specified index, or default(T) if the index is not valid.

In my original design, the caller could provide a value to return on failure, but this would not allow T to be marked as "out" in C# 4. For the same reason, we cannot have a ref/out T parameter. Instead, the following extension methods are provided:

bool TryGet(int index, ref T value);
T TryGet(int, T defaultValue);

Implements Loyc.Collections.IListSource< out T >.

Property Documentation

int Loyc.Collections.Impl.InternalList< T >.Capacity
getset

Gets or sets the array length.

Changing this property requires O(Count) time and temporary space. Attempting to set the capacity lower than Count has no effect.