PooledList<T> Class

Summary

Implements a variable-size list that uses a pooled array to store the elements. A PooledList has a capacity, which is the allocated length of the internal array. As elements are added to a PooledList, the capacity of the PooledList is automatically increased as required by reallocating the internal array.
Namespace
Avalonia.Collections.Pooled
Interfaces
  • IList<T>
  • ICollection<T>
  • IReadOnlyPooledList<T>
  • IReadOnlyList<T>
  • IReadOnlyCollection<T>
  • IEnumerable<T>
  • IList
  • ICollection
  • IEnumerable
  • IDisposable
  • IDeserializationCallback
Base Types
  • object
graph BT Type-->Base0["object"] Type-.->Interface0["IList<T>"] Type-.->Interface1["ICollection<T>"] Type-.->Interface2["IReadOnlyPooledList<T>"] Type-.->Interface3["IReadOnlyList<T>"] Type-.->Interface4["IReadOnlyCollection<T>"] Type-.->Interface5["IEnumerable<T>"] Type-.->Interface6["IList"] Type-.->Interface7["ICollection"] Type-.->Interface8["IEnumerable"] Type-.->Interface9["IDisposable"] Type-.->Interface10["IDeserializationCallback"] Type["PooledList<T>"] class Type type-node

Syntax

[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(ICollectionDebugView<>))]
[Serializable]
public class PooledList<T> : IList<T>, ICollection<T>, IReadOnlyPooledList<T>, IReadOnlyList<T>, 
    IReadOnlyCollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable, IDisposable, 
    IDeserializationCallback

Remarks

This class is based on the code for List<T> but it supports System.Span`1 and uses ArrayPool<T> when allocating internal arrays.

Attributes

Type Description
DebuggerDisplayAttribute
DebuggerTypeProxyAttribute
SerializableAttribute

Type Parameters

Name Description
T

Constructors

Name Summary
PooledList() Constructs a PooledList. The list is initially empty and has a capacity of zero. Upon adding the first element to the list the capacity is increased to DefaultCapacity, and then increased in multiples of two as required.
PooledList(ArrayPool<T>) Constructs a PooledList. The list is initially empty and has a capacity of zero. Upon adding the first element to the list the capacity is increased to DefaultCapacity, and then increased in multiples of two as required.
PooledList(ClearMode) Constructs a PooledList. The list is initially empty and has a capacity of zero. Upon adding the first element to the list the capacity is increased to DefaultCapacity, and then increased in multiples of two as required.
PooledList(ClearMode, ArrayPool<T>) Constructs a PooledList. The list is initially empty and has a capacity of zero. Upon adding the first element to the list the capacity is increased to DefaultCapacity, and then increased in multiples of two as required.
PooledList(IEnumerable<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(IEnumerable<T>, ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(IEnumerable<T>, ClearMode) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(IEnumerable<T>, ClearMode, ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(int) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ArrayPool<T>) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ArrayPool<T>, bool) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, bool) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ClearMode) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ClearMode, ArrayPool<T>) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ClearMode, ArrayPool<T>, bool) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(int, ClearMode, bool) Constructs a List with a given initial capacity. The list is initially empty, but will have room for the given number of elements before any reallocations are required.
PooledList(ReadOnlySpan<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(ReadOnlySpan<T>, ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(ReadOnlySpan<T>, ClearMode) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(ReadOnlySpan<T>, ClearMode, ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(T[]) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(T[], ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(T[], ClearMode) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.
PooledList(T[], ClearMode, ArrayPool<T>) Constructs a PooledList, copying the contents of the given collection. The size and capacity of the new list will both be equal to the size of the given collection.

Properties

Name Value Summary
Capacity int
Gets and sets the capacity of this list. The capacity is the size of the internal array used to hold items. When set, the internal Memory of the list is reallocated to the given capacity. Note that the return value for this property may be larger than the property was set to.
ClearMode ClearMode
Returns the ClearMode behavior for the collection, denoting whether values are cleared from internal arrays before returning them to the pool.
Count int
Read-only property describing how many elements are in the List.
Span Span<T>
Gets a Span<T> for the items currently in the collection.
this[int] T
Gets or sets the element at the given index.

Methods

Name Value Summary
Add(T) void
Adds the given object to the end of this list. The size of the list is increased by one. If required, the capacity of the list is doubled before adding the new element.
AddRange(IEnumerable<T>) void
Adds the elements of the given collection to the end of this list. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger.
AddRange(ReadOnlySpan<T>) void
Adds the elements of the given ReadOnlySpan<T> to the end of this list. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger.
AddRange(T[]) void
Adds the elements of the given array to the end of this list. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger.
AddSpan(int) Span<T>
Advances the Count by the number of items specified, increasing the capacity if required, then returns a Span representing the set of items to be added, allowing direct writes to that section of the collection.
AsReadOnly() ReadOnlyCollection<T>
BinarySearch(int, int, T, IComparer<T>) int
Searches a section of the list for a given element using a binary search algorithm.
BinarySearch(T) int
Searches the list for a given element using a binary search algorithm. If the item implements System.IComparable`1 then that is used for comparison, otherwise System.Collections.Generic.Comparer`1.Default is used.
BinarySearch(T, IComparer<T>) int
Searches the list for a given element using a binary search algorithm. If the item implements System.IComparable`1 then that is used for comparison, otherwise System.Collections.Generic.Comparer`1.Default is used.
Clear() void
Clears the contents of the PooledList.
Contains(T) bool
Contains returns true if the specified element is in the List. It does a linear, O(n) search. Equality is determined by calling EqualityComparer{T}.Default.Equals.
ConvertAll<TOutput>(Func<T, TOutput>) PooledList<TOutput>
CopyTo(Span<T>) void
Copies this list to the given span.
Dispose() void
Returns the internal buffers to the ArrayPool.
Exists(Func<T, bool>) bool
FindAll(Func<T, bool>) PooledList<T>
FindIndex(Func<T, bool>) int
FindIndex(int, Func<T, bool>) int
FindIndex(int, int, Func<T, bool>) int
FindLastIndex(Func<T, bool>) int
FindLastIndex(int, Func<T, bool>) int
FindLastIndex(int, int, Func<T, bool>) int
ForEach(Action<T>) void
GetEnumerator() PooledList<T>.Enumerator
Returns an enumerator for this list with the given permission for removal of elements. If modifications made to the list while an enumeration is in progress, the MoveNext and GetObject methods of the enumerator will throw an exception.
GetRange(int, int) Span<T>
Equivalent to PooledList.Span.Slice(index, count).
IndexOf(T) int
Returns the index of the first occurrence of a given value in this list. The list is searched forwards from beginning to end.
IndexOf(T, int) int
Returns the index of the first occurrence of a given value in a range of this list. The list is searched forwards, starting at index index and ending at count number of elements.
IndexOf(T, int, int) int
Returns the index of the first occurrence of a given value in a range of this list. The list is searched forwards, starting at index index and upto count number of elements.
Insert(int, T) void
Inserts an element into this list at a given index. The size of the list is increased by one. If required, the capacity of the list is doubled before inserting the new element.
InsertRange(int, IEnumerable<T>) void
Inserts the elements of the given collection at a given index. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger. Ranges may be added to the end of the list by setting index to the List's size.
InsertRange(int, ReadOnlySpan<T>) void
Inserts the elements of the given collection at a given index. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger. Ranges may be added to the end of the list by setting index to the List's size.
InsertRange(int, T[]) void
Inserts the elements of the given collection at a given index. If required, the capacity of the list is increased to twice the previous capacity or the new size, whichever is larger. Ranges may be added to the end of the list by setting index to the List's size.
InsertSpan(int, int) Span<T>
Advances the Count by the number of items specified, increasing the capacity if required, then returns a Span representing the set of items to be added, allowing direct writes to that section of the collection.
LastIndexOf(T) int
Returns the index of the last occurrence of a given value in a range of this list. The list is searched backwards, starting at the end and ending at the first element in the list.
LastIndexOf(T, int) int
Returns the index of the last occurrence of a given value in a range of this list. The list is searched backwards, starting at index index and ending at the first element in the list.
LastIndexOf(T, int, int) int
Returns the index of the last occurrence of a given value in a range of this list. The list is searched backwards, starting at index index and upto count elements
Remove(T) bool
RemoveAll(Func<T, bool>) int
This method removes all items which match the predicate. The complexity is O(n).
RemoveAt(int) void
Removes the element at the given index. The size of the list is decreased by one.
RemoveRange(int, int) void
Removes a range of elements from this list.
Reverse() void
Reverses the elements in this list.
Reverse(int, int) void
Reverses the elements in a range of this list. Following a call to this method, an element in the range given by index and count which was previously located at index i will now be located at index index + (index + count - i - 1).
Sort() void
Sorts the elements in this list. Uses the default comparer and Array.Sort.
Sort(Func<T, T, int>) void
Sort(IComparer<T>) void
Sorts the elements in this list. Uses Array.Sort with the provided comparer.
Sort(int, int, IComparer<T>) void
Sorts the elements in a section of this list. The sort compares the elements to each other using the given IComparer interface. If comparer is null, the elements are compared to each other using the IComparable interface, which in that case must be implemented by all elements of the list. This method uses the Array.Sort method to sort the elements.
ToArray() T[]
ToArray returns an array containing the contents of the List. This requires copying the List, which is an O(n) operation.
TrimExcess() void
Sets the capacity of this list to the size of the list. This method can be used to minimize a list's memory overhead once it is known that no new elements will be added to the list. To completely clear a list and release all memory referenced by the list, execute the following statements:
list.Clear();
list.TrimExcess();
TrueForAll(Func<T, bool>) bool
TryFind(Func<T, bool>, T) bool
TryFindLast(Func<T, bool>, T) bool