First pass implementation of Slab collection
This commit is contained in:
parent
a0c30163a2
commit
b1739f0cf8
|
@ -0,0 +1,56 @@
|
||||||
|
const debug = @import("./debug.zig");
|
||||||
|
|
||||||
|
const io = @import("./io.zig");
|
||||||
|
|
||||||
|
// TODO: Finish implementing.
|
||||||
|
|
||||||
|
pub fn Map(comptime Index: type, comptime Element: type) type {
|
||||||
|
return struct {
|
||||||
|
free_index: Index,
|
||||||
|
entries: []Entry,
|
||||||
|
|
||||||
|
const Entry = union (enum) {
|
||||||
|
free_index: usize,
|
||||||
|
element: Element,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn fetch(self: *Self, index: Index) *Element {
|
||||||
|
const entry = &self.entries[index];
|
||||||
|
|
||||||
|
debug.assert(entry.* == .element);
|
||||||
|
|
||||||
|
return &entry.element;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Self, allocator: io.Allocator) void {
|
||||||
|
io.deallocate(allocator, self.entries);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init(allocator: io.Allocator) io.AllocationError!Self {
|
||||||
|
const entries = try io.allocate_many(Entry, 4, allocator);
|
||||||
|
|
||||||
|
errdefer io.deallocate(allocator, entries);
|
||||||
|
|
||||||
|
return Self{
|
||||||
|
.free_index = 0,
|
||||||
|
.entries = entries,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(self: *Self, allocator: io.Allocator, value: Element) io.AllocationError!Index {
|
||||||
|
_ = self;
|
||||||
|
_ = allocator;
|
||||||
|
_ = value;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(self: *Self, index: Index) void {
|
||||||
|
const entry = &self.entries[index];
|
||||||
|
|
||||||
|
debug.assert(entry.* == .element);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue