Skip to content

Commit

Permalink
Fix OrdMap::range including keys outside of requested range if end bo…
Browse files Browse the repository at this point in the history
…und doesn't exist in tree.

Fixes issue #143
  • Loading branch information
theli-ua authored and bodil committed Jul 27, 2020
1 parent 2cae6fb commit 3f4e01a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/nodes/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl<A: BTreeValue> Node<A> {
None if index == 0 => Vec::new(),
None => match self.keys.get(index - 1) {
Some(_) => {
path.push((self, index));
path.push((self, index - 1));
path
}
None => Vec::new(),
Expand Down Expand Up @@ -638,7 +638,7 @@ impl<A: BTreeValue> Node<A> {
match (&self.children[index], &self.children[index + 1]) {
// If we're a leaf, just delete the entry.
(&None, &None) => RemoveAction::DeleteAt(index),
// Right is empty. Attempt to steal from left if enough capacity,
// Right is empty. Attempt to steal from left if enough capacity,
// otherwise pull the predecessor up.
(&Some(ref left), &None) => {
if !left.too_small() {
Expand All @@ -656,7 +656,7 @@ impl<A: BTreeValue> Node<A> {
RemoveAction::PullUp(0, index, index + 1)
}
}
// Both left and right are non empty. Attempt to steal from left or
// Both left and right are non empty. Attempt to steal from left or
// right if enough capacity, otherwise just merge the children.
(&Some(ref left), &Some(ref right)) => {
if left.has_room() && !right.too_small() {
Expand Down
14 changes: 9 additions & 5 deletions src/ord/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,19 +2354,19 @@ mod test {

#[test]
fn ranged_iter() {
let map: OrdMap<i32, i32> = ordmap![1=>2, 2=>3, 3=>4, 4=>5, 5=>6];
let map: OrdMap<i32, i32> = ordmap![1=>2, 2=>3, 3=>4, 4=>5, 5=>6, 7=>8];
let range: Vec<(i32, i32)> = map.range(..).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], range);
assert_eq!(vec![(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (7, 8)], range);
let range: Vec<(i32, i32)> = map.range(..).rev().map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(5, 6), (4, 5), (3, 4), (2, 3), (1, 2)], range);
assert_eq!(vec![(7, 8), (5, 6), (4, 5), (3, 4), (2, 3), (1, 2)], range);
let range: Vec<(i32, i32)> = map.range(2..5).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(2, 3), (3, 4), (4, 5)], range);
let range: Vec<(i32, i32)> = map.range(2..5).rev().map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(4, 5), (3, 4), (2, 3)], range);
let range: Vec<(i32, i32)> = map.range(3..).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(3, 4), (4, 5), (5, 6)], range);
assert_eq!(vec![(3, 4), (4, 5), (5, 6), (7, 8)], range);
let range: Vec<(i32, i32)> = map.range(3..).rev().map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(5, 6), (4, 5), (3, 4)], range);
assert_eq!(vec![(7, 8), (5, 6), (4, 5), (3, 4)], range);
let range: Vec<(i32, i32)> = map.range(..4).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(1, 2), (2, 3), (3, 4)], range);
let range: Vec<(i32, i32)> = map.range(..4).rev().map(|(k, v)| (*k, *v)).collect();
Expand All @@ -2375,6 +2375,10 @@ mod test {
assert_eq!(vec![(1, 2), (2, 3), (3, 4)], range);
let range: Vec<(i32, i32)> = map.range(..=3).rev().map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(3, 4), (2, 3), (1, 2)], range);
let range: Vec<(i32, i32)> = map.range(..6).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], range);
let range: Vec<(i32, i32)> = map.range(..=6).map(|(k, v)| (*k, *v)).collect();
assert_eq!(vec![(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], range);
}

#[test]
Expand Down

0 comments on commit 3f4e01a

Please sign in to comment.