| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("index.zig"); | 1 | const std = @import("index.zig"); |
| 2 | const debug = std.debug; | 2 | const debug = std.debug; |
| 3 | const assert = debug.assert; | 3 | const assert = debug.assert; |
| | 4 | const assertError = debug.assertError; |
| 4 | const mem = std.mem; | 5 | const mem = std.mem; |
| 5 | const Allocator = mem.Allocator; | 6 | const Allocator = mem.Allocator; |
| 6 | | 7 | |
| ... | @@ -28,25 +29,33 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -28,25 +29,33 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 28 | }; | 29 | }; |
| 29 | } | 30 | } |
| 30 | | 31 | |
| 31 | pub fn deinit(l: *const Self) void { | 32 | pub fn deinit(self: *const Self) void { |
| 32 | l.allocator.free(l.items); | 33 | self.allocator.free(self.items); |
| 33 | } | 34 | } |
| 34 | | 35 | |
| 35 | pub fn toSlice(l: *const Self) []align(A) T { | 36 | pub fn toSlice(self: *const Self) []align(A) T { |
| 36 | return l.items[0..l.len]; | 37 | return self.items[0..self.len]; |
| 37 | } | 38 | } |
| 38 | | 39 | |
| 39 | pub fn toSliceConst(l: *const Self) []align(A) const T { | 40 | pub fn toSliceConst(self: *const Self) []align(A) const T { |
| 40 | return l.items[0..l.len]; | 41 | return self.items[0..self.len]; |
| 41 | } | 42 | } |
| 42 | | 43 | |
| 43 | pub fn at(l: *const Self, n: usize) T { | 44 | pub fn at(self: *const Self, n: usize) T { |
| 44 | return l.toSliceConst()[n]; | 45 | return self.toSliceConst()[n]; |
| 45 | } | 46 | } |
| 46 | | 47 | |
| 47 | pub fn set(self: *const Self, n: usize, item: *const T) !void { | 48 | /// Sets the value at index `i`, or returns `error.OutOfBounds` if |
| 48 | if (n >= self.len) return error.OutOfBounds; | 49 | /// the index is not in range. |
| 49 | self.items[n] = item.*; | 50 | pub fn setOrError(self: *const Self, i: usize, item: *const T) !void { |
| | 51 | if (i >= self.len) return error.OutOfBounds; |
| | 52 | self.items[i] = item.*; |
| | 53 | } |
| | 54 | |
| | 55 | /// Sets the value at index `i`, asserting that the value is in range. |
| | 56 | pub fn set(self: *const Self, i: usize, item: *const T) void { |
| | 57 | assert(i < self.len); |
| | 58 | self.items[i] = item.*; |
| 50 | } | 59 | } |
| 51 | | 60 | |
| 52 | pub fn count(self: *const Self) usize { | 61 | pub fn count(self: *const Self) usize { |
| ... | @@ -72,58 +81,58 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -72,58 +81,58 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 72 | return result; | 81 | return result; |
| 73 | } | 82 | } |
| 74 | | 83 | |
| 75 | pub fn insert(l: *Self, n: usize, item: *const T) !void { | 84 | pub fn insert(self: *Self, n: usize, item: *const T) !void { |
| 76 | try l.ensureCapacity(l.len + 1); | 85 | try self.ensureCapacity(self.len + 1); |
| 77 | l.len += 1; | 86 | self.len += 1; |
| 78 | | 87 | |
| 79 | mem.copy(T, l.items[n + 1 .. l.len], l.items[n .. l.len - 1]); | 88 | mem.copy(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]); |
| 80 | l.items[n] = item.*; | 89 | self.items[n] = item.*; |
| 81 | } | 90 | } |
| 82 | | 91 | |
| 83 | pub fn insertSlice(l: *Self, n: usize, items: []align(A) const T) !void { | 92 | pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void { |
| 84 | try l.ensureCapacity(l.len + items.len); | 93 | try self.ensureCapacity(self.len + items.len); |
| 85 | l.len += items.len; | 94 | self.len += items.len; |
| 86 | | 95 | |
| 87 | mem.copy(T, l.items[n + items.len .. l.len], l.items[n .. l.len - items.len]); | 96 | mem.copy(T, self.items[n + items.len .. self.len], self.items[n .. self.len - items.len]); |
| 88 | mem.copy(T, l.items[n .. n + items.len], items); | 97 | mem.copy(T, self.items[n .. n + items.len], items); |
| 89 | } | 98 | } |
| 90 | | 99 | |
| 91 | pub fn append(l: *Self, item: *const T) !void { | 100 | pub fn append(self: *Self, item: *const T) !void { |
| 92 | const new_item_ptr = try l.addOne(); | 101 | const new_item_ptr = try self.addOne(); |
| 93 | new_item_ptr.* = item.*; | 102 | new_item_ptr.* = item.*; |
| 94 | } | 103 | } |
| 95 | | 104 | |
| 96 | pub fn appendSlice(l: *Self, items: []align(A) const T) !void { | 105 | pub fn appendSlice(self: *Self, items: []align(A) const T) !void { |
| 97 | try l.ensureCapacity(l.len + items.len); | 106 | try self.ensureCapacity(self.len + items.len); |
| 98 | mem.copy(T, l.items[l.len..], items); | 107 | mem.copy(T, self.items[self.len..], items); |
| 99 | l.len += items.len; | 108 | self.len += items.len; |
| 100 | } | 109 | } |
| 101 | | 110 | |
| 102 | pub fn resize(l: *Self, new_len: usize) !void { | 111 | pub fn resize(self: *Self, new_len: usize) !void { |
| 103 | try l.ensureCapacity(new_len); | 112 | try self.ensureCapacity(new_len); |
| 104 | l.len = new_len; | 113 | self.len = new_len; |
| 105 | } | 114 | } |
| 106 | | 115 | |
| 107 | pub fn shrink(l: *Self, new_len: usize) void { | 116 | pub fn shrink(self: *Self, new_len: usize) void { |
| 108 | assert(new_len <= l.len); | 117 | assert(new_len <= self.len); |
| 109 | l.len = new_len; | 118 | self.len = new_len; |
| 110 | } | 119 | } |
| 111 | | 120 | |
| 112 | pub fn ensureCapacity(l: *Self, new_capacity: usize) !void { | 121 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { |
| 113 | var better_capacity = l.items.len; | 122 | var better_capacity = self.items.len; |
| 114 | if (better_capacity >= new_capacity) return; | 123 | if (better_capacity >= new_capacity) return; |
| 115 | while (true) { | 124 | while (true) { |
| 116 | better_capacity += better_capacity / 2 + 8; | 125 | better_capacity += better_capacity / 2 + 8; |
| 117 | if (better_capacity >= new_capacity) break; | 126 | if (better_capacity >= new_capacity) break; |
| 118 | } | 127 | } |
| 119 | l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity); | 128 | self.items = try self.allocator.alignedRealloc(T, A, self.items, better_capacity); |
| 120 | } | 129 | } |
| 121 | | 130 | |
| 122 | pub fn addOne(l: *Self) !*T { | 131 | pub fn addOne(self: *Self) !*T { |
| 123 | const new_length = l.len + 1; | 132 | const new_length = self.len + 1; |
| 124 | try l.ensureCapacity(new_length); | 133 | try self.ensureCapacity(new_length); |
| 125 | const result = &l.items[l.len]; | 134 | const result = &self.items[self.len]; |
| 126 | l.len = new_length; | 135 | self.len = new_length; |
| 127 | return result; | 136 | return result; |
| 128 | } | 137 | } |
| 129 | | 138 | |
| ... | @@ -164,13 +173,14 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -164,13 +173,14 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 164 | } | 173 | } |
| 165 | | 174 | |
| 166 | test "basic ArrayList test" { | 175 | test "basic ArrayList test" { |
| 167 | var list = ArrayList(i32).init(debug.global_allocator); | 176 | var bytes: [1024]u8 = undefined; |
| | 177 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; |
| | 178 | |
| | 179 | var list = ArrayList(i32).init(allocator); |
| 168 | defer list.deinit(); | 180 | defer list.deinit(); |
| 169 | | 181 | |
| 170 | // setting on empty list is out of bounds | 182 | // setting on empty list is out of bounds |
| 171 | list.set(0, 1) catch |err| { | 183 | assertError(list.setOrError(0, 1), error.OutOfBounds); |
| 172 | assert(err == error.OutOfBounds); | | |
| 173 | }; | | |
| 174 | | 184 | |
| 175 | { | 185 | { |
| 176 | var i: usize = 0; | 186 | var i: usize = 0; |
| ... | @@ -210,17 +220,13 @@ test "basic ArrayList test" { | ... | @@ -210,17 +220,13 @@ test "basic ArrayList test" { |
| 210 | | 220 | |
| 211 | list.appendSlice([]const i32{}) catch unreachable; | 221 | list.appendSlice([]const i32{}) catch unreachable; |
| 212 | assert(list.len == 9); | 222 | assert(list.len == 9); |
| 213 | | 223 | |
| 214 | // can only set on indices < self.len | 224 | // can only set on indices < self.len |
| 215 | list.set(7, 33) catch unreachable; | 225 | list.set(7, 33); |
| 216 | list.set(8, 42) catch unreachable; | 226 | list.set(8, 42); |
| 217 | | 227 | |
| 218 | list.set(9, 99) catch |err| { | 228 | assertError(list.setOrError(9, 99), error.OutOfBounds); |
| 219 | assert(err == error.OutOfBounds); | 229 | assertError(list.setOrError(10, 123), error.OutOfBounds); |
| 220 | }; | | |
| 221 | list.set(10, 123) catch |err| { | | |
| 222 | assert(err == error.OutOfBounds); | | |
| 223 | }; | | |
| 224 | | 230 | |
| 225 | assert(list.pop() == 42); | 231 | assert(list.pop() == 42); |
| 226 | assert(list.pop() == 33); | 232 | assert(list.pop() == 33); |