Vixen
02b45cc4
csharp
public struct PooledList<T>

A growable list over a pooled array, for the case a frame does constantly: collect an unknown number of things, use them, throw the buffer away.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Meant to be used as using var list = new PooledList<T>(64);. Disposal returns the buffer, and the same clearing policy as PooledArray applies.

It is a mutable struct, which brings the usual caveats. Copying one — assigning it, passing it by value, capturing it in a lambda — produces a second list over the same buffer whose Count then diverges, and disposing either one invalidates both. Pass it by ref, or hand out Span instead.

Fields and properties (5)

  • public readonly int Count

    How many elements the list holds.

  • public readonly int Capacity

    How many elements it can hold before the buffer is replaced.

  • public readonly bool IsEmpty

    Whether the list is empty.

  • public readonly Span<T> Span

    The elements, as a span. Invalidated by anything that grows the list.

  • public readonly ref T this[int index]

    A reference to the element at .

Methods (10)

  • public PooledList(int capacity)

    Creates a list with room for at least elements.

  • public void Add(T item)

    Appends an element.

  • public void AddRange(ReadOnlySpan<T> items)

    Appends a run of elements.

  • public Span<T> AppendSpan(int length)

    Extends the list by elements and returns them for the caller to fill, skipping the copy an AddRange from a temporary would cost.

  • public void RemoveAt(int index)

    Removes the element at , keeping the order of the rest.

  • public void RemoveAtSwapBack(int index)

    Removes the element at by moving the last one into its place. O(1), and the right choice whenever order does not matter.

  • public void Clear()

    Empties the list, keeping the buffer.

  • public readonly T[] ToArray()

    Copies the elements into a new array — the one place this type allocates.

  • public void Dispose()

    Returns the buffer to the pool. The list is empty and reusable afterwards.

  • public readonly Span<T>.Enumerator GetEnumerator()

    Enumerates the elements.

Used by (1)

  • PoolingTestsVixen.Core.Tests