From f725b20de68f744cc8931599266921f8eb7ba8a0 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Sun, 10 Sep 2017 05:49:40 +1200 Subject: [PATCH] Add appendSlice function (#448) --- std/array_list.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/std/array_list.zig b/std/array_list.zig index 564ac23fa4..e01451792e 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -39,6 +39,12 @@ pub fn ArrayList(comptime T: type) -> type{ *new_item_ptr = *item; } + pub fn appendSlice(l: &Self, items: []const T) -> %void { + %return l.ensureCapacity(l.len + items.len); + mem.copy(T, l.items[l.len..], items); + l.len += items.len; + } + pub fn resize(l: &Self, new_len: usize) -> %void { %return l.ensureCapacity(new_len); l.len = new_len; @@ -88,4 +94,14 @@ test "basic ArrayList test" { assert(list.pop() == 10); assert(list.len == 9); + + %%list.appendSlice([]const i32 { 1, 2, 3 }); + assert(list.len == 12); + assert(list.pop() == 3); + assert(list.pop() == 2); + assert(list.pop() == 1); + assert(list.len == 9); + + %%list.appendSlice([]const i32 {}); + assert(list.len == 9); }