Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Protected Member Functions | List of all members
Loyc.Collections.AList< T > Class Template Reference

An all-purpose list structure with the following additional features beyond what's offered by List<T>: fast insertion and deletion (O(log N)), batch insertion and deletion, observability, fast cloning, freezability, and fast splitting and joining of large collections. More...


Source file:
Inheritance diagram for Loyc.Collections.AList< T >:
Loyc.Collections.AListBase< T > Loyc.Collections.IListEx< T > Loyc.Collections.IListRangeMethods< T > Loyc.Collections.IListAndListSource< T > Loyc.Collections.ICollectionEx< T > Loyc.Collections.IArray< T > Loyc.Collections.IListRangeMethods< T > Loyc.Collections.IArraySink< T > Loyc.Collections.IListSource< T > Loyc.Collections.IIsEmpty Loyc.Collections.IAddRange< T > Loyc.Collections.ICollectionImpl< T > Loyc.Collections.ICollectionAndSource< T > Loyc.Collections.IListSource< T > Loyc.Collections.IListAndReadOnly< T > Loyc.Collections.IndexedAList< T >

Remarks

An all-purpose list structure with the following additional features beyond what's offered by List<T>: fast insertion and deletion (O(log N)), batch insertion and deletion, observability, fast cloning, freezability, and fast splitting and joining of large collections.

An article about this class is available.

Template Parameters
TType of each element in the list

The name "A-List" is short for All-purpose List. It is so named because it has a very large amount of functionality and extension points for further extending this functionality. Essentially, this data structure is jack-of-all-trades, master of none.

Structurally, ALists (like BLists) are very similar to B+trees. They use memory almost as efficiently as arrays, and offer O(log N) insertion and deletion in exchange for a O(log N) indexer, which is distinctly slower than the indexer of List<T>. They use slightly more memory than List<T> for all list sizes.

That said, you should use an AList whenever you know that the list might be large and need insertions or deletions somewhere in the middle. If you expect to do insertions and deletions at random locations, but only occasionally, DList<T> is sometimes a better choice because it has a faster indexer. Both classes provide fast enumeration (O(1) per element), but DList<T> enumerators initialize faster.

Although AList isn't the fastest or smallest data structure for any single task, it is very useful when you need several different capabilities, and there are certain tasks for which it excels; for example, have you ever wanted to remove all items that meet certain criteria from a list? You cannot accomplish this with a foreach loop such as this:

foreach (T item in list) if (MeetsCriteria(item)) list.Remove(item); // Exception occurs! foreach loop cannot continue after Remove()!

When you are using a List<T>, you might try to solve this problem with a reverse for-loop such as this:

for (int i = list.Count - 1; i >= 0; i–) if (MeetsCriteria(list[i])) list.RemoveAt(i);

This works, but it runs in O(N^2) time, so it's very slow if the list is large. The easiest way to solve this problem that is also efficient is to duplicate all the items that you want to keep in a new list:

var list2 = new List<T>(); foreach (T item in list) if (!MeetsCriteria(item)) list2.Add(item); list = list2;

But what if you didn't think of that solution and already wrote the O(N^2) version? There's a lot of code out there already that relies on slow List<T> operations. An easy way to solve performance caused by poor use of List<T> is simply to add "A" in front. AList is pretty much a drop-in replacement for List, so you can convert O(N^2) into faster O(N log N) code simply by using an AList instead of a List.

I like to think of AList as the ultimate novice data structure. Novices like indexed lists, although for many tasks they are not the most efficient choice. AList isn't optimized for any particular task, but it isn't downright slow for any task except IndexOf, so it's very friendly to novices that don't know about all the different types of data structures and how to choose one. Don't worry about it! Just pick AList. It's also a good choice when you're just too busy to think about performance, such as in a scripting environment.

Plus, you can subscribe to the AListBase<K,T>.ListChanging event to find out when the list changes. AList's observability is more lightweight than that of ObservableCollection{T}.

Although single insertions, deletions, and random access require O(log N) time, you can get better performance using any overload of InsertRange or AListBase<K,T>.RemoveRange. These methods require only O(log N + M) time, where M is the number of elements you are inserting, removing or enumerating.

AList is an excellent choice if you need to make occasional snapshots of the tree. Cloning is fast and memory-efficient, because none of the tree is copied at first. The root node is simply marked as frozen, and nodes are duplicated on-demand as changes are made. Thus, AList can be used as a so-called "persistent" data structure, but it is fairly expensive to clone the tree after every modification. When modifying a tree that was just cloned (remember, AList is really a tree), the leaf node being changed and all of its ancestors must be duplicated. Therefore, it's better if you can arrange to have a high ratio of changes to clones.

AList is also freezable, which is useful if you need to construct a list in a read-only or freezable data structure. You could also freeze the list in order to return a read-only copy of it, which, compared to cloning, has the advantage that no memory allocation is required at the time you return the list. If you need to edit the list later, you can clone the list (the clone can be modified).

As explained in the documentation of AListBase<T>, this class is NOT multithread-safe. Multiple concurrent readers are allowed, as long as the collection is not modified, so frozen instances ARE multithread-safe.

See also
BList<T>, BDictionary<K,V>, DList<T>

Properties

sealed override T? this[int index] [get, set]
 
- Properties inherited from Loyc.Collections.IListEx< T >
new int Count [get]
 
- Properties inherited from Loyc.Collections.IIsEmpty
bool IsEmpty [get]
 
- Properties inherited from Loyc.Collections.IArray< T >
new T this[int index] [get, set]
 Gets or sets an element of the array-like collection. More...
 
- Properties inherited from Loyc.Collections.IArraySink< T >
this[int index] [set]
 

Public Member Functions

 AList (IEnumerable< T > items)
 
 AList (IListSource< T > items)
 
 AList (int maxNodeSize)
 
 AList (int maxLeafSize, int maxInnerSize)
 
 AList (AList< T > items, bool keepListChangingHandlers)
 
sealed override void Add (T item)
 
void AddRange (IEnumerable< T > list)
 
void InsertRange (int index, AList< T > source)
 
void InsertRange (int index, AList< T > source, bool move)
 
sealed override void Insert (int index, T item)
 
sealed override void InsertRange (int index, IEnumerable< T > list)
 
sealed override void InsertRange (int index, IListSource< T > source)
 
sealed override bool TrySet (int index, T value)
 
new AList< T > Clone ()
 
AList< T > Clone (bool keepListChangingHandlers)
 
AList< T > CopySection (int start, int subcount)
 
new AList< T > RemoveSection (int start, int count)
 
void Swap (AList< T > other)
 Swaps the contents of two AList<T>s in O(1) time. More...
 
virtual void Append (AList< T > other)
 
virtual void Append (AList< T > other, bool move)
 Appends another AList to this list in sublinear time. More...
 
virtual void Prepend (AList< T > other)
 Prepends an AList to this list in sublinear time. More...
 
virtual void Prepend (AList< T > other, bool move)
 Prepends an AList to this list in sublinear time. More...
 
void Sort ()
 Uses a specialized "tree quicksort" algorithm to sort this list using Comparer<T>.Default. More...
 
void Sort (Comparer< T > comp)
 Uses a specialized "tree quicksort" algorithm to sort this list using the specified Comparer<T>. More...
 
void Sort (Comparison< T > comp)
 Uses a specialized "tree quicksort" algorithm to sort this list using the specified Comparison<T>. More...
 
void Sort (int start, int subcount, Comparison< T > comp)
 
- Public Member Functions inherited from Loyc.Collections.IListSource< T >
IRange< T > Slice (int start, int count=int.MaxValue)
 Returns a sub-range of this list. More...
 
- Public Member Functions inherited from Loyc.Collections.ICollectionSource< T >
void CopyTo (T[] array, int arrayIndex)
 Copies the elements of the collection to an Array, starting at a particular array index. More...
 
- Public Member Functions inherited from Loyc.Collections.IContains< T >
bool Contains (T item)
 Returns true if and only if the collection contains the specified item. More...
 
- Public Member Functions inherited from Loyc.Collections.ICollectionSink< T >
void Clear ()
 
bool Remove (T item)
 
- Public Member Functions inherited from Loyc.Collections.IAddRange< T >
void AddRange (IEnumerable< T > e)
 
void AddRange (IReadOnlyCollection< T > s)
 
- Public Member Functions inherited from Loyc.Collections.IListRangeMethods< T >
void InsertRange (int index, IEnumerable< T > s)
 
void InsertRange (int index, IReadOnlyCollection< T > s)
 
void RemoveRange (int index, int amount)
 

Protected Member Functions

 AList (AListBase< int, T > original, AListNode< int, T > section)
 
override AListNode< int, T > NewRootLeaf ()
 
override void Clone (out AListBase< T > clone)
 
override AListBase< T > cov_RemoveSection (int start, int count)
 
void Sort (uint start, uint subcount, Comparison< T > comp)
 
virtual void SortCore (uint start, uint subcount, Comparison< T > comp)
 
void ForceThaw (uint start, uint subcount)
 

Member Function Documentation

◆ Append()

virtual void Loyc.Collections.AList< T >.Append ( AList< T >  other,
bool  move 
)
inlinevirtual

Appends another AList to this list in sublinear time.

Parameters
otherA list of items to be added to this list.
moveIf this parameter is true, items from the other list are transferred to this list, causing the other list to be cleared. This parameter does not affect the speed of this method itself, but if you use "true" then future modifications to the combined list may be faster. If this parameter is "false" then it will be necessary to freeze the contents of the other list so that both lists can share the same tree nodes. Using "true" instead avoids the freeze operation, which in turn avoids the performance penalty on future modifications.

The default value of the 'move' parameter is false.

When the 'source' list is short, this method doesn't perform any better than a standard AddRange() operation (in fact, the operation is delegated to InsertRange()). However, when 'source' has several hundred or thousand items, the append/prepend operation is performed in roughly O(log N) time where N is the combined list size.

Parts of the tree that end up shared between this list and the other list will be frozen. Frozen parts of the tree must be cloned in order to be modified, which will slow down future operations on the tree. In order to avoid this problem, use move semantics (which clears the other list).

◆ Prepend() [1/2]

virtual void Loyc.Collections.AList< T >.Prepend ( AList< T >  other)
inlinevirtual

Prepends an AList to this list in sublinear time.

Parameters
otherA list of items to be added to the front of this list (at index 0).

◆ Prepend() [2/2]

virtual void Loyc.Collections.AList< T >.Prepend ( AList< T >  other,
bool  move 
)
inlinevirtual

Prepends an AList to this list in sublinear time.

Parameters
otherA list of items to be added to the front of this list (at index 0).

◆ Sort() [1/4]

void Loyc.Collections.AList< T >.Sort ( )
inline

Uses a specialized "tree quicksort" algorithm to sort this list using Comparer<T>.Default.

Referenced by Loyc.Collections.AList< T >.Sort().

◆ Sort() [2/4]

void Loyc.Collections.AList< T >.Sort ( Comparer< T >  comp)
inline

Uses a specialized "tree quicksort" algorithm to sort this list using the specified Comparer<T>.

References Loyc.Collections.AList< T >.Sort().

◆ Sort() [3/4]

void Loyc.Collections.AList< T >.Sort ( Comparison< T >  comp)
inline

Uses a specialized "tree quicksort" algorithm to sort this list using the specified Comparison<T>.

References Loyc.Collections.AList< T >.Sort().

◆ Sort() [4/4]

void Loyc.Collections.AList< T >.Sort ( int  start,
int  subcount,
Comparison< T >  comp 
)
inline
Parameters
startIndex of first item in a range of items to sort.
subcountSize of the range of items to sort.

References Loyc.Collections.AList< T >.Sort().

◆ Swap()

void Loyc.Collections.AList< T >.Swap ( AList< T >  other)
inline

Swaps the contents of two AList<T>s in O(1) time.

Any observers are also swapped.