btree_slab/generic/map/
ext.rs

1use crate::generic::{
2	map::{BTreeMap, M},
3	node::{Address, Balance, Item, Node, Offset},
4};
5use cc_traits::{SimpleCollectionMut, SimpleCollectionRef, Slab, SlabMut};
6use smallvec::SmallVec;
7use std::{borrow::Borrow, mem::MaybeUninit};
8
9/// Extended API.
10///
11/// This trait can be imported to access the internal functions of the B-Tree.
12/// These functions are not intended to be directly called by the users, but can be used to
13/// extends the data structure with new functionalities.
14///
15/// # Addressing
16///
17/// In this implementation of B-Trees, each node of a tree is addressed
18/// by the [`Address`] type.
19/// Each node is identified by a `usize`, and each item/entry in the node by an [`Offset`].
20/// This extended API allows the caller to explore, access and modify the
21/// internal structure of the tree using this addressing system.
22///
23/// Note that a valid address does not always refer to an actual item in the tree.
24/// See the [`Address`] type documentation for more details.
25pub trait BTreeExt<K, V> {
26	/// Get the root node id.
27	///
28	/// Returns `None` if the tree is empty.
29	fn root_id(&self) -> Option<usize>;
30
31	/// Get the node associated to the given `id`.
32	///
33	/// Panics if `id` is out of bounds.
34	fn node(&self, id: usize) -> &Node<K, V>;
35
36	/// Get a reference to the value associated to the given `key` in the node `id`, if any.
37	fn get_in<Q: ?Sized>(&self, key: &Q, id: usize) -> Option<&V>
38	where
39		K: Borrow<Q>,
40		Q: Ord;
41
42	/// Get a reference to the item located at the given address.
43	fn item(&self, addr: Address) -> Option<&Item<K, V>>;
44
45	/// Get the first item address, if any.
46	///
47	/// Returns the first occupied valid address, or `None` if the tree is empty.
48	fn first_item_address(&self) -> Option<Address>;
49
50	/// Get the first back address.
51	///
52	/// The returned address may not be occupied if the tree is empty.
53	fn first_back_address(&self) -> Address;
54
55	/// Get the last item address, if any.
56	///
57	/// Returns the last occupied valid address, or `None` if the tree is empty.
58	fn last_item_address(&self) -> Option<Address>;
59
60	/// Get the last valid address in the tree.
61	fn last_valid_address(&self) -> Address;
62
63	/// Normalizes the given item address so that an out-of-node-bounds address points to the next item.
64	fn normalize(&self, addr: Address) -> Option<Address>;
65
66	/// Returns the greatest valid leaf address that directly precedes the given address.
67	///
68	/// A "leaf address" is an address located in a leaf node.
69	fn leaf_address(&self, addr: Address) -> Address;
70
71	/// Get the previous item address.
72	///
73	/// Returns the previous valid occupied address.
74	///
75	/// The following diagram shows the order between addresses defined by this function.
76	/// ```text
77	///                                          ┌───────────┐
78	///                            ╔═════════════╪══╗  ╔══╗  │
79	///                            ║             │┌─v─┐║┌─v─┐│  
80	///                ┌───────────╫─────────────││ 0 │║│ 1 ││──────────────────────┐
81	///                │           ║             │└─v─┘║└─v─┘│                      │
82	///                │           ║             └──╫──╫──╫──┘                      │
83	///    start v     │           ║                ║  ║│ ╚══════════════════════╗  │  ^ end
84	///          ║     │           ║             ╔══╝  ╚╪══════════╗             ║  │  ║
85	///       ┌──╫──────────────┐  ║          ┌──╫──────────────┐  ║          ┌──╫─────╫──┐
86	///       │  ║     ╔═════╗  │  ║          │  ║     ╔═════╗  │  ║          │  ║     ║  │
87	///       │┌─v─┐ ┌─^─┐ ┌─v─┐│  ║          │┌─v─┐ ┌─^─┐ ┌─v─┐│  ║          │┌─v─┐ ┌─^─┐│
88	///       ││ 0 │ │ 1 │ │ 2 ││  ║          ││ 0 │ │ 1 │ │ 2 ││  ║          ││ 0 │ │ 1 ││
89	///       │└─v─┘ └─^─┘ └─v─┘│  ║          │└─v─┘ └─^─┘ └─v─┘│  ║          │└─v─┘ └─^─┘│
90	///       │  ╚═════╝     ╚══╪══╝          │  ╚═════╝     ╚══╪══╝          │  ╚═════╝  │
91	///       └─────────────────┘             └─────────────────┘             └───────────┘
92	/// ```
93	fn previous_item_address(&self, addr: Address) -> Option<Address>;
94
95	/// Get the previous front address.
96	///
97	/// A "front address" is a valid address whose offset is less that the number of items in the node.
98	/// If `addr.offset` is equal to `-1`, then it doesn't actually refer to an existing item in the node.
99	///
100	/// The following diagram shows the order between addresses defined by this function.
101	/// ```text
102	///                                                         ^ end
103	///                                               ┌─────────║──┐
104	///                            ╔═══════════════╗  │╔══╗     ║  │
105	///                            ║             ┌─v─┐│║┌─v─┐ ┌─^─┐│
106	///                      ┌─────╫─────────────│-1 ││║│ 0 │ │ 1 ││ ─────────────────────┐
107	///                      │     ║             └─v─┘│║└─v─┘ └─^─┘│                      │
108	///                      │     ║               ║  └╫──╫─────╫──┘                      │
109	///                      │     ║               ║   ║  ║  │  ╚═════════════════════════╪═══════╗
110	///    start v           │     ║               ║   ║  ╚══╪═══════════════════╗        │       ║
111	///          ║           │     ║             ╔═╝   ╚═════╪═════╗             ║        │       ║
112	///          ║  ┌──────────────╫──┐          ║  ┌──────────────╫──┐          ║  ┌───────────┐ ║
113	///          ║  │  ╔═════╗     ║  │          ║  │  ╔═════╗     ║  │          ║  │  ╔═════╗  │ ║
114	///        ┌─v─┐│┌─^─┐ ┌─v─┐ ┌─^─┐│        ┌─v─┐│┌─^─┐ ┌─v─┐ ┌─^─┐│        ┌─v─┐│┌─^─┐ ┌─v─┐│ ║
115	///        │-1 │││ 0 │ │ 1 │ │ 2 ││        │-1 │││ 0 │ │ 1 │ │ 2 ││        │-1 │││ 0 │ │ 1 ││ ║
116	///        └─v─┘│└─^─┘ └─v─┘ └─^─┘│        └─v─┘│└─^─┘ └─v─┘ └─^─┘│        └─v─┘│└─^─┘ └─v─┘│ ║
117	///          ╚══╪══╝     ╚═════╝  │          ╚══╪══╝     ╚═════╝  │          ╚══╪══╝     ╚══╪═╝
118	///             └─────────────────┘             └─────────────────┘             └───────────┘
119	/// ```
120	fn previous_front_address(&self, addr: Address) -> Option<Address>;
121
122	/// Get the next item address.
123	///
124	/// Returns the next valid occupied address.
125	///
126	/// The following diagram shows the order between addresses defined by this function.
127	/// ```text
128	///                                          ┌───────────┐
129	///                            ╔═════════════╪══╗  ╔══╗  │
130	///                            ║             │┌─v─┐║┌─v─┐│  
131	///                ┌───────────╫─────────────││ 0 │║│ 1 ││──────────────────────┐
132	///                │           ║             │└─v─┘║└─v─┘│                      │
133	///                │           ║             └──╫──╫──╫──┘                      │
134	///    start v     │           ║                ║  ║│ ╚══════════════════════╗  │  ^ end
135	///          ║     │           ║             ╔══╝  ╚╪══════════╗             ║  │  ║
136	///       ┌──╫──────────────┐  ║          ┌──╫──────────────┐  ║          ┌──╫─────╫──┐
137	///       │  ║     ╔═════╗  │  ║          │  ║     ╔═════╗  │  ║          │  ║     ║  │
138	///       │┌─v─┐ ┌─^─┐ ┌─v─┐│  ║          │┌─v─┐ ┌─^─┐ ┌─v─┐│  ║          │┌─v─┐ ┌─^─┐│
139	///       ││ 0 │ │ 1 │ │ 2 ││  ║          ││ 0 │ │ 1 │ │ 2 ││  ║          ││ 0 │ │ 1 ││
140	///       │└─v─┘ └─^─┘ └─v─┘│  ║          │└─v─┘ └─^─┘ └─v─┘│  ║          │└─v─┘ └─^─┘│
141	///       │  ╚═════╝     ╚══╪══╝          │  ╚═════╝     ╚══╪══╝          │  ╚═════╝  │
142	///       └─────────────────┘             └─────────────────┘             └───────────┘
143	/// ```
144	fn next_item_address(&self, addr: Address) -> Option<Address>;
145
146	/// Get the next back address.
147	///
148	/// A "back address" is a valid address whose offset is at least `0`.
149	/// If `addr.offset` is equal to the number of items in the node then it doesn't actually refer
150	/// to an existing item in the node,
151	/// but it can be used to insert a new item with `BTreeExt::insert_at`.
152	///
153	/// The following diagram shows the order between addresses defined by this function.
154	/// ```text
155	///                                          ┌───────────┐  ^ end
156	///                            ╔═════════════╪══╗  ╔══╗  │  ║
157	///                            ║             │┌─v─┐║┌─v─┐│┌─^─┐
158	///                ┌───────────╫─────────────││ 0 │║│ 1 │││ 2 │─────────────────┐
159	///                │           ║             │└─v─┘║└─v─┘│└─^─┘                 │
160	///                │           ║             └──╫──╫──╫──┘  ╚═══════════════════╪════════════╗
161	///    start v     │           ║                ║  ║│ ╚══════════════════════╗  │            ║
162	///          ║     │           ║             ╔══╝  ╚╪══════════╗             ║  │            ║
163	///       ┌──╫──────────────┐  ║          ┌──╫──────────────┐  ║          ┌──╫────────┐      ║
164	///       │  ║     ╔═════╗  │  ║          │  ║     ╔═════╗  │  ║          │  ║     ╔══╪══╗   ║
165	///       │┌─v─┐ ┌─^─┐ ┌─v─┐│┌─^─┐        │┌─v─┐ ┌─^─┐ ┌─v─┐│┌─^─┐        │┌─v─┐ ┌─^─┐│┌─v─┐ ║
166	///       ││ 0 │ │ 1 │ │ 2 │││ 3 │        ││ 0 │ │ 1 │ │ 2 │││ 3 │        ││ 0 │ │ 1 │││ 2 >═╝
167	///       │└─v─┘ └─^─┘ └─v─┘│└─^─┘        │└─v─┘ └─^─┘ └─v─┘│└─^─┘        │└─v─┘ └─^─┘│└───┘
168	///       │  ╚═════╝     ╚══╪══╝          │  ╚═════╝     ╚══╪══╝          │  ╚═════╝  │
169	///       └─────────────────┘             └─────────────────┘             └───────────┘
170	/// ```
171	fn next_back_address(&self, addr: Address) -> Option<Address>;
172
173	/// Get the next item address if any, or the next back address otherwise.
174	fn next_item_or_back_address(&self, addr: Address) -> Option<Address>;
175
176	/// Get the address of the given key.
177	///
178	/// Returns `Ok(addr)` if the key is used in the tree.
179	/// If the key is not used in the tree then `Err(addr)` is returned,
180	/// where `addr` can be used to insert the missing key.
181	fn address_of<Q: ?Sized>(&self, key: &Q) -> Result<Address, Address>
182	where
183		K: Borrow<Q>,
184		Q: Ord;
185
186	/// Search for the address of the given key from the given node `id`.
187	///
188	/// Users should directly use [`BTreeExt::address_of`].
189	fn address_in<Q: ?Sized>(&self, id: usize, key: &Q) -> Result<Address, Address>
190	where
191		K: Borrow<Q>,
192		Q: Ord;
193
194	/// Validate the tree.
195	///
196	/// Panics if the tree is not a valid B-Tree.
197	#[cfg(debug_assertions)]
198	fn validate(&self)
199	where
200		K: Ord;
201
202	/// Validate the given node and returns the depth of the node.
203	///
204	/// Panics if the tree is not a valid B-Tree.
205	#[cfg(debug_assertions)]
206	fn validate_node(
207		&self,
208		id: usize,
209		parent: Option<usize>,
210		min: Option<&K>,
211		max: Option<&K>,
212	) -> usize
213	where
214		K: Ord;
215}
216
217/// Extended mutable API.
218///
219/// This trait can be imported to access and modify the internal functions of the B-Tree.
220/// These functions are not intended to be directly called by the users, but can be used to
221/// extends the data structure with new functionalities.
222///
223/// # Correctness
224///
225/// The user of this trait is responsible to preserve the invariants of the data-structure.
226/// In particular, no item must be modified or inserted in a way that
227/// break the order between keys.
228pub trait BTreeExtMut<K, V> {
229	/// Set the new known number of items in the tree.
230	fn set_len(&mut self, len: usize);
231
232	/// Set the root node identifier.
233	fn set_root_id(&mut self, id: Option<usize>);
234
235	/// Get the node associated to the given `id` mutably.
236	///
237	/// Panics if `id` is out of bounds.
238	fn node_mut(&mut self, id: usize) -> &mut Node<K, V>;
239
240	/// Get a mutable reference to the value associated to the given `key` in the node `id`, if any.
241	fn get_mut_in(&mut self, key: &K, id: usize) -> Option<&mut V>
242	where
243		K: Ord;
244
245	/// Get a mutable reference to the item located at the given address.
246	fn item_mut(&mut self, addr: Address) -> Option<&mut Item<K, V>>;
247
248	/// Insert an item at the given address.
249	///
250	/// The address is first converted into a leaf address using [`BTreeExt::leaf_address`]
251	/// and the item inserted using [`BTreeExtMut::insert_exactly_at`].
252	fn insert_at(&mut self, addr: Address, item: Item<K, V>) -> Address;
253
254	/// Insert an item at the given address.
255	///
256	/// If the address refers to an internal node,
257	/// `opt_right_id` defines the identifier of the child node inserted on the right of the inserted item.
258	///
259	/// Returns the address of the inserted item in the tree
260	/// (it may differ from the input address if the tree is rebalanced).
261	///
262	/// # Correctness
263	///
264	/// It is assumed that it is btree-correct to insert the given item at the given address.
265	///
266	/// # Panic
267	///
268	/// This function panics if the address refers to an internal node and `opt_right_id` is `None`.
269	fn insert_exactly_at(
270		&mut self,
271		addr: Address,
272		item: Item<K, V>,
273		opt_right_id: Option<usize>,
274	) -> Address;
275
276	/// Replaces the key-value binding at the given address.
277	fn replace_at(&mut self, addr: Address, key: K, value: V) -> (K, V);
278
279	/// Replaces the value at the given address.
280	fn replace_value_at(&mut self, addr: Address, value: V) -> V;
281
282	/// Removes the item at the given address, if any.
283	///
284	/// If an item is removed then
285	/// this function returns a pair where the first hand side is the removed item,
286	/// and the right hand side is the updated address where the item can be reinserted at.
287	fn remove_at(&mut self, addr: Address) -> Option<(Item<K, V>, Address)>;
288
289	/// Rebalance a node, if necessary.
290	fn rebalance(&mut self, node_id: usize, addr: Address) -> Address;
291
292	/// Update a value in the given node `node_id`.
293	fn update_in<T, F>(&mut self, id: usize, key: K, action: F) -> T
294	where
295		K: Ord,
296		F: FnOnce(Option<V>) -> (Option<V>, T);
297
298	/// Update a valud at the given address.
299	fn update_at<T, F>(&mut self, addr: Address, action: F) -> T
300	where
301		K: Ord,
302		F: FnOnce(V) -> (Option<V>, T);
303
304	/// Take the right-most leaf value in the given node.
305	///
306	/// Note that this does not change the registred length of the tree.
307	/// The returned item is expected to be reinserted in the tree.
308	fn remove_rightmost_leaf_of(&mut self, node_id: usize) -> (Item<K, V>, usize);
309
310	/// Allocate a free identifier for the given node.
311	fn allocate_node(&mut self, node: Node<K, V>) -> usize;
312
313	/// Release the given node identifier and return the node it used to identify.
314	fn release_node(&mut self, id: usize) -> Node<K, V>;
315}
316
317impl<K, V, C: Slab<Node<K, V>>> BTreeExt<K, V> for BTreeMap<K, V, C>
318where
319	C: SimpleCollectionRef,
320{
321	#[inline]
322	fn root_id(&self) -> Option<usize> {
323		self.root
324	}
325
326	#[inline]
327	fn node(&self, id: usize) -> &Node<K, V> {
328		C::into_ref(self.nodes.get(id).unwrap())
329	}
330
331	#[inline]
332	fn get_in<Q: ?Sized>(&self, key: &Q, mut id: usize) -> Option<&V>
333	where
334		K: Borrow<Q>,
335		Q: Ord,
336	{
337		loop {
338			match self.node(id).get(key) {
339				Ok(value_opt) => return value_opt,
340				Err(child_id) => id = child_id,
341			}
342		}
343	}
344
345	fn item(&self, addr: Address) -> Option<&Item<K, V>> {
346		self.node(addr.id).item(addr.offset)
347	}
348
349	fn first_item_address(&self) -> Option<Address> {
350		match self.root {
351			Some(mut id) => loop {
352				match self.node(id).child_id_opt(0) {
353					Some(child_id) => id = child_id,
354					None => return Some(Address::new(id, 0.into())),
355				}
356			},
357			None => None,
358		}
359	}
360
361	fn first_back_address(&self) -> Address {
362		match self.root {
363			Some(mut id) => loop {
364				match self.node(id).child_id_opt(0) {
365					Some(child_id) => id = child_id,
366					None => return Address::new(id, 0.into()), // TODO FIXME thechnically not the first
367				}
368			},
369			None => Address::nowhere(),
370		}
371	}
372
373	fn last_item_address(&self) -> Option<Address> {
374		match self.root {
375			Some(mut id) => loop {
376				let node = self.node(id);
377				let index = node.item_count();
378				match node.child_id_opt(index) {
379					Some(child_id) => id = child_id,
380					None => return Some(Address::new(id, (index - 1).into())),
381				}
382			},
383			None => None,
384		}
385	}
386
387	fn last_valid_address(&self) -> Address {
388		match self.root {
389			Some(mut id) => loop {
390				let node = self.node(id);
391				let index = node.item_count();
392				match node.child_id_opt(index) {
393					Some(child_id) => id = child_id,
394					None => return Address::new(id, index.into()),
395				}
396			},
397			None => Address::nowhere(),
398		}
399	}
400
401	fn normalize(&self, mut addr: Address) -> Option<Address> {
402		if addr.is_nowhere() {
403			None
404		} else {
405			loop {
406				let node = self.node(addr.id);
407				if addr.offset >= node.item_count() {
408					match node.parent() {
409						Some(parent_id) => {
410							addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
411							addr.id = parent_id;
412						}
413						None => return None,
414					}
415				} else {
416					return Some(addr);
417				}
418			}
419		}
420	}
421
422	#[inline]
423	fn leaf_address(&self, mut addr: Address) -> Address {
424		if !addr.is_nowhere() {
425			loop {
426				let node = self.node(addr.id);
427				match node.child_id_opt(addr.offset.unwrap()) {
428					// TODO unwrap may fail here!
429					Some(child_id) => {
430						addr.id = child_id;
431						addr.offset = self.node(child_id).item_count().into()
432					}
433					None => break,
434				}
435			}
436		}
437
438		addr
439	}
440
441	/// Get the address of the item located before this address.
442	#[inline]
443	fn previous_item_address(&self, mut addr: Address) -> Option<Address> {
444		if addr.is_nowhere() {
445			return None;
446		}
447
448		loop {
449			let node = self.node(addr.id);
450
451			match node.child_id_opt(addr.offset.unwrap()) {
452				// TODO unwrap may fail here.
453				Some(child_id) => {
454					addr.offset = self.node(child_id).item_count().into();
455					addr.id = child_id;
456				}
457				None => loop {
458					if addr.offset > 0 {
459						addr.offset.decr();
460						return Some(addr);
461					}
462
463					match self.node(addr.id).parent() {
464						Some(parent_id) => {
465							addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
466							addr.id = parent_id;
467						}
468						None => return None,
469					}
470				},
471			}
472		}
473	}
474
475	#[inline]
476	fn previous_front_address(&self, mut addr: Address) -> Option<Address> {
477		if addr.is_nowhere() {
478			return None;
479		}
480
481		loop {
482			let node = self.node(addr.id);
483			match addr.offset.value() {
484				Some(offset) => {
485					let index = if offset < node.item_count() {
486						offset
487					} else {
488						node.item_count()
489					};
490
491					match node.child_id_opt(index) {
492						Some(child_id) => {
493							addr.offset = (self.node(child_id).item_count()).into();
494							addr.id = child_id;
495						}
496						None => {
497							addr.offset.decr();
498							break;
499						}
500					}
501				}
502				None => match node.parent() {
503					Some(parent_id) => {
504						addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
505						addr.offset.decr();
506						addr.id = parent_id;
507						break;
508					}
509					None => return None,
510				},
511			}
512		}
513
514		Some(addr)
515	}
516
517	/// Get the address of the item located after this address if any.
518	#[inline]
519	fn next_item_address(&self, mut addr: Address) -> Option<Address> {
520		if addr.is_nowhere() {
521			return None;
522		}
523
524		let item_count = self.node(addr.id).item_count();
525		match addr.offset.partial_cmp(&item_count) {
526			Some(std::cmp::Ordering::Less) => {
527				addr.offset.incr();
528			}
529			Some(std::cmp::Ordering::Greater) => {
530				return None;
531			}
532			_ => (),
533		}
534
535		// let original_addr_shifted = addr;
536
537		loop {
538			let node = self.node(addr.id);
539
540			match node.child_id_opt(addr.offset.unwrap()) {
541				// unwrap may fail here.
542				Some(child_id) => {
543					addr.offset = 0.into();
544					addr.id = child_id;
545				}
546				None => {
547					loop {
548						let node = self.node(addr.id);
549
550						if addr.offset < node.item_count() {
551							return Some(addr);
552						}
553
554						match node.parent() {
555							Some(parent_id) => {
556								addr.offset =
557									self.node(parent_id).child_index(addr.id).unwrap().into();
558								addr.id = parent_id;
559							}
560							None => {
561								// return Some(original_addr_shifted)
562								return None;
563							}
564						}
565					}
566				}
567			}
568		}
569	}
570
571	#[inline]
572	fn next_back_address(&self, mut addr: Address) -> Option<Address> {
573		if addr.is_nowhere() {
574			return None;
575		}
576
577		loop {
578			let node = self.node(addr.id);
579			let index = match addr.offset.value() {
580				Some(offset) => offset + 1,
581				None => 0,
582			};
583
584			if index <= node.item_count() {
585				match node.child_id_opt(index) {
586					Some(child_id) => {
587						addr.offset = Offset::before();
588						addr.id = child_id;
589					}
590					None => {
591						addr.offset = index.into();
592						break;
593					}
594				}
595			} else {
596				match node.parent() {
597					Some(parent_id) => {
598						addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
599						addr.id = parent_id;
600						break;
601					}
602					None => return None,
603				}
604			}
605		}
606
607		Some(addr)
608	}
609
610	#[inline]
611	fn next_item_or_back_address(&self, mut addr: Address) -> Option<Address> {
612		if addr.is_nowhere() {
613			return None;
614		}
615
616		let item_count = self.node(addr.id).item_count();
617		match addr.offset.partial_cmp(&item_count) {
618			Some(std::cmp::Ordering::Less) => {
619				addr.offset.incr();
620			}
621			Some(std::cmp::Ordering::Greater) => {
622				return None;
623			}
624			_ => (),
625		}
626
627		let original_addr_shifted = addr;
628
629		loop {
630			let node = self.node(addr.id);
631
632			match node.child_id_opt(addr.offset.unwrap()) {
633				// TODO unwrap may fail here.
634				Some(child_id) => {
635					addr.offset = 0.into();
636					addr.id = child_id;
637				}
638				None => loop {
639					let node = self.node(addr.id);
640
641					if addr.offset < node.item_count() {
642						return Some(addr);
643					}
644
645					match node.parent() {
646						Some(parent_id) => {
647							addr.offset = self.node(parent_id).child_index(addr.id).unwrap().into();
648							addr.id = parent_id;
649						}
650						None => return Some(original_addr_shifted),
651					}
652				},
653			}
654		}
655	}
656
657	fn address_of<Q: ?Sized>(&self, key: &Q) -> Result<Address, Address>
658	where
659		K: Borrow<Q>,
660		Q: Ord,
661	{
662		match self.root {
663			Some(id) => self.address_in(id, key),
664			None => Err(Address::nowhere()),
665		}
666	}
667
668	fn address_in<Q: ?Sized>(&self, mut id: usize, key: &Q) -> Result<Address, Address>
669	where
670		K: Borrow<Q>,
671		Q: Ord,
672	{
673		loop {
674			match self.node(id).offset_of(key) {
675				Ok(offset) => return Ok(Address { id, offset }),
676				Err((offset, None)) => return Err(Address::new(id, offset.into())),
677				Err((_, Some(child_id))) => {
678					id = child_id;
679				}
680			}
681		}
682	}
683
684	#[cfg(debug_assertions)]
685	fn validate(&self)
686	where
687		K: Ord,
688	{
689		if let Some(id) = self.root {
690			self.validate_node(id, None, None, None);
691		}
692	}
693
694	/// Validate the given node and returns the depth of the node.
695	#[cfg(debug_assertions)]
696	fn validate_node(
697		&self,
698		id: usize,
699		parent: Option<usize>,
700		mut min: Option<&K>,
701		mut max: Option<&K>,
702	) -> usize
703	where
704		K: Ord,
705	{
706		let node = self.node(id);
707		node.validate(parent, min, max);
708
709		let mut depth = None;
710		for (i, child_id) in node.children().enumerate() {
711			let (child_min, child_max) = node.separators(i);
712			let min = child_min.or_else(|| min.take());
713			let max = child_max.or_else(|| max.take());
714
715			let child_depth = self.validate_node(child_id, Some(id), min, max);
716			match depth {
717				None => depth = Some(child_depth),
718				Some(depth) => {
719					if depth != child_depth {
720						panic!("tree not balanced")
721					}
722				}
723			}
724		}
725
726		match depth {
727			Some(depth) => depth + 1,
728			None => 0,
729		}
730	}
731}
732
733impl<K, V, C: SlabMut<Node<K, V>>> BTreeExtMut<K, V> for BTreeMap<K, V, C>
734where
735	C: SimpleCollectionRef,
736	C: SimpleCollectionMut,
737{
738	#[inline]
739	fn set_len(&mut self, new_len: usize) {
740		self.len = new_len
741	}
742
743	#[inline]
744	fn set_root_id(&mut self, id: Option<usize>) {
745		self.root = id
746	}
747
748	#[inline]
749	fn node_mut(&mut self, id: usize) -> &mut Node<K, V> {
750		C::into_mut(self.nodes.get_mut(id).unwrap())
751	}
752
753	#[inline]
754	fn get_mut_in<'a>(&'a mut self, key: &K, mut id: usize) -> Option<&'a mut V>
755	where
756		K: Ord,
757	{
758		// The borrow checker is unable to predict that `*self`
759		// is not borrowed more that once at a time.
760		// That's why we need this little unsafe pointer gymnastic.
761
762		let value_ptr = loop {
763			match self.node_mut(id).get_mut(key) {
764				Ok(value_opt) => break value_opt.map(|value_ref| value_ref as *mut V),
765				Err(child_id) => id = child_id,
766			}
767		};
768
769		unsafe { value_ptr.map(|ptr| &mut *ptr) }
770	}
771
772	fn item_mut(&mut self, addr: Address) -> Option<&mut Item<K, V>> {
773		self.node_mut(addr.id).item_mut(addr.offset)
774	}
775
776	fn insert_at(&mut self, addr: Address, item: Item<K, V>) -> Address {
777		self.insert_exactly_at(self.leaf_address(addr), item, None)
778	}
779
780	fn insert_exactly_at(
781		&mut self,
782		addr: Address,
783		item: Item<K, V>,
784		opt_right_id: Option<usize>,
785	) -> Address {
786		if addr.is_nowhere() {
787			if self.is_empty() {
788				let new_root = Node::leaf(None, item);
789				let id = self.allocate_node(new_root);
790				self.root = Some(id);
791				self.len += 1;
792				Address {
793					id,
794					offset: 0.into(),
795				}
796			} else {
797				panic!("invalid item address")
798			}
799		} else if self.is_empty() {
800			panic!("invalid item address")
801		} else {
802			self.node_mut(addr.id)
803				.insert(addr.offset, item, opt_right_id);
804			let new_addr = self.rebalance(addr.id, addr);
805			self.len += 1;
806			new_addr
807		}
808	}
809
810	fn replace_at(&mut self, addr: Address, key: K, value: V) -> (K, V) {
811		self.node_mut(addr.id)
812			.item_mut(addr.offset)
813			.unwrap()
814			.set(key, value)
815	}
816
817	fn replace_value_at(&mut self, addr: Address, value: V) -> V {
818		self.node_mut(addr.id)
819			.item_mut(addr.offset)
820			.unwrap()
821			.set_value(value)
822	}
823
824	#[inline]
825	fn remove_at(&mut self, addr: Address) -> Option<(Item<K, V>, Address)> {
826		self.len -= 1;
827		match self.node_mut(addr.id).leaf_remove(addr.offset) {
828			Some(Ok(item)) => {
829				// removed from a leaf.
830				let addr = self.rebalance(addr.id, addr);
831				Some((item, addr))
832			}
833			Some(Err(left_child_id)) => {
834				// removed from an internal node.
835				let new_addr = self.next_item_or_back_address(addr).unwrap();
836				let (separator, leaf_id) = self.remove_rightmost_leaf_of(left_child_id);
837				let item = self.node_mut(addr.id).replace(addr.offset, separator);
838				let addr = self.rebalance(leaf_id, new_addr);
839				Some((item, addr))
840			}
841			None => None,
842		}
843	}
844
845	fn update_in<T, F>(&mut self, mut id: usize, key: K, action: F) -> T
846	where
847		K: Ord,
848		F: FnOnce(Option<V>) -> (Option<V>, T),
849	{
850		loop {
851			match self.node(id).offset_of(&key) {
852				Ok(offset) => unsafe {
853					let mut value = MaybeUninit::uninit();
854					let item = self.node_mut(id).item_mut(offset).unwrap();
855					std::mem::swap(&mut value, item.maybe_uninit_value_mut());
856					let (opt_new_value, result) = action(Some(value.assume_init()));
857					match opt_new_value {
858						Some(new_value) => {
859							let mut new_value = MaybeUninit::new(new_value);
860							std::mem::swap(&mut new_value, item.maybe_uninit_value_mut());
861						}
862						None => {
863							let (item, _) = self.remove_at(Address::new(id, offset)).unwrap();
864							// item's value is NOT initialized here.
865							// It must not be dropped.
866							item.forget_value()
867						}
868					}
869
870					return result;
871				},
872				Err((offset, None)) => {
873					let (opt_new_value, result) = action(None);
874					if let Some(new_value) = opt_new_value {
875						let leaf_addr = Address::new(id, offset.into());
876						self.insert_exactly_at(leaf_addr, Item::new(key, new_value), None);
877					}
878
879					return result;
880				}
881				Err((_, Some(child_id))) => {
882					id = child_id;
883				}
884			}
885		}
886	}
887
888	fn update_at<T, F>(&mut self, addr: Address, action: F) -> T
889	where
890		K: Ord,
891		F: FnOnce(V) -> (Option<V>, T),
892	{
893		unsafe {
894			let mut value = MaybeUninit::uninit();
895			let item = self.node_mut(addr.id).item_mut(addr.offset).unwrap();
896			std::mem::swap(&mut value, item.maybe_uninit_value_mut());
897			let (opt_new_value, result) = action(value.assume_init());
898			match opt_new_value {
899				Some(new_value) => {
900					let mut new_value = MaybeUninit::new(new_value);
901					std::mem::swap(&mut new_value, item.maybe_uninit_value_mut());
902				}
903				None => {
904					let (item, _) = self.remove_at(addr).unwrap();
905					// item's value is NOT initialized here.
906					// It must not be dropped.
907					item.forget_value()
908				}
909			}
910
911			result
912		}
913	}
914
915	#[inline]
916	fn rebalance(&mut self, mut id: usize, mut addr: Address) -> Address {
917		let mut balance = self.node(id).balance();
918
919		loop {
920			match balance {
921				Balance::Balanced => break,
922				Balance::Overflow => {
923					assert!(!self.node_mut(id).is_underflowing());
924					let (median_offset, median, right_node) = self.node_mut(id).split();
925					let right_id = self.allocate_node(right_node);
926
927					match self.node(id).parent() {
928						Some(parent_id) => {
929							let parent = self.node_mut(parent_id);
930							let offset = parent.child_index(id).unwrap().into();
931							parent.insert(offset, median, Some(right_id));
932
933							// new address.
934							if addr.id == id {
935								match addr.offset.partial_cmp(&median_offset) {
936									Some(std::cmp::Ordering::Equal) => {
937										addr = Address {
938											id: parent_id,
939											offset,
940										}
941									}
942									Some(std::cmp::Ordering::Greater) => {
943										addr = Address {
944											id: right_id,
945											offset: (addr.offset.unwrap() - median_offset - 1)
946												.into(),
947										}
948									}
949									_ => (),
950								}
951							} else if addr.id == parent_id && addr.offset >= offset {
952								addr.offset.incr()
953							}
954
955							id = parent_id;
956							balance = parent.balance()
957						}
958						None => {
959							let left_id = id;
960							let new_root = Node::binary(None, left_id, median, right_id);
961							let root_id = self.allocate_node(new_root);
962
963							self.root = Some(root_id);
964							self.node_mut(left_id).set_parent(Some(root_id));
965							self.node_mut(right_id).set_parent(Some(root_id));
966
967							// new address.
968							if addr.id == id {
969								match addr.offset.partial_cmp(&median_offset) {
970									Some(std::cmp::Ordering::Equal) => {
971										addr = Address {
972											id: root_id,
973											offset: 0.into(),
974										}
975									}
976									Some(std::cmp::Ordering::Greater) => {
977										addr = Address {
978											id: right_id,
979											offset: (addr.offset.unwrap() - median_offset - 1)
980												.into(),
981										}
982									}
983									_ => (),
984								}
985							}
986
987							break;
988						}
989					};
990				}
991				Balance::Underflow(is_empty) => {
992					match self.node(id).parent() {
993						Some(parent_id) => {
994							let index = self.node(parent_id).child_index(id).unwrap();
995							// An underflow append in the child node.
996							// First we try to rebalance the tree by rotation.
997							if self.try_rotate_left(parent_id, index, &mut addr)
998								|| self.try_rotate_right(parent_id, index, &mut addr)
999							{
1000								break;
1001							} else {
1002								// Rotation didn't work.
1003								// This means that all existing child sibling have enough few elements to be merged with this child.
1004								let (new_balance, new_addr) = self.merge(parent_id, index, addr);
1005								balance = new_balance;
1006								addr = new_addr;
1007								// The `merge` function returns the current balance of the parent node,
1008								// since it may underflow after the merging operation.
1009								id = parent_id
1010							}
1011						}
1012						None => {
1013							// if root is empty.
1014							if is_empty {
1015								self.root = self.node(id).child_id_opt(0);
1016
1017								// update root's parent and addr.
1018								match self.root {
1019									Some(root_id) => {
1020										let root = self.node_mut(root_id);
1021										root.set_parent(None);
1022
1023										if addr.id == id {
1024											addr.id = root_id;
1025											addr.offset = root.item_count().into()
1026										}
1027									}
1028									None => addr = Address::nowhere(),
1029								}
1030
1031								self.release_node(id);
1032							}
1033
1034							break;
1035						}
1036					}
1037				}
1038			}
1039		}
1040
1041		addr
1042	}
1043
1044	#[inline]
1045	fn remove_rightmost_leaf_of(&mut self, mut id: usize) -> (Item<K, V>, usize) {
1046		loop {
1047			match self.node_mut(id).remove_rightmost_leaf() {
1048				Ok(result) => return (result, id),
1049				Err(child_id) => {
1050					id = child_id;
1051				}
1052			}
1053		}
1054	}
1055
1056	#[inline]
1057	fn allocate_node(&mut self, node: Node<K, V>) -> usize {
1058		let mut children: SmallVec<[usize; M]> = SmallVec::new();
1059		let id = self.nodes.insert(node);
1060
1061		for child_id in self.node(id).children() {
1062			children.push(child_id)
1063		}
1064
1065		for child_id in children {
1066			self.node_mut(child_id).set_parent(Some(id))
1067		}
1068
1069		id
1070	}
1071
1072	#[inline]
1073	fn release_node(&mut self, id: usize) -> Node<K, V> {
1074		self.nodes.remove(id).unwrap()
1075	}
1076}