| ... | ... | @@ -141,11 +141,21 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 141 | 141 | return cloned; |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | | /// Insert `item` at index `n` by moving `list[n .. list.len]` to make room. |
| 144 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` to higher indices to make room. |
| 145 | /// If `n` is equal to the length of the list this operation is equivalent to append. |
| 145 | 146 | /// This operation is O(N). |
| 146 | 147 | /// Invalidates pointers if additional memory is needed. |
| 147 | 148 | pub fn insert(self: *Self, n: usize, item: T) Allocator.Error!void { |
| 148 | 149 | try self.ensureUnusedCapacity(1); |
| 150 | self.insertAssumeCapacity(n, item); |
| 151 | } |
| 152 | |
| 153 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` to higher indices to make room. |
| 154 | /// If `n` is equal to the length of the list this operation is equivalent to append. |
| 155 | /// This operation is O(N). |
| 156 | /// Asserts that there is enough capacity for the new item. |
| 157 | pub fn insertAssumeCapacity(self: *Self, n: usize, item: T) void { |
| 158 | assert(self.items.len < self.capacity); |
| 149 | 159 | self.items.len += 1; |
| 150 | 160 | |
| 151 | 161 | mem.copyBackwards(T, self.items[n + 1 .. self.items.len], self.items[n .. self.items.len - 1]); |
| ... | ... | @@ -609,12 +619,21 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 609 | 619 | return cloned; |
| 610 | 620 | } |
| 611 | 621 | |
| 612 | | /// Insert `item` at index `n`. Moves `list[n .. list.len]` |
| 613 | | /// to higher indices to make room. |
| 622 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` to higher indices to make room. |
| 623 | /// If `n` is equal to the length of the list this operation is equivalent to append. |
| 614 | 624 | /// This operation is O(N). |
| 615 | 625 | /// Invalidates pointers if additional memory is needed. |
| 616 | 626 | pub fn insert(self: *Self, allocator: Allocator, n: usize, item: T) Allocator.Error!void { |
| 617 | 627 | try self.ensureUnusedCapacity(allocator, 1); |
| 628 | self.insertAssumeCapacity(n, item); |
| 629 | } |
| 630 | |
| 631 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` to higher indices to make room. |
| 632 | /// If `n` is equal to the length of the list this operation is equivalent to append. |
| 633 | /// This operation is O(N). |
| 634 | /// Asserts that there is enough capacity for the new item. |
| 635 | pub fn insertAssumeCapacity(self: *Self, n: usize, item: T) void { |
| 636 | assert(self.items.len < self.capacity); |
| 618 | 637 | self.items.len += 1; |
| 619 | 638 | |
| 620 | 639 | mem.copyBackwards(T, self.items[n + 1 .. self.items.len], self.items[n .. self.items.len - 1]); |
| ... | ... | @@ -1309,9 +1328,9 @@ test "std.ArrayList/ArrayListUnmanaged.insert" { |
| 1309 | 1328 | var list = ArrayList(i32).init(a); |
| 1310 | 1329 | defer list.deinit(); |
| 1311 | 1330 | |
| 1312 | | try list.append(1); |
| 1331 | try list.insert(0, 1); |
| 1313 | 1332 | try list.append(2); |
| 1314 | | try list.append(3); |
| 1333 | try list.insert(2, 3); |
| 1315 | 1334 | try list.insert(0, 5); |
| 1316 | 1335 | try testing.expect(list.items[0] == 5); |
| 1317 | 1336 | try testing.expect(list.items[1] == 1); |
| ... | ... | @@ -1322,9 +1341,9 @@ test "std.ArrayList/ArrayListUnmanaged.insert" { |
| 1322 | 1341 | var list = ArrayListUnmanaged(i32){}; |
| 1323 | 1342 | defer list.deinit(a); |
| 1324 | 1343 | |
| 1325 | | try list.append(a, 1); |
| 1344 | try list.insert(a, 0, 1); |
| 1326 | 1345 | try list.append(a, 2); |
| 1327 | | try list.append(a, 3); |
| 1346 | try list.insert(a, 2, 3); |
| 1328 | 1347 | try list.insert(a, 0, 5); |
| 1329 | 1348 | try testing.expect(list.items[0] == 5); |
| 1330 | 1349 | try testing.expect(list.items[1] == 1); |