summaryrefslogtreecommitdiff
path: root/src/datastruct/array_list_collection.zig
blob: d3fbddb13e87fb9d1cfbba9a4a0043014f47c786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
            }
        }
    };
}