]> git.lizzy.rs Git - rust.git/commitdiff
be explicit about doing a binary search; fix out-of-bounds check
authorRalf Jung <post@ralfj.de>
Sat, 5 Jan 2019 15:16:08 +0000 (16:16 +0100)
committerRalf Jung <post@ralfj.de>
Mon, 7 Jan 2019 14:10:22 +0000 (15:10 +0100)
src/range_map.rs

index d157cbf0549f2195a3e42437f7e02267154f89a0..d21f1ed8964d1e132f032492d987c3bdb31d10fa 100644 (file)
@@ -40,10 +40,11 @@ pub fn new(size: Size, init: T) -> RangeMap<T> {
 
     /// Find the index containing the given offset.
     fn find_offset(&self, offset: u64) -> usize {
-        debug_assert!(self.v.len() > 0);
+        // We do a binary search
         let mut left = 0usize; // inclusive
         let mut right = self.v.len(); // exclusive
         loop {
+            debug_assert!(left < right, "find_offset: offset {} is out-of-bounds", offset);
             let candidate = left.checked_add(right).unwrap() / 2;
             let elem = &self.v[candidate];
             if offset < elem.range.start {
@@ -54,7 +55,6 @@ fn find_offset(&self, offset: u64) -> usize {
                 // we are too far left (offset is further right)
                 debug_assert!(candidate >= left); // we are making progress
                 left = candidate+1;
-                debug_assert!(left < right, "find_offset: offset {} is out-of-bounds", offset);
             } else {
                 // This is it!
                 return candidate;