-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
@@ -76,4 +76,40 @@ component Ranges { | |||
} | |||
return i; | |||
} | |||
// Performs binary search and returns the index. |
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.
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; } |
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.
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 { |
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.
I think we should name these three methods binarySearchEq
, binarySearchLt
, and binarySearchGt
and comment them uniformly. See below.
Added various binary search functions from the optimization PR in Wizard to
Ranges.v3
.TODO: add tests in
RangesTest.v3