diff options
| author | Alan Moyano <alanmoyano203@gmail.com> | 2025-06-29 15:59:29 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-29 15:59:29 -0300 |
| commit | 43a3338491b3f6c409606ebfaab3a38c96455144 (patch) | |
| tree | ef2dd3fa426b45307e376a5809d2d41d23afb26b /src/datastruct/array_list_collection.zig | |
| parent | cf7e76d8f23f7e048ff55fd4db5da7a08e5176c6 (diff) | |
| parent | 0d55a1deefa3f67abb2981cc83b46af5058789b5 (diff) | |
Merge branch 'ghostty-org:main' into main
Diffstat (limited to 'src/datastruct/array_list_collection.zig')
| -rw-r--r-- | src/datastruct/array_list_collection.zig | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/datastruct/array_list_collection.zig b/src/datastruct/array_list_collection.zig new file mode 100644 index 000000000..d3fbddb13 --- /dev/null +++ b/src/datastruct/array_list_collection.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +/// A collection of ArrayLists with methods for bulk operations. +pub fn ArrayListCollection(comptime T: type) type { + return struct { + const Self = ArrayListCollection(T); + const ArrayListT = std.ArrayListUnmanaged(T); + + // An array containing the lists that belong to this collection. + lists: []ArrayListT, + + // The collection 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 collection, retaining capacity. + pub fn reset(self: *Self) void { + for (self.lists) |*list| { + list.clearRetainingCapacity(); + } + } + }; +} |
