Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in remove_index for OrdMap #141

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/nodes/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ impl<A> Node<A> {
self.keys.len() < MEDIAN
}

#[inline]
fn is_leaf(&self) -> bool {
self.children[0].is_none()
}

#[inline]
pub(crate) fn unit(value: A) -> Self {
Node {
Expand Down Expand Up @@ -643,26 +638,35 @@ 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),
// If the left hand child has capacity, pull the predecessor up.
(&Some(ref left), _) if !left.too_small() => {
if left.is_leaf() {
RemoveAction::PullUp(left.keys.len() - 1, index, index)
} else {
// Right is empty. Attempt to steal from left if enough capacity,
// otherwise pull the predecessor up.
(&Some(ref left), &None) => {
if !left.too_small() {
RemoveAction::StealFromLeft(index + 1)
} else {
RemoveAction::PullUp(left.keys.len() - 1, index, index)
}
}
// If the right hand child has capacity, pull the successor up.
(_, &Some(ref right)) if !right.too_small() => {
if right.is_leaf() {
RemoveAction::PullUp(0, index, index + 1)
// Left is empty. Attempt to steal from right if enough capacity,
// otherwise pull the successor up.
(&None, &Some(ref right)) => {
if !right.too_small() {
RemoveAction::StealFromRight(index)
} else {
RemoveAction::PullUp(0, index, index + 1)
}
}
// 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() {
RemoveAction::StealFromRight(index)
} else if right.has_room() && !left.too_small() {
RemoveAction::StealFromLeft(index + 1)
} else {
RemoveAction::Merge(index)
}
}
// If neither child has capacity, we'll have to merge them.
(&Some(_), &Some(_)) => RemoveAction::Merge(index),
// If one child exists and the other doesn't, we're in a bad state.
_ => unreachable!(),
}
}
// Key is adjacent to some key in node
Expand Down
13 changes: 13 additions & 0 deletions src/ord/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,19 @@ mod test {
assert_eq!(vec![(3, 4), (2, 3), (1, 2)], range);
}

#[test]
fn issue_124() {
let mut map = OrdMap::new();
let contents = include_str!("../../test-fixtures/issue_124.txt");
for line in contents.split('\n') {
if line.starts_with("insert ") {
map.insert(line[7..].parse::<u32>().unwrap(), 0);
} else if line.starts_with("remove ") {
map.remove(&line[7..].parse::<u32>().unwrap());
}
}
}

proptest! {
#[test]
fn length(ref input in collection::btree_map(i16::ANY, i16::ANY, 0..1000)) {
Expand Down