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

Keep track of position when deleting from a BTreeMap #70795

Merged
merged 2 commits into from
Apr 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2728,8 +2728,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
let mut at_leaf = true;
while cur_node.len() < node::MIN_LEN {
match handle_underfull_node(cur_node) {
AtRoot(_) => break,
EmptyParent(_) => unreachable!(),
AtRoot => break,
Merged(edge, merged_with_left, offset) => {
// If we merged with our right sibling then our tracked
// position has not changed. However if we merged with our
Expand All @@ -2740,7 +2739,6 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
Leaf(leaf) => leaf,
Internal(_) => unreachable!(),
};
debug_assert!(idx <= node.len());
pos = unsafe { Handle::new_edge(node, idx) };
}

Expand All @@ -2754,7 +2752,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
at_leaf = false;
}
}
Stole(_, stole_from_left) => {
Stole(stole_from_left) => {
// Adjust the tracked position if we stole from a left sibling
if stole_from_left && at_leaf {
// SAFETY: This is safe since we just added an element to our node.
Expand All @@ -2781,28 +2779,25 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
}

enum UnderflowResult<'a, K, V> {
AtRoot(NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>),
EmptyParent(NodeRef<marker::Mut<'a>, K, V, marker::Internal>),
AtRoot,
Merged(Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge>, bool, usize),
Stole(NodeRef<marker::Mut<'a>, K, V, marker::Internal>, bool),
Stole(bool),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but now Stole is begging me to be split up into something like StoleFromLeft and StoleFromRight.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then I would have to do the same with MergedWithRight and MergedWithLeft, and that makes just makes the match above more complicated.

}

fn handle_underfull_node<K, V>(
node: NodeRef<marker::Mut<'_>, K, V, marker::LeafOrInternal>,
) -> UnderflowResult<'_, K, V> {
let parent = match node.ascend() {
Ok(parent) => parent,
Err(root) => return AtRoot(root),
Err(_) => return AtRoot,
};

let (is_left, mut handle) = match parent.left_kv() {
Ok(left) => (true, left),
Err(parent) => match parent.right_kv() {
Ok(right) => (false, right),
Err(parent) => {
return EmptyParent(parent.into_node());
}
},
Err(parent) => {
let right = unsafe { unwrap_unchecked(parent.right_kv().ok()) };
(false, right)
}
};

if handle.can_merge() {
Expand All @@ -2814,7 +2809,7 @@ fn handle_underfull_node<K, V>(
} else {
handle.steal_right();
}
Stole(handle.into_node(), is_left)
Stole(is_left)
}
}

Expand Down