From 17d11ebe6e9599d804c0378900b92a5a8054aec3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 5 Jan 2019 16:16:08 +0100 Subject: [PATCH] be explicit about doing a binary search; fix out-of-bounds check --- src/range_map.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/range_map.rs b/src/range_map.rs index d157cbf0549..d21f1ed8964 100644 --- a/src/range_map.rs +++ b/src/range_map.rs @@ -40,10 +40,11 @@ pub fn new(size: Size, init: T) -> RangeMap { /// 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; -- 2.44.0