std.pool
std.pool: generational handles (Example 3 of the design doc). A `Pool(T)` keeps values in slots, and a `Handle(T)` is a slot index plus the slot's generation when the value was inserted. Removing a value bumps the slot's generation, so `get` with a stale handle returns `null` instead of reading whatever the slot holds now.
Handle
pub fn Handle(comptime T: type) type {
return struct {
index: u32
generation: u32
}
}
Names a value of a `Pool(T)`.
Pool
pub fn Pool(comptime T: type) type {
return struct {
slots: []mut Slot(T)
free_list: ?u32
alloc: Allocator
len: u32
capacity: u32
pub fn init(alloc: Allocator, capacity: u32) Self
pub fn deinit(self: *mut Self)
pub fn insert(self: *mut Self, value: T) AllocError!Handle(T)
pub fn remove(self: *mut Self, h: Handle(T))
pub fn get(self: *Self, h: Handle(T)) ?*mut T
}
}
Values of type `T` in slots that are reused after `remove`.
pub fn init(alloc: Allocator, capacity: u32) Self-
An empty pool. Allocates nothing yet: the first `insert` allocates `capacity` slots (at least 1) and fills the spare ones with copies of its value; later the slots double.
pub fn deinit(self: *mut Self)-
Frees the slots.
pub fn insert(self: *mut Self, value: T) AllocError!Handle(T)-
Stores `value` in a free slot (or a new one) and returns its handle.
pub fn remove(self: *mut Self, h: Handle(T))-
Removes the value of `h`; its slot is reused. A stale handle does nothing.
pub fn get(self: *Self, h: Handle(T)) ?*mut T-
A pointer to the value of `h`; null for a stale handle. Valid until the next insert.