Minor stylistic fixes.

This commit is contained in:
whitequark 2018-05-24 21:17:37 +00:00
parent 4299b43859
commit 1328e59fc7
1 changed files with 22 additions and 20 deletions

View File

@ -127,7 +127,7 @@ fn binary_search_by_key_range<'a, K, V, Q: 'a, R>(slice: &[Option<(K, V)>], rang
if slice.len() == 0 { if slice.len() == 0 {
return Err(()) return Err(())
} }
let (mut left, mut right) = (0, slice.len()-1); let (mut left, mut right) = (0, slice.len() - 1);
macro_rules! key { macro_rules! key {
( $e:expr) => { $e.as_ref().map(|&(ref key, _)| key.borrow()) } ( $e:expr) => { $e.as_ref().map(|&(ref key, _)| key.borrow()) }
@ -139,64 +139,66 @@ fn binary_search_by_key_range<'a, K, V, Q: 'a, R>(slice: &[Option<(K, V)>], rang
// any of these guarantees. // any of these guarantees.
// Find the beginning // Find the beginning
let begin = if let Bound::Unbounded = range.start() { let begin;
0 if let Bound::Unbounded = range.start() {
begin = 0;
} else { } else {
macro_rules! is_before_range { macro_rules! is_before_range {
( $item: expr) => { ( $item: expr) => {
match &range.start() { match &range.start() {
Bound::Included(ref key_begin) => $item < Some(key_begin.borrow()), Bound::Included(ref key_begin) => $item < Some(key_begin.borrow()),
Bound::Excluded(ref key_begin) => $item <= Some(key_begin.borrow()), Bound::Excluded(ref key_begin) => $item <= Some(key_begin.borrow()),
Bound::Unbounded => unreachable!(), Bound::Unbounded => unreachable!()
} }
} }
}; };
while left < right { while left < right {
let middle = left + (right-left)/2; let middle = left + (right - left) / 2;
if is_before_range!(key!(slice[middle])) { if is_before_range!(key!(slice[middle])) {
left = middle+1; left = middle + 1;
} else if middle > 0 && !is_before_range!(key!(slice[middle-1])) { } else if middle > 0 && !is_before_range!(key!(slice[middle - 1])) {
right = middle-1; right = middle - 1;
} else { } else {
left = middle; left = middle;
break; break
} }
} }
if left == slice.len() || is_before_range!(key!(slice[left])) { if left == slice.len() || is_before_range!(key!(slice[left])) {
return Err(()) return Err(())
} }
left begin = left
}; };
// Find the ending // Find the ending
let end = if let Bound::Unbounded = range.end() { let end;
slice.len() if let Bound::Unbounded = range.end() {
end = slice.len()
} else { } else {
macro_rules! is_after_range { macro_rules! is_after_range {
( $item:expr ) => { ( $item:expr ) => {
match &range.end() { match &range.end() {
Bound::Included(ref key_end) => $item > Some(key_end.borrow()), Bound::Included(ref key_end) => $item > Some(key_end.borrow()),
Bound::Excluded(ref key_end) => $item >= Some(key_end.borrow()), Bound::Excluded(ref key_end) => $item >= Some(key_end.borrow()),
Bound::Unbounded => unreachable!(), Bound::Unbounded => unreachable!()
} }
} }
}; };
right = slice.len(); // no need to reset left right = slice.len(); // no need to reset left
while left < right { while left < right {
let middle = left + (right-left+1)/2; let middle = left + (right - left + 1) / 2;
if is_after_range!(key!(slice[middle-1])) { if is_after_range!(key!(slice[middle - 1])) {
right = middle-1; right = middle - 1;
} else if middle < slice.len() && !is_after_range!(key!(slice[middle])) { } else if middle < slice.len() && !is_after_range!(key!(slice[middle])) {
left = middle+1; left = middle + 1;
} else { } else {
right = middle; right = middle;
break; break
} }
} }
if right == 0 || is_after_range!(key!(slice[right-1])) { if right == 0 || is_after_range!(key!(slice[right - 1])) {
return Err(()) return Err(())
} }
right end = right
}; };
Ok((begin, end)) Ok((begin, end))