authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-09 12:04:48-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-09 12:04:48-04:00
log670a0a3eed2e8806177e4eac3382e8b1fad4059e
tree9620a3c93ebdaed9b7219c359cafddab83188ef7
parent9046b5eac01540a783740451a6593ef0207c181e
parentfc6446702ed8261a1d02b7fbb8410a303cb5daaa

Merge branch 'clownpriest-arraylist_set'


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

std/array_list.zig+69-39
......@@ -1,6 +1,7 @@
11const std = @import("index.zig");
22const debug = std.debug;
33const assert = debug.assert;
4const assertError = debug.assertError;
45const mem = std.mem;
56const Allocator = mem.Allocator;
67
......@@ -28,20 +29,33 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
2829 };
2930 }
3031
31 pub fn deinit(l: *const Self) void {
32 l.allocator.free(l.items);
32 pub fn deinit(self: *const Self) void {
33 self.allocator.free(self.items);
3334 }
3435
35 pub fn toSlice(l: *const Self) []align(A) T {
36 return l.items[0..l.len];
36 pub fn toSlice(self: *const Self) []align(A) T {
37 return self.items[0..self.len];
3738 }
3839
39 pub fn toSliceConst(l: *const Self) []align(A) const T {
40 return l.items[0..l.len];
40 pub fn toSliceConst(self: *const Self) []align(A) const T {
41 return self.items[0..self.len];
4142 }
4243
43 pub fn at(l: *const Self, n: usize) T {
44 return l.toSliceConst()[n];
44 pub fn at(self: *const Self, n: usize) T {
45 return self.toSliceConst()[n];
46 }
47
48 /// Sets the value at index `i`, or returns `error.OutOfBounds` if
49 /// the index is not in range.
50 pub fn setOrError(self: *const Self, i: usize, item: *const T) !void {
51 if (i >= self.len) return error.OutOfBounds;
52 self.items[i] = item.*;
53 }
54
55 /// Sets the value at index `i`, asserting that the value is in range.
56 pub fn set(self: *const Self, i: usize, item: *const T) void {
57 assert(i < self.len);
58 self.items[i] = item.*;
4559 }
4660
4761 pub fn count(self: *const Self) usize {
......@@ -67,58 +81,58 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
6781 return result;
6882 }
6983
70 pub fn insert(l: *Self, n: usize, item: *const T) !void {
71 try l.ensureCapacity(l.len + 1);
72 l.len += 1;
84 pub fn insert(self: *Self, n: usize, item: *const T) !void {
85 try self.ensureCapacity(self.len + 1);
86 self.len += 1;
7387
74 mem.copy(T, l.items[n + 1 .. l.len], l.items[n .. l.len - 1]);
75 l.items[n] = item.*;
88 mem.copy(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
89 self.items[n] = item.*;
7690 }
7791
78 pub fn insertSlice(l: *Self, n: usize, items: []align(A) const T) !void {
79 try l.ensureCapacity(l.len + items.len);
80 l.len += items.len;
92 pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void {
93 try self.ensureCapacity(self.len + items.len);
94 self.len += items.len;
8195
82 mem.copy(T, l.items[n + items.len .. l.len], l.items[n .. l.len - items.len]);
83 mem.copy(T, l.items[n .. n + items.len], items);
96 mem.copy(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);
8498 }
8599
86 pub fn append(l: *Self, item: *const T) !void {
87 const new_item_ptr = try l.addOne();
100 pub fn append(self: *Self, item: *const T) !void {
101 const new_item_ptr = try self.addOne();
88102 new_item_ptr.* = item.*;
89103 }
90104
91 pub fn appendSlice(l: *Self, items: []align(A) const T) !void {
92 try l.ensureCapacity(l.len + items.len);
93 mem.copy(T, l.items[l.len..], items);
94 l.len += items.len;
105 pub fn appendSlice(self: *Self, items: []align(A) const T) !void {
106 try self.ensureCapacity(self.len + items.len);
107 mem.copy(T, self.items[self.len..], items);
108 self.len += items.len;
95109 }
96110
97 pub fn resize(l: *Self, new_len: usize) !void {
98 try l.ensureCapacity(new_len);
99 l.len = new_len;
111 pub fn resize(self: *Self, new_len: usize) !void {
112 try self.ensureCapacity(new_len);
113 self.len = new_len;
100114 }
101115
102 pub fn shrink(l: *Self, new_len: usize) void {
103 assert(new_len <= l.len);
104 l.len = new_len;
116 pub fn shrink(self: *Self, new_len: usize) void {
117 assert(new_len <= self.len);
118 self.len = new_len;
105119 }
106120
107 pub fn ensureCapacity(l: *Self, new_capacity: usize) !void {
108 var better_capacity = l.items.len;
121 pub fn ensureCapacity(self: *Self, new_capacity: usize) !void {
122 var better_capacity = self.items.len;
109123 if (better_capacity >= new_capacity) return;
110124 while (true) {
111125 better_capacity += better_capacity / 2 + 8;
112126 if (better_capacity >= new_capacity) break;
113127 }
114 l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity);
128 self.items = try self.allocator.alignedRealloc(T, A, self.items, better_capacity);
115129 }
116130
117 pub fn addOne(l: *Self) !*T {
118 const new_length = l.len + 1;
119 try l.ensureCapacity(new_length);
120 const result = &l.items[l.len];
121 l.len = new_length;
131 pub fn addOne(self: *Self) !*T {
132 const new_length = self.len + 1;
133 try self.ensureCapacity(new_length);
134 const result = &self.items[self.len];
135 self.len = new_length;
122136 return result;
123137 }
124138
......@@ -159,9 +173,15 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
159173}
160174
161175test "basic ArrayList test" {
162 var list = ArrayList(i32).init(debug.global_allocator);
176 var bytes: [1024]u8 = undefined;
177 const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
178
179 var list = ArrayList(i32).init(allocator);
163180 defer list.deinit();
164181
182 // setting on empty list is out of bounds
183 assertError(list.setOrError(0, 1), error.OutOfBounds);
184
165185 {
166186 var i: usize = 0;
167187 while (i < 10) : (i += 1) {
......@@ -200,6 +220,16 @@ test "basic ArrayList test" {
200220
201221 list.appendSlice([]const i32{}) catch unreachable;
202222 assert(list.len == 9);
223
224 // can only set on indices < self.len
225 list.set(7, 33);
226 list.set(8, 42);
227
228 assertError(list.setOrError(9, 99), error.OutOfBounds);
229 assertError(list.setOrError(10, 123), error.OutOfBounds);
230
231 assert(list.pop() == 42);
232 assert(list.pop() == 33);
203233}
204234
205235test "iterator ArrayList test" {
std/debug/index.zig+10
......@@ -88,6 +88,16 @@ pub fn assert(ok: bool) void {
8888 }
8989}
9090
91/// TODO: add `==` operator for `error_union == error_set`, and then
92/// remove this function
93pub fn assertError(value: var, expected_error: error) void {
94 if (value) {
95 @panic("expected error");
96 } else |actual_error| {
97 assert(actual_error == expected_error);
98 }
99}
100
91101/// Call this function when you want to panic if the condition is not true.
92102/// If `ok` is `false`, this function will panic in every release mode.
93103pub fn assertOrPanic(ok: bool) void {