| ... | @@ -257,6 +257,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -257,6 +257,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { |
| 257 | return &self.items[self.items.len - 1]; | 257 | return &self.items[self.items.len - 1]; |
| 258 | } | 258 | } |
| 259 | | 259 | |
| | 260 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| | 261 | /// The return value is an array pointing to the newly allocated elements. |
| | 262 | pub fn addManyAsArray(self: *Self, comptime n: usize) !*[n]T { |
| | 263 | const prev_len = self.items.len; |
| | 264 | try self.resize(self.items.len + n); |
| | 265 | return self.items[prev_len..][0..n]; |
| | 266 | } |
| | 267 | |
| | 268 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| | 269 | /// The return value is an array pointing to the newly allocated elements. |
| | 270 | /// Asserts that there is already space for the new item without allocating more. |
| | 271 | pub fn addManyAsArrayAssumeCapacity(self: *Self, comptime n: usize) *[n]T { |
| | 272 | assert(self.items.len + n <= self.capacity); |
| | 273 | const prev_len = self.items.len; |
| | 274 | self.items.len += n; |
| | 275 | return self.items[prev_len..][0..n]; |
| | 276 | } |
| | 277 | |
| 260 | /// Remove and return the last element from the list. | 278 | /// Remove and return the last element from the list. |
| 261 | /// Asserts the list has at least one item. | 279 | /// Asserts the list has at least one item. |
| 262 | pub fn pop(self: *Self) T { | 280 | pub fn pop(self: *Self) T { |
| ... | @@ -488,6 +506,24 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -488,6 +506,24 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 488 | return &self.items[self.items.len - 1]; | 506 | return &self.items[self.items.len - 1]; |
| 489 | } | 507 | } |
| 490 | | 508 | |
| | 509 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| | 510 | /// The return value is an array pointing to the newly allocated elements. |
| | 511 | pub fn addManyAsArray(self: *Self, allocator: *Allocator, comptime n: usize) !*[n]T { |
| | 512 | const prev_len = self.items.len; |
| | 513 | try self.resize(allocator, self.items.len + n); |
| | 514 | return self.items[prev_len..][0..n]; |
| | 515 | } |
| | 516 | |
| | 517 | /// Resize the array, adding `n` new elements, which have `undefined` values. |
| | 518 | /// The return value is an array pointing to the newly allocated elements. |
| | 519 | /// Asserts that there is already space for the new item without allocating more. |
| | 520 | pub fn addManyAsArrayAssumeCapacity(self: *Self, comptime n: usize) *[n]T { |
| | 521 | assert(self.items.len + n <= self.capacity); |
| | 522 | const prev_len = self.items.len; |
| | 523 | self.items.len += n; |
| | 524 | return self.items[prev_len..][0..n]; |
| | 525 | } |
| | 526 | |
| 491 | /// Remove and return the last element from the list. | 527 | /// Remove and return the last element from the list. |
| 492 | /// Asserts the list has at least one item. | 528 | /// Asserts the list has at least one item. |
| 493 | /// This operation does not invalidate any element pointers. | 529 | /// This operation does not invalidate any element pointers. |
| ... | @@ -727,3 +763,27 @@ test "std.ArrayList.writer" { | ... | @@ -727,3 +763,27 @@ test "std.ArrayList.writer" { |
| 727 | try writer.writeAll("efg"); | 763 | try writer.writeAll("efg"); |
| 728 | testing.expectEqualSlices(u8, list.items, "abcdefg"); | 764 | testing.expectEqualSlices(u8, list.items, "abcdefg"); |
| 729 | } | 765 | } |
| | 766 | |
| | 767 | test "addManyAsArray" { |
| | 768 | const a = std.testing.allocator; |
| | 769 | { |
| | 770 | var list = ArrayList(u8).init(a); |
| | 771 | defer list.deinit(); |
| | 772 | |
| | 773 | (try list.addManyAsArray(4)).* = "aoeu".*; |
| | 774 | try list.ensureCapacity(8); |
| | 775 | list.addManyAsArrayAssumeCapacity(4).* = "asdf".*; |
| | 776 | |
| | 777 | testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| | 778 | } |
| | 779 | { |
| | 780 | var list = ArrayListUnmanaged(u8){}; |
| | 781 | defer list.deinit(a); |
| | 782 | |
| | 783 | (try list.addManyAsArray(a, 4)).* = "aoeu".*; |
| | 784 | try list.ensureCapacity(a, 8); |
| | 785 | list.addManyAsArrayAssumeCapacity(4).* = "asdf".*; |
| | 786 | |
| | 787 | testing.expectEqualSlices(u8, list.items, "aoeuasdf"); |
| | 788 | } |
| | 789 | } |