aboutsummaryrefslogtreecommitdiff
path: root/std/array_list.zig
diff options
context:
space:
mode:
authortgschultz <tgschultz@gmail.com>2018-05-11 21:36:02 -0500
committerGitHub <noreply@github.com>2018-05-11 21:36:02 -0500
commit8c1872543c8cf76215cc4bf3ced4637bb1065a4e (patch)
tree72dfebb643ab61579e3fb8dd58cd4610ffe876fa /std/array_list.zig
parent7186e92c86982950d0aa7c0c2deef9ef96bc1264 (diff)
parent6e821078f625a03eb8b7794c983da0f7793366ab (diff)
downloadzig-8c1872543c8cf76215cc4bf3ced4637bb1065a4e.tar.gz
zig-8c1872543c8cf76215cc4bf3ced4637bb1065a4e.zip
Merge pull request #1 from zig-lang/master
Sync with zig-lang/zig master
Diffstat (limited to 'std/array_list.zig')
-rw-r--r--std/array_list.zig66
1 files changed, 64 insertions, 2 deletions
diff --git a/std/array_list.zig b/std/array_list.zig
index 2a44b66518..f1881cd7f3 100644
--- a/std/array_list.zig
+++ b/std/array_list.zig
@@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
};
}
- pub fn deinit(l: &Self) void {
+ pub fn deinit(l: &const Self) void {
l.allocator.free(l.items);
}
- pub fn toSlice(l: &Self) []align(A) T {
+ pub fn toSlice(l: &const Self) []align(A) T {
return l.items[0..l.len];
}
@@ -44,6 +44,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
return l.toSliceConst()[n];
}
+ pub fn count(self: &const Self) usize {
+ return self.len;
+ }
+
/// ArrayList takes ownership of the passed in slice. The slice must have been
/// allocated with `allocator`.
/// Deinitialize with `deinit` or use `toOwnedSlice`.
@@ -128,6 +132,27 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
return null;
return self.pop();
}
+
+ pub const Iterator = struct {
+ list: &const Self,
+ // how many items have we returned
+ count: usize,
+
+ pub fn next(it: &Iterator) ?T {
+ if (it.count >= it.list.len) return null;
+ const val = it.list.at(it.count);
+ it.count += 1;
+ return val;
+ }
+
+ pub fn reset(it: &Iterator) void {
+ it.count = 0;
+ }
+ };
+
+ pub fn iterator(self: &const Self) Iterator {
+ return Iterator { .list = self, .count = 0 };
+ }
};
}
@@ -143,6 +168,14 @@ test "basic ArrayList test" {
assert(list.items[i] == i32(i + 1));
}}
+ for (list.toSlice()) |v, i| {
+ assert(v == i32(i + 1));
+ }
+
+ for (list.toSliceConst()) |v, i| {
+ assert(v == i32(i + 1));
+ }
+
assert(list.pop() == 10);
assert(list.len == 9);
@@ -157,6 +190,35 @@ test "basic ArrayList test" {
assert(list.len == 9);
}
+test "iterator ArrayList test" {
+ var list = ArrayList(i32).init(debug.global_allocator);
+ defer list.deinit();
+
+ try list.append(1);
+ try list.append(2);
+ try list.append(3);
+
+ var count : i32 = 0;
+ var it = list.iterator();
+ while (it.next()) |next| {
+ assert(next == count + 1);
+ count += 1;
+ }
+
+ assert(count == 3);
+ assert(it.next() == null);
+ it.reset();
+ count = 0;
+ while (it.next()) |next| {
+ assert(next == count + 1);
+ count += 1;
+ if (count == 2) break;
+ }
+
+ it.reset();
+ assert(?? it.next() == 1);
+}
+
test "insert ArrayList test" {
var list = ArrayList(i32).init(debug.global_allocator);
defer list.deinit();