diff --git a/source/coral/slab.zig b/source/coral/slab.zig new file mode 100644 index 0000000..09c994d --- /dev/null +++ b/source/coral/slab.zig @@ -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); + } + }; +}