Pool

Source: lib/Pool.cs

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

Pool: items in an Arena that can be removed one by one, found again by a Handle, and that notice when a handle is out of date.

var nodes = Pool<TreeNode>.Create(arena);
Handle<TreeNode> root = nodes.Add(new TreeNode { Key = 5 });
nodes[root].Left = nodes.Add(new TreeNode { Key = 3 });
nodes.Remove(root);
nodes[root].Key = 1;       // stops the program: root was removed

A handle is its pool's id, a slot number and the slot's generation (12 bytes, and a plain unmanaged struct, so items can hold handles to each other: trees, graphs, lists with cycles). Removing an item bumps its slot's generation, so every copy of the old handle is then refused, even after the slot holds a new item: a handle that is out of date stops the program instead of reaching the wrong item. Contains and TryGet ask without stopping.

Move hands an item to a new owner: the item stays, but it gets a new handle and every copy of the old one is refused from then on. MoveTo does the same into another pool, for example from a ScratchPool into one you keep.

The items live in chunks that never move, so a ref from poolhandle stays good while the pool grows. After Remove, a later Add can put a new item in the same slot: take the ref again rather than keep it across a Remove. Like every collection, the pool goes when its arena is reset, restored to before it or destroyed; default(Pool) is "not created": Add returns a null handle and lookups find nothing.

A handle used with another pool than its own is always refused: each pool gets an id of its own when it is created, and its handles carry it.

Items in an arena that can be removed one by one, each found by a Handle{T}.

Members

Create

public static Pool<T> Create(Arena arena, int chunkSize = 64)

Returns a new pool in arena that grows by chunks of chunkSize items (rounded up to a power of two), or a not-created pool if the arena is out of memory.

IsCreated

public bool IsCreated

False for default(...): a pool that was never created.

Count

public int Count

How many items the pool holds.

Add

public Handle<T> Add(T item)

Adds an item and returns its handle, or a null handle if the pool isn't created or the arena is out of memory. Reuses a removed item's slot.

this[]

public ref T this[Handle<T> handle]

The item handle finds, to read or change in place. A null handle, or one whose item was removed or moved, stops the program.

Contains

public bool Contains(Handle<T> handle)

Whether handle finds an item: not null, not removed, not moved.

TryGet

public bool TryGet(Handle<T> handle, out T item)

The item handle finds, if it finds one.

Remove

public bool Remove(Handle<T> handle)

Removes the item handle finds; every copy of the handle is refused from then on. False if it finds none.

Move

public Handle<T> Move(ref Handle<T> handle)

Gives the item a new handle and returns it, and sets handle to null: it and every other copy of it are refused from then on, so only the new owner can reach the item. A handle that finds nothing stops the program.

MoveTo

public Handle<T> MoveTo(Pool<T> target, ref Handle<T> handle)

Moves the item into target and returns its handle there; it is removed from this pool and handle is set to null. If the target is full, returns a null handle and changes nothing. A handle that finds nothing stops the program.

Clear

public void Clear()

Removes every item; every handle is refused from then on.

GetEnumerator

public Enumerator GetEnumerator()

For foreach: the handle of each item, in slot order.

Handle

Finds an item in a Pool{T}; refused once the item is removed or moved.

IsNull

public bool IsNull

True for the default handle, which finds nothing.

Equals

public bool Equals(Handle<T> other)

Whether both are the same handle (the same pool, slot and generation).

public override bool Equals(object obj)

Whether obj is the same handle.

GetHashCode

public override int GetHashCode()

A hash of the pool, slot and generation, matching Equals.

operator ==

public static bool operator ==(Handle<T> left, Handle<T> right)

Whether both are the same handle.

operator !=

public static bool operator !=(Handle<T> left, Handle<T> right)

Whether they are different handles.