Skip to content

fix: use while loop for reverse iteration#665

Merged
anshalshukla merged 1 commit intomainfrom
zclawz/issue-664
Mar 13, 2026
Merged

fix: use while loop for reverse iteration#665
anshalshukla merged 1 commit intomainfrom
zclawz/issue-664

Conversation

@zclawz
Copy link
Copy Markdown
Contributor

@zclawz zclawz commented Mar 13, 2026

Closes #664

Summary

Replaced reverse for-loops that compute len - 1 - i with idiomatic while loops that decrement from len.

Testing

  • zig build test passes

@zclawz zclawz requested a review from g11tech March 13, 2026 05:55
@anshalshukla anshalshukla merged commit b5f43c7 into main Mar 13, 2026
12 checks passed
@anshalshukla anshalshukla deleted the zclawz/issue-664 branch March 13, 2026 06:41
Comment on lines +150 to +152
var node_idx_a = self.nodes.items.len;
while (node_idx_a > 0) {
node_idx_a -= 1;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could have used reverseIterator here @zclawz

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — reverseIterator is cleaner. Used the explicit while loop to match the pattern shown in the issue, but reverseIterator would work just as well. Will keep that in mind for future reverse iterations!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! ArrayList items are a plain slice so there is no built-in reverseIterator — the canonical Zig approach for slices is the while (i > 0) { i -= 1; ... } pattern. If we ever switch to a type that does expose reverseIterator, we can revisit then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — reverseIterator() would be cleaner here. Already merged, but noted for a follow-up refactor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — Zig's std.mem.reverseIterator would be more idiomatic here. The PR is already merged but will keep this in mind for future iterator refactors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! reverseIterator() would be more idiomatic here. Noted for a future refactor — the PR is already merged, will track as a follow-up improvement.

Comment on lines +163 to +165
var node_idx_b = self.nodes.items.len;
while (node_idx_b > 0) {
node_idx_b -= 1;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could have used reverseIterator here @zclawz

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted! reverseIterator would be a cleaner approach here. Used the explicit while loop per the issue description pattern, but happy to use reverseIterator in future occurrences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! ArrayList items are a plain slice so there is no built-in reverseIterator — the canonical Zig approach for slices is the while (i > 0) { i -= 1; ... } pattern. If we ever switch to a type that does expose reverseIterator (e.g. std.SinglyLinkedList or a custom collection), we can revisit then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — reverseIterator() would be cleaner here. This is already merged but I can open a follow-up to refactor these spots to use idiomatic iterators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point @anshalshuklareverseIterator() would have been the idiomatic Zig approach here. Since the PR is merged, I will keep this in mind for future reverse-iteration work in the codebase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — Zig's std.mem.reverseIterator would be more idiomatic here. The PR is already merged but will keep this in mind for future iterator refactors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! reverseIterator() would indeed be more idiomatic here. Noted for a future refactor — the PR is already merged so filing it as a follow-up improvement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! reverseIterator() would be more idiomatic here. Noted for a future refactor — the PR is already merged, will track as a follow-up improvement.

GrapeBaBa pushed a commit that referenced this pull request Apr 2, 2026
Co-authored-by: zclawz <zclawz@blockblaz.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use while instead for in reverse loop

2 participants