authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2017-09-10 05:49:40+12:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-09 13:49:40-04:00
logf725b20de68f744cc8931599266921f8eb7ba8a0
tree972675ef93472422bb565fb79376374671f6a913
parent67a31befa672890e23ef530fa4376a787edff892

Add appendSlice function (#448)


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

std/array_list.zig+16
......@@ -39,6 +39,12 @@ pub fn ArrayList(comptime T: type) -> type{
3939 *new_item_ptr = *item;
4040 }
4141
42 pub fn appendSlice(l: &Self, items: []const T) -> %void {
43 %return l.ensureCapacity(l.len + items.len);
44 mem.copy(T, l.items[l.len..], items);
45 l.len += items.len;
46 }
47
4248 pub fn resize(l: &Self, new_len: usize) -> %void {
4349 %return l.ensureCapacity(new_len);
4450 l.len = new_len;
......@@ -88,4 +94,14 @@ test "basic ArrayList test" {
8894
8995 assert(list.pop() == 10);
9096 assert(list.len == 9);
97
98 %%list.appendSlice([]const i32 { 1, 2, 3 });
99 assert(list.len == 12);
100 assert(list.pop() == 3);
101 assert(list.pop() == 2);
102 assert(list.pop() == 1);
103 assert(list.len == 9);
104
105 %%list.appendSlice([]const i32 {});
106 assert(list.len == 9);
91107}