diff options
| author | Qwerasd <qwerasd205@users.noreply.github.com> | 2025-06-23 12:24:30 -0600 |
|---|---|---|
| committer | Qwerasd <qwerasd205@users.noreply.github.com> | 2025-06-23 13:06:41 -0600 |
| commit | 7eb3e813dd1334283436623744518142915cce4a (patch) | |
| tree | b648c7087e0fb059516f22dd703fb01bf4e1982d /src/datastruct | |
| parent | 4b01cc1d8880c69cd9db06568be5c5fa55893660 (diff) | |
datastruct: move ArrayListPool from renderer/cell.zig
Diffstat (limited to 'src/datastruct')
| -rw-r--r-- | src/datastruct/array_list_pool.zig | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/datastruct/array_list_pool.zig b/src/datastruct/array_list_pool.zig new file mode 100644 index 000000000..72cf8d29f --- /dev/null +++ b/src/datastruct/array_list_pool.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +/// A pool of ArrayLists with methods for bulk operations. +pub fn ArrayListPool(comptime T: type) type { + return struct { + const Self = ArrayListPool(T); + const ArrayListT = std.ArrayListUnmanaged(T); + + // An array containing the lists that belong to this pool. + lists: []ArrayListT = &[_]ArrayListT{}, + + // The pool will be initialized with empty ArrayLists. + pub fn init( + alloc: Allocator, + list_count: usize, + initial_capacity: usize, + ) Allocator.Error!Self { + const self: Self = .{ + .lists = try alloc.alloc(ArrayListT, list_count), + }; + + for (self.lists) |*list| { + list.* = try .initCapacity(alloc, initial_capacity); + } + + return self; + } + + pub fn deinit(self: *Self, alloc: Allocator) void { + for (self.lists) |*list| { + list.deinit(alloc); + } + alloc.free(self.lists); + } + + /// Clear all lists in the pool. + pub fn reset(self: *Self) void { + for (self.lists) |*list| { + list.clearRetainingCapacity(); + } + } + }; +} |
