1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::sync::Arc;

use crate::*;

#[derive(Debug)]
pub struct Node<K, V> {
    pub key: Option<Item<K, V>>,
    length: usize,
    pub children: Vec<N<K, V>>,
}

impl<K, V> Node<K, V>
where
    K: Ord,
{
    pub fn instance(children: Vec<N<K, V>>) -> N<K, V> {
        let key = if children.is_empty() {
            None
        } else {
            children[0].key().cloned()
        };
        let length = children.iter().map(|v| v.len()).sum();
        Arc::new(BTreeType::Node(Self {
            key,
            length,
            children,
        }))
    }

    pub fn put(&self, m: usize, k: K, v: V) -> PutResult<K, V> {
        let index = self.search_index(&k);

        let (values, old) = self.children[index].put(m, k, v);

        let mut children = Vec::with_capacity(self.children.len() + values.len());

        children.extend(self.children[..index].iter().cloned());
        children.extend(values);
        children.extend(self.children[index + 1..].iter().cloned());

        if children.len() < m {
            return (vec![Self::instance(children)], old);
        }

        let mid = m / 2;

        let left = children[..mid].to_vec();
        let right = children[mid..].to_vec();

        (vec![Self::instance(left), Self::instance(right)], old)
    }

    pub fn get(&self, k: &K) -> Option<&V> {
        self.children[self.search_index(k)].get(k)
    }

    pub fn remove(&self, k: &K) -> Option<(N<K, V>, Item<K, V>)> {
        let index = self.search_index(k);

        let (child, item) = self.children[index].remove(k)?;

        let mut children = Vec::with_capacity(self.children.len() - 1);
        children.extend(self.children[..index].iter().cloned());
        if child.len() > 0 {
            children.push(child);
        }
        children.extend(self.children[index + 1..].iter().cloned());

        Some((Self::instance(children), item))
    }

    pub fn write(&self, m: usize, mut actions: BTreeMap<K, Action<V>>) -> Vec<N<K, V>> {
        let mut children = Vec::with_capacity(self.children.len() + actions.len());

        let mut start_index = 0;

        loop {
            if let Some((k, _)) = actions.first_key_value() {
                let index = self.search_index(k);

                if start_index < index {
                    children.extend_from_slice(&self.children[start_index..index]);
                }

                if index + 1 < self.children.len() {
                    //get next key for current childs
                    if let Some(k) = self.children[index + 1].key() {
                        let temp = actions.split_off(&k.0);
                        children.extend(self.children[index].write(m, actions));
                        start_index = index + 1;
                        actions = temp;
                    }
                } else {
                    children.extend(self.children[index].write(m, actions));
                    break;
                }
            } else {
                children.extend_from_slice(&self.children[start_index..]);
                break;
            }
        }

        children
            .chunks(m)
            .filter_map(|c| {
                if c.is_empty() {
                    None
                } else {
                    Some(Self::instance(c.to_vec()))
                }
            })
            .collect()
    }

    pub fn len(&self) -> usize {
        self.length
    }

    pub fn split_off(&self, k: &K) -> (N<K, V>, N<K, V>) {
        let index = self.search_index(k);

        let (l, r) = self.children[index].split_off(k);

        let mut left = Vec::with_capacity(index);
        left.extend_from_slice(&self.children[..index]);
        if l.len() > 0 {
            left.push(l);
        }

        let mut right = Vec::with_capacity(self.children.len() - index);
        if r.len() > 0 {
            right.push(r);
        }
        right.extend_from_slice(&self.children[index + 1..]);

        (Self::instance(left), Self::instance(right))
    }

    pub fn search_index(&self, k: &K) -> usize {
        match self.children.binary_search_by(|c| cmp(c.key(), Some(k))) {
            Ok(i) => i,
            Err(i) => {
                if i == 0 {
                    i
                } else {
                    i - 1
                }
            }
        }
    }
}