alloc/collections/btree/
remove.rs1use core::alloc::Allocator;
2
3use super::map::MIN_LEN;
4use super::node::ForceResult::*;
5use super::node::LeftOrRight::*;
6use super::node::{Handle, NodeRef, marker};
7
8impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV> {
9 pub(super) fn remove_kv_tracking<F: FnOnce(), A: Allocator + Clone>(
14 self,
15 handle_emptied_internal_root: F,
16 alloc: A,
17 ) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
18 match self.force() {
19 Leaf(node) => node.remove_leaf_kv(handle_emptied_internal_root, alloc),
20 Internal(node) => node.remove_internal_kv(handle_emptied_internal_root, alloc),
21 }
22 }
23}
24
25impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV> {
26 fn remove_leaf_kv<F: FnOnce(), A: Allocator + Clone>(
27 self,
28 handle_emptied_internal_root: F,
29 alloc: A,
30 ) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
31 let (old_kv, mut pos) = self.remove();
32 let len = pos.reborrow().into_node().len();
33 if len < MIN_LEN {
34 let idx = pos.idx();
35 let new_pos = match pos.into_node().forget_type().choose_parent_kv() {
38 Ok(Left(left_parent_kv)) => {
39 debug_assert!(left_parent_kv.right_child_len() == MIN_LEN - 1);
40 if left_parent_kv.can_merge() {
41 left_parent_kv.merge_tracking_child_edge(Right(idx), alloc.clone())
42 } else {
43 debug_assert!(left_parent_kv.left_child_len() > MIN_LEN);
44 left_parent_kv.steal_left(idx)
45 }
46 }
47 Ok(Right(right_parent_kv)) => {
48 debug_assert!(right_parent_kv.left_child_len() == MIN_LEN - 1);
49 if right_parent_kv.can_merge() {
50 right_parent_kv.merge_tracking_child_edge(Left(idx), alloc.clone())
51 } else {
52 debug_assert!(right_parent_kv.right_child_len() > MIN_LEN);
53 right_parent_kv.steal_right(idx)
54 }
55 }
56 Err(pos) => unsafe { Handle::new_edge(pos, idx) },
57 };
58 pos = unsafe { new_pos.cast_to_leaf_unchecked() };
60
61 if let Ok(parent) = unsafe { pos.reborrow_mut() }.into_node().ascend() {
69 if !parent.into_node().forget_type().fix_node_and_affected_ancestors(alloc) {
70 handle_emptied_internal_root();
71 }
72 }
73 }
74 (old_kv, pos)
75 }
76}
77
78impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::KV> {
79 fn remove_internal_kv<F: FnOnce(), A: Allocator + Clone>(
80 self,
81 handle_emptied_internal_root: F,
82 alloc: A,
83 ) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
84 let left_leaf_kv = self.left_edge().descend().last_leaf_edge().left_kv();
88 let left_leaf_kv = unsafe { left_leaf_kv.ok().unwrap_unchecked() };
89 let (left_kv, left_hole) = left_leaf_kv.remove_leaf_kv(handle_emptied_internal_root, alloc);
90
91 let mut internal = unsafe { left_hole.next_kv().ok().unwrap_unchecked() };
94 let old_kv = internal.replace_kv(left_kv.0, left_kv.1);
95 let pos = internal.next_leaf_edge();
96 (old_kv, pos)
97 }
98}