| ... | ... | @@ -113,6 +113,14 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { |
| 113 | 113 | return old_item; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | /// Removes the element at the specified index and returns it |
| 117 | /// or an error.OutOfBounds is returned. If no error then |
| 118 | /// the empty slot is filled from the end of the list. |
| 119 | pub fn swapRemoveOrError(self: *Self, i: usize) !T { |
| 120 | if (i >= self.len) return error.OutOfBounds; |
| 121 | return self.swapRemove(i); |
| 122 | } |
| 123 | |
| 116 | 124 | pub fn appendSlice(self: *Self, items: []align(A) const T) !void { |
| 117 | 125 | try self.ensureCapacity(self.len + items.len); |
| 118 | 126 | mem.copy(T, self.items[self.len..], items); |
| ... | ... | @@ -270,6 +278,34 @@ test "std.ArrayList.swapRemove" { |
| 270 | 278 | assert(list.len == 4); |
| 271 | 279 | } |
| 272 | 280 | |
| 281 | test "std.ArrayList.swapRemoveOrError" { |
| 282 | var list = ArrayList(i32).init(debug.global_allocator); |
| 283 | defer list.deinit(); |
| 284 | |
| 285 | // Test just after initialization |
| 286 | assertError(list.swapRemoveOrError(0), error.OutOfBounds); |
| 287 | |
| 288 | // Test after adding one item and remote it |
| 289 | try list.append(1); |
| 290 | assert((try list.swapRemoveOrError(0)) == 1); |
| 291 | assertError(list.swapRemoveOrError(0), error.OutOfBounds); |
| 292 | |
| 293 | // Test after adding two items and remote both |
| 294 | try list.append(1); |
| 295 | try list.append(2); |
| 296 | assert((try list.swapRemoveOrError(1)) == 2); |
| 297 | assert((try list.swapRemoveOrError(0)) == 1); |
| 298 | assertError(list.swapRemoveOrError(0), error.OutOfBounds); |
| 299 | |
| 300 | // Test out of bounds with one item |
| 301 | try list.append(1); |
| 302 | assertError(list.swapRemoveOrError(1), error.OutOfBounds); |
| 303 | |
| 304 | // Test out of bounds with two items |
| 305 | try list.append(2); |
| 306 | assertError(list.swapRemoveOrError(2), error.OutOfBounds); |
| 307 | } |
| 308 | |
| 273 | 309 | test "std.ArrayList.iterator" { |
| 274 | 310 | var list = ArrayList(i32).init(debug.global_allocator); |
| 275 | 311 | defer list.deinit(); |