Skip to content
CShift0.45

SlotMap<T>

Struct · namespace System · Since 0.42

struct SlotMap<T>

A container that gives its elements stable handles: Add returns a Handle that finds the element again until it is removed. Places of removed elements are reused, but a handle of a removed element is rejected (its generation no longer matches), so it can never reach the element that took its place. Adding, removing and looking up take constant time; the elements lie in one array.

using System;
struct Unit { string Name; int Hp; Handle<Unit> Target; }
var units = SlotMap<Unit>.Create();
var tank = units.Add(Unit { Name = "Tank", Hp = 100 });
var scout = units.Add(Unit { Name = "Scout", Hp = 30, Target = tank });
units.Remove(tank);
if (units.TryGet(units[scout].Target) is Unit target) // null: the tank is gone
Console.WriteLine(target.Name);

This is how things that are made and removed refer to each other without references (which CShift does not have) and without cycles of reference counts: units and their targets, nodes and their parents, entries of an undo history. Like List, a SlotMap is a handle to shared storage (copies see the same elements); the zero value is empty and cannot change. map[handle] reads (SlotMap<T>.Get) and writes (SlotMap<T>.Set) an element; foreach (var (handle, value) in map) goes through the elements.

Name Description
Create A new, empty map.
static SlotMap<T> Create()

A new, empty map.

static SlotMap<T> Create(int capacity)

A new, empty map with room for capacity elements before it has to grow.

Name Description
IsCreated Whether the map has storage: made by Create(), not the zero value (which cannot change).
Count The number of elements.
Add Adds value and returns its handle.
Contains Whether handle refers to an element of the map (false for a removed element and for Handle<T>.IsNone).
Get The element of handle (also map[handle]).
TryGet The element of handle, or null if there is none (it was removed).
Set Replaces the element of handle (also map[handle] = value).
Remove Removes the element of handle: its handles become stale and its place is used again by a later Add.
Clear Removes all elements (all handles become stale).
GetEnumerator A cursor over the elements for foreach, without allocating: foreach (var (handle, unit) in units).
Handles A new array with the handles of all elements, in the order of their places.
bool IsCreated()

Whether the map has storage: made by Create(), not the zero value (which cannot change).

int Count()

The number of elements.

Handle<T> Add(T value)

Adds value and returns its handle.

Panics when the map was not created (SlotMap<T>.IsCreated).

bool Contains(Handle<T> handle)

Whether handle refers to an element of the map (false for a removed element and for Handle<T>.IsNone).

T Get(Handle<T> handle)

The element of handle (also map[handle]).

Panics when the handle does not refer to an element (it was removed, or it is of no element).

Optional<T> TryGet(Handle<T> handle)

The element of handle, or null if there is none (it was removed).

void Set(Handle<T> handle, T value)

Replaces the element of handle (also map[handle] = value).

Panics when the handle does not refer to an element.

bool Remove(Handle<T> handle)

Removes the element of handle: its handles become stale and its place is used again by a later Add. Returns whether there was such an element.

void Clear()

Removes all elements (all handles become stale).

SlotMapEnumerator<T> GetEnumerator()

A cursor over the elements for foreach, without allocating: foreach (var (handle, unit) in units). The elements come in the order of their places; elements added or removed during the loop may or may not be visited.

Handle<T>[] Handles()

A new array with the handles of all elements, in the order of their places.