authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-14 09:35:50-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-07-14 09:35:50-04:00
log5f1aa3505d387bcc164cb102be11fd5cb054f4ef
treed1b3880c6a0624f14b17c7111e349523e5899fd8
parente78b1b810fd15dfd135c80d06d621851a59f42c6
parentfe98a2da70cedaad47ea70d58399e04eb1c7ead2
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1232 from BarabasGitHub/fix-array-list-insert

Fix array list insert

2 files changed, 39 insertions(+), 4 deletions(-)

std/array_list.zig+21-4
......@@ -85,7 +85,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
8585 try self.ensureCapacity(self.len + 1);
8686 self.len += 1;
8787
88 mem.copy(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
88 mem.copyBackwards(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
8989 self.items[n] = item;
9090 }
9191
......@@ -93,7 +93,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
9393 try self.ensureCapacity(self.len + items.len);
9494 self.len += items.len;
9595
96 mem.copy(T, self.items[n + items.len .. self.len], self.items[n .. self.len - items.len]);
96 mem.copyBackwards(T, self.items[n + items.len .. self.len], self.items[n .. self.len - items.len]);
9797 mem.copy(T, self.items[n .. n + items.len], items);
9898 }
9999
......@@ -266,19 +266,36 @@ test "insert ArrayList test" {
266266 defer list.deinit();
267267
268268 try list.append(1);
269 try list.append(2);
270 try list.append(3);
269271 try list.insert(0, 5);
270272 assert(list.items[0] == 5);
271273 assert(list.items[1] == 1);
274 assert(list.items[2] == 2);
275 assert(list.items[3] == 3);
276}
277
278test "insertSlice ArrayList test" {
279 var list = ArrayList(i32).init(debug.global_allocator);
280 defer list.deinit();
272281
282 try list.append(1);
283 try list.append(2);
284 try list.append(3);
285 try list.append(4);
273286 try list.insertSlice(1, []const i32{
274287 9,
275288 8,
276289 });
277 assert(list.items[0] == 5);
290 assert(list.items[0] == 1);
278291 assert(list.items[1] == 9);
279292 assert(list.items[2] == 8);
293 assert(list.items[3] == 2);
294 assert(list.items[4] == 3);
295 assert(list.items[5] == 4);
280296
281297 const items = []const i32{1};
282298 try list.insertSlice(0, items[0..0]);
283 assert(list.items[0] == 5);
299 assert(list.len == 6);
300 assert(list.items[0] == 1);
284301}
std/mem.zig+18
......@@ -125,6 +125,7 @@ pub const Allocator = struct {
125125
126126/// Copy all of source into dest at position 0.
127127/// dest.len must be >= source.len.
128/// dest.ptr must be <= src.ptr.
128129pub fn copy(comptime T: type, dest: []T, source: []const T) void {
129130 // TODO instead of manually doing this check for the whole array
130131 // and turning off runtime safety, the compiler should detect loops like
......@@ -135,6 +136,23 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
135136 dest[i] = s;
136137}
137138
139/// Copy all of source into dest at position 0.
140/// dest.len must be >= source.len.
141/// dest.ptr must be >= src.ptr.
142pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
143 // TODO instead of manually doing this check for the whole array
144 // and turning off runtime safety, the compiler should detect loops like
145 // this and automatically omit safety checks for loops
146 @setRuntimeSafety(false);
147 assert(dest.len >= source.len);
148 var i = source.len;
149 while(i > 0){
150 i -= 1;
151 dest[i] = source[i];
152 }
153}
154
155
138156pub fn set(comptime T: type, dest: []T, value: T) void {
139157 for (dest) |*d|
140158 d.* = value;