rune_alloc/btree/
split.rs

1use core::borrow::Borrow;
2use core::cmp::Ordering;
3
4use super::node::{ForceResult::*, Root};
5use super::search::SearchResult::*;
6
7use crate::alloc::{AllocError, Allocator};
8
9impl<K, V> Root<K, V> {
10    /// Calculates the length of both trees that result from splitting up
11    /// a given number of distinct key-value pairs.
12    pub(crate) fn calc_split_length(
13        total_num: usize,
14        root_a: &Root<K, V>,
15        root_b: &Root<K, V>,
16    ) -> (usize, usize) {
17        let (length_a, length_b);
18        if root_a.height() < root_b.height() {
19            length_a = root_a.reborrow().calc_length();
20            length_b = total_num - length_a;
21            debug_assert_eq!(length_b, root_b.reborrow().calc_length());
22        } else {
23            length_b = root_b.reborrow().calc_length();
24            length_a = total_num - length_b;
25            debug_assert_eq!(length_a, root_a.reborrow().calc_length());
26        }
27        (length_a, length_b)
28    }
29
30    /// Split off a tree with key-value pairs at and after the given key.
31    /// The result is meaningful only if the tree is ordered by key,
32    /// and if the ordering of `Q` corresponds to that of `K`.
33    /// If `self` respects all `BTreeMap` tree invariants, then both
34    /// `self` and the returned tree will respect those invariants.
35    pub(crate) fn split_off<C: ?Sized, Q: ?Sized, A: Allocator, E>(
36        &mut self,
37        cx: &mut C,
38        key: &Q,
39        alloc: &A,
40        cmp: fn(&mut C, &Q, &Q) -> Result<Ordering, E>,
41    ) -> Result<Result<Self, AllocError>, E>
42    where
43        K: Borrow<Q>,
44    {
45        let left_root = self;
46
47        let mut right_root = match Root::new_pillar(left_root.height(), alloc) {
48            Ok(root) => root,
49            Err(e) => return Ok(Err(e)),
50        };
51
52        let mut left_node = left_root.borrow_mut();
53        let mut right_node = right_root.borrow_mut();
54
55        loop {
56            let mut split_edge = match left_node.search_node(cx, key, cmp)? {
57                // key is going to the right tree
58                Found(kv) => kv.left_edge(),
59                GoDown(edge) => edge,
60            };
61
62            split_edge.move_suffix(&mut right_node);
63
64            match (split_edge.force(), right_node.force()) {
65                (Internal(edge), Internal(node)) => {
66                    left_node = edge.descend();
67                    right_node = node.first_edge().descend();
68                }
69                (Leaf(_), Leaf(_)) => break,
70                _ => unreachable!(),
71            }
72        }
73
74        left_root.fix_right_border(alloc);
75        right_root.fix_left_border(alloc);
76        Ok(Ok(right_root))
77    }
78
79    /// Creates a tree consisting of empty nodes.
80    fn new_pillar<A: Allocator>(height: usize, alloc: &A) -> Result<Self, AllocError> {
81        let mut root = Root::new(alloc)?;
82
83        for _ in 0..height {
84            root.push_internal_level(alloc)?;
85        }
86
87        Ok(root)
88    }
89}