Skip to content
/ rust Public
forked from rust-lang/rust

Commit 1e347e1

Browse files
committed
Use Vec's binary search instead of hand-written one.
1 parent 31e9f7a commit 1e347e1

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/tools/miri/src/concurrency/range_object_map.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,18 @@ impl<T> RangeObjectMap<T> {
4242
/// in an existing allocation, then returns Err containing the position
4343
/// where such allocation should be inserted
4444
fn find_offset(&self, offset: Size) -> Result<Position, Position> {
45-
// We do a binary search.
46-
let mut left = 0usize; // inclusive
47-
let mut right = self.v.len(); // exclusive
48-
loop {
49-
if left == right {
50-
// No element contains the given offset. But the
51-
// position is where such element should be placed at.
52-
return Err(left);
53-
}
54-
let candidate = left.checked_add(right).unwrap() / 2;
55-
let elem = &self.v[candidate];
45+
self.v.binary_search_by(|elem| -> std::cmp::Ordering {
5646
if offset < elem.range.start {
5747
// We are too far right (offset is further left).
58-
debug_assert!(candidate < right); // we are making progress
59-
right = candidate;
48+
std::cmp::Ordering::Greater
6049
} else if offset >= elem.range.end() {
6150
// We are too far left (offset is further right).
62-
debug_assert!(candidate >= left); // we are making progress
63-
left = candidate + 1;
51+
std::cmp::Ordering::Less
6452
} else {
6553
// This is it!
66-
return Ok(candidate);
54+
std::cmp::Ordering::Equal
6755
}
68-
}
56+
})
6957
}
7058

7159
/// Determines whether a given access on `range` overlaps with

0 commit comments

Comments
 (0)