authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-03-31 02:47:19+02:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-04-02 15:14:18+02:00
logdd570dbc0dd78e3b2f9cfb5a1febb325e585e12d
treeb8d2fa188cdc55069e02b86c197025fb7f6a9cde
parentf6d384450fd7a9c6da236928ef84629bfbfbb781

new ArrayList API, fix enough std lib to test


3 files changed, 100 insertions(+), 93 deletions(-)

lib/std/array_list.zig+95-88
......@@ -20,11 +20,9 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
2020 return struct {
2121 const Self = @This();
2222
23 /// Use `span` instead of slicing this directly, because if you don't
24 /// specify the end position of the slice, this will potentially give
25 /// you uninitialized memory.
23 /// Content of the ArrayList
2624 items: Slice,
27 len: usize,
25 capacity: usize,
2826 allocator: *Allocator,
2927
3028 pub const Slice = if (alignment) |a| ([]align(a) T) else []T;
......@@ -34,7 +32,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
3432 pub fn init(allocator: *Allocator) Self {
3533 return Self{
3634 .items = &[_]T{},
37 .len = 0,
35 .capacity = 0,
3836 .allocator = allocator,
3937 };
4038 }
......@@ -49,60 +47,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
4947
5048 /// Release all allocated memory.
5149 pub fn deinit(self: Self) void {
52 self.allocator.free(self.items);
50 self.allocator.free(self.allocatedSlice());
5351 }
5452
53 /// Deprecated: use `items` field directly.
5554 /// Return contents as a slice. Only valid while the list
5655 /// doesn't change size.
57 pub fn span(self: var) @TypeOf(self.items[0..self.len]) {
58 return self.items[0..self.len];
56 pub fn span(self: var) @TypeOf(self.items) {
57 return self.items;
5958 }
6059
61 /// Deprecated: use `span`.
60 /// Deprecated: use `items` field directly.
6261 pub fn toSlice(self: Self) Slice {
63 return self.span();
62 return self.items;
6463 }
6564
66 /// Deprecated: use `span`.
65 /// Deprecated: use `items` field directly.
6766 pub fn toSliceConst(self: Self) SliceConst {
68 return self.span();
67 return self.items;
6968 }
7069
71 /// Deprecated: use `span()[i]`.
70 /// Deprecated: use `list.items[i]`.
7271 pub fn at(self: Self, i: usize) T {
73 return self.span()[i];
72 return self.items[i];
7473 }
7574
76 /// Deprecated: use `&span()[i]`.
75 /// Deprecated: use `&list.items[i]`.
7776 pub fn ptrAt(self: Self, i: usize) *T {
78 return &self.span()[i];
77 return &self.items[i];
7978 }
8079
81 /// Deprecated: use `if (i >= list.len) return error.OutOfBounds else span()[i] = item`.
80 /// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.items[i] = item`.
8281 pub fn setOrError(self: Self, i: usize, item: T) !void {
83 if (i >= self.len) return error.OutOfBounds;
82 if (i >= self.items.len) return error.OutOfBounds;
8483 self.items[i] = item;
8584 }
8685
87 /// Deprecated: use `list.span()[i] = item`.
86 /// Deprecated: use `list.items[i] = item`.
8887 pub fn set(self: *Self, i: usize, item: T) void {
89 assert(i < self.len);
88 assert(i < self.items.len);
9089 self.items[i] = item;
9190 }
9291
93 /// Return the maximum number of items the list can hold
94 /// without allocating more memory.
95 pub fn capacity(self: Self) usize {
96 return self.items.len;
97 }
98
9992 /// ArrayList takes ownership of the passed in slice. The slice must have been
10093 /// allocated with `allocator`.
10194 /// Deinitialize with `deinit` or use `toOwnedSlice`.
10295 pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self {
10396 return Self{
10497 .items = slice,
105 .len = slice.len,
98 .capacity = slice.len,
10699 .allocator = allocator,
107100 };
108101 }
......@@ -110,7 +103,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
110103 /// The caller owns the returned memory. ArrayList becomes empty.
111104 pub fn toOwnedSlice(self: *Self) Slice {
112105 const allocator = self.allocator;
113 const result = allocator.shrink(self.items, self.len);
106 const result = allocator.shrink(self.allocatedSlice(), self.items.len);
114107 self.* = init(allocator);
115108 return result;
116109 }
......@@ -118,10 +111,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
118111 /// Insert `item` at index `n`. Moves `list[n .. list.len]`
119112 /// to make room.
120113 pub fn insert(self: *Self, n: usize, item: T) !void {
121 try self.ensureCapacity(self.len + 1);
122 self.len += 1;
114 try self.ensureCapacity(self.items.len + 1);
115 self.items.len += 1;
123116
124 mem.copyBackwards(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
117 mem.copyBackwards(T, self.items[n + 1 .. self.items.len], self.items[n .. self.items.len - 1]);
125118 self.items[n] = item;
126119 }
127120
......@@ -129,10 +122,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
129122 /// `list[i .. list.len]` to make room.
130123 /// This operation is O(N).
131124 pub fn insertSlice(self: *Self, i: usize, items: SliceConst) !void {
132 try self.ensureCapacity(self.len + items.len);
133 self.len += items.len;
125 try self.ensureCapacity(self.items.len + items.len);
126 self.items.len += items.len;
134127
135 mem.copyBackwards(T, self.items[i + items.len .. self.len], self.items[i .. self.len - items.len]);
128 mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]);
136129 mem.copy(T, self.items[i .. i + items.len], items);
137130 }
138131
......@@ -153,13 +146,13 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
153146 /// Asserts the array has at least one item.
154147 /// This operation is O(N).
155148 pub fn orderedRemove(self: *Self, i: usize) T {
156 const newlen = self.len - 1;
149 const newlen = self.items.len - 1;
157150 if (newlen == i) return self.pop();
158151
159 const old_item = self.at(i);
152 const old_item = self.items[i];
160153 for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j];
161154 self.items[newlen] = undefined;
162 self.len = newlen;
155 self.items.len = newlen;
163156 return old_item;
164157 }
165158
......@@ -167,26 +160,28 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
167160 /// The empty slot is filled from the end of the list.
168161 /// This operation is O(1).
169162 pub fn swapRemove(self: *Self, i: usize) T {
170 if (self.len - 1 == i) return self.pop();
163 if (self.items.len - 1 == i) return self.pop();
171164
172 const slice = self.span();
173 const old_item = slice[i];
174 slice[i] = self.pop();
165 const old_item = self.items[i];
166 self.items[i] = self.pop();
175167 return old_item;
176168 }
177169
178 /// Deprecated: use `if (i >= list.len) return error.OutOfBounds else list.swapRemove(i)`.
170 /// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.swapRemove(i)`.
179171 pub fn swapRemoveOrError(self: *Self, i: usize) !T {
180 if (i >= self.len) return error.OutOfBounds;
172 if (i >= self.items.len) return error.OutOfBounds;
181173 return self.swapRemove(i);
182174 }
183175
184176 /// Append the slice of items to the list. Allocates more
185177 /// memory as necessary.
186178 pub fn appendSlice(self: *Self, items: SliceConst) !void {
187 try self.ensureCapacity(self.len + items.len);
188 mem.copy(T, self.items[self.len..], items);
189 self.len += items.len;
179 const oldlen = self.items.len;
180 const newlen = self.items.len + items.len;
181
182 try self.ensureCapacity(newlen);
183 self.items.len = newlen;
184 mem.copy(T, self.items[oldlen..], items);
190185 }
191186
192187 /// Same as `append` except it returns the number of bytes written, which is always the same
......@@ -206,50 +201,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
206201 /// Append a value to the list `n` times.
207202 /// Allocates more memory as necessary.
208203 pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
209 const old_len = self.len;
210 try self.resize(self.len + n);
211 mem.set(T, self.items[old_len..self.len], value);
204 const old_len = self.items.len;
205 try self.resize(self.items.len + n);
206 mem.set(T, self.items[old_len..self.items.len], value);
212207 }
213208
214209 /// Adjust the list's length to `new_len`.
215210 /// Does not initialize added items if any.
216211 pub fn resize(self: *Self, new_len: usize) !void {
217212 try self.ensureCapacity(new_len);
218 self.len = new_len;
213 self.items.len = new_len;
219214 }
220215
221216 /// Reduce allocated capacity to `new_len`.
222217 /// Invalidates element pointers.
223218 pub fn shrink(self: *Self, new_len: usize) void {
224 assert(new_len <= self.len);
225 self.len = new_len;
226 self.items = self.allocator.realloc(self.items, new_len) catch |e| switch (e) {
219 assert(new_len <= self.items.len);
220
221 self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) {
227222 error.OutOfMemory => return, // no problem, capacity is still correct then.
228223 };
224 self.capacity = new_len;
229225 }
230226
231227 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
232 var better_capacity = self.capacity();
228 var better_capacity = self.capacity;
233229 if (better_capacity >= new_capacity) return;
230
234231 while (true) {
235232 better_capacity += better_capacity / 2 + 8;
236233 if (better_capacity >= new_capacity) break;
237234 }
238 self.items = try self.allocator.realloc(self.items, better_capacity);
235
236 const new_memory = try self.allocator.realloc(self.allocatedSlice(), better_capacity);
237 self.items.ptr = new_memory.ptr;
238 self.capacity = new_memory.len;
239239 }
240240
241241 /// Increases the array's length to match the full capacity that is already allocated.
242242 /// The new elements have `undefined` values. This operation does not invalidate any
243243 /// element pointers.
244244 pub fn expandToCapacity(self: *Self) void {
245 self.len = self.items.len;
245 self.items.len = self.capacity;
246246 }
247247
248248 /// Increase length by 1, returning pointer to the new item.
249249 /// The returned pointer becomes invalid when the list is resized.
250250 pub fn addOne(self: *Self) !*T {
251 const new_length = self.len + 1;
252 try self.ensureCapacity(new_length);
251 const newlen = self.items.len + 1;
252 try self.ensureCapacity(newlen);
253253 return self.addOneAssumeCapacity();
254254 }
255255
......@@ -257,25 +257,32 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
257257 /// Asserts that there is already space for the new item without allocating more.
258258 /// The returned pointer becomes invalid when the list is resized.
259259 pub fn addOneAssumeCapacity(self: *Self) *T {
260 assert(self.len < self.capacity());
261 const result = &self.items[self.len];
262 self.len += 1;
263 return result;
260 assert(self.items.len < self.capacity);
261
262 self.items.len += 1;
263 return &self.items[self.items.len - 1];
264264 }
265265
266266 /// Remove and return the last element from the list.
267267 /// Asserts the list has at least one item.
268268 pub fn pop(self: *Self) T {
269 self.len -= 1;
270 return self.items[self.len];
269 const val = self.items[self.items.len - 1];
270 self.items.len -= 1;
271 return val;
271272 }
272273
273274 /// Remove and return the last element from the list.
274275 /// If the list is empty, returns `null`.
275276 pub fn popOrNull(self: *Self) ?T {
276 if (self.len == 0) return null;
277 if (self.items.len == 0) return null;
277278 return self.pop();
278279 }
280
281 // For a nicer API, `items.len` is the length, not the capacity.
282 // This requires "unsafe" slicing.
283 fn allocatedSlice(self: Self) Slice {
284 return self.items.ptr[0..self.capacity];
285 }
279286 };
280287}
281288
......@@ -283,15 +290,15 @@ test "std.ArrayList.init" {
283290 var list = ArrayList(i32).init(testing.allocator);
284291 defer list.deinit();
285292
286 testing.expect(list.len == 0);
287 testing.expect(list.capacity() == 0);
293 testing.expect(list.items.len == 0);
294 testing.expect(list.capacity == 0);
288295}
289296
290297test "std.ArrayList.initCapacity" {
291298 var list = try ArrayList(i8).initCapacity(testing.allocator, 200);
292299 defer list.deinit();
293 testing.expect(list.len == 0);
294 testing.expect(list.capacity() >= 200);
300 testing.expect(list.items.len == 0);
301 testing.expect(list.capacity >= 200);
295302}
296303
297304test "std.ArrayList.basic" {
......@@ -315,7 +322,7 @@ test "std.ArrayList.basic" {
315322 }
316323 }
317324
318 for (list.span()) |v, i| {
325 for (list.items) |v, i| {
319326 testing.expect(v == @intCast(i32, i + 1));
320327 }
321328
......@@ -324,19 +331,19 @@ test "std.ArrayList.basic" {
324331 }
325332
326333 testing.expect(list.pop() == 10);
327 testing.expect(list.len == 9);
334 testing.expect(list.items.len == 9);
328335
329336 list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable;
330 testing.expect(list.len == 12);
337 testing.expect(list.items.len == 12);
331338 testing.expect(list.pop() == 3);
332339 testing.expect(list.pop() == 2);
333340 testing.expect(list.pop() == 1);
334 testing.expect(list.len == 9);
341 testing.expect(list.items.len == 9);
335342
336343 list.appendSlice(&[_]i32{}) catch unreachable;
337 testing.expect(list.len == 9);
344 testing.expect(list.items.len == 9);
338345
339 // can only set on indices < self.len
346 // can only set on indices < self.items.len
340347 list.set(7, 33);
341348 list.set(8, 42);
342349
......@@ -352,8 +359,8 @@ test "std.ArrayList.appendNTimes" {
352359 defer list.deinit();
353360
354361 try list.appendNTimes(2, 10);
355 testing.expectEqual(@as(usize, 10), list.len);
356 for (list.span()) |element| {
362 testing.expectEqual(@as(usize, 10), list.items.len);
363 for (list.items) |element| {
357364 testing.expectEqual(@as(i32, 2), element);
358365 }
359366}
......@@ -378,17 +385,17 @@ test "std.ArrayList.orderedRemove" {
378385
379386 //remove from middle
380387 testing.expectEqual(@as(i32, 4), list.orderedRemove(3));
381 testing.expectEqual(@as(i32, 5), list.at(3));
382 testing.expectEqual(@as(usize, 6), list.len);
388 testing.expectEqual(@as(i32, 5), list.items[3]);
389 testing.expectEqual(@as(usize, 6), list.items.len);
383390
384391 //remove from end
385392 testing.expectEqual(@as(i32, 7), list.orderedRemove(5));
386 testing.expectEqual(@as(usize, 5), list.len);
393 testing.expectEqual(@as(usize, 5), list.items.len);
387394
388395 //remove from front
389396 testing.expectEqual(@as(i32, 1), list.orderedRemove(0));
390 testing.expectEqual(@as(i32, 2), list.at(0));
391 testing.expectEqual(@as(usize, 4), list.len);
397 testing.expectEqual(@as(i32, 2), list.items[0]);
398 testing.expectEqual(@as(usize, 4), list.items.len);
392399}
393400
394401test "std.ArrayList.swapRemove" {
......@@ -405,17 +412,17 @@ test "std.ArrayList.swapRemove" {
405412
406413 //remove from middle
407414 testing.expect(list.swapRemove(3) == 4);
408 testing.expect(list.at(3) == 7);
409 testing.expect(list.len == 6);
415 testing.expect(list.items[3] == 7);
416 testing.expect(list.items.len == 6);
410417
411418 //remove from end
412419 testing.expect(list.swapRemove(5) == 6);
413 testing.expect(list.len == 5);
420 testing.expect(list.items.len == 5);
414421
415422 //remove from front
416423 testing.expect(list.swapRemove(0) == 1);
417 testing.expect(list.at(0) == 5);
418 testing.expect(list.len == 4);
424 testing.expect(list.items[0] == 5);
425 testing.expect(list.items.len == 4);
419426}
420427
421428test "std.ArrayList.swapRemoveOrError" {
......@@ -478,7 +485,7 @@ test "std.ArrayList.insertSlice" {
478485
479486 const items = [_]i32{1};
480487 try list.insertSlice(0, items[0..0]);
481 testing.expect(list.len == 6);
488 testing.expect(list.items.len == 6);
482489 testing.expect(list.items[0] == 1);
483490}
484491
lib/std/dwarf.zig+4-4
......@@ -206,7 +206,7 @@ const LineNumberProgram = struct {
206206 if (self.target_address >= self.prev_address and self.target_address < self.address) {
207207 const file_entry = if (self.prev_file == 0) {
208208 return error.MissingDebugInfo;
209 } else if (self.prev_file - 1 >= self.file_entries.len) {
209 } else if (self.prev_file - 1 >= self.file_entries.items.len) {
210210 return error.InvalidDebugInfo;
211211 } else
212212 &self.file_entries.items[self.prev_file - 1];
......@@ -645,7 +645,7 @@ pub const DwarfInfo = struct {
645645 .offset = abbrev_offset,
646646 .table = try di.parseAbbrevTable(abbrev_offset),
647647 });
648 return &di.abbrev_table_list.items[di.abbrev_table_list.len - 1].table;
648 return &di.abbrev_table_list.items[di.abbrev_table_list.items.len - 1].table;
649649 }
650650
651651 fn parseAbbrevTable(di: *DwarfInfo, offset: u64) !AbbrevTable {
......@@ -665,7 +665,7 @@ pub const DwarfInfo = struct {
665665 .has_children = (try in.readByte()) == CHILDREN_yes,
666666 .attrs = ArrayList(AbbrevAttr).init(di.allocator()),
667667 });
668 const attrs = &result.items[result.len - 1].attrs;
668 const attrs = &result.items[result.items.len - 1].attrs;
669669
670670 while (true) {
671671 const attr_id = try leb.readULEB128(u64, in);
......@@ -689,7 +689,7 @@ pub const DwarfInfo = struct {
689689 .has_children = table_entry.has_children,
690690 .attrs = ArrayList(Die.Attr).init(di.allocator()),
691691 };
692 try result.attrs.resize(table_entry.attrs.len);
692 try result.attrs.resize(table_entry.attrs.items.len);
693693 for (table_entry.attrs.span()) |attr, i| {
694694 result.attrs.items[i] = Die.Attr{
695695 .id = attr.attr_id,
lib/std/io/in_stream.zig+1-1
......@@ -106,7 +106,7 @@ pub fn InStream(
106106 return;
107107 }
108108
109 if (array_list.len == max_size) {
109 if (array_list.items.len == max_size) {
110110 return error.StreamTooLong;
111111 }
112112