Skip to content
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

Added search functions in Ranges.v3 #288

Merged
merged 2 commits into from
Oct 11, 2024
Merged

Conversation

linxuanm
Copy link
Contributor

@linxuanm linxuanm commented Oct 6, 2024

Added various binary search functions from the optimization PR in Wizard to Ranges.v3.

TODO: add tests in RangesTest.v3

@@ -76,4 +76,40 @@ component Ranges {
}
return i;
}
// Performs binary search and returns the index.
Copy link
Owner

Choose a reason for hiding this comment

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

This code should follow the tab-per-indent level style of the surrounding Virgil code.

while (count > 0) {
var step = count >> 1;
var mid = start + step;
if (!lt(val, range[mid])) { start = mid + 1; count -= step + 1; }
Copy link
Owner

Choose a reason for hiding this comment

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

I think there is a subtle distinction between !lt(x, y) and lt(y, x). I think this should code should use the latter. I think it should be accompanied with a comment that illustrates, like so:

// Assumes that {range} is sorted according to `lt` and returns the first element that is *greater* than `val`.
// e in         | elem  elem  elem  elem  elem  elem  |
// lt(e, val)   | true  true  false false false false |
// lt(val, e)   | false false false true  true  true  |
//                                  ^

I think all three methods could use this comment.

@@ -78,38 +78,38 @@ component Ranges {
}
// Performs binary search and returns the index.
def binarySearch<T>(range: Range<T>, val: T, lt: (T, T) -> bool) -> int {
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should name these three methods binarySearchEq, binarySearchLt, and binarySearchGt and comment them uniformly. See below.

@titzer titzer marked this pull request as ready for review October 11, 2024 15:19
@titzer titzer merged commit 3afb109 into titzer:master Oct 11, 2024
10 checks passed
@linxuanm linxuanm deleted the range-search branch October 27, 2024 05:41
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.

2 participants