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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/util/Ranges.v3
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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.

var l = 0, r = range.length;
while (l < r) {
var mid = (l + r) >> 1;
var mid_val = range[mid];
if (lt(val, mid_val)) r = mid - 1;
else if (lt(mid_val, val)) l = mid + 1;
else return mid;
}
return -1;
}
// Returns the *first* element that is *not less* than `val` in a sorted range.
def lowerbound<T>(range: Range<T>, val: T, lt: (T, T) -> bool) -> int {
var start = 0;
var count = range.length;
while (count > 0) {
var step = count >> 1;
var mid = start + step;
if (lt(range[mid], val)) { start = mid + 1; count -= step + 1; }
else count = step;
}
return start;
}
// Returns the *first* element that is *greater* than `val` in a sorted range.
def upperbound<T>(range: Range<T>, val: T, lt: (T, T) -> bool) -> int {
var start = 0;
var count = range.length;
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.

else count = step;
}
return start;
}
}