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 {...@@ -85,7 +85,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
85 try self.ensureCapacity(self.len + 1);85 try self.ensureCapacity(self.len + 1);
86 self.len += 1;86 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]);
89 self.items[n] = item;89 self.items[n] = item;
90 }90 }
9191
...@@ -93,7 +93,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {...@@ -93,7 +93,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
93 try self.ensureCapacity(self.len + items.len);93 try self.ensureCapacity(self.len + items.len);
94 self.len += items.len;94 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]);
97 mem.copy(T, self.items[n .. n + items.len], items);97 mem.copy(T, self.items[n .. n + items.len], items);
98 }98 }
9999
...@@ -266,19 +266,36 @@ test "insert ArrayList test" {...@@ -266,19 +266,36 @@ test "insert ArrayList test" {
266 defer list.deinit();266 defer list.deinit();
267267
268 try list.append(1);268 try list.append(1);
269 try list.append(2);
270 try list.append(3);
269 try list.insert(0, 5);271 try list.insert(0, 5);
270 assert(list.items[0] == 5);272 assert(list.items[0] == 5);
271 assert(list.items[1] == 1);273 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);
273 try list.insertSlice(1, []const i32{286 try list.insertSlice(1, []const i32{
274 9,287 9,
275 8,288 8,
276 });289 });
277 assert(list.items[0] == 5);290 assert(list.items[0] == 1);
278 assert(list.items[1] == 9);291 assert(list.items[1] == 9);
279 assert(list.items[2] == 8);292 assert(list.items[2] == 8);
293 assert(list.items[3] == 2);
294 assert(list.items[4] == 3);
295 assert(list.items[5] == 4);
280296
281 const items = []const i32{1};297 const items = []const i32{1};
282 try list.insertSlice(0, items[0..0]);298 try list.insertSlice(0, items[0..0]);
283 assert(list.items[0] == 5);299 assert(list.len == 6);
300 assert(list.items[0] == 1);
284}301}
std/mem.zig+18
...@@ -125,6 +125,7 @@ pub const Allocator = struct {...@@ -125,6 +125,7 @@ pub const Allocator = struct {
125125
126/// Copy all of source into dest at position 0.126/// Copy all of source into dest at position 0.
127/// dest.len must be >= source.len.127/// dest.len must be >= source.len.
128/// dest.ptr must be <= src.ptr.
128pub fn copy(comptime T: type, dest: []T, source: []const T) void {129pub fn copy(comptime T: type, dest: []T, source: []const T) void {
129 // TODO instead of manually doing this check for the whole array130 // TODO instead of manually doing this check for the whole array
130 // and turning off runtime safety, the compiler should detect loops like131 // 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 {...@@ -135,6 +136,23 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
135 dest[i] = s;136 dest[i] = s;
136}137}
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
138pub fn set(comptime T: type, dest: []T, value: T) void {156pub fn set(comptime T: type, dest: []T, value: T) void {
139 for (dest) |*d|157 for (dest) |*d|
140 d.* = value;158 d.* = value;