From 3f4e01a43254fe228d1ce64e47dfaf4edc8f4f19 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 20 Jul 2020 16:49:10 -0700 Subject: [PATCH] Fix OrdMap::range including keys outside of requested range if end bound doesn't exist in tree. Fixes issue #143 --- src/nodes/btree.rs | 6 +++--- src/ord/map.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/nodes/btree.rs b/src/nodes/btree.rs index 62c6dbb..79a5bc1 100644 --- a/src/nodes/btree.rs +++ b/src/nodes/btree.rs @@ -427,7 +427,7 @@ impl Node { 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(), @@ -638,7 +638,7 @@ impl Node { 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() { @@ -656,7 +656,7 @@ impl Node { 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() { diff --git a/src/ord/map.rs b/src/ord/map.rs index 46c0962..14931f7 100644 --- a/src/ord/map.rs +++ b/src/ord/map.rs @@ -2354,19 +2354,19 @@ mod test { #[test] fn ranged_iter() { - let map: OrdMap = ordmap![1=>2, 2=>3, 3=>4, 4=>5, 5=>6]; + let map: OrdMap = 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(); @@ -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]