arena.zig (899B)
1 const std = @import("std"); 2 const tree = @import("tree.zig"); 3 4 pub const Arena = struct { 5 allocator: std.mem.Allocator, 6 nodes: std.ArrayList(tree.Node), 7 8 pub fn init(allocator: std.mem.Allocator) Arena { 9 return .{ 10 .allocator = allocator, 11 .nodes = .empty, 12 }; 13 } 14 15 pub fn deinit(self: *Arena) void { 16 self.nodes.deinit(self.allocator); 17 } 18 19 pub fn alloc(self: *Arena, node: tree.Node) !u32 { 20 const idx: u32 = @intCast(self.nodes.items.len); 21 try self.nodes.append(self.allocator, node); 22 return idx; 23 } 24 25 pub fn get(self: *Arena, idx: u32) *tree.Node { 26 return &self.nodes.items[idx]; 27 } 28 29 pub fn len(self: *const Arena) u32 { 30 return @intCast(self.nodes.items.len); 31 } 32 33 pub fn reset(self: *Arena, keep: u32) void { 34 self.nodes.shrinkRetainingCapacity(keep); 35 } 36 };