RefDict

Source: lib/RefDict.cs

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

RefDict<TKey, TValue>: a Dict whose values are references (string, classes, arrays), in an Arena. Keys are unmanaged, as in Dict; for string keys, use StrDict (lib/StrDict.cs).

RefDict<int, string> names = RefDict<int, string>.Create(keep);
names.Set(42, Str.ToArenaString(keep, line));
if (names.TryGet(42, out string name)) Out.Line(name);

Each value is stored as its address in a Dict<TKey, nint>, as RefList does; see RefList for why that works. The map holds references, not the objects: each one must outlive the map's use of it, so copy a string out of a scratch arena before storing it.

Members

Create

public static RefDict<TKey, TValue> Create(Arena arena, int capacity = 32)

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

IsCreated

public bool IsCreated

Count

public int Count

Set

public bool Set(TKey key, TValue value)

Inserts or overwrites (null is allowed). Returns false if the map isn't created or the arena is full.

TryGet

public bool TryGet(TKey key, out TValue value)

ContainsKey

public bool ContainsKey(TKey key)

Remove

public bool Remove(TKey key)

Removes a key. Returns false if it wasn't there.

Clone

public RefDict<TKey, TValue> Clone(Arena target)

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

Only the references are copied: both maps point at the same objects.

GetEnumerator

public Enumerator GetEnumerator()

For foreach: every key and value, in no particular order.

Don't Set or Remove during the loop: entries move, so some may be skipped or seen twice.

RefDict.Pair

A key with its value, as foreach yields them.

Key

public TKey Key

Value

public TValue Value