RefList

Source: lib/RefList.cs

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

RefList: a List for references (string, classes, arrays), in an Arena.

RefList<string> names = RefList<string>.Create(keep);
names.Add(Str.ToArenaString(keep, line));   // the string itself must live on
foreach (string name in names) Out.Line(name);

List takes only unmanaged types, because it stores items in arena memory through a T*. A reference is just an address in zero mode (there is no GC to move objects or to find them), so this stores each one as an nint in a List and converts it back when read. It has List's behavior: a handle whose copies share contents, safe to use when not created, growth in place.

The list holds references, not the objects: the objects stay wherever they were allocated. Each one must outlive the list's use of it. A string built in a scratch arena dangles once that arena is reset, even if the list lives in another one, so copy it into a long-lived arena before adding it.

Members

Create

public static RefList<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 (null is allowed). 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)

RemoveAt

public bool RemoveAt(int index)

Clear

public void Clear()

Sort

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

Sorts the items in place, like List.Sort: 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. Heapsort, not stable.

static int ByName(string a, string b) => Str.Compare(a, b);
names.Sort(&ByName);

Clone

public RefList<T> Clone(Arena target)

Returns an independent copy of the list in target, or a not-created list if this one isn't created or the target is out of memory.

Only the references are copied: both lists point at the same objects, so the copy outlives the original's arena but not the objects' arenas.

GetEnumerator

public Enumerator GetEnumerator()

For foreach: every item, in order.

Don't Add or RemoveAt during the loop.