@@ -8,23 +8,39 @@ the two as needed. See the [Small Capacity Limit](#small-capacity-limit) for
88details on this storage mechanism.
99
1010``` zig
11+ // We'll just use SmallArrayList for this example, but there are variants
12+ // that allow further parameterization.
13+ const SmallArrayList = @import("small_array_list").SmallArrayList;
14+
15+ // Setup whichever allocator you'd like to use.
16+ var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
17+ defer arena.deinit();
18+ const allocator = arena.allocator();
19+
1120var list: SmallArrayList(i32) = .empty;
1221defer list.deinit(allocator);
1322
14- list.append(allocator, 1);
15- list.append(allocator, 2);
16- list.append(allocator, 3);
23+ // Append works just like the std.array_list.ArrayListUnmanaged append.
24+ try list.append(allocator, 1);
1725
18- std.debug.print("len={} capacity={}\n", .{ list.len, list.capacity });
19- // On 64-bit systems, this will be: len=3 capacity=4
26+ // Nearly all of the standard library methods are available.
27+ var many = try list.addManyAsArray(allocator, 2);
28+ many[0] = 2;
29+ many[1] = 3;
2030
31+ // Unlike ArrayList, items cannot be accessed directly as a slice.
2132for (list.items()) |item| {
22- std.debug.print("item={}\n", .{ item });
33+ std.debug.print("item={}\n", .{item});
2334}
2435
36+ // You can see if the SmallArrayList is using an allocation or not.
2537if (!list.hasAllocation()) {
2638 std.debug.print("no allocations!\n", .{});
2739}
40+
41+ // Unlike ArrayList, len is a top-level field.
42+ // On 64-bit systems, this will be: len=3 capacity=4
43+ std.debug.print("len={} capacity={}\n", .{ list.len, list.capacity });
2844```
2945
3046> [ !IMPORTANT]
@@ -34,6 +50,52 @@ if (!list.hasAllocation()) {
3450> 1 . To get the number of items in the list, use ` list.len ` instead of ` list.items.len ` .
3551> 1 . To get the items of in the list, use ` list.items() ` instead of ` list.items ` .
3652
53+ ## Installing
54+
55+ First, add the dependency to your ` build.zig.zon ` :
56+
57+ ``` sh
58+ zig fetch --save git+https://github.com/kalamay/small-array-list#v0.1.1
59+ ```
60+
61+ Next add the dependecy to your ` build.zig ` :
62+
63+ ``` zig
64+ const small_array_list_mod = b.dependency("small_array_list", .{
65+ .target = target,
66+ .optimize = optimize,
67+ }).module("small_array_list");
68+
69+ exe_mod.addImport("small_array_list", small_array_list_mod);
70+ // or lib_mod, or any other module
71+ ```
72+
73+ Now you can import the library:
74+
75+ ``` zig
76+ const small_array_list = @import("small_array_list");
77+ ```
78+
79+ ### Using a Different Name
80+
81+ If you'd like to use a different name within your project, you can choose a
82+ different import name for the module:
83+
84+ ``` zig
85+ const small_array_list_mod = b.dependency("small_array_list", .{
86+ .target = target,
87+ .optimize = optimize,
88+ }).module("small_array_list");
89+
90+ exe_mod.addImport("array", small_array_list_mod);
91+ ```
92+
93+ And then you'd be able to import it as ` "array" ` :
94+
95+ ``` zig
96+ const array = @import("array");
97+ ```
98+
3799## Small Capacity Limit
38100
39101The available small capacity limit is determined by the type stored and the
0 commit comments