1#![cfg_attr(not(any(test, feature = "internal_benches")), no_std)]
5#![warn(missing_docs)]
6
7use core::mem;
8
9use allocator_api2::alloc::{Allocator, Global};
10use int::BTreeInteger;
11use node::{NodePool, NodeRef, UninitNodeRef};
12use stack::Height;
13
14#[macro_use]
15mod node;
16
17mod cursor;
18mod int;
19mod iter;
20mod simd;
21mod stack;
22#[cfg(test)]
23mod tests;
24
25pub use nonmax;
26
27pub use cursor::*;
28pub use iter::*;
29
30use crate::int::int_from_key;
31
32pub trait BTreeKey: Copy {
42 #[allow(private_bounds)]
56 type Int: BTreeInteger;
57
58 fn to_int(self) -> Self::Int;
60
61 fn from_int(int: Self::Int) -> Self;
63}
64
65pub struct BTree<K: BTreeKey, V, A: Allocator = Global> {
87 internal: NodePool<K::Int, NodeRef>,
88 leaf: NodePool<K::Int, V>,
89 height: Height<K::Int>,
90 root: NodeRef,
91 alloc: A,
92}
93
94impl<K: BTreeKey, V> BTree<K, V, Global> {
95 #[inline]
99 pub fn new() -> Self {
100 Self::new_in(Global)
101 }
102}
103
104impl<K: BTreeKey, V, A: Allocator> BTree<K, V, A> {
105 #[inline]
109 pub fn new_in(alloc: A) -> Self {
110 let mut out = Self {
111 internal: NodePool::new(),
112 leaf: NodePool::new(),
113 height: Height::leaf(),
114 root: NodeRef::zero(),
115 alloc,
116 };
117 let root = unsafe { out.leaf.alloc_node(&out.alloc) };
118 out.init_root(root);
119 out
120 }
121
122 #[inline]
124 fn init_root(&mut self, root: UninitNodeRef) {
125 let root = unsafe { root.init_keys(&mut self.leaf) };
126 unsafe {
127 root.set_next_leaf(None, &mut self.leaf);
128 }
129 debug_assert_eq!(root, NodeRef::zero());
130 self.root = NodeRef::zero();
131 }
132
133 #[inline]
135 pub fn clear(&mut self) {
136 if mem::needs_drop::<V>() {
139 let mut iter = self.raw_iter();
140 while let Some((_key, value_ptr)) = unsafe { iter.next(&self.leaf) } {
141 unsafe {
142 value_ptr.drop_in_place();
143 }
144 }
145 }
146
147 let root = self.leaf.clear_and_alloc_node();
149 self.internal.clear();
150
151 self.height = Height::leaf();
153 self.init_root(root);
154 }
155
156 #[inline]
158 pub fn is_empty(&self) -> bool {
159 if self.height != Height::leaf() {
160 return false;
161 }
162 let first_key = unsafe { self.root.key(pos!(0), &self.leaf) };
163 first_key == K::Int::MAX
164 }
165
166 #[inline]
168 pub fn get(&self, key: K) -> Option<&V> {
169 self.range(key..=key).next().map(|(_k, v)| v)
170 }
171
172 #[inline]
174 pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
175 self.range_mut(key..=key).next().map(|(_k, v)| v)
176 }
177
178 #[inline]
185 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
186 let mut cursor = unsafe { CursorMut::uninit(self) };
187 cursor.seek(int_from_key(key));
188 if let Some((k, v)) = cursor.entry_mut()
189 && k.to_int() == key.to_int()
190 {
191 return Some(mem::replace(v, value));
192 }
193 cursor.insert_before(key, value);
194 None
195 }
196
197 #[inline]
205 pub fn insert_multi(&mut self, key: K, value: V) {
206 let mut cursor = unsafe { CursorMut::uninit(self) };
207 cursor.seek(int_from_key(key));
208 cursor.insert_before(key, value);
209 }
210
211 #[inline]
214 pub fn remove(&mut self, key: K) -> Option<V> {
215 let mut cursor = unsafe { CursorMut::uninit(self) };
216 cursor.seek(int_from_key(key));
217 if cursor.key()?.to_int() == key.to_int() {
218 return Some(cursor.remove().1);
219 }
220 None
221 }
222}
223
224impl<K: BTreeKey, V, A: Allocator> Drop for BTree<K, V, A> {
225 #[inline]
226 fn drop(&mut self) {
227 if mem::needs_drop::<V>() {
230 let mut iter = self.raw_iter();
231 while let Some((_key, value_ptr)) = unsafe { iter.next(&self.leaf) } {
232 unsafe {
233 value_ptr.drop_in_place();
234 }
235 }
236 }
237
238 unsafe {
240 self.internal.clear_and_free(&self.alloc);
241 self.leaf.clear_and_free(&self.alloc);
242 }
243 }
244}
245
246impl<K: BTreeKey, V, A: Default + Allocator> Default for BTree<K, V, A> {
247 #[inline]
248 fn default() -> Self {
249 Self::new_in(Default::default())
250 }
251}
252
253impl<K: BTreeKey, V> FromIterator<(K, V)> for BTree<K, V> {
254 #[inline]
255 fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
256 let mut btree = BTree::new();
257 btree.extend(iter);
258 btree
259 }
260}
261
262impl<K: BTreeKey, V, A: Allocator> Extend<(K, V)> for BTree<K, V, A> {
263 #[inline]
264 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
265 iter.into_iter().for_each(|(k, v)| {
266 self.insert(k, v);
267 });
268 }
269}
270
271impl<K: BTreeKey, V: Clone, A: Allocator + Clone> Clone for BTree<K, V, A> {
272 #[inline]
273 fn clone(&self) -> Self {
274 let mut btree = BTree::new_in(self.alloc.clone());
275 btree.extend(self.iter());
276 btree
277 }
278}
279
280impl<'a, K: BTreeKey, V: Clone, A: Allocator> Extend<(K, &'a V)> for BTree<K, V, A> {
281 #[inline]
282 fn extend<T: IntoIterator<Item = (K, &'a V)>>(&mut self, iter: T) {
283 iter.into_iter().for_each(|(k, v)| {
284 self.insert(k, v.clone());
285 });
286 }
287}