Enhanced C#
Language of your choice: library documentation
|
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...
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... | |
T | this[int index] [get, set] |
T | this[int index, T defaultValue] [get] |
T | First [get, set] |
T | 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 () |
T | 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) |
|
inline |
Clears the list and frees the memory used by the list. Can also be used to initialize a list whose constructor was never called.
|
inline |
Makes a copy of the list with the same capacity
Implements Loyc.ICloneable< out T >.
|
inline |
Makes a copy of the list with Capacity = Count
|
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].
|
inline |
Makes the list larger or smaller, depending on whether newSize
is larger or smaller than Count.
allowReduceCapacity | If 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. |
newSize | New 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().
|
inline |
Returns a sub-range of this list.
start | The new range will start at this index in the current list (this location will be index [0] in the new range). |
count | The desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list. |
ArgumentException | The start index was below zero. |
The (start, count) range is allowed to be invalid, as long as start is zero or above.
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 >.
|
inline |
Returns a sub-range of this list.
start | The new range will start at this index in the current list (this location will be index [0] in the new range). |
count | The desired number of elements in the new range, or int.MaxValue to get all elements until the end of the list. |
ArgumentException | The start index was below zero. |
The (start, count) range is allowed to be invalid, as long as start is zero or above.
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 >.
|
inline |
Makes a copy of the list, as an array
|
inline |
Gets the item at the specified index, and does not throw an exception on failure.
index | An index in the range 0 to Count-1. |
fail | A flag that is set on failure. |
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:
Implements Loyc.Collections.IListSource< out T >.
|
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.