@@ -42,30 +42,19 @@ impl<T> RangeObjectMap<T> {
42
42
/// in an existing allocation, then returns Err containing the position
43
43
/// where such allocation should be inserted
44
44
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 {
56
46
if offset < elem. range . start {
57
47
// 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
60
50
} else if offset >= elem. range . end ( ) {
61
51
// 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
64
53
} else {
65
54
// This is it!
66
- return Ok ( candidate ) ;
55
+ std :: cmp :: Ordering :: Equal
67
56
}
68
- }
57
+ } )
69
58
}
70
59
71
60
/// Determines whether a given access on `range` overlaps with
0 commit comments