| ... | @@ -44,6 +44,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { | ... | @@ -44,6 +44,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 44 | return l.toSliceConst()[n]; | 44 | return l.toSliceConst()[n]; |
| 45 | } | 45 | } |
| 46 | | 46 | |
| | 47 | pub fn set(self: *const Self, n: usize, item: *const T) !void { |
| | 48 | if (n >= self.len) return error.OutOfBounds; |
| | 49 | self.items[n] = item.*; |
| | 50 | } |
| | 51 | |
| 47 | pub fn count(self: *const Self) usize { | 52 | pub fn count(self: *const Self) usize { |
| 48 | return self.len; | 53 | return self.len; |
| 49 | } | 54 | } |
| ... | @@ -162,6 +167,11 @@ test "basic ArrayList test" { | ... | @@ -162,6 +167,11 @@ test "basic ArrayList test" { |
| 162 | var list = ArrayList(i32).init(debug.global_allocator); | 167 | var list = ArrayList(i32).init(debug.global_allocator); |
| 163 | defer list.deinit(); | 168 | defer list.deinit(); |
| 164 | | 169 | |
| | 170 | // setting on empty list is out of bounds |
| | 171 | list.set(0, 1) catch |err| { |
| | 172 | assert(err == error.OutOfBounds); |
| | 173 | }; |
| | 174 | |
| 165 | { | 175 | { |
| 166 | var i: usize = 0; | 176 | var i: usize = 0; |
| 167 | while (i < 10) : (i += 1) { | 177 | while (i < 10) : (i += 1) { |
| ... | @@ -200,6 +210,20 @@ test "basic ArrayList test" { | ... | @@ -200,6 +210,20 @@ test "basic ArrayList test" { |
| 200 | | 210 | |
| 201 | list.appendSlice([]const i32{}) catch unreachable; | 211 | list.appendSlice([]const i32{}) catch unreachable; |
| 202 | assert(list.len == 9); | 212 | assert(list.len == 9); |
| | 213 | |
| | 214 | // can only set on indices < self.len |
| | 215 | list.set(7, 33) catch unreachable; |
| | 216 | list.set(8, 42) catch unreachable; |
| | 217 | |
| | 218 | list.set(9, 99) catch |err| { |
| | 219 | assert(err == error.OutOfBounds); |
| | 220 | }; |
| | 221 | list.set(10, 123) catch |err| { |
| | 222 | assert(err == error.OutOfBounds); |
| | 223 | }; |
| | 224 | |
| | 225 | assert(list.pop() == 42); |
| | 226 | assert(list.pop() == 33); |
| 203 | } | 227 | } |
| 204 | | 228 | |
| 205 | test "iterator ArrayList test" { | 229 | test "iterator ArrayList test" { |