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

Commit 43c869d

Browse files
committed
Auto merge of rust-lang#3021 - ttsugriy:bin-search, r=RalfJung
Use Vec's binary search instead of hand-written one.
2 parents f21c657 + 2c69b46 commit 43c869d

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,19 @@ 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+
// (`Greater` means that `elem` is greater than the desired target.)
49+
std::cmp::Ordering::Greater
6050
} else if offset >= elem.range.end() {
6151
// We are too far left (offset is further right).
62-
debug_assert!(candidate >= left); // we are making progress
63-
left = candidate + 1;
52+
std::cmp::Ordering::Less
6453
} else {
6554
// This is it!
66-
return Ok(candidate);
55+
std::cmp::Ordering::Equal
6756
}
68-
}
57+
})
6958
}
7059

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

0 commit comments

Comments
 (0)