1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // This module represents all the internal representation and logic for a B-Tree's node
12 // with a safe interface, so that BTreeMap itself does not depend on any of these details.
14 pub use self::InsertionResult::*;
15 pub use self::SearchResult::*;
16 pub use self::ForceResult::*;
17 pub use self::TraversalItem::*;
21 use core::{slice, mem, ptr, cmp, num, raw};
23 use core::borrow::BorrowFrom;
24 use core::ptr::Unique;
27 /// Represents the result of an Insertion: either the item fit, or the node had to split
28 pub enum InsertionResult<K, V> {
29 /// The inserted element fit
31 /// The inserted element did not fit, so the node was split
32 Split(K, V, Node<K, V>),
35 /// Represents the result of a search for a key in a single node
36 pub enum SearchResult<NodeRef> {
37 /// The element was found at the given index
38 Found(Handle<NodeRef, handle::KV, handle::LeafOrInternal>),
39 /// The element wasn't found, but if it's anywhere, it must be beyond this edge
40 GoDown(Handle<NodeRef, handle::Edge, handle::LeafOrInternal>),
43 /// A B-Tree Node. We keep keys/edges/values separate to optimize searching for keys.
44 #[unsafe_no_drop_flag]
45 pub struct Node<K, V> {
46 // To avoid the need for multiple allocations, we allocate a single buffer with enough space
47 // for `capacity` keys, `capacity` values, and (in internal nodes) `capacity + 1` edges.
48 // Despite this, we store three separate pointers to the three "chunks" of the buffer because
49 // the performance drops significantly if the locations of the vals and edges need to be
50 // recalculated upon access.
52 // These will never be null during normal usage of a `Node`. However, to avoid the need for a
53 // drop flag, `Node::drop` zeroes `keys`, signaling that the `Node` has already been cleaned
58 // In leaf nodes, this will be null, and no space will be allocated for edges.
59 edges: Unique<Node<K, V>>,
61 // At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
62 // `_len + 1` edges. In a leaf node, there will never be any edges.
64 // Note: instead of accessing this field directly, please call the `len()` method, which should
65 // be more stable in the face of representation changes.
68 // FIXME(gereeter) It shouldn't be necessary to store the capacity in every node, as it should
69 // be constant throughout the tree. Once a solution to this is found, it might be possible to
70 // also pass down the offsets into the buffer that vals and edges are stored at, removing the
71 // need for those two pointers.
73 // Note: instead of accessing this field directly, please call the `capacity()` method, which
74 // should be more stable in the face of representation changes.
78 /// Rounds up to a multiple of a power of two. Returns the closest multiple
79 /// of `target_alignment` that is higher or equal to `unrounded`.
83 /// Fails if `target_alignment` is not a power of two.
85 fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint {
86 assert!(num::UnsignedInt::is_power_of_two(target_alignment));
87 (unrounded + target_alignment - 1) & !(target_alignment - 1)
92 assert_eq!(round_up_to_next(0, 4), 0);
93 assert_eq!(round_up_to_next(1, 4), 4);
94 assert_eq!(round_up_to_next(2, 4), 4);
95 assert_eq!(round_up_to_next(3, 4), 4);
96 assert_eq!(round_up_to_next(4, 4), 4);
97 assert_eq!(round_up_to_next(5, 4), 8);
100 // Returns a tuple of (val_offset, edge_offset),
101 // from the start of a mallocated array.
103 fn calculate_offsets(keys_size: uint,
104 vals_size: uint, vals_align: uint,
107 let vals_offset = round_up_to_next(keys_size, vals_align);
108 let end_of_vals = vals_offset + vals_size;
110 let edges_offset = round_up_to_next(end_of_vals, edges_align);
112 (vals_offset, edges_offset)
115 // Returns a tuple of (minimum required alignment, array_size),
116 // from the start of a mallocated array.
118 fn calculate_allocation(keys_size: uint, keys_align: uint,
119 vals_size: uint, vals_align: uint,
120 edges_size: uint, edges_align: uint)
122 let (_, edges_offset) = calculate_offsets(keys_size,
123 vals_size, vals_align,
125 let end_of_edges = edges_offset + edges_size;
127 let min_align = cmp::max(keys_align, cmp::max(vals_align, edges_align));
129 (min_align, end_of_edges)
133 fn test_offset_calculation() {
134 assert_eq!(calculate_allocation(128, 8, 15, 1, 4, 4), (8, 148));
135 assert_eq!(calculate_allocation(3, 1, 2, 1, 1, 1), (1, 6));
136 assert_eq!(calculate_allocation(6, 2, 12, 4, 24, 8), (8, 48));
137 assert_eq!(calculate_offsets(128, 15, 1, 4), (128, 144));
138 assert_eq!(calculate_offsets(3, 2, 1, 1), (3, 5));
139 assert_eq!(calculate_offsets(6, 12, 4, 8), (8, 24));
142 fn calculate_allocation_generic<K, V>(capacity: uint, is_leaf: bool) -> (uint, uint) {
143 let (keys_size, keys_align) = (capacity * mem::size_of::<K>(), mem::min_align_of::<K>());
144 let (vals_size, vals_align) = (capacity * mem::size_of::<V>(), mem::min_align_of::<V>());
145 let (edges_size, edges_align) = if is_leaf {
148 ((capacity + 1) * mem::size_of::<Node<K, V>>(), mem::min_align_of::<Node<K, V>>())
151 calculate_allocation(
152 keys_size, keys_align,
153 vals_size, vals_align,
154 edges_size, edges_align
158 fn calculate_offsets_generic<K, V>(capacity: uint, is_leaf: bool) -> (uint, uint) {
159 let keys_size = capacity * mem::size_of::<K>();
160 let vals_size = capacity * mem::size_of::<V>();
161 let vals_align = mem::min_align_of::<V>();
162 let edges_align = if is_leaf {
165 mem::min_align_of::<Node<K, V>>()
170 vals_size, vals_align,
175 /// An iterator over a slice that owns the elements of the slice but not the allocation.
181 impl<T> RawItems<T> {
182 unsafe fn from_slice(slice: &[T]) -> RawItems<T> {
183 RawItems::from_parts(slice.as_ptr(), slice.len())
186 unsafe fn from_parts(ptr: *const T, len: uint) -> RawItems<T> {
187 if mem::size_of::<T>() == 0 {
190 tail: (ptr as uint + len) as *const T,
195 tail: ptr.offset(len as int),
200 unsafe fn push(&mut self, val: T) {
201 ptr::write(self.tail as *mut T, val);
203 if mem::size_of::<T>() == 0 {
204 self.tail = (self.tail as uint + 1) as *const T;
206 self.tail = self.tail.offset(1);
211 impl<T> Iterator<T> for RawItems<T> {
212 fn next(&mut self) -> Option<T> {
213 if self.head == self.tail {
217 let ret = Some(ptr::read(self.head));
219 if mem::size_of::<T>() == 0 {
220 self.head = (self.head as uint + 1) as *const T;
222 self.head = self.head.offset(1);
231 impl<T> DoubleEndedIterator<T> for RawItems<T> {
232 fn next_back(&mut self) -> Option<T> {
233 if self.head == self.tail {
237 if mem::size_of::<T>() == 0 {
238 self.tail = (self.tail as uint - 1) as *const T;
240 self.tail = self.tail.offset(-1);
243 Some(ptr::read(self.tail))
250 impl<T> Drop for RawItems<T> {
257 impl<K, V> Drop for Node<K, V> {
259 if self.keys.0.is_null() {
260 // We have already cleaned up this node.
264 // Do the actual cleanup.
266 drop(RawItems::from_slice(self.keys()));
267 drop(RawItems::from_slice(self.vals()));
268 drop(RawItems::from_slice(self.edges()));
273 self.keys.0 = ptr::null_mut();
277 impl<K, V> Node<K, V> {
278 /// Make a new internal node. The caller must initialize the result to fix the invariant that
279 /// there are `len() + 1` edges.
280 unsafe fn new_internal(capacity: uint) -> Node<K, V> {
281 let (alignment, size) = calculate_allocation_generic::<K, V>(capacity, false);
283 let buffer = heap::allocate(size, alignment);
284 if buffer.is_null() { ::alloc::oom(); }
286 let (vals_offset, edges_offset) = calculate_offsets_generic::<K, V>(capacity, false);
289 keys: Unique(buffer as *mut K),
290 vals: Unique(buffer.offset(vals_offset as int) as *mut V),
291 edges: Unique(buffer.offset(edges_offset as int) as *mut Node<K, V>),
297 /// Make a new leaf node
298 fn new_leaf(capacity: uint) -> Node<K, V> {
299 let (alignment, size) = calculate_allocation_generic::<K, V>(capacity, true);
301 let buffer = unsafe { heap::allocate(size, alignment) };
302 if buffer.is_null() { ::alloc::oom(); }
304 let (vals_offset, _) = calculate_offsets_generic::<K, V>(capacity, true);
307 keys: Unique(buffer as *mut K).
308 vals: Unique(unsafe { buffer.offset(vals_offset as int) as *mut V }),
309 edges: Unique(ptr::null_mut::<u8>()),
315 unsafe fn destroy(&mut self) {
316 let (alignment, size) =
317 calculate_allocation_generic::<K, V>(self.capacity(), self.is_leaf());
318 heap::deallocate(self.keys.0 as *mut u8, size, alignment);
322 pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
324 mem::transmute(raw::Slice {
325 data: self.keys.0 as *const K,
328 mem::transmute(raw::Slice {
329 data: self.vals.0 as *const V,
336 pub fn as_slices_mut<'a>(&'a mut self) -> (&'a mut [K], &'a mut [V]) {
337 unsafe { mem::transmute(self.as_slices()) }
341 pub fn as_slices_internal<'a>(&'a self) -> (&'a [K], &'a [V], &'a [Node<K, V>]) {
342 let (keys, vals) = self.as_slices();
343 let edges: &[_] = if self.is_leaf() {
347 mem::transmute(raw::Slice {
348 data: self.edges.0 as *const Node<K, V>,
357 pub fn as_slices_internal_mut<'a>(&'a mut self) -> (&'a mut [K], &'a mut [V],
358 &'a mut [Node<K, V>]) {
359 unsafe { mem::transmute(self.as_slices_internal()) }
363 pub fn keys<'a>(&'a self) -> &'a [K] {
368 pub fn keys_mut<'a>(&'a mut self) -> &'a mut [K] {
369 self.as_slices_mut().0
373 pub fn vals<'a>(&'a self) -> &'a [V] {
378 pub fn vals_mut<'a>(&'a mut self) -> &'a mut [V] {
379 self.as_slices_mut().1
383 pub fn edges<'a>(&'a self) -> &'a [Node<K, V>] {
384 self.as_slices_internal().2
388 pub fn edges_mut<'a>(&'a mut self) -> &'a mut [Node<K, V>] {
389 self.as_slices_internal_mut().2
393 // FIXME(gereeter) Write an efficient clone_from
395 impl<K: Clone, V: Clone> Clone for Node<K, V> {
396 fn clone(&self) -> Node<K, V> {
397 let mut ret = if self.is_leaf() {
398 Node::new_leaf(self.capacity())
400 unsafe { Node::new_internal(self.capacity()) }
404 // For failure safety
405 let mut keys = RawItems::from_parts(ret.keys().as_ptr(), 0);
406 let mut vals = RawItems::from_parts(ret.vals().as_ptr(), 0);
407 let mut edges = RawItems::from_parts(ret.edges().as_ptr(), 0);
409 for key in self.keys().iter() {
410 keys.push(key.clone())
412 for val in self.vals().iter() {
413 vals.push(val.clone())
415 for edge in self.edges().iter() {
416 edges.push(edge.clone())
423 ret._len = self.len();
430 /// A reference to something in the middle of a `Node`. There are two `Type`s of `Handle`s,
431 /// namely `KV` handles, which point to key/value pairs, and `Edge` handles, which point to edges
432 /// before or after key/value pairs. Methods are provided for removing pairs, inserting into edges,
433 /// accessing the stored values, and moving around the `Node`.
435 /// This handle is generic, and can take any sort of reference to a `Node`. The reason for this is
436 /// two-fold. First of all, it reduces the amount of repetitive code, implementing functions that
437 /// don't need mutability on both mutable and immutable references. Secondly and more importantly,
438 /// this allows users of the `Handle` API to associate metadata with the reference. This is used in
439 /// `BTreeMap` to give `Node`s temporary "IDs" that persist to when the `Node` is used in a
442 /// # A note on safety
444 /// Unfortunately, the extra power afforded by being generic also means that safety can technically
445 /// be broken. For sensible implementations of `Deref` and `DerefMut`, these handles are perfectly
446 /// safe. As long as repeatedly calling `.deref()` results in the same Node being returned each
447 /// time, everything should work fine. However, if the `Deref` implementation swaps in multiple
448 /// different nodes, then the indices that are assumed to be in bounds suddenly stop being so. For
452 /// struct Nasty<'a> {
453 /// first: &'a Node<uint, uint>,
454 /// second: &'a Node<uint, uint>,
455 /// flag: &'a Cell<bool>,
458 /// impl<'a> Deref<Node<uint, uint>> for Nasty<'a> {
459 /// fn deref(&self) -> &Node<uint, uint> {
460 /// if self.flag.get() {
469 /// let flag = Cell::new(false);
470 /// let mut small_node = Node::make_leaf_root(3);
471 /// let mut large_node = Node::make_leaf_root(100);
473 /// for i in range(0, 100) {
474 /// // Insert to the end
475 /// large_node.edge_handle(i).insert_as_leaf(i, i);
478 /// let nasty = Nasty {
479 /// first: &large_node,
480 /// second: &small_node,
484 /// // The handle points at index 75.
485 /// let handle = Node::search(nasty, 75);
487 /// // Now the handle still points at index 75, but on the small node, which has no index 75.
490 /// println!("Uninitialized memory: {}", handle.into_kv());
494 pub struct Handle<NodeRef, Type, NodeType> {
504 // Handle node types.
505 pub enum LeafOrInternal {}
510 impl<K: Ord, V> Node<K, V> {
511 /// Searches for the given key in the node. If it finds an exact match,
512 /// `Found` will be yielded with the matching index. If it doesn't find an exact match,
513 /// `GoDown` will be yielded with the index of the subtree the key must lie in.
514 pub fn search<Sized? Q, NodeRef: Deref<Node<K, V>>>(node: NodeRef, key: &Q)
515 -> SearchResult<NodeRef> where Q: BorrowFrom<K> + Ord {
516 // FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V).
517 // For the B configured as of this writing (B = 6), binary search was *significantly*
519 let (found, index) = node.search_linear(key);
533 fn search_linear<Sized? Q>(&self, key: &Q) -> (bool, uint) where Q: BorrowFrom<K> + Ord {
534 for (i, k) in self.keys().iter().enumerate() {
535 match key.cmp(BorrowFrom::borrow_from(k)) {
537 Equal => return (true, i),
538 Less => return (false, i),
546 impl <K, V> Node<K, V> {
547 /// Make a leaf root from scratch
548 pub fn make_leaf_root(b: uint) -> Node<K, V> {
549 Node::new_leaf(capacity_from_b(b))
552 /// Make an internal root and swap it with an old root
553 pub fn make_internal_root(left_and_out: &mut Node<K,V>, b: uint, key: K, value: V,
555 let node = mem::replace(left_and_out, unsafe { Node::new_internal(capacity_from_b(b)) });
556 left_and_out._len = 1;
558 ptr::write(left_and_out.keys_mut().unsafe_mut(0), key);
559 ptr::write(left_and_out.vals_mut().unsafe_mut(0), value);
560 ptr::write(left_and_out.edges_mut().unsafe_mut(0), node);
561 ptr::write(left_and_out.edges_mut().unsafe_mut(1), right);
565 /// How many key-value pairs the node contains
566 pub fn len(&self) -> uint {
570 /// How many key-value pairs the node can fit
571 pub fn capacity(&self) -> uint {
575 /// If the node has any children
576 pub fn is_leaf(&self) -> bool {
580 /// if the node has too few elements
581 pub fn is_underfull(&self) -> bool {
582 self.len() < min_load_from_capacity(self.capacity())
585 /// if the node cannot fit any more elements
586 pub fn is_full(&self) -> bool {
587 self.len() == self.capacity()
591 impl<K, V, NodeRef: Deref<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
592 /// Returns a reference to the node that contains the pointed-to edge or key/value pair. This
593 /// is very different from `edge` and `edge_mut` because those return children of the node
594 /// returned by `node`.
595 pub fn node(&self) -> &Node<K, V> {
600 impl<K, V, NodeRef: DerefMut<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
601 /// Converts a handle into one that stores the same information using a raw pointer. This can
602 /// be useful in conjunction with `from_raw` when the type system is insufficient for
603 /// determining the lifetimes of the nodes.
604 pub fn as_raw(&mut self) -> Handle<*mut Node<K, V>, Type, NodeType> {
606 node: &mut *self.node as *mut _,
612 impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
613 /// Converts from a handle stored with a raw pointer, which isn't directly usable, to a handle
614 /// stored with a reference. This is an unsafe inverse of `as_raw`, and together they allow
615 /// unsafely extending the lifetime of the reference to the `Node`.
616 pub unsafe fn from_raw<'a>(&'a self) -> Handle<&'a Node<K, V>, Type, NodeType> {
623 /// Converts from a handle stored with a raw pointer, which isn't directly usable, to a handle
624 /// stored with a mutable reference. This is an unsafe inverse of `as_raw`, and together they
625 /// allow unsafely extending the lifetime of the reference to the `Node`.
626 pub unsafe fn from_raw_mut<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, Type, NodeType> {
628 node: &mut *self.node,
634 impl<'a, K: 'a, V: 'a> Handle<&'a Node<K, V>, handle::Edge, handle::Internal> {
635 /// Turns the handle into a reference to the edge it points at. This is necessary because the
636 /// returned pointer has a larger lifetime than what would be returned by `edge` or `edge_mut`,
637 /// making it more suitable for moving down a chain of nodes.
638 pub fn into_edge(self) -> &'a Node<K, V> {
640 self.node.edges().unsafe_get(self.index)
645 impl<'a, K: 'a, V: 'a> Handle<&'a mut Node<K, V>, handle::Edge, handle::Internal> {
646 /// Turns the handle into a mutable reference to the edge it points at. This is necessary
647 /// because the returned pointer has a larger lifetime than what would be returned by
648 /// `edge_mut`, making it more suitable for moving down a chain of nodes.
649 pub fn into_edge_mut(self) -> &'a mut Node<K, V> {
651 self.node.edges_mut().unsafe_mut(self.index)
656 impl<K, V, NodeRef: Deref<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
657 // This doesn't exist because there are no uses for it,
658 // but is fine to add, analagous to edge_mut.
660 // /// Returns a reference to the edge pointed-to by this handle. This should not be
661 // /// confused with `node`, which references the parent node of what is returned here.
662 // pub fn edge(&self) -> &Node<K, V>
665 pub enum ForceResult<NodeRef, Type> {
666 Leaf(Handle<NodeRef, Type, handle::Leaf>),
667 Internal(Handle<NodeRef, Type, handle::Internal>)
670 impl<K, V, NodeRef: Deref<Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafOrInternal> {
671 /// Figure out whether this handle is pointing to something in a leaf node or to something in
672 /// an internal node, clarifying the type according to the result.
673 pub fn force(self) -> ForceResult<NodeRef, Type> {
674 if self.node.is_leaf() {
688 impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Leaf> {
689 /// Tries to insert this key-value pair at the given index in this leaf node
690 /// If the node is full, we have to split it.
692 /// Returns a *mut V to the inserted value, because the caller may want this when
693 /// they're done mutating the tree, but we don't want to borrow anything for now.
694 pub fn insert_as_leaf(mut self, key: K, value: V) ->
695 (InsertionResult<K, V>, *mut V) {
696 if !self.node.is_full() {
697 // The element can fit, just insert it
698 (Fit, unsafe { self.node.insert_kv(self.index, key, value) as *mut _ })
700 // The element can't fit, this node is full. Split it into two nodes.
701 let (new_key, new_val, mut new_right) = self.node.split();
702 let left_len = self.node.len();
705 if self.index <= left_len {
706 self.node.insert_kv(self.index, key, value)
708 // We need to subtract 1 because in splitting we took out new_key and new_val.
709 // Just being in the right node means we are past left_len k/v pairs in the
710 // left node and 1 k/v pair in the parent node.
711 new_right.insert_kv(self.index - left_len - 1, key, value)
715 (Split(new_key, new_val, new_right), ptr)
720 impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
721 /// Returns a mutable reference to the edge pointed-to by this handle. This should not be
722 /// confused with `node`, which references the parent node of what is returned here.
723 pub fn edge_mut(&mut self) -> &mut Node<K, V> {
725 self.node.edges_mut().unsafe_mut(self.index)
729 /// Tries to insert this key-value pair at the given index in this internal node
730 /// If the node is full, we have to split it.
731 pub fn insert_as_internal(mut self, key: K, value: V, right: Node<K, V>)
732 -> InsertionResult<K, V> {
733 if !self.node.is_full() {
734 // The element can fit, just insert it
736 self.node.insert_kv(self.index, key, value);
737 self.node.insert_edge(self.index + 1, right); // +1 to insert to the right
741 // The element can't fit, this node is full. Split it into two nodes.
742 let (new_key, new_val, mut new_right) = self.node.split();
743 let left_len = self.node.len();
745 if self.index <= left_len {
747 self.node.insert_kv(self.index, key, value);
748 self.node.insert_edge(self.index + 1, right); // +1 to insert to the right
752 // The -1 here is for the same reason as in insert_as_internal - because we
753 // split, there are actually left_len + 1 k/v pairs before the right node, with
754 // the extra 1 being put in the parent.
755 new_right.insert_kv(self.index - left_len - 1, key, value);
756 new_right.insert_edge(self.index - left_len, right);
760 Split(new_key, new_val, new_right)
764 /// Handle an underflow in this node's child. We favour handling "to the left" because we know
765 /// we're empty, but our neighbour can be full. Handling to the left means when we choose to
766 /// steal, we pop off the end of our neighbour (always fast) and "unshift" ourselves
767 /// (always slow, but at least faster since we know we're half-empty).
768 /// Handling "to the right" reverses these roles. Of course, we merge whenever possible
769 /// because we want dense nodes, and merging is about equal work regardless of direction.
770 pub fn handle_underflow(mut self) {
773 self.handle_underflow_to_left();
775 self.handle_underflow_to_right();
780 /// Right is underflowed. Tries to steal from left,
781 /// but merges left and right if left is low too.
782 unsafe fn handle_underflow_to_left(&mut self) {
783 let left_len = self.node.edges()[self.index - 1].len();
784 if left_len > min_load_from_capacity(self.node.capacity()) {
785 self.left_kv().steal_rightward();
787 self.left_kv().merge_children();
791 /// Left is underflowed. Tries to steal from the right,
792 /// but merges left and right if right is low too.
793 unsafe fn handle_underflow_to_right(&mut self) {
794 let right_len = self.node.edges()[self.index + 1].len();
795 if right_len > min_load_from_capacity(self.node.capacity()) {
796 self.right_kv().steal_leftward();
798 self.right_kv().merge_children();
803 impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::Edge, NodeType> {
804 /// Gets the handle pointing to the key/value pair just to the left of the pointed-to edge.
805 /// This is unsafe because the handle might point to the first edge in the node, which has no
806 /// pair to its left.
807 unsafe fn left_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
809 node: &mut *self.node,
810 index: self.index - 1
814 /// Gets the handle pointing to the key/value pair just to the right of the pointed-to edge.
815 /// This is unsafe because the handle might point to the last edge in the node, which has no
816 /// pair to its right.
817 unsafe fn right_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
819 node: &mut *self.node,
825 impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a Node<K, V>, handle::KV, NodeType> {
826 /// Turns the handle into references to the key and value it points at. This is necessary
827 /// because the returned pointers have larger lifetimes than what would be returned by `key`
829 pub fn into_kv(self) -> (&'a K, &'a V) {
830 let (keys, vals) = self.node.as_slices();
833 keys.unsafe_get(self.index),
834 vals.unsafe_get(self.index)
840 impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
841 /// Turns the handle into mutable references to the key and value it points at. This is
842 /// necessary because the returned pointers have larger lifetimes than what would be returned
843 /// by `key_mut` or `val_mut`.
844 pub fn into_kv_mut(self) -> (&'a mut K, &'a mut V) {
845 let (keys, vals) = self.node.as_slices_mut();
848 keys.unsafe_mut(self.index),
849 vals.unsafe_mut(self.index)
854 /// Convert this handle into one pointing at the edge immediately to the left of the key/value
855 /// pair pointed-to by this handle. This is useful because it returns a reference with larger
856 /// lifetime than `left_edge`.
857 pub fn into_left_edge(self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
859 node: &mut *self.node,
865 impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
867 // These are fine to include, but are currently unneeded.
869 // /// Returns a reference to the key pointed-to by this handle. This doesn't return a
870 // /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
872 // pub fn key(&'a self) -> &'a K {
873 // unsafe { self.node.keys().unsafe_get(self.index) }
876 // /// Returns a reference to the value pointed-to by this handle. This doesn't return a
877 // /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
879 // pub fn val(&'a self) -> &'a V {
880 // unsafe { self.node.vals().unsafe_get(self.index) }
884 impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
886 /// Returns a mutable reference to the key pointed-to by this handle. This doesn't return a
887 /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
889 pub fn key_mut(&'a mut self) -> &'a mut K {
890 unsafe { self.node.keys_mut().unsafe_mut(self.index) }
893 /// Returns a mutable reference to the value pointed-to by this handle. This doesn't return a
894 /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
896 pub fn val_mut(&'a mut self) -> &'a mut V {
897 unsafe { self.node.vals_mut().unsafe_mut(self.index) }
901 impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::KV, NodeType> {
902 /// Gets the handle pointing to the edge immediately to the left of the key/value pair pointed
903 /// to by this handle.
904 pub fn left_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
906 node: &mut *self.node,
911 /// Gets the handle pointing to the edge immediately to the right of the key/value pair pointed
912 /// to by this handle.
913 pub fn right_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
915 node: &mut *self.node,
916 index: self.index + 1
921 impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Leaf> {
922 /// Removes the key/value pair at the handle's location.
924 /// # Panics (in debug build)
926 /// Panics if the node containing the pair is not a leaf node.
927 pub fn remove_as_leaf(mut self) -> (K, V) {
928 unsafe { self.node.remove_kv(self.index) }
932 impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Internal> {
933 /// Steal! Stealing is roughly analogous to a binary tree rotation.
934 /// In this case, we're "rotating" right.
935 unsafe fn steal_rightward(&mut self) {
936 // Take the biggest stuff off left
937 let (mut key, mut val, edge) = {
938 let mut left_handle = self.left_edge();
939 let left = left_handle.edge_mut();
940 let (key, val) = left.pop_kv();
941 let edge = if left.is_leaf() {
944 Some(left.pop_edge())
950 // Swap the parent's separating key-value pair with left's
951 mem::swap(&mut key, self.key_mut());
952 mem::swap(&mut val, self.val_mut());
954 // Put them at the start of right
955 let mut right_handle = self.right_edge();
956 let right = right_handle.edge_mut();
957 right.insert_kv(0, key, val);
959 Some(edge) => right.insert_edge(0, edge),
964 /// Steal! Stealing is roughly analogous to a binary tree rotation.
965 /// In this case, we're "rotating" left.
966 unsafe fn steal_leftward(&mut self) {
967 // Take the smallest stuff off right
968 let (mut key, mut val, edge) = {
969 let mut right_handle = self.right_edge();
970 let right = right_handle.edge_mut();
971 let (key, val) = right.remove_kv(0);
972 let edge = if right.is_leaf() {
975 Some(right.remove_edge(0))
981 // Swap the parent's separating key-value pair with right's
982 mem::swap(&mut key, self.key_mut());
983 mem::swap(&mut val, self.val_mut());
985 // Put them at the end of left
986 let mut left_handle = self.left_edge();
987 let left = left_handle.edge_mut();
988 left.push_kv(key, val);
990 Some(edge) => left.push_edge(edge),
995 /// Merge! Smooshes left and right into one node, along with the key-value
996 /// pair that separated them in their parent.
997 unsafe fn merge_children(mut self) {
998 // Permanently remove right's index, and the key-value pair that separates
1000 let (key, val) = self.node.remove_kv(self.index);
1001 let right = self.node.remove_edge(self.index + 1);
1003 // Give left right's stuff.
1004 self.left_edge().edge_mut()
1005 .absorb(key, val, right);
1009 impl<K, V> Node<K, V> {
1010 /// Returns the mutable handle pointing to the key/value pair at a given index.
1012 /// # Panics (in debug build)
1014 /// Panics if the given index is out of bounds.
1015 pub fn kv_handle(&mut self, index: uint) -> Handle<&mut Node<K, V>, handle::KV,
1016 handle::LeafOrInternal> {
1017 // Necessary for correctness, but in a private module
1018 debug_assert!(index < self.len(), "kv_handle index out of bounds");
1025 pub fn iter<'a>(&'a self) -> Traversal<'a, K, V> {
1026 let is_leaf = self.is_leaf();
1027 let (keys, vals, edges) = self.as_slices_internal();
1029 inner: ElemsAndEdges(
1030 keys.iter().zip(vals.iter()),
1035 has_edges: !is_leaf,
1039 pub fn iter_mut<'a>(&'a mut self) -> MutTraversal<'a, K, V> {
1040 let is_leaf = self.is_leaf();
1041 let (keys, vals, edges) = self.as_slices_internal_mut();
1043 inner: ElemsAndEdges(
1044 keys.iter().zip(vals.iter_mut()),
1049 has_edges: !is_leaf,
1053 pub fn into_iter(self) -> MoveTraversal<K, V> {
1055 let ret = MoveTraversal {
1056 inner: MoveTraversalImpl {
1057 keys: RawItems::from_slice(self.keys()),
1058 vals: RawItems::from_slice(self.vals()),
1059 edges: RawItems::from_slice(self.edges()),
1061 ptr: self.keys as *mut u8,
1062 capacity: self.capacity(),
1063 is_leaf: self.is_leaf()
1067 has_edges: !self.is_leaf(),
1074 /// When a node has no keys or values and only a single edge, extract that edge.
1075 pub fn hoist_lone_child(&mut self) {
1076 // Necessary for correctness, but in a private module
1077 debug_assert!(self.len() == 0);
1078 debug_assert!(!self.is_leaf());
1081 let ret = ptr::read(self.edges().unsafe_get(0));
1083 ptr::write(self, ret);
1088 // Vector functions (all unchecked)
1089 impl<K, V> Node<K, V> {
1090 // This must be followed by push_edge on an internal node.
1092 unsafe fn push_kv(&mut self, key: K, val: V) {
1093 let len = self.len();
1095 ptr::write(self.keys_mut().unsafe_mut(len), key);
1096 ptr::write(self.vals_mut().unsafe_mut(len), val);
1101 // This can only be called immediately after a call to push_kv.
1103 unsafe fn push_edge(&mut self, edge: Node<K, V>) {
1104 let len = self.len();
1106 ptr::write(self.edges_mut().unsafe_mut(len), edge);
1109 // This must be followed by insert_edge on an internal node.
1111 unsafe fn insert_kv(&mut self, index: uint, key: K, val: V) -> &mut V {
1113 self.keys_mut().as_mut_ptr().offset(index as int + 1),
1114 self.keys().as_ptr().offset(index as int),
1118 self.vals_mut().as_mut_ptr().offset(index as int + 1),
1119 self.vals().as_ptr().offset(index as int),
1123 ptr::write(self.keys_mut().unsafe_mut(index), key);
1124 ptr::write(self.vals_mut().unsafe_mut(index), val);
1128 self.vals_mut().unsafe_mut(index)
1131 // This can only be called immediately after a call to insert_kv.
1133 unsafe fn insert_edge(&mut self, index: uint, edge: Node<K, V>) {
1135 self.edges_mut().as_mut_ptr().offset(index as int + 1),
1136 self.edges().as_ptr().offset(index as int),
1139 ptr::write(self.edges_mut().unsafe_mut(index), edge);
1142 // This must be followed by pop_edge on an internal node.
1144 unsafe fn pop_kv(&mut self) -> (K, V) {
1145 let key = ptr::read(self.keys().unsafe_get(self.len() - 1));
1146 let val = ptr::read(self.vals().unsafe_get(self.len() - 1));
1153 // This can only be called immediately after a call to pop_kv.
1155 unsafe fn pop_edge(&mut self) -> Node<K, V> {
1156 let edge = ptr::read(self.edges().unsafe_get(self.len() + 1));
1161 // This must be followed by remove_edge on an internal node.
1163 unsafe fn remove_kv(&mut self, index: uint) -> (K, V) {
1164 let key = ptr::read(self.keys().unsafe_get(index));
1165 let val = ptr::read(self.vals().unsafe_get(index));
1168 self.keys_mut().as_mut_ptr().offset(index as int),
1169 self.keys().as_ptr().offset(index as int + 1),
1170 self.len() - index - 1
1173 self.vals_mut().as_mut_ptr().offset(index as int),
1174 self.vals().as_ptr().offset(index as int + 1),
1175 self.len() - index - 1
1183 // This can only be called immediately after a call to remove_kv.
1185 unsafe fn remove_edge(&mut self, index: uint) -> Node<K, V> {
1186 let edge = ptr::read(self.edges().unsafe_get(index));
1189 self.edges_mut().as_mut_ptr().offset(index as int),
1190 self.edges().as_ptr().offset(index as int + 1),
1191 self.len() - index + 1
1198 // Private implementation details
1199 impl<K, V> Node<K, V> {
1200 /// Node is full, so split it into two nodes, and yield the middle-most key-value pair
1201 /// because we have one too many, and our parent now has one too few
1202 fn split(&mut self) -> (K, V, Node<K, V>) {
1203 // Necessary for correctness, but in a private funtion
1204 debug_assert!(self.len() > 0);
1206 let mut right = if self.is_leaf() {
1207 Node::new_leaf(self.capacity())
1209 unsafe { Node::new_internal(self.capacity()) }
1213 right._len = self.len() / 2;
1214 let right_offset = self.len() - right.len();
1215 ptr::copy_nonoverlapping_memory(
1216 right.keys_mut().as_mut_ptr(),
1217 self.keys().as_ptr().offset(right_offset as int),
1220 ptr::copy_nonoverlapping_memory(
1221 right.vals_mut().as_mut_ptr(),
1222 self.vals().as_ptr().offset(right_offset as int),
1225 if !self.is_leaf() {
1226 ptr::copy_nonoverlapping_memory(
1227 right.edges_mut().as_mut_ptr(),
1228 self.edges().as_ptr().offset(right_offset as int),
1233 let key = ptr::read(self.keys().unsafe_get(right_offset - 1));
1234 let val = ptr::read(self.vals().unsafe_get(right_offset - 1));
1236 self._len = right_offset - 1;
1242 /// Take all the values from right, seperated by the given key and value
1243 fn absorb(&mut self, key: K, val: V, mut right: Node<K, V>) {
1244 // Necessary for correctness, but in a private function
1245 // Just as a sanity check, make sure we can fit this guy in
1246 debug_assert!(self.len() + right.len() <= self.capacity());
1247 debug_assert!(self.is_leaf() == right.is_leaf());
1250 let old_len = self.len();
1251 self._len += right.len() + 1;
1253 ptr::write(self.keys_mut().unsafe_mut(old_len), key);
1254 ptr::write(self.vals_mut().unsafe_mut(old_len), val);
1256 ptr::copy_nonoverlapping_memory(
1257 self.keys_mut().as_mut_ptr().offset(old_len as int + 1),
1258 right.keys().as_ptr(),
1261 ptr::copy_nonoverlapping_memory(
1262 self.vals_mut().as_mut_ptr().offset(old_len as int + 1),
1263 right.vals().as_ptr(),
1266 if !self.is_leaf() {
1267 ptr::copy_nonoverlapping_memory(
1268 self.edges_mut().as_mut_ptr().offset(old_len as int + 1),
1269 right.edges().as_ptr(),
1280 /// Get the capacity of a node from the order of the parent B-Tree
1281 fn capacity_from_b(b: uint) -> uint {
1285 /// Get the minimum load of a node from its capacity
1286 fn min_load_from_capacity(cap: uint) -> uint {
1291 /// A trait for pairs of `Iterator`s, one over edges and the other over key/value pairs. This is
1292 /// necessary, as the `MoveTraversalImpl` needs to have a destructor that deallocates the `Node`,
1293 /// and a pair of `Iterator`s would require two independent destructors.
1294 trait TraversalImpl<K, V, E> {
1295 fn next_kv(&mut self) -> Option<(K, V)>;
1296 fn next_kv_back(&mut self) -> Option<(K, V)>;
1298 fn next_edge(&mut self) -> Option<E>;
1299 fn next_edge_back(&mut self) -> Option<E>;
1302 /// A `TraversalImpl` that actually is backed by two iterators. This works in the non-moving case,
1303 /// as no deallocation needs to be done.
1304 struct ElemsAndEdges<Elems, Edges>(Elems, Edges);
1306 impl<K, V, E, Elems: DoubleEndedIterator<(K, V)>, Edges: DoubleEndedIterator<E>>
1307 TraversalImpl<K, V, E> for ElemsAndEdges<Elems, Edges> {
1309 fn next_kv(&mut self) -> Option<(K, V)> { self.0.next() }
1310 fn next_kv_back(&mut self) -> Option<(K, V)> { self.0.next_back() }
1312 fn next_edge(&mut self) -> Option<E> { self.1.next() }
1313 fn next_edge_back(&mut self) -> Option<E> { self.1.next_back() }
1316 /// A `TraversalImpl` taking a `Node` by value.
1317 struct MoveTraversalImpl<K, V> {
1320 edges: RawItems<Node<K, V>>,
1322 // For deallocation when we are done iterating.
1328 impl<K, V> TraversalImpl<K, V, Node<K, V>> for MoveTraversalImpl<K, V> {
1329 fn next_kv(&mut self) -> Option<(K, V)> {
1330 match (self.keys.next(), self.vals.next()) {
1331 (Some(k), Some(v)) => Some((k, v)),
1336 fn next_kv_back(&mut self) -> Option<(K, V)> {
1337 match (self.keys.next_back(), self.vals.next_back()) {
1338 (Some(k), Some(v)) => Some((k, v)),
1343 fn next_edge(&mut self) -> Option<Node<K, V>> {
1344 // Necessary for correctness, but in a private module
1345 debug_assert!(!self.is_leaf);
1349 fn next_edge_back(&mut self) -> Option<Node<K, V>> {
1350 // Necessary for correctness, but in a private module
1351 debug_assert!(!self.is_leaf);
1352 self.edges.next_back()
1356 #[unsafe_destructor]
1357 impl<K, V> Drop for MoveTraversalImpl<K, V> {
1358 fn drop(&mut self) {
1359 // We need to cleanup the stored values manually, as the RawItems destructor would run
1360 // after our deallocation.
1361 for _ in self.keys {}
1362 for _ in self.vals {}
1363 for _ in self.edges {}
1365 let (alignment, size) =
1366 calculate_allocation_generic::<K, V>(self.capacity, self.is_leaf);
1367 unsafe { heap::deallocate(self.ptr, size, alignment) };
1371 /// An abstraction over all the different kinds of traversals a node supports
1372 struct AbsTraversal<Impl> {
1379 /// A single atomic step in a traversal. Either an element is visited, or an edge is followed
1380 pub enum TraversalItem<K, V, E> {
1385 /// A traversal over a node's entries and edges
1386 pub type Traversal<'a, K, V> = AbsTraversal<ElemsAndEdges<Zip<slice::Iter<'a, K>,
1387 slice::Iter<'a, V>>,
1388 slice::Iter<'a, Node<K, V>>>>;
1390 /// A mutable traversal over a node's entries and edges
1391 pub type MutTraversal<'a, K, V> = AbsTraversal<ElemsAndEdges<Zip<slice::Iter<'a, K>,
1392 slice::IterMut<'a, V>>,
1393 slice::IterMut<'a, Node<K, V>>>>;
1395 /// An owning traversal over a node's entries and edges
1396 pub type MoveTraversal<K, V> = AbsTraversal<MoveTraversalImpl<K, V>>;
1399 impl<K, V, E, Impl: TraversalImpl<K, V, E>>
1400 Iterator<TraversalItem<K, V, E>> for AbsTraversal<Impl> {
1402 fn next(&mut self) -> Option<TraversalItem<K, V, E>> {
1403 let head_is_edge = self.head_is_edge;
1404 self.head_is_edge = !head_is_edge;
1406 if head_is_edge && self.has_edges {
1407 self.inner.next_edge().map(|node| Edge(node))
1409 self.inner.next_kv().map(|(k, v)| Elem(k, v))
1414 impl<K, V, E, Impl: TraversalImpl<K, V, E>>
1415 DoubleEndedIterator<TraversalItem<K, V, E>> for AbsTraversal<Impl> {
1417 fn next_back(&mut self) -> Option<TraversalItem<K, V, E>> {
1418 let tail_is_edge = self.tail_is_edge;
1419 self.tail_is_edge = !tail_is_edge;
1421 if tail_is_edge && self.has_edges {
1422 self.inner.next_edge_back().map(|node| Edge(node))
1424 self.inner.next_kv_back().map(|(k, v)| Elem(k, v))