List

Source: lib/List.cs

This page is generated from the source by scripts/gen-reference.py.

List: growable array backed by an Arena.

List is a handle (one pointer) to a header that lives in the arena, so copies of a List share the same contents, like passing List* in C. A default(List) is "not created": reads see an empty list and Add fails.

Growing extends the buffer in place while it is still the arena's most recent allocation; otherwise it copies into a fresh buffer and the old one stays allocated until the arena is reset. Out-of-range Get/Set fail fast.

Members

Create

public static List<T> Create(Arena arena, int initialCapacity = 16)

Returns a new list in arena, or a not-created list if it is out of memory.

IsCreated

public bool IsCreated

Count

public int Count

Capacity

public int Capacity

Add

public bool Add(T item)

Appends an item. Returns false if the list isn't created or the arena is full.

Get

public T Get(int index)

Set

public void Set(int index, T value)

GetPtr

public T* GetPtr(int index)

Pointer to an element. Invalidated by Add (growth) and RemoveAt.

RemoveAt

public bool RemoveAt(int index)

Clear

public void Clear()

AsSpan

public unsafe Span<T> AsSpan()

View of the current items.

Safety: The span is invalidated by Add (growth), RemoveAt, Clear and resetting the arena.

Sort

public void Sort(delegate*<T, T, int> compare)

Sorts the items in place. compare returns a negative number if its first argument goes before its second, a positive one if after, and zero if either order will do:

static int ByAge(Person a, Person b) => a.Age - b.Age;
people.Sort(&ByAge);

Heapsort: no extra memory, and n log n comparisons however the items start out. Not stable: items that compare equal may end up in either order.

Clone

public List<T> Clone(Arena target)

Returns an independent copy in target (this list's own arena or another one), with just enough capacity for the current items. Returns a not-created list if this one isn't created or the target is out of memory.

Items are copied as they are. An item that is, or contains, a pointer still points wherever it did: into the original's arena, if that's where it led.