-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
ArrayList iterator, unifying API of HashMap and its derivatives #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: &Self) Iterator { | ||
| return Iterator { .list = self, .count = 0 }; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -157,6 +182,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(); | ||
|
|
@@ -174,4 +228,4 @@ test "insert ArrayList test" { | |
| const items = []const i32 { 1 }; | ||
| try list.insertSlice(0, items[0..0]); | ||
| assert(list.items[0] == 5); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newlines were removed from the end of all the files you modified.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't remember having them be part of a standard, if they are I'll add them back; just my visual studio code having its own mind :). |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewrk Currently this seems like the best way to do it? Though I think I could also just index the
.itemsproperty since I do check bounds.