authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-06 06:09:47+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-06 06:09:47+00:00
loga2fd8f72c155d9c3ffeb3f625598fa81ed9629dc
treee8bd089f74c5734ad75065262b5e5e140c479dc6
parent8fb392dbb443688cdf62e965935e17a4ae4a4267

std: add new array list functions


1 files changed, 60 insertions(+), 0 deletions(-)

lib/std/array_list.zig+60
......@@ -257,6 +257,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
257257 return &self.items[self.items.len - 1];
258258 }
259259
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
260278 /// Remove and return the last element from the list.
261279 /// Asserts the list has at least one item.
262280 pub fn pop(self: *Self) T {
......@@ -488,6 +506,24 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
488506 return &self.items[self.items.len - 1];
489507 }
490508
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
491527 /// Remove and return the last element from the list.
492528 /// Asserts the list has at least one item.
493529 /// This operation does not invalidate any element pointers.
......@@ -727,3 +763,27 @@ test "std.ArrayList.writer" {
727763 try writer.writeAll("efg");
728764 testing.expectEqualSlices(u8, list.items, "abcdefg");
729765}
766
767test "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}