Use Vec's binary search instead of hand-written one.
This commit is contained in:
parent
31e9f7a6ef
commit
1e347e1e73
1 changed files with 5 additions and 17 deletions
|
|
@ -42,30 +42,18 @@ impl<T> RangeObjectMap<T> {
|
|||
/// in an existing allocation, then returns Err containing the position
|
||||
/// where such allocation should be inserted
|
||||
fn find_offset(&self, offset: Size) -> Result<Position, Position> {
|
||||
// We do a binary search.
|
||||
let mut left = 0usize; // inclusive
|
||||
let mut right = self.v.len(); // exclusive
|
||||
loop {
|
||||
if left == right {
|
||||
// No element contains the given offset. But the
|
||||
// position is where such element should be placed at.
|
||||
return Err(left);
|
||||
}
|
||||
let candidate = left.checked_add(right).unwrap() / 2;
|
||||
let elem = &self.v[candidate];
|
||||
self.v.binary_search_by(|elem| -> std::cmp::Ordering {
|
||||
if offset < elem.range.start {
|
||||
// We are too far right (offset is further left).
|
||||
debug_assert!(candidate < right); // we are making progress
|
||||
right = candidate;
|
||||
std::cmp::Ordering::Greater
|
||||
} else if offset >= elem.range.end() {
|
||||
// We are too far left (offset is further right).
|
||||
debug_assert!(candidate >= left); // we are making progress
|
||||
left = candidate + 1;
|
||||
std::cmp::Ordering::Less
|
||||
} else {
|
||||
// This is it!
|
||||
return Ok(candidate);
|
||||
std::cmp::Ordering::Equal
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Determines whether a given access on `range` overlaps with
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue