Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
btree.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/container/btree.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2008-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_CONTAINER_BTREE_HEADER
12 #define TLX_CONTAINER_BTREE_HEADER
13 
14 #include <tlx/die.hpp>
15 
16 // *** Required Headers from the STL
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <cstddef>
21 #include <functional>
22 #include <istream>
23 #include <memory>
24 #include <ostream>
25 #include <utility>
26 
27 namespace tlx {
28 
29 //! \addtogroup tlx_container
30 //! \{
31 //! \defgroup tlx_container_btree B+ Trees
32 //! B+ tree variants
33 //! \{
34 
35 // *** Debugging Macros
36 
37 #ifdef TLX_BTREE_DEBUG
38 
39 #include <iostream>
40 
41 //! Print out debug information to std::cout if TLX_BTREE_DEBUG is defined.
42 #define TLX_BTREE_PRINT(x) \
43  do { if (debug) (std::cout << x << std::endl); } while (0)
44 
45 //! Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
46 #define TLX_BTREE_ASSERT(x) \
47  do { assert(x); } while (0)
48 
49 #else
50 
51 //! Print out debug information to std::cout if TLX_BTREE_DEBUG is defined.
52 #define TLX_BTREE_PRINT(x) do { } while (0)
53 
54 //! Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
55 #define TLX_BTREE_ASSERT(x) do { } while (0)
56 
57 #endif
58 
59 //! The maximum of a and b. Used in some compile-time formulas.
60 #define TLX_BTREE_MAX(a, b) ((a) < (b) ? (b) : (a))
61 
62 #ifndef TLX_BTREE_FRIENDS
63 //! The macro TLX_BTREE_FRIENDS can be used by outside class to access the B+
64 //! tree internals. This was added for wxBTreeDemo to be able to draw the
65 //! tree.
66 #define TLX_BTREE_FRIENDS friend class btree_friend
67 #endif
68 
69 /*!
70  * Generates default traits for a B+ tree used as a set or map. It estimates
71  * leaf and inner node sizes by assuming a cache line multiple of 256 bytes.
72 */
73 template <typename Key, typename Value>
75  //! If true, the tree will self verify its invariants after each insert() or
76  //! erase(). The header must have been compiled with TLX_BTREE_DEBUG
77  //! defined.
78  static const bool self_verify = false;
79 
80  //! If true, the tree will print out debug information and a tree dump
81  //! during insert() or erase() operation. The header must have been
82  //! compiled with TLX_BTREE_DEBUG defined and key_type must be std::ostream
83  //! printable.
84  static const bool debug = false;
85 
86  //! Number of slots in each leaf of the tree. Estimated so that each node
87  //! has a size of about 256 bytes.
88  static const int leaf_slots =
89  TLX_BTREE_MAX(8, 256 / (sizeof(Value)));
90 
91  //! Number of slots in each inner node of the tree. Estimated so that each
92  //! node has a size of about 256 bytes.
93  static const int inner_slots =
94  TLX_BTREE_MAX(8, 256 / (sizeof(Key) + sizeof(void*)));
95 
96  //! As of stx-btree-0.9, the code does linear search in find_lower() and
97  //! find_upper() instead of binary_search, unless the node size is larger
98  //! than this threshold. See notes at
99  //! http://panthema.net/2013/0504-STX-B+Tree-Binary-vs-Linear-Search
100  static const size_t binsearch_threshold = 256;
101 };
102 
103 /*!
104  * Basic class implementing a B+ tree data structure in memory.
105  *
106  * The base implementation of an in-memory B+ tree. It is based on the
107  * implementation in Cormen's Introduction into Algorithms, Jan Jannink's paper
108  * and other algorithm resources. Almost all STL-required function calls are
109  * implemented. The asymptotic time requirements of the STL are not always
110  * fulfilled in theory, however, in practice this B+ tree performs better than a
111  * red-black tree and almost always uses less memory. The insertion function
112  * splits the nodes on the recursion unroll. Erase is largely based on Jannink's
113  * ideas.
114  *
115  * This class is specialized into btree_set, btree_multiset, btree_map and
116  * btree_multimap using default template parameters and facade functions.
117  */
118 template <typename Key_, typename Value_,
119  typename KeyOfValue_,
120  typename Compare_ = std::less<Key_>,
121  typename Traits_ = btree_default_traits<Key_, Value_>,
122  bool Duplicates_ = false,
123  typename Alloc_ = std::allocator<Value_> >
124 class btree
125 {
126 public:
127  //! \name Template Parameter Types
128  //! \{
129 
130  //! First template parameter: The key type of the B+ tree. This is stored in
131  //! inner nodes.
132  typedef Key_ key_type;
133 
134  //! Second template parameter: Composition pair of key and data types, or
135  //! just the key for set containers. This data type is stored in the leaves.
136  typedef Value_ value_type;
137 
138  //! Third template: key extractor class to pull key_type from value_type.
139  typedef KeyOfValue_ key_of_value;
140 
141  //! Fourth template parameter: key_type comparison function object
142  typedef Compare_ key_compare;
143 
144  //! Fifth template parameter: Traits object used to define more parameters
145  //! of the B+ tree
146  typedef Traits_ traits;
147 
148  //! Sixth template parameter: Allow duplicate keys in the B+ tree. Used to
149  //! implement multiset and multimap.
150  static const bool allow_duplicates = Duplicates_;
151 
152  //! Seventh template parameter: STL allocator for tree nodes
153  typedef Alloc_ allocator_type;
154 
155  //! \}
156 
157  // The macro TLX_BTREE_FRIENDS can be used by outside class to access the B+
158  // tree internals. This was added for wxBTreeDemo to be able to draw the
159  // tree.
161 
162 public:
163  //! \name Constructed Types
164  //! \{
165 
166  //! Typedef of our own type
169 
170  //! Size type used to count keys
171  typedef size_t size_type;
172 
173  //! \}
174 
175 public:
176  //! \name Static Constant Options and Values of the B+ Tree
177  //! \{
178 
179  //! Base B+ tree parameter: The number of key/data slots in each leaf
180  static const unsigned short leaf_slotmax = traits::leaf_slots;
181 
182  //! Base B+ tree parameter: The number of key slots in each inner node,
183  //! this can differ from slots in each leaf.
184  static const unsigned short inner_slotmax = traits::inner_slots;
185 
186  //! Computed B+ tree parameter: The minimum number of key/data slots used
187  //! in a leaf. If fewer slots are used, the leaf will be merged or slots
188  //! shifted from it's siblings.
189  static const unsigned short leaf_slotmin = (leaf_slotmax / 2);
190 
191  //! Computed B+ tree parameter: The minimum number of key slots used
192  //! in an inner node. If fewer slots are used, the inner node will be
193  //! merged or slots shifted from it's siblings.
194  static const unsigned short inner_slotmin = (inner_slotmax / 2);
195 
196  //! Debug parameter: Enables expensive and thorough checking of the B+ tree
197  //! invariants after each insert/erase operation.
198  static const bool self_verify = traits::self_verify;
199 
200  //! Debug parameter: Prints out lots of debug information about how the
201  //! algorithms change the tree. Requires the header file to be compiled
202  //! with TLX_BTREE_DEBUG and the key type must be std::ostream printable.
203  static const bool debug = traits::debug;
204 
205  //! \}
206 
207 private:
208  //! \name Node Classes for In-Memory Nodes
209  //! \{
210 
211  //! The header structure of each node in-memory. This structure is extended
212  //! by inner_node or leaf_node.
213  struct node {
214  //! Level in the b-tree, if level == 0 -> leaf node
215  unsigned short level;
216 
217  //! Number of key slotuse use, so the number of valid children or data
218  //! pointers
219  unsigned short slotuse;
220 
221  //! Delayed initialisation of constructed node.
222  void initialize(const unsigned short l) {
223  level = l;
224  slotuse = 0;
225  }
226 
227  //! True if this is a leaf node.
228  bool is_leafnode() const {
229  return (level == 0);
230  }
231  };
232 
233  //! Extended structure of a inner node in-memory. Contains only keys and no
234  //! data items.
235  struct inner_node : public node {
236  //! Define an related allocator for the inner_node structs.
237  typedef typename Alloc_::template rebind<inner_node>::other alloc_type;
238 
239  //! Keys of children or data pointers
241 
242  //! Pointers to children
243  node* childid[inner_slotmax + 1]; // NOLINT
244 
245  //! Set variables to initial values.
246  void initialize(const unsigned short l) {
247  node::initialize(l);
248  }
249 
250  //! Return key in slot s
251  const key_type& key(size_t s) const {
252  return slotkey[s];
253  }
254 
255  //! True if the node's slots are full.
256  bool is_full() const {
257  return (node::slotuse == inner_slotmax);
258  }
259 
260  //! True if few used entries, less than half full.
261  bool is_few() const {
262  return (node::slotuse <= inner_slotmin);
263  }
264 
265  //! True if node has too few entries.
266  bool is_underflow() const {
267  return (node::slotuse < inner_slotmin);
268  }
269  };
270 
271  //! Extended structure of a leaf node in memory. Contains pairs of keys and
272  //! data items. Key and data slots are kept together in value_type.
273  struct leaf_node : public node {
274  //! Define an related allocator for the leaf_node structs.
275  typedef typename Alloc_::template rebind<leaf_node>::other alloc_type;
276 
277  //! Double linked list pointers to traverse the leaves
279 
280  //! Double linked list pointers to traverse the leaves
282 
283  //! Array of (key, data) pairs
285 
286  //! Set variables to initial values
287  void initialize() {
288  node::initialize(0);
289  prev_leaf = next_leaf = nullptr;
290  }
291 
292  //! Return key in slot s.
293  const key_type& key(size_t s) const {
294  return key_of_value::get(slotdata[s]);
295  }
296 
297  //! True if the node's slots are full.
298  bool is_full() const {
299  return (node::slotuse == leaf_slotmax);
300  }
301 
302  //! True if few used entries, less than half full.
303  bool is_few() const {
304  return (node::slotuse <= leaf_slotmin);
305  }
306 
307  //! True if node has too few entries.
308  bool is_underflow() const {
309  return (node::slotuse < leaf_slotmin);
310  }
311 
312  //! Set the (key,data) pair in slot. Overloaded function used by
313  //! bulk_load().
314  void set_slot(unsigned short slot, const value_type& value) {
316  slotdata[slot] = value;
317  }
318  };
319 
320  //! \}
321 
322 public:
323  //! \name Iterators and Reverse Iterators
324  //! \{
325 
326  class iterator;
327  class const_iterator;
328  class reverse_iterator;
329  class const_reverse_iterator;
330 
331  //! STL-like iterator object for B+ tree items. The iterator points to a
332  //! specific slot number in a leaf.
333  class iterator
334  {
335  public:
336  // *** Types
337 
338  //! The key type of the btree. Returned by key().
339  typedef typename btree::key_type key_type;
340 
341  //! The value type of the btree. Returned by operator*().
342  typedef typename btree::value_type value_type;
343 
344  //! Reference to the value_type. STL required.
346 
347  //! Pointer to the value_type. STL required.
348  typedef value_type* pointer;
349 
350  //! STL-magic iterator category
351  typedef std::bidirectional_iterator_tag iterator_category;
352 
353  //! STL-magic
354  typedef ptrdiff_t difference_type;
355 
356  //! Our own type
357  typedef iterator self;
358 
359  private:
360  // *** Members
361 
362  //! The currently referenced leaf node of the tree
364 
365  //! Current key/data slot referenced
366  unsigned short curr_slot;
367 
368  //! Friendly to the const_iterator, so it may access the two data items
369  //! directly.
370  friend class const_iterator;
371 
372  //! Also friendly to the reverse_iterator, so it may access the two
373  //! data items directly.
374  friend class reverse_iterator;
375 
376  //! Also friendly to the const_reverse_iterator, so it may access the
377  //! two data items directly.
379 
380  //! Also friendly to the base btree class, because erase_iter() needs
381  //! to read the curr_leaf and curr_slot values directly.
384 
385  // The macro TLX_BTREE_FRIENDS can be used by outside class to access
386  // the B+ tree internals. This was added for wxBTreeDemo to be able to
387  // draw the tree.
389 
390  public:
391  // *** Methods
392 
393  //! Default-Constructor of a mutable iterator
395  : curr_leaf(nullptr), curr_slot(0)
396  { }
397 
398  //! Initializing-Constructor of a mutable iterator
399  iterator(typename btree::leaf_node* l, unsigned short s)
400  : curr_leaf(l), curr_slot(s)
401  { }
402 
403  //! Copy-constructor from a reverse iterator
404  iterator(const reverse_iterator& it) // NOLINT
406  { }
407 
408  //! Dereference the iterator.
410  return curr_leaf->slotdata[curr_slot];
411  }
412 
413  //! Dereference the iterator.
415  return &curr_leaf->slotdata[curr_slot];
416  }
417 
418  //! Key of the current slot.
419  const key_type& key() const {
420  return curr_leaf->key(curr_slot);
421  }
422 
423  //! Prefix++ advance the iterator to the next slot.
425  if (curr_slot + 1u < curr_leaf->slotuse) {
426  ++curr_slot;
427  }
428  else if (curr_leaf->next_leaf != nullptr) {
430  curr_slot = 0;
431  }
432  else {
433  // this is end()
435  }
436 
437  return *this;
438  }
439 
440  //! Postfix++ advance the iterator to the next slot.
442  iterator tmp = *this; // copy ourselves
443 
444  if (curr_slot + 1u < curr_leaf->slotuse) {
445  ++curr_slot;
446  }
447  else if (curr_leaf->next_leaf != nullptr) {
449  curr_slot = 0;
450  }
451  else {
452  // this is end()
454  }
455 
456  return tmp;
457  }
458 
459  //! Prefix-- backstep the iterator to the last slot.
461  if (curr_slot > 0) {
462  --curr_slot;
463  }
464  else if (curr_leaf->prev_leaf != nullptr) {
466  curr_slot = curr_leaf->slotuse - 1;
467  }
468  else {
469  // this is begin()
470  curr_slot = 0;
471  }
472 
473  return *this;
474  }
475 
476  //! Postfix-- backstep the iterator to the last slot.
478  iterator tmp = *this; // copy ourselves
479 
480  if (curr_slot > 0) {
481  --curr_slot;
482  }
483  else if (curr_leaf->prev_leaf != nullptr) {
485  curr_slot = curr_leaf->slotuse - 1;
486  }
487  else {
488  // this is begin()
489  curr_slot = 0;
490  }
491 
492  return tmp;
493  }
494 
495  //! Equality of iterators.
496  bool operator == (const iterator& x) const {
497  return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
498  }
499 
500  //! Inequality of iterators.
501  bool operator != (const iterator& x) const {
502  return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
503  }
504  };
505 
506  //! STL-like read-only iterator object for B+ tree items. The iterator
507  //! points to a specific slot number in a leaf.
509  {
510  public:
511  // *** Types
512 
513  //! The key type of the btree. Returned by key().
514  typedef typename btree::key_type key_type;
515 
516  //! The value type of the btree. Returned by operator*().
517  typedef typename btree::value_type value_type;
518 
519  //! Reference to the value_type. STL required.
520  typedef const value_type& reference;
521 
522  //! Pointer to the value_type. STL required.
523  typedef const value_type* pointer;
524 
525  //! STL-magic iterator category
526  typedef std::bidirectional_iterator_tag iterator_category;
527 
528  //! STL-magic
529  typedef ptrdiff_t difference_type;
530 
531  //! Our own type
532  typedef const_iterator self;
533 
534  private:
535  // *** Members
536 
537  //! The currently referenced leaf node of the tree
538  const typename btree::leaf_node* curr_leaf;
539 
540  //! Current key/data slot referenced
541  unsigned short curr_slot;
542 
543  //! Friendly to the reverse_const_iterator, so it may access the two
544  //! data items directly
546 
547  // The macro TLX_BTREE_FRIENDS can be used by outside class to access
548  // the B+ tree internals. This was added for wxBTreeDemo to be able to
549  // draw the tree.
551 
552  public:
553  // *** Methods
554 
555  //! Default-Constructor of a const iterator
557  : curr_leaf(nullptr), curr_slot(0)
558  { }
559 
560  //! Initializing-Constructor of a const iterator
561  const_iterator(const typename btree::leaf_node* l, unsigned short s)
562  : curr_leaf(l), curr_slot(s)
563  { }
564 
565  //! Copy-constructor from a mutable iterator
566  const_iterator(const iterator& it) // NOLINT
568  { }
569 
570  //! Copy-constructor from a mutable reverse iterator
571  const_iterator(const reverse_iterator& it) // NOLINT
573  { }
574 
575  //! Copy-constructor from a const reverse iterator
578  { }
579 
580  //! Dereference the iterator.
582  return curr_leaf->slotdata[curr_slot];
583  }
584 
585  //! Dereference the iterator.
587  return &curr_leaf->slotdata[curr_slot];
588  }
589 
590  //! Key of the current slot.
591  const key_type& key() const {
592  return curr_leaf->key(curr_slot);
593  }
594 
595  //! Prefix++ advance the iterator to the next slot.
597  if (curr_slot + 1u < curr_leaf->slotuse) {
598  ++curr_slot;
599  }
600  else if (curr_leaf->next_leaf != nullptr) {
602  curr_slot = 0;
603  }
604  else {
605  // this is end()
607  }
608 
609  return *this;
610  }
611 
612  //! Postfix++ advance the iterator to the next slot.
614  const_iterator tmp = *this; // copy ourselves
615 
616  if (curr_slot + 1u < curr_leaf->slotuse) {
617  ++curr_slot;
618  }
619  else if (curr_leaf->next_leaf != nullptr) {
621  curr_slot = 0;
622  }
623  else {
624  // this is end()
626  }
627 
628  return tmp;
629  }
630 
631  //! Prefix-- backstep the iterator to the last slot.
633  if (curr_slot > 0) {
634  --curr_slot;
635  }
636  else if (curr_leaf->prev_leaf != nullptr) {
638  curr_slot = curr_leaf->slotuse - 1;
639  }
640  else {
641  // this is begin()
642  curr_slot = 0;
643  }
644 
645  return *this;
646  }
647 
648  //! Postfix-- backstep the iterator to the last slot.
650  const_iterator tmp = *this; // copy ourselves
651 
652  if (curr_slot > 0) {
653  --curr_slot;
654  }
655  else if (curr_leaf->prev_leaf != nullptr) {
657  curr_slot = curr_leaf->slotuse - 1;
658  }
659  else {
660  // this is begin()
661  curr_slot = 0;
662  }
663 
664  return tmp;
665  }
666 
667  //! Equality of iterators.
668  bool operator == (const const_iterator& x) const {
669  return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
670  }
671 
672  //! Inequality of iterators.
673  bool operator != (const const_iterator& x) const {
674  return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
675  }
676  };
677 
678  //! STL-like mutable reverse iterator object for B+ tree items. The
679  //! iterator points to a specific slot number in a leaf.
681  {
682  public:
683  // *** Types
684 
685  //! The key type of the btree. Returned by key().
686  typedef typename btree::key_type key_type;
687 
688  //! The value type of the btree. Returned by operator*().
689  typedef typename btree::value_type value_type;
690 
691  //! Reference to the value_type. STL required.
693 
694  //! Pointer to the value_type. STL required.
695  typedef value_type* pointer;
696 
697  //! STL-magic iterator category
698  typedef std::bidirectional_iterator_tag iterator_category;
699 
700  //! STL-magic
701  typedef ptrdiff_t difference_type;
702 
703  //! Our own type
704  typedef reverse_iterator self;
705 
706  private:
707  // *** Members
708 
709  //! The currently referenced leaf node of the tree
711 
712  //! One slot past the current key/data slot referenced.
713  unsigned short curr_slot;
714 
715  //! Friendly to the const_iterator, so it may access the two data items
716  //! directly
717  friend class iterator;
718 
719  //! Also friendly to the const_iterator, so it may access the two data
720  //! items directly
721  friend class const_iterator;
722 
723  //! Also friendly to the const_iterator, so it may access the two data
724  //! items directly
726 
727  // The macro TLX_BTREE_FRIENDS can be used by outside class to access
728  // the B+ tree internals. This was added for wxBTreeDemo to be able to
729  // draw the tree.
731 
732  public:
733  // *** Methods
734 
735  //! Default-Constructor of a reverse iterator
737  : curr_leaf(nullptr), curr_slot(0)
738  { }
739 
740  //! Initializing-Constructor of a mutable reverse iterator
741  reverse_iterator(typename btree::leaf_node* l, unsigned short s)
742  : curr_leaf(l), curr_slot(s)
743  { }
744 
745  //! Copy-constructor from a mutable iterator
746  reverse_iterator(const iterator& it) // NOLINT
748  { }
749 
750  //! Dereference the iterator.
753  return curr_leaf->slotdata[curr_slot - 1];
754  }
755 
756  //! Dereference the iterator.
759  return &curr_leaf->slotdata[curr_slot - 1];
760  }
761 
762  //! Key of the current slot.
763  const key_type& key() const {
765  return curr_leaf->key(curr_slot - 1);
766  }
767 
768  //! Prefix++ advance the iterator to the next slot.
770  if (curr_slot > 1) {
771  --curr_slot;
772  }
773  else if (curr_leaf->prev_leaf != nullptr) {
776  }
777  else {
778  // this is begin() == rend()
779  curr_slot = 0;
780  }
781 
782  return *this;
783  }
784 
785  //! Postfix++ advance the iterator to the next slot.
787  reverse_iterator tmp = *this; // copy ourselves
788 
789  if (curr_slot > 1) {
790  --curr_slot;
791  }
792  else if (curr_leaf->prev_leaf != nullptr) {
795  }
796  else {
797  // this is begin() == rend()
798  curr_slot = 0;
799  }
800 
801  return tmp;
802  }
803 
804  //! Prefix-- backstep the iterator to the last slot.
806  if (curr_slot < curr_leaf->slotuse) {
807  ++curr_slot;
808  }
809  else if (curr_leaf->next_leaf != nullptr) {
811  curr_slot = 1;
812  }
813  else {
814  // this is end() == rbegin()
816  }
817 
818  return *this;
819  }
820 
821  //! Postfix-- backstep the iterator to the last slot.
823  reverse_iterator tmp = *this; // copy ourselves
824 
825  if (curr_slot < curr_leaf->slotuse) {
826  ++curr_slot;
827  }
828  else if (curr_leaf->next_leaf != nullptr) {
830  curr_slot = 1;
831  }
832  else {
833  // this is end() == rbegin()
835  }
836 
837  return tmp;
838  }
839 
840  //! Equality of iterators.
841  bool operator == (const reverse_iterator& x) const {
842  return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
843  }
844 
845  //! Inequality of iterators.
846  bool operator != (const reverse_iterator& x) const {
847  return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
848  }
849  };
850 
851  //! STL-like read-only reverse iterator object for B+ tree items. The
852  //! iterator points to a specific slot number in a leaf.
854  {
855  public:
856  // *** Types
857 
858  //! The key type of the btree. Returned by key().
859  typedef typename btree::key_type key_type;
860 
861  //! The value type of the btree. Returned by operator*().
862  typedef typename btree::value_type value_type;
863 
864  //! Reference to the value_type. STL required.
865  typedef const value_type& reference;
866 
867  //! Pointer to the value_type. STL required.
868  typedef const value_type* pointer;
869 
870  //! STL-magic iterator category
871  typedef std::bidirectional_iterator_tag iterator_category;
872 
873  //! STL-magic
874  typedef ptrdiff_t difference_type;
875 
876  //! Our own type
878 
879  private:
880  // *** Members
881 
882  //! The currently referenced leaf node of the tree
883  const typename btree::leaf_node* curr_leaf;
884 
885  //! One slot past the current key/data slot referenced.
886  unsigned short curr_slot;
887 
888  //! Friendly to the const_iterator, so it may access the two data items
889  //! directly.
890  friend class reverse_iterator;
891 
892  // The macro TLX_BTREE_FRIENDS can be used by outside class to access
893  // the B+ tree internals. This was added for wxBTreeDemo to be able to
894  // draw the tree.
896 
897  public:
898  // *** Methods
899 
900  //! Default-Constructor of a const reverse iterator.
902  : curr_leaf(nullptr), curr_slot(0)
903  { }
904 
905  //! Initializing-Constructor of a const reverse iterator.
907  const typename btree::leaf_node* l, unsigned short s)
908  : curr_leaf(l), curr_slot(s)
909  { }
910 
911  //! Copy-constructor from a mutable iterator.
912  const_reverse_iterator(const iterator& it) // NOLINT
914  { }
915 
916  //! Copy-constructor from a const iterator.
919  { }
920 
921  //! Copy-constructor from a mutable reverse iterator.
924  { }
925 
926  //! Dereference the iterator.
929  return curr_leaf->slotdata[curr_slot - 1];
930  }
931 
932  //! Dereference the iterator.
935  return &curr_leaf->slotdata[curr_slot - 1];
936  }
937 
938  //! Key of the current slot.
939  const key_type& key() const {
941  return curr_leaf->key(curr_slot - 1);
942  }
943 
944  //! Prefix++ advance the iterator to the previous slot.
946  if (curr_slot > 1) {
947  --curr_slot;
948  }
949  else if (curr_leaf->prev_leaf != nullptr) {
952  }
953  else {
954  // this is begin() == rend()
955  curr_slot = 0;
956  }
957 
958  return *this;
959  }
960 
961  //! Postfix++ advance the iterator to the previous slot.
963  const_reverse_iterator tmp = *this; // copy ourselves
964 
965  if (curr_slot > 1) {
966  --curr_slot;
967  }
968  else if (curr_leaf->prev_leaf != nullptr) {
971  }
972  else {
973  // this is begin() == rend()
974  curr_slot = 0;
975  }
976 
977  return tmp;
978  }
979 
980  //! Prefix-- backstep the iterator to the next slot.
982  if (curr_slot < curr_leaf->slotuse) {
983  ++curr_slot;
984  }
985  else if (curr_leaf->next_leaf != nullptr) {
987  curr_slot = 1;
988  }
989  else {
990  // this is end() == rbegin()
992  }
993 
994  return *this;
995  }
996 
997  //! Postfix-- backstep the iterator to the next slot.
999  const_reverse_iterator tmp = *this; // copy ourselves
1000 
1001  if (curr_slot < curr_leaf->slotuse) {
1002  ++curr_slot;
1003  }
1004  else if (curr_leaf->next_leaf != nullptr) {
1006  curr_slot = 1;
1007  }
1008  else {
1009  // this is end() == rbegin()
1011  }
1012 
1013  return tmp;
1014  }
1015 
1016  //! Equality of iterators.
1018  return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
1019  }
1020 
1021  //! Inequality of iterators.
1023  return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
1024  }
1025  };
1026 
1027  //! \}
1028 
1029 public:
1030  //! \name Small Statistics Structure
1031  //! \{
1032 
1033  /*!
1034  * A small struct containing basic statistics about the B+ tree. It can be
1035  * fetched using get_stats().
1036  */
1037  struct tree_stats {
1038  //! Number of items in the B+ tree
1040 
1041  //! Number of leaves in the B+ tree
1043 
1044  //! Number of inner nodes in the B+ tree
1046 
1047  //! Base B+ tree parameter: The number of key/data slots in each leaf
1048  static const unsigned short leaf_slots = btree_self::leaf_slotmax;
1049 
1050  //! Base B+ tree parameter: The number of key slots in each inner node.
1051  static const unsigned short inner_slots = btree_self::inner_slotmax;
1052 
1053  //! Zero initialized
1055  : size(0),
1056  leaves(0), inner_nodes(0)
1057  { }
1058 
1059  //! Return the total number of nodes
1060  size_type nodes() const {
1061  return inner_nodes + leaves;
1062  }
1063 
1064  //! Return the average fill of leaves
1065  double avgfill_leaves() const {
1066  return static_cast<double>(size) / (leaves * leaf_slots);
1067  }
1068  };
1069 
1070  //! \}
1071 
1072 private:
1073  //! \name Tree Object Data Members
1074  //! \{
1075 
1076  //! Pointer to the B+ tree's root node, either leaf or inner node.
1077  node* root_;
1078 
1079  //! Pointer to first leaf in the double linked leaf chain.
1080  leaf_node* head_leaf_;
1081 
1082  //! Pointer to last leaf in the double linked leaf chain.
1083  leaf_node* tail_leaf_;
1084 
1085  //! Other small statistics about the B+ tree.
1086  tree_stats stats_;
1087 
1088  //! Key comparison object. More comparison functions are generated from
1089  //! this < relation.
1091 
1092  //! Memory allocator.
1094 
1095  //! \}
1096 
1097 public:
1098  //! \name Constructors and Destructor
1099  //! \{
1100 
1101  //! Default constructor initializing an empty B+ tree with the standard key
1102  //! comparison function.
1103  explicit btree(const allocator_type& alloc = allocator_type())
1104  : root_(nullptr), head_leaf_(nullptr), tail_leaf_(nullptr),
1105  allocator_(alloc)
1106  { }
1107 
1108  //! Constructor initializing an empty B+ tree with a special key
1109  //! comparison object.
1110  explicit btree(const key_compare& kcf,
1111  const allocator_type& alloc = allocator_type())
1112  : root_(nullptr), head_leaf_(nullptr), tail_leaf_(nullptr),
1113  key_less_(kcf), allocator_(alloc)
1114  { }
1115 
1116  //! Constructor initializing a B+ tree with the range [first,last). The
1117  //! range need not be sorted. To create a B+ tree from a sorted range, use
1118  //! bulk_load().
1119  template <class InputIterator>
1120  btree(InputIterator first, InputIterator last,
1121  const allocator_type& alloc = allocator_type())
1122  : root_(nullptr), head_leaf_(nullptr), tail_leaf_(nullptr),
1123  allocator_(alloc) {
1124  insert(first, last);
1125  }
1126 
1127  //! Constructor initializing a B+ tree with the range [first,last) and a
1128  //! special key comparison object. The range need not be sorted. To create
1129  //! a B+ tree from a sorted range, use bulk_load().
1130  template <class InputIterator>
1131  btree(InputIterator first, InputIterator last, const key_compare& kcf,
1132  const allocator_type& alloc = allocator_type())
1133  : root_(nullptr), head_leaf_(nullptr), tail_leaf_(nullptr),
1134  key_less_(kcf), allocator_(alloc) {
1135  insert(first, last);
1136  }
1137 
1138  //! Frees up all used B+ tree memory pages
1140  clear();
1141  }
1142 
1143  //! Fast swapping of two identical B+ tree objects.
1144  void swap(btree& from) {
1145  std::swap(root_, from.root_);
1148  std::swap(stats_, from.stats_);
1149  std::swap(key_less_, from.key_less_);
1151  }
1152 
1153  //! \}
1154 
1155 public:
1156  //! \name Key and Value Comparison Function Objects
1157  //! \{
1158 
1159  //! Function class to compare value_type objects. Required by the STL
1161  {
1162  protected:
1163  //! Key comparison function from the template parameter
1165 
1166  //! Constructor called from btree::value_comp()
1168  : key_comp(kc)
1169  { }
1170 
1171  //! Friendly to the btree class so it may call the constructor
1174 
1175  public:
1176  //! Function call "less"-operator resulting in true if x < y.
1177  bool operator () (const value_type& x, const value_type& y) const {
1178  return key_comp(x.first, y.first);
1179  }
1180  };
1181 
1182  //! Constant access to the key comparison object sorting the B+ tree.
1184  return key_less_;
1185  }
1186 
1187  //! Constant access to a constructed value_type comparison object. Required
1188  //! by the STL.
1189  value_compare value_comp() const {
1190  return value_compare(key_less_);
1191  }
1192 
1193  //! \}
1194 
1195 private:
1196  //! \name Convenient Key Comparison Functions Generated From key_less
1197  //! \{
1198 
1199  //! True if a < b ? "constructed" from key_less_()
1200  bool key_less(const key_type& a, const key_type b) const {
1201  return key_less_(a, b);
1202  }
1203 
1204  //! True if a <= b ? constructed from key_less()
1205  bool key_lessequal(const key_type& a, const key_type b) const {
1206  return !key_less_(b, a);
1207  }
1208 
1209  //! True if a > b ? constructed from key_less()
1210  bool key_greater(const key_type& a, const key_type& b) const {
1211  return key_less_(b, a);
1212  }
1213 
1214  //! True if a >= b ? constructed from key_less()
1215  bool key_greaterequal(const key_type& a, const key_type b) const {
1216  return !key_less_(a, b);
1217  }
1218 
1219  //! True if a == b ? constructed from key_less(). This requires the <
1220  //! relation to be a total order, otherwise the B+ tree cannot be sorted.
1221  bool key_equal(const key_type& a, const key_type& b) const {
1222  return !key_less_(a, b) && !key_less_(b, a);
1223  }
1224 
1225  //! \}
1226 
1227 public:
1228  //! \name Allocators
1229  //! \{
1230 
1231  //! Return the base node allocator provided during construction.
1233  return allocator_;
1234  }
1235 
1236  //! \}
1237 
1238 private:
1239  //! \name Node Object Allocation and Deallocation Functions
1240  //! \{
1241 
1242  //! Return an allocator for leaf_node objects.
1244  return typename leaf_node::alloc_type(allocator_);
1245  }
1246 
1247  //! Return an allocator for inner_node objects.
1249  return typename inner_node::alloc_type(allocator_);
1250  }
1251 
1252  //! Allocate and initialize a leaf node
1253  leaf_node * allocate_leaf() {
1254  leaf_node* n = new (leaf_node_allocator().allocate(1)) leaf_node();
1255  n->initialize();
1256  stats_.leaves++;
1257  return n;
1258  }
1259 
1260  //! Allocate and initialize an inner node
1261  inner_node * allocate_inner(unsigned short level) {
1262  inner_node* n = new (inner_node_allocator().allocate(1)) inner_node();
1263  n->initialize(level);
1264  stats_.inner_nodes++;
1265  return n;
1266  }
1267 
1268  //! Correctly free either inner or leaf node, destructs all contained key
1269  //! and value objects.
1270  void free_node(node* n) {
1271  if (n->is_leafnode()) {
1272  leaf_node* ln = static_cast<leaf_node*>(n);
1274  a.destroy(ln);
1275  a.deallocate(ln, 1);
1276  stats_.leaves--;
1277  }
1278  else {
1279  inner_node* in = static_cast<inner_node*>(n);
1281  a.destroy(in);
1282  a.deallocate(in, 1);
1283  stats_.inner_nodes--;
1284  }
1285  }
1286 
1287  //! \}
1288 
1289 public:
1290  //! \name Fast Destruction of the B+ Tree
1291  //! \{
1292 
1293  //! Frees all key/data pairs and all nodes of the tree.
1294  void clear() {
1295  if (root_)
1296  {
1298  free_node(root_);
1299 
1300  root_ = nullptr;
1301  head_leaf_ = tail_leaf_ = nullptr;
1302 
1303  stats_ = tree_stats();
1304  }
1305 
1307  }
1308 
1309 private:
1310  //! Recursively free up nodes.
1311  void clear_recursive(node* n) {
1312  if (n->is_leafnode())
1313  {
1314  leaf_node* leafnode = static_cast<leaf_node*>(n);
1315 
1316  for (unsigned int slot = 0; slot < leafnode->slotuse; ++slot)
1317  {
1318  // data objects are deleted by leaf_node's destructor
1319  }
1320  }
1321  else
1322  {
1323  inner_node* innernode = static_cast<inner_node*>(n);
1324 
1325  for (unsigned short slot = 0; slot < innernode->slotuse + 1; ++slot)
1326  {
1327  clear_recursive(innernode->childid[slot]);
1328  free_node(innernode->childid[slot]);
1329  }
1330  }
1331  }
1332 
1333  //! \}
1334 
1335 public:
1336  //! \name STL Iterator Construction Functions
1337  //! \{
1338 
1339  //! Constructs a read/data-write iterator that points to the first slot in
1340  //! the first leaf of the B+ tree.
1341  iterator begin() {
1342  return iterator(head_leaf_, 0);
1343  }
1344 
1345  //! Constructs a read/data-write iterator that points to the first invalid
1346  //! slot in the last leaf of the B+ tree.
1347  iterator end() {
1348  return iterator(tail_leaf_, tail_leaf_ ? tail_leaf_->slotuse : 0);
1349  }
1350 
1351  //! Constructs a read-only constant iterator that points to the first slot
1352  //! in the first leaf of the B+ tree.
1353  const_iterator begin() const {
1354  return const_iterator(head_leaf_, 0);
1355  }
1356 
1357  //! Constructs a read-only constant iterator that points to the first
1358  //! invalid slot in the last leaf of the B+ tree.
1359  const_iterator end() const {
1360  return const_iterator(tail_leaf_, tail_leaf_ ? tail_leaf_->slotuse : 0);
1361  }
1362 
1363  //! Constructs a read/data-write reverse iterator that points to the first
1364  //! invalid slot in the last leaf of the B+ tree. Uses STL magic.
1365  reverse_iterator rbegin() {
1366  return reverse_iterator(end());
1367  }
1368 
1369  //! Constructs a read/data-write reverse iterator that points to the first
1370  //! slot in the first leaf of the B+ tree. Uses STL magic.
1371  reverse_iterator rend() {
1372  return reverse_iterator(begin());
1373  }
1374 
1375  //! Constructs a read-only reverse iterator that points to the first
1376  //! invalid slot in the last leaf of the B+ tree. Uses STL magic.
1377  const_reverse_iterator rbegin() const {
1378  return const_reverse_iterator(end());
1379  }
1380 
1381  //! Constructs a read-only reverse iterator that points to the first slot
1382  //! in the first leaf of the B+ tree. Uses STL magic.
1383  const_reverse_iterator rend() const {
1384  return const_reverse_iterator(begin());
1385  }
1386 
1387  //! \}
1388 
1389 private:
1390  //! \name B+ Tree Node Binary Search Functions
1391  //! \{
1392 
1393  //! Searches for the first key in the node n greater or equal to key. Uses
1394  //! binary search with an optional linear self-verification. This is a
1395  //! template function, because the slotkey array is located at different
1396  //! places in leaf_node and inner_node.
1397  template <typename node_type>
1398  int find_lower(const node_type* n, const key_type& key) const {
1399  if (sizeof(*n) > traits::binsearch_threshold)
1400  {
1401  if (n->slotuse == 0) return 0;
1402 
1403  int lo = 0, hi = n->slotuse;
1404 
1405  while (lo < hi)
1406  {
1407  int mid = (lo + hi) >> 1;
1408 
1409  if (key_lessequal(key, n->key(mid))) {
1410  hi = mid; // key <= mid
1411  }
1412  else {
1413  lo = mid + 1; // key > mid
1414  }
1415  }
1416 
1417  TLX_BTREE_PRINT("btree::find_lower: on " << n <<
1418  " key " << key << " -> " << lo << " / " << hi);
1419 
1420  // verify result using simple linear search
1421  if (self_verify)
1422  {
1423  int i = 0;
1424  while (i < n->slotuse && key_less(n->key(i), key)) ++i;
1425 
1426  TLX_BTREE_PRINT("btree::find_lower: testfind: " << i);
1427  TLX_BTREE_ASSERT(i == lo);
1428  }
1429 
1430  return lo;
1431  }
1432  else // for nodes <= binsearch_threshold do linear search.
1433  {
1434  int lo = 0;
1435  while (lo < n->slotuse && key_less(n->key(lo), key)) ++lo;
1436  return lo;
1437  }
1438  }
1439 
1440  //! Searches for the first key in the node n greater than key. Uses binary
1441  //! search with an optional linear self-verification. This is a template
1442  //! function, because the slotkey array is located at different places in
1443  //! leaf_node and inner_node.
1444  template <typename node_type>
1445  int find_upper(const node_type* n, const key_type& key) const {
1446  if (sizeof(*n) > traits::binsearch_threshold)
1447  {
1448  if (n->slotuse == 0) return 0;
1449 
1450  int lo = 0, hi = n->slotuse;
1451 
1452  while (lo < hi)
1453  {
1454  int mid = (lo + hi) >> 1;
1455 
1456  if (key_less(key, n->key(mid))) {
1457  hi = mid; // key < mid
1458  }
1459  else {
1460  lo = mid + 1; // key >= mid
1461  }
1462  }
1463 
1464  TLX_BTREE_PRINT("btree::find_upper: on " << n <<
1465  " key " << key << " -> " << lo << " / " << hi);
1466 
1467  // verify result using simple linear search
1468  if (self_verify)
1469  {
1470  int i = 0;
1471  while (i < n->slotuse && key_lessequal(n->key(i), key)) ++i;
1472 
1473  TLX_BTREE_PRINT("btree::find_upper testfind: " << i);
1474  TLX_BTREE_ASSERT(i == hi);
1475  }
1476 
1477  return lo;
1478  }
1479  else // for nodes <= binsearch_threshold do linear search.
1480  {
1481  int lo = 0;
1482  while (lo < n->slotuse && key_lessequal(n->key(lo), key)) ++lo;
1483  return lo;
1484  }
1485  }
1486 
1487  //! \}
1488 
1489 public:
1490  //! \name Access Functions to the Item Count
1491  //! \{
1492 
1493  //! Return the number of key/data pairs in the B+ tree
1494  size_type size() const {
1495  return stats_.size;
1496  }
1497 
1498  //! Returns true if there is at least one key/data pair in the B+ tree
1499  bool empty() const {
1500  return (size() == size_type(0));
1501  }
1502 
1503  //! Returns the largest possible size of the B+ Tree. This is just a
1504  //! function required by the STL standard, the B+ Tree can hold more items.
1506  return size_type(-1);
1507  }
1508 
1509  //! Return a const reference to the current statistics.
1510  const struct tree_stats& get_stats() const {
1511  return stats_;
1512  }
1513 
1514  //! \}
1515 
1516 public:
1517  //! \name STL Access Functions Querying the Tree by Descending to a Leaf
1518  //! \{
1519 
1520  //! Non-STL function checking whether a key is in the B+ tree. The same as
1521  //! (find(k) != end()) or (count() != 0).
1522  bool exists(const key_type& key) const {
1523  const node* n = root_;
1524  if (!n) return false;
1525 
1526  while (!n->is_leafnode())
1527  {
1528  const inner_node* inner = static_cast<const inner_node*>(n);
1529  int slot = find_lower(inner, key);
1530 
1531  n = inner->childid[slot];
1532  }
1533 
1534  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1535 
1536  int slot = find_lower(leaf, key);
1537  return (slot < leaf->slotuse && key_equal(key, leaf->key(slot)));
1538  }
1539 
1540  //! Tries to locate a key in the B+ tree and returns an iterator to the
1541  //! key/data slot if found. If unsuccessful it returns end().
1542  iterator find(const key_type& key) {
1543  node* n = root_;
1544  if (!n) return end();
1545 
1546  while (!n->is_leafnode())
1547  {
1548  const inner_node* inner = static_cast<const inner_node*>(n);
1549  int slot = find_lower(inner, key);
1550 
1551  n = inner->childid[slot];
1552  }
1553 
1554  leaf_node* leaf = static_cast<leaf_node*>(n);
1555 
1556  int slot = find_lower(leaf, key);
1557  return (slot < leaf->slotuse && key_equal(key, leaf->key(slot)))
1558  ? iterator(leaf, slot) : end();
1559  }
1560 
1561  //! Tries to locate a key in the B+ tree and returns an constant iterator to
1562  //! the key/data slot if found. If unsuccessful it returns end().
1563  const_iterator find(const key_type& key) const {
1564  const node* n = root_;
1565  if (!n) return end();
1566 
1567  while (!n->is_leafnode())
1568  {
1569  const inner_node* inner = static_cast<const inner_node*>(n);
1570  int slot = find_lower(inner, key);
1571 
1572  n = inner->childid[slot];
1573  }
1574 
1575  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1576 
1577  int slot = find_lower(leaf, key);
1578  return (slot < leaf->slotuse && key_equal(key, leaf->key(slot)))
1579  ? const_iterator(leaf, slot) : end();
1580  }
1581 
1582  //! Tries to locate a key in the B+ tree and returns the number of identical
1583  //! key entries found.
1584  size_type count(const key_type& key) const {
1585  const node* n = root_;
1586  if (!n) return 0;
1587 
1588  while (!n->is_leafnode())
1589  {
1590  const inner_node* inner = static_cast<const inner_node*>(n);
1591  int slot = find_lower(inner, key);
1592 
1593  n = inner->childid[slot];
1594  }
1595 
1596  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1597 
1598  int slot = find_lower(leaf, key);
1599  size_type num = 0;
1600 
1601  while (leaf && slot < leaf->slotuse && key_equal(key, leaf->key(slot)))
1602  {
1603  ++num;
1604  if (++slot >= leaf->slotuse)
1605  {
1606  leaf = leaf->next_leaf;
1607  slot = 0;
1608  }
1609  }
1610 
1611  return num;
1612  }
1613 
1614  //! Searches the B+ tree and returns an iterator to the first pair equal to
1615  //! or greater than key, or end() if all keys are smaller.
1616  iterator lower_bound(const key_type& key) {
1617  node* n = root_;
1618  if (!n) return end();
1619 
1620  while (!n->is_leafnode())
1621  {
1622  const inner_node* inner = static_cast<const inner_node*>(n);
1623  int slot = find_lower(inner, key);
1624 
1625  n = inner->childid[slot];
1626  }
1627 
1628  leaf_node* leaf = static_cast<leaf_node*>(n);
1629 
1630  int slot = find_lower(leaf, key);
1631  return iterator(leaf, slot);
1632  }
1633 
1634  //! Searches the B+ tree and returns a constant iterator to the first pair
1635  //! equal to or greater than key, or end() if all keys are smaller.
1636  const_iterator lower_bound(const key_type& key) const {
1637  const node* n = root_;
1638  if (!n) return end();
1639 
1640  while (!n->is_leafnode())
1641  {
1642  const inner_node* inner = static_cast<const inner_node*>(n);
1643  int slot = find_lower(inner, key);
1644 
1645  n = inner->childid[slot];
1646  }
1647 
1648  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1649 
1650  int slot = find_lower(leaf, key);
1651  return const_iterator(leaf, slot);
1652  }
1653 
1654  //! Searches the B+ tree and returns an iterator to the first pair greater
1655  //! than key, or end() if all keys are smaller or equal.
1656  iterator upper_bound(const key_type& key) {
1657  node* n = root_;
1658  if (!n) return end();
1659 
1660  while (!n->is_leafnode())
1661  {
1662  const inner_node* inner = static_cast<const inner_node*>(n);
1663  int slot = find_upper(inner, key);
1664 
1665  n = inner->childid[slot];
1666  }
1667 
1668  leaf_node* leaf = static_cast<leaf_node*>(n);
1669 
1670  int slot = find_upper(leaf, key);
1671  return iterator(leaf, slot);
1672  }
1673 
1674  //! Searches the B+ tree and returns a constant iterator to the first pair
1675  //! greater than key, or end() if all keys are smaller or equal.
1676  const_iterator upper_bound(const key_type& key) const {
1677  const node* n = root_;
1678  if (!n) return end();
1679 
1680  while (!n->is_leafnode())
1681  {
1682  const inner_node* inner = static_cast<const inner_node*>(n);
1683  int slot = find_upper(inner, key);
1684 
1685  n = inner->childid[slot];
1686  }
1687 
1688  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1689 
1690  int slot = find_upper(leaf, key);
1691  return const_iterator(leaf, slot);
1692  }
1693 
1694  //! Searches the B+ tree and returns both lower_bound() and upper_bound().
1695  std::pair<iterator, iterator> equal_range(const key_type& key) {
1696  return std::pair<iterator, iterator>(
1697  lower_bound(key), upper_bound(key));
1698  }
1699 
1700  //! Searches the B+ tree and returns both lower_bound() and upper_bound().
1701  std::pair<const_iterator, const_iterator>
1702  equal_range(const key_type& key) const {
1703  return std::pair<const_iterator, const_iterator>(
1704  lower_bound(key), upper_bound(key));
1705  }
1706 
1707  //! \}
1708 
1709 public:
1710  //! \name B+ Tree Object Comparison Functions
1711  //! \{
1712 
1713  //! Equality relation of B+ trees of the same type. B+ trees of the same
1714  //! size and equal elements (both key and data) are considered equal. Beware
1715  //! of the random ordering of duplicate keys.
1716  bool operator == (const btree& other) const {
1717  return (size() == other.size()) &&
1718  std::equal(begin(), end(), other.begin());
1719  }
1720 
1721  //! Inequality relation. Based on operator==.
1722  bool operator != (const btree& other) const {
1723  return !(*this == other);
1724  }
1725 
1726  //! Total ordering relation of B+ trees of the same type. It uses
1727  //! std::lexicographical_compare() for the actual comparison of elements.
1728  bool operator < (const btree& other) const {
1729  return std::lexicographical_compare(
1730  begin(), end(), other.begin(), other.end());
1731  }
1732 
1733  //! Greater relation. Based on operator<.
1734  bool operator > (const btree& other) const {
1735  return other < *this;
1736  }
1737 
1738  //! Less-equal relation. Based on operator<.
1739  bool operator <= (const btree& other) const {
1740  return !(other < *this);
1741  }
1742 
1743  //! Greater-equal relation. Based on operator<.
1744  bool operator >= (const btree& other) const {
1745  return !(*this < other);
1746  }
1747 
1748  //! \}
1749 
1750 public:
1751  //! \name Fast Copy: Assign Operator and Copy Constructors
1752  //! \{
1753 
1754  //! Assignment operator. All the key/data pairs are copied.
1755  btree& operator = (const btree& other) {
1756  if (this != &other)
1757  {
1758  clear();
1759 
1760  key_less_ = other.key_comp();
1761  allocator_ = other.get_allocator();
1762 
1763  if (other.size() != 0)
1764  {
1766  if (other.root_) {
1767  root_ = copy_recursive(other.root_);
1768  }
1769  stats_ = other.stats_;
1770  }
1771 
1772  if (self_verify) verify();
1773  }
1774  return *this;
1775  }
1776 
1777  //! Copy constructor. The newly initialized B+ tree object will contain a
1778  //! copy of all key/data pairs.
1779  btree(const btree& other)
1780  : root_(nullptr), head_leaf_(nullptr), tail_leaf_(nullptr),
1781  stats_(other.stats_),
1782  key_less_(other.key_comp()),
1783  allocator_(other.get_allocator()) {
1784  if (size() > 0)
1785  {
1787  if (other.root_) {
1788  root_ = copy_recursive(other.root_);
1789  }
1790  if (self_verify) verify();
1791  }
1792  }
1793 
1794 private:
1795  //! Recursively copy nodes from another B+ tree object
1796  struct node * copy_recursive(const node* n) {
1797  if (n->is_leafnode())
1798  {
1799  const leaf_node* leaf = static_cast<const leaf_node*>(n);
1800  leaf_node* newleaf = allocate_leaf();
1801 
1802  newleaf->slotuse = leaf->slotuse;
1803  std::copy(leaf->slotdata, leaf->slotdata + leaf->slotuse,
1804  newleaf->slotdata);
1805 
1806  if (head_leaf_ == nullptr)
1807  {
1808  head_leaf_ = tail_leaf_ = newleaf;
1809  newleaf->prev_leaf = newleaf->next_leaf = nullptr;
1810  }
1811  else
1812  {
1813  newleaf->prev_leaf = tail_leaf_;
1814  tail_leaf_->next_leaf = newleaf;
1815  tail_leaf_ = newleaf;
1816  }
1817 
1818  return newleaf;
1819  }
1820  else
1821  {
1822  const inner_node* inner = static_cast<const inner_node*>(n);
1823  inner_node* newinner = allocate_inner(inner->level);
1824 
1825  newinner->slotuse = inner->slotuse;
1826  std::copy(inner->slotkey, inner->slotkey + inner->slotuse,
1827  newinner->slotkey);
1828 
1829  for (unsigned short slot = 0; slot <= inner->slotuse; ++slot)
1830  {
1831  newinner->childid[slot] = copy_recursive(inner->childid[slot]);
1832  }
1833 
1834  return newinner;
1835  }
1836  }
1837 
1838  //! \}
1839 
1840 public:
1841  //! \name Public Insertion Functions
1842  //! \{
1843 
1844  //! Attempt to insert a key/data pair into the B+ tree. If the tree does not
1845  //! allow duplicate keys, then the insert may fail if it is already present.
1846  std::pair<iterator, bool> insert(const value_type& x) {
1847  return insert_start(key_of_value::get(x), x);
1848  }
1849 
1850  //! Attempt to insert a key/data pair into the B+ tree. The iterator hint is
1851  //! currently ignored by the B+ tree insertion routine.
1852  iterator insert(iterator /* hint */, const value_type& x) {
1853  return insert_start(key_of_value::get(x), x).first;
1854  }
1855 
1856  //! Attempt to insert the range [first,last) of value_type pairs into the B+
1857  //! tree. Each key/data pair is inserted individually; to bulk load the
1858  //! tree, use a constructor with range.
1859  template <typename InputIterator>
1860  void insert(InputIterator first, InputIterator last) {
1861  InputIterator iter = first;
1862  while (iter != last)
1863  {
1864  insert(*iter);
1865  ++iter;
1866  }
1867  }
1868 
1869  //! \}
1870 
1871 private:
1872  //! \name Private Insertion Functions
1873  //! \{
1874 
1875  //! Start the insertion descent at the current root and handle root splits.
1876  //! Returns true if the item was inserted
1877  std::pair<iterator, bool>
1878  insert_start(const key_type& key, const value_type& value) {
1879 
1880  node* newchild = nullptr;
1881  key_type newkey = key_type();
1882 
1883  if (root_ == nullptr) {
1885  }
1886 
1887  std::pair<iterator, bool> r =
1888  insert_descend(root_, key, value, &newkey, &newchild);
1889 
1890  if (newchild)
1891  {
1892  inner_node* newroot = allocate_inner(root_->level + 1);
1893  newroot->slotkey[0] = newkey;
1894 
1895  newroot->childid[0] = root_;
1896  newroot->childid[1] = newchild;
1897 
1898  newroot->slotuse = 1;
1899 
1900  root_ = newroot;
1901  }
1902 
1903  // increment size if the item was inserted
1904  if (r.second) ++stats_.size;
1905 
1906 #ifdef TLX_BTREE_DEBUG
1907  if (debug) print(std::cout);
1908 #endif
1909 
1910  if (self_verify) {
1911  verify();
1912  TLX_BTREE_ASSERT(exists(key));
1913  }
1914 
1915  return r;
1916  }
1917 
1918  /*!
1919  * Insert an item into the B+ tree.
1920  *
1921  * Descend down the nodes to a leaf, insert the key/data pair in a free
1922  * slot. If the node overflows, then it must be split and the new split node
1923  * inserted into the parent. Unroll / this splitting up to the root.
1924  */
1925  std::pair<iterator, bool> insert_descend(
1926  node* n, const key_type& key, const value_type& value,
1927  key_type* splitkey, node** splitnode) {
1928 
1929  if (!n->is_leafnode())
1930  {
1931  inner_node* inner = static_cast<inner_node*>(n);
1932 
1933  key_type newkey = key_type();
1934  node* newchild = nullptr;
1935 
1936  int slot = find_lower(inner, key);
1937 
1938  TLX_BTREE_PRINT("btree::insert_descend into " << inner->childid[slot]);
1939 
1940  std::pair<iterator, bool> r =
1941  insert_descend(inner->childid[slot],
1942  key, value, &newkey, &newchild);
1943 
1944  if (newchild)
1945  {
1946  TLX_BTREE_PRINT("btree::insert_descend newchild" <<
1947  " with key " << newkey <<
1948  " node " << newchild << " at slot " << slot);
1949 
1950  if (inner->is_full())
1951  {
1952  split_inner_node(inner, splitkey, splitnode, slot);
1953 
1954  TLX_BTREE_PRINT("btree::insert_descend done split_inner:" <<
1955  " putslot: " << slot <<
1956  " putkey: " << newkey <<
1957  " upkey: " << *splitkey);
1958 
1959 #ifdef TLX_BTREE_DEBUG
1960  if (debug)
1961  {
1962  print_node(std::cout, inner);
1963  print_node(std::cout, *splitnode);
1964  }
1965 #endif
1966 
1967  // check if insert slot is in the split sibling node
1968  TLX_BTREE_PRINT("btree::insert_descend switch: "
1969  << slot << " > " << inner->slotuse + 1);
1970 
1971  if (slot == inner->slotuse + 1 && inner->slotuse < (*splitnode)->slotuse)
1972  {
1973  // special case when the insert slot matches the split
1974  // place between the two nodes, then the insert key
1975  // becomes the split key.
1976 
1977  TLX_BTREE_ASSERT(inner->slotuse + 1 < inner_slotmax);
1978 
1979  inner_node* splitinner = static_cast<inner_node*>(*splitnode);
1980 
1981  // move the split key and it's datum into the left node
1982  inner->slotkey[inner->slotuse] = *splitkey;
1983  inner->childid[inner->slotuse + 1] = splitinner->childid[0];
1984  inner->slotuse++;
1985 
1986  // set new split key and move corresponding datum into right node
1987  splitinner->childid[0] = newchild;
1988  *splitkey = newkey;
1989 
1990  return r;
1991  }
1992  else if (slot >= inner->slotuse + 1)
1993  {
1994  // in case the insert slot is in the newly create split
1995  // node, we reuse the code below.
1996 
1997  slot -= inner->slotuse + 1;
1998  inner = static_cast<inner_node*>(*splitnode);
1999  TLX_BTREE_PRINT("btree::insert_descend switching to splitted node " << inner << " slot " << slot);
2000  }
2001  }
2002 
2003  // move items and put pointer to child node into correct slot
2004  TLX_BTREE_ASSERT(slot >= 0 && slot <= inner->slotuse);
2005 
2006  std::copy_backward(inner->slotkey + slot, inner->slotkey + inner->slotuse,
2007  inner->slotkey + inner->slotuse + 1);
2008  std::copy_backward(inner->childid + slot, inner->childid + inner->slotuse + 1,
2009  inner->childid + inner->slotuse + 2);
2010 
2011  inner->slotkey[slot] = newkey;
2012  inner->childid[slot + 1] = newchild;
2013  inner->slotuse++;
2014  }
2015 
2016  return r;
2017  }
2018  else // n->is_leafnode() == true
2019  {
2020  leaf_node* leaf = static_cast<leaf_node*>(n);
2021 
2022  int slot = find_lower(leaf, key);
2023 
2024  if (!allow_duplicates && slot < leaf->slotuse && key_equal(key, leaf->key(slot))) {
2025  return std::pair<iterator, bool>(iterator(leaf, slot), false);
2026  }
2027 
2028  if (leaf->is_full())
2029  {
2030  split_leaf_node(leaf, splitkey, splitnode);
2031 
2032  // check if insert slot is in the split sibling node
2033  if (slot >= leaf->slotuse)
2034  {
2035  slot -= leaf->slotuse;
2036  leaf = static_cast<leaf_node*>(*splitnode);
2037  }
2038  }
2039 
2040  // move items and put data item into correct data slot
2041  TLX_BTREE_ASSERT(slot >= 0 && slot <= leaf->slotuse);
2042 
2043  std::copy_backward(leaf->slotdata + slot, leaf->slotdata + leaf->slotuse,
2044  leaf->slotdata + leaf->slotuse + 1);
2045 
2046  leaf->slotdata[slot] = value;
2047  leaf->slotuse++;
2048 
2049  if (splitnode && leaf != *splitnode && slot == leaf->slotuse - 1)
2050  {
2051  // special case: the node was split, and the insert is at the
2052  // last slot of the old node. then the splitkey must be updated.
2053  *splitkey = key;
2054  }
2055 
2056  return std::pair<iterator, bool>(iterator(leaf, slot), true);
2057  }
2058  }
2059 
2060  //! Split up a leaf node into two equally-filled sibling leaves. Returns the
2061  //! new nodes and it's insertion key in the two parameters.
2062  void split_leaf_node(leaf_node* leaf, key_type* out_newkey, node** out_newleaf) {
2063  TLX_BTREE_ASSERT(leaf->is_full());
2064 
2065  unsigned int mid = (leaf->slotuse >> 1);
2066 
2067  TLX_BTREE_PRINT("btree::split_leaf_node on " << leaf);
2068 
2069  leaf_node* newleaf = allocate_leaf();
2070 
2071  newleaf->slotuse = leaf->slotuse - mid;
2072 
2073  newleaf->next_leaf = leaf->next_leaf;
2074  if (newleaf->next_leaf == nullptr) {
2075  TLX_BTREE_ASSERT(leaf == tail_leaf_);
2076  tail_leaf_ = newleaf;
2077  }
2078  else {
2079  newleaf->next_leaf->prev_leaf = newleaf;
2080  }
2081 
2082  std::copy(leaf->slotdata + mid, leaf->slotdata + leaf->slotuse,
2083  newleaf->slotdata);
2084 
2085  leaf->slotuse = mid;
2086  leaf->next_leaf = newleaf;
2087  newleaf->prev_leaf = leaf;
2088 
2089  *out_newkey = leaf->key(leaf->slotuse - 1);
2090  *out_newleaf = newleaf;
2091  }
2092 
2093  //! Split up an inner node into two equally-filled sibling nodes. Returns
2094  //! the new nodes and it's insertion key in the two parameters. Requires the
2095  //! slot of the item will be inserted, so the nodes will be the same size
2096  //! after the insert.
2097  void split_inner_node(inner_node* inner, key_type* out_newkey,
2098  node** out_newinner, unsigned int addslot) {
2099  TLX_BTREE_ASSERT(inner->is_full());
2100 
2101  unsigned int mid = (inner->slotuse >> 1);
2102 
2103  TLX_BTREE_PRINT("btree::split_inner: mid " << mid << " addslot " << addslot);
2104 
2105  // if the split is uneven and the overflowing item will be put into the
2106  // larger node, then the smaller split node may underflow
2107  if (addslot <= mid && mid > inner->slotuse - (mid + 1))
2108  mid--;
2109 
2110  TLX_BTREE_PRINT("btree::split_inner: mid " << mid << " addslot " << addslot);
2111 
2112  TLX_BTREE_PRINT("btree::split_inner_node on " << inner << " into two nodes " << mid << " and " << inner->slotuse - (mid + 1) << " sized");
2113 
2114  inner_node* newinner = allocate_inner(inner->level);
2115 
2116  newinner->slotuse = inner->slotuse - (mid + 1);
2117 
2118  std::copy(inner->slotkey + mid + 1, inner->slotkey + inner->slotuse,
2119  newinner->slotkey);
2120  std::copy(inner->childid + mid + 1, inner->childid + inner->slotuse + 1,
2121  newinner->childid);
2122 
2123  inner->slotuse = mid;
2124 
2125  *out_newkey = inner->key(mid);
2126  *out_newinner = newinner;
2127  }
2128 
2129  //! \}
2130 
2131 public:
2132  //! \name Bulk Loader - Construct Tree from Sorted Sequence
2133  //! \{
2134 
2135  //! Bulk load a sorted range. Loads items into leaves and constructs a
2136  //! B-tree above them. The tree must be empty when calling this function.
2137  template <typename Iterator>
2138  void bulk_load(Iterator ibegin, Iterator iend) {
2140 
2141  stats_.size = iend - ibegin;
2142 
2143  // calculate number of leaves needed, round up.
2144  size_t num_items = iend - ibegin;
2145  size_t num_leaves = (num_items + leaf_slotmax - 1) / leaf_slotmax;
2146 
2147  TLX_BTREE_PRINT("btree::bulk_load, level 0: " << stats_.size << " items into " << num_leaves << " leaves with up to " << ((iend - ibegin + num_leaves - 1) / num_leaves) << " items per leaf.");
2148 
2149  Iterator it = ibegin;
2150  for (size_t i = 0; i < num_leaves; ++i)
2151  {
2152  // allocate new leaf node
2153  leaf_node* leaf = allocate_leaf();
2154 
2155  // copy keys or (key,value) pairs into leaf nodes, uses template
2156  // switch leaf->set_slot().
2157  leaf->slotuse = static_cast<int>(num_items / (num_leaves - i));
2158  for (size_t s = 0; s < leaf->slotuse; ++s, ++it)
2159  leaf->set_slot(s, *it);
2160 
2161  if (tail_leaf_ != nullptr) {
2162  tail_leaf_->next_leaf = leaf;
2163  leaf->prev_leaf = tail_leaf_;
2164  }
2165  else {
2166  head_leaf_ = leaf;
2167  }
2168  tail_leaf_ = leaf;
2169 
2170  num_items -= leaf->slotuse;
2171  }
2172 
2173  TLX_BTREE_ASSERT(it == iend && num_items == 0);
2174 
2175  // if the btree is so small to fit into one leaf, then we're done.
2176  if (head_leaf_ == tail_leaf_) {
2177  root_ = head_leaf_;
2178  return;
2179  }
2180 
2181  TLX_BTREE_ASSERT(stats_.leaves == num_leaves);
2182 
2183  // create first level of inner nodes, pointing to the leaves.
2184  size_t num_parents = (num_leaves + (inner_slotmax + 1) - 1) / (inner_slotmax + 1);
2185 
2186  TLX_BTREE_PRINT("btree::bulk_load, level 1: " << num_leaves << " leaves in " << num_parents << " inner nodes with up to " << ((num_leaves + num_parents - 1) / num_parents) << " leaves per inner node.");
2187 
2188  // save inner nodes and maxkey for next level.
2189  typedef std::pair<inner_node*, const key_type*> nextlevel_type;
2190  nextlevel_type* nextlevel = new nextlevel_type[num_parents];
2191 
2192  leaf_node* leaf = head_leaf_;
2193  for (size_t i = 0; i < num_parents; ++i)
2194  {
2195  // allocate new inner node at level 1
2196  inner_node* n = allocate_inner(1);
2197 
2198  n->slotuse = static_cast<int>(num_leaves / (num_parents - i));
2199  TLX_BTREE_ASSERT(n->slotuse > 0);
2200  --n->slotuse; // this counts keys, but an inner node has keys+1 children.
2201 
2202  // copy last key from each leaf and set child
2203  for (unsigned short s = 0; s < n->slotuse; ++s)
2204  {
2205  n->slotkey[s] = leaf->key(leaf->slotuse - 1);
2206  n->childid[s] = leaf;
2207  leaf = leaf->next_leaf;
2208  }
2209  n->childid[n->slotuse] = leaf;
2210 
2211  // track max key of any descendant.
2212  nextlevel[i].first = n;
2213  nextlevel[i].second = &leaf->key(leaf->slotuse - 1);
2214 
2215  leaf = leaf->next_leaf;
2216  num_leaves -= n->slotuse + 1;
2217  }
2218 
2219  TLX_BTREE_ASSERT(leaf == nullptr && num_leaves == 0);
2220 
2221  // recursively build inner nodes pointing to inner nodes.
2222  for (int level = 2; num_parents != 1; ++level)
2223  {
2224  size_t num_children = num_parents;
2225  num_parents = (num_children + (inner_slotmax + 1) - 1) / (inner_slotmax + 1);
2226 
2227  TLX_BTREE_PRINT("btree::bulk_load, level " << level << ": " << num_children << " children in " << num_parents << " inner nodes with up to " << ((num_children + num_parents - 1) / num_parents) << " children per inner node.");
2228 
2229  size_t inner_index = 0;
2230  for (size_t i = 0; i < num_parents; ++i)
2231  {
2232  // allocate new inner node at level
2233  inner_node* n = allocate_inner(level);
2234 
2235  n->slotuse = static_cast<int>(num_children / (num_parents - i));
2236  TLX_BTREE_ASSERT(n->slotuse > 0);
2237  --n->slotuse; // this counts keys, but an inner node has keys+1 children.
2238 
2239  // copy children and maxkeys from nextlevel
2240  for (unsigned short s = 0; s < n->slotuse; ++s)
2241  {
2242  n->slotkey[s] = *nextlevel[inner_index].second;
2243  n->childid[s] = nextlevel[inner_index].first;
2244  ++inner_index;
2245  }
2246  n->childid[n->slotuse] = nextlevel[inner_index].first;
2247 
2248  // reuse nextlevel array for parents, because we can overwrite
2249  // slots we've already consumed.
2250  nextlevel[i].first = n;
2251  nextlevel[i].second = nextlevel[inner_index].second;
2252 
2253  ++inner_index;
2254  num_children -= n->slotuse + 1;
2255  }
2256 
2257  TLX_BTREE_ASSERT(num_children == 0);
2258  }
2259 
2260  root_ = nextlevel[0].first;
2261  delete[] nextlevel;
2262 
2263  if (self_verify) verify();
2264  }
2265 
2266  //! \}
2267 
2268 private:
2269  //! \name Support Class Encapsulating Deletion Results
2270  //! \{
2271 
2272  //! Result flags of recursive deletion.
2274  //! Deletion successful and no fix-ups necessary.
2276 
2277  //! Deletion not successful because key was not found.
2279 
2280  //! Deletion successful, the last key was updated so parent slotkeys
2281  //! need updates.
2283 
2284  //! Deletion successful, children nodes were merged and the parent needs
2285  //! to remove the empty node.
2287  };
2288 
2289  //! B+ tree recursive deletion has much information which is needs to be
2290  //! passed upward.
2291  struct result_t {
2292  //! Merged result flags
2294 
2295  //! The key to be updated at the parent's slot
2297 
2298  //! Constructor of a result with a specific flag, this can also be used
2299  //! as for implicit conversion.
2301  : flags(f), lastkey()
2302  { }
2303 
2304  //! Constructor with a lastkey value.
2306  : flags(f), lastkey(k)
2307  { }
2308 
2309  //! Test if this result object has a given flag set.
2310  bool has(result_flags_t f) const {
2311  return (flags & f) != 0;
2312  }
2313 
2314  //! Merge two results OR-ing the result flags and overwriting lastkeys.
2315  result_t& operator |= (const result_t& other) {
2316  flags = result_flags_t(flags | other.flags);
2317 
2318  // we overwrite existing lastkeys on purpose
2319  if (other.has(btree_update_lastkey))
2320  lastkey = other.lastkey;
2321 
2322  return *this;
2323  }
2324  };
2325 
2326  //! \}
2327 
2328 public:
2329  //! \name Public Erase Functions
2330  //! \{
2331 
2332  //! Erases one (the first) of the key/data pairs associated with the given
2333  //! key.
2334  bool erase_one(const key_type& key) {
2335  TLX_BTREE_PRINT("btree::erase_one(" << key << ") on btree size " << size());
2336 
2337  if (self_verify) verify();
2338 
2339  if (!root_) return false;
2340 
2341  result_t result = erase_one_descend(key, root_, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
2342 
2343  if (!result.has(btree_not_found))
2344  --stats_.size;
2345 
2346 #ifdef TLX_BTREE_DEBUG
2347  if (debug) print(std::cout);
2348 #endif
2349  if (self_verify) verify();
2350 
2351  return !result.has(btree_not_found);
2352  }
2353 
2354  //! Erases all the key/data pairs associated with the given key. This is
2355  //! implemented using erase_one().
2356  size_type erase(const key_type& key) {
2357  size_type c = 0;
2358 
2359  while (erase_one(key))
2360  {
2361  ++c;
2362  if (!allow_duplicates) break;
2363  }
2364 
2365  return c;
2366  }
2367 
2368  //! Erase the key/data pair referenced by the iterator.
2369  void erase(iterator iter) {
2370  TLX_BTREE_PRINT("btree::erase_iter(" << iter.curr_leaf << "," << iter.curr_slot << ") on btree size " << size());
2371 
2372  if (self_verify) verify();
2373 
2374  if (!root_) return;
2375 
2376  result_t result = erase_iter_descend(iter, root_, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
2377 
2378  if (!result.has(btree_not_found))
2379  --stats_.size;
2380 
2381 #ifdef TLX_BTREE_DEBUG
2382  if (debug) print(std::cout);
2383 #endif
2384  if (self_verify) verify();
2385  }
2386 
2387 #ifdef BTREE_TODO
2388  //! Erase all key/data pairs in the range [first,last). This function is
2389  //! currently not implemented by the B+ Tree.
2390  void erase(iterator /* first */, iterator /* last */) {
2391  abort();
2392  }
2393 #endif
2394 
2395  //! \}
2396 
2397 private:
2398  //! \name Private Erase Functions
2399  //! \{
2400 
2401  /*!
2402  * Erase one (the first) key/data pair in the B+ tree matching key.
2403  *
2404  * Descends down the tree in search of key. During the descent the parent,
2405  * left and right siblings and their parents are computed and passed
2406  * down. Once the key/data pair is found, it is removed from the leaf. If
2407  * the leaf underflows 6 different cases are handled. These cases resolve
2408  * the underflow by shifting key/data pairs from adjacent sibling nodes,
2409  * merging two sibling nodes or trimming the tree.
2410  */
2411  result_t erase_one_descend(const key_type& key,
2412  node* curr,
2413  node* left, node* right,
2414  inner_node* left_parent, inner_node* right_parent,
2415  inner_node* parent, unsigned int parentslot) {
2416  if (curr->is_leafnode())
2417  {
2418  leaf_node* leaf = static_cast<leaf_node*>(curr);
2419  leaf_node* left_leaf = static_cast<leaf_node*>(left);
2420  leaf_node* right_leaf = static_cast<leaf_node*>(right);
2421 
2422  int slot = find_lower(leaf, key);
2423 
2424  if (slot >= leaf->slotuse || !key_equal(key, leaf->key(slot)))
2425  {
2426  TLX_BTREE_PRINT("Could not find key " << key << " to erase.");
2427 
2428  return btree_not_found;
2429  }
2430 
2431  TLX_BTREE_PRINT("Found key in leaf " << curr << " at slot " << slot);
2432 
2433  std::copy(leaf->slotdata + slot + 1, leaf->slotdata + leaf->slotuse,
2434  leaf->slotdata + slot);
2435 
2436  leaf->slotuse--;
2437 
2438  result_t myres = btree_ok;
2439 
2440  // if the last key of the leaf was changed, the parent is notified
2441  // and updates the key of this leaf
2442  if (slot == leaf->slotuse)
2443  {
2444  if (parent && parentslot < parent->slotuse)
2445  {
2446  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2447  parent->slotkey[parentslot] = leaf->key(leaf->slotuse - 1);
2448  }
2449  else
2450  {
2451  if (leaf->slotuse >= 1)
2452  {
2453  TLX_BTREE_PRINT("Scheduling lastkeyupdate: key " << leaf->key(leaf->slotuse - 1));
2454  myres |= result_t(btree_update_lastkey, leaf->key(leaf->slotuse - 1));
2455  }
2456  else
2457  {
2458  TLX_BTREE_ASSERT(leaf == root_);
2459  }
2460  }
2461  }
2462 
2463  if (leaf->is_underflow() && !(leaf == root_ && leaf->slotuse >= 1))
2464  {
2465  // determine what to do about the underflow
2466 
2467  // case : if this empty leaf is the root, then delete all nodes
2468  // and set root to nullptr.
2469  if (left_leaf == nullptr && right_leaf == nullptr)
2470  {
2471  TLX_BTREE_ASSERT(leaf == root_);
2472  TLX_BTREE_ASSERT(leaf->slotuse == 0);
2473 
2474  free_node(root_);
2475 
2476  root_ = leaf = nullptr;
2477  head_leaf_ = tail_leaf_ = nullptr;
2478 
2479  // will be decremented soon by insert_start()
2483 
2484  return btree_ok;
2485  }
2486  // case : if both left and right leaves would underflow in case
2487  // of a shift, then merging is necessary. choose the more local
2488  // merger with our parent
2489  else if ((left_leaf == nullptr || left_leaf->is_few()) && (right_leaf == nullptr || right_leaf->is_few()))
2490  {
2491  if (left_parent == parent)
2492  myres |= merge_leaves(left_leaf, leaf, left_parent);
2493  else
2494  myres |= merge_leaves(leaf, right_leaf, right_parent);
2495  }
2496  // case : the right leaf has extra data, so balance right with
2497  // current
2498  else if ((left_leaf != nullptr && left_leaf->is_few()) && (right_leaf != nullptr && !right_leaf->is_few()))
2499  {
2500  if (right_parent == parent)
2501  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2502  else
2503  myres |= merge_leaves(left_leaf, leaf, left_parent);
2504  }
2505  // case : the left leaf has extra data, so balance left with
2506  // current
2507  else if ((left_leaf != nullptr && !left_leaf->is_few()) && (right_leaf != nullptr && right_leaf->is_few()))
2508  {
2509  if (left_parent == parent)
2510  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2511  else
2512  myres |= merge_leaves(leaf, right_leaf, right_parent);
2513  }
2514  // case : both the leaf and right leaves have extra data and our
2515  // parent, choose the leaf with more data
2516  else if (left_parent == right_parent)
2517  {
2518  if (left_leaf->slotuse <= right_leaf->slotuse)
2519  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2520  else
2521  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2522  }
2523  else
2524  {
2525  if (left_parent == parent)
2526  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2527  else
2528  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2529  }
2530  }
2531 
2532  return myres;
2533  }
2534  else // !curr->is_leafnode()
2535  {
2536  inner_node* inner = static_cast<inner_node*>(curr);
2537  inner_node* left_inner = static_cast<inner_node*>(left);
2538  inner_node* right_inner = static_cast<inner_node*>(right);
2539 
2540  node* myleft, * myright;
2541  inner_node* myleft_parent, * myright_parent;
2542 
2543  int slot = find_lower(inner, key);
2544 
2545  if (slot == 0) {
2546  myleft = (left == nullptr) ? nullptr : (static_cast<inner_node*>(left))->childid[left->slotuse - 1];
2547  myleft_parent = left_parent;
2548  }
2549  else {
2550  myleft = inner->childid[slot - 1];
2551  myleft_parent = inner;
2552  }
2553 
2554  if (slot == inner->slotuse) {
2555  myright = (right == nullptr) ? nullptr : (static_cast<inner_node*>(right))->childid[0];
2556  myright_parent = right_parent;
2557  }
2558  else {
2559  myright = inner->childid[slot + 1];
2560  myright_parent = inner;
2561  }
2562 
2563  TLX_BTREE_PRINT("erase_one_descend into " << inner->childid[slot]);
2564 
2565  result_t result = erase_one_descend(key,
2566  inner->childid[slot],
2567  myleft, myright,
2568  myleft_parent, myright_parent,
2569  inner, slot);
2570 
2571  result_t myres = btree_ok;
2572 
2573  if (result.has(btree_not_found))
2574  {
2575  return result;
2576  }
2577 
2578  if (result.has(btree_update_lastkey))
2579  {
2580  if (parent && parentslot < parent->slotuse)
2581  {
2582  TLX_BTREE_PRINT("Fixing lastkeyupdate: key " << result.lastkey << " into parent " << parent << " at parentslot " << parentslot);
2583 
2584  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2585  parent->slotkey[parentslot] = result.lastkey;
2586  }
2587  else
2588  {
2589  TLX_BTREE_PRINT("Forwarding lastkeyupdate: key " << result.lastkey);
2590  myres |= result_t(btree_update_lastkey, result.lastkey);
2591  }
2592  }
2593 
2594  if (result.has(btree_fixmerge))
2595  {
2596  // either the current node or the next is empty and should be removed
2597  if (inner->childid[slot]->slotuse != 0)
2598  slot++;
2599 
2600  // this is the child slot invalidated by the merge
2601  TLX_BTREE_ASSERT(inner->childid[slot]->slotuse == 0);
2602 
2603  free_node(inner->childid[slot]);
2604 
2605  std::copy(inner->slotkey + slot, inner->slotkey + inner->slotuse,
2606  inner->slotkey + slot - 1);
2607  std::copy(inner->childid + slot + 1, inner->childid + inner->slotuse + 1,
2608  inner->childid + slot);
2609 
2610  inner->slotuse--;
2611 
2612  if (inner->level == 1)
2613  {
2614  // fix split key for children leaves
2615  slot--;
2616  leaf_node* child = static_cast<leaf_node*>(inner->childid[slot]);
2617  inner->slotkey[slot] = child->key(child->slotuse - 1);
2618  }
2619  }
2620 
2621  if (inner->is_underflow() && !(inner == root_ && inner->slotuse >= 1))
2622  {
2623  // case: the inner node is the root and has just one child. that child becomes the new root
2624  if (left_inner == nullptr && right_inner == nullptr)
2625  {
2626  TLX_BTREE_ASSERT(inner == root_);
2627  TLX_BTREE_ASSERT(inner->slotuse == 0);
2628 
2629  root_ = inner->childid[0];
2630 
2631  inner->slotuse = 0;
2632  free_node(inner);
2633 
2634  return btree_ok;
2635  }
2636  // case : if both left and right leaves would underflow in case
2637  // of a shift, then merging is necessary. choose the more local
2638  // merger with our parent
2639  else if ((left_inner == nullptr || left_inner->is_few()) && (right_inner == nullptr || right_inner->is_few()))
2640  {
2641  if (left_parent == parent)
2642  myres |= merge_inner(left_inner, inner, left_parent, parentslot - 1);
2643  else
2644  myres |= merge_inner(inner, right_inner, right_parent, parentslot);
2645  }
2646  // case : the right leaf has extra data, so balance right with
2647  // current
2648  else if ((left_inner != nullptr && left_inner->is_few()) && (right_inner != nullptr && !right_inner->is_few()))
2649  {
2650  if (right_parent == parent)
2651  shift_left_inner(inner, right_inner, right_parent, parentslot);
2652  else
2653  myres |= merge_inner(left_inner, inner, left_parent, parentslot - 1);
2654  }
2655  // case : the left leaf has extra data, so balance left with
2656  // current
2657  else if ((left_inner != nullptr && !left_inner->is_few()) && (right_inner != nullptr && right_inner->is_few()))
2658  {
2659  if (left_parent == parent)
2660  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2661  else
2662  myres |= merge_inner(inner, right_inner, right_parent, parentslot);
2663  }
2664  // case : both the leaf and right leaves have extra data and our
2665  // parent, choose the leaf with more data
2666  else if (left_parent == right_parent)
2667  {
2668  if (left_inner->slotuse <= right_inner->slotuse)
2669  shift_left_inner(inner, right_inner, right_parent, parentslot);
2670  else
2671  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2672  }
2673  else
2674  {
2675  if (left_parent == parent)
2676  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2677  else
2678  shift_left_inner(inner, right_inner, right_parent, parentslot);
2679  }
2680  }
2681 
2682  return myres;
2683  }
2684  }
2685 
2686  /*!
2687  * Erase one key/data pair referenced by an iterator in the B+ tree.
2688  *
2689  * Descends down the tree in search of an iterator. During the descent the
2690  * parent, left and right siblings and their parents are computed and passed
2691  * down. The difficulty is that the iterator contains only a pointer to a
2692  * leaf_node, which means that this function must do a recursive depth first
2693  * search for that leaf node in the subtree containing all pairs of the same
2694  * key. This subtree can be very large, even the whole tree, though in
2695  * practice it would not make sense to have so many duplicate keys.
2696  *
2697  * Once the referenced key/data pair is found, it is removed from the leaf
2698  * and the same underflow cases are handled as in erase_one_descend.
2699  */
2700  result_t erase_iter_descend(const iterator& iter,
2701  node* curr,
2702  node* left, node* right,
2703  inner_node* left_parent, inner_node* right_parent,
2704  inner_node* parent, unsigned int parentslot) {
2705  if (curr->is_leafnode())
2706  {
2707  leaf_node* leaf = static_cast<leaf_node*>(curr);
2708  leaf_node* left_leaf = static_cast<leaf_node*>(left);
2709  leaf_node* right_leaf = static_cast<leaf_node*>(right);
2710 
2711  // if this is not the correct leaf, get next step in recursive
2712  // search
2713  if (leaf != iter.curr_leaf)
2714  {
2715  return btree_not_found;
2716  }
2717 
2718  if (iter.curr_slot >= leaf->slotuse)
2719  {
2720  TLX_BTREE_PRINT("Could not find iterator (" << iter.curr_leaf << "," << iter.curr_slot << ") to erase. Invalid leaf node?");
2721 
2722  return btree_not_found;
2723  }
2724 
2725  int slot = iter.curr_slot;
2726 
2727  TLX_BTREE_PRINT("Found iterator in leaf " << curr << " at slot " << slot);
2728 
2729  std::copy(leaf->slotdata + slot + 1, leaf->slotdata + leaf->slotuse,
2730  leaf->slotdata + slot);
2731 
2732  leaf->slotuse--;
2733 
2734  result_t myres = btree_ok;
2735 
2736  // if the last key of the leaf was changed, the parent is notified
2737  // and updates the key of this leaf
2738  if (slot == leaf->slotuse)
2739  {
2740  if (parent && parentslot < parent->slotuse)
2741  {
2742  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2743  parent->slotkey[parentslot] = leaf->key(leaf->slotuse - 1);
2744  }
2745  else
2746  {
2747  if (leaf->slotuse >= 1)
2748  {
2749  TLX_BTREE_PRINT("Scheduling lastkeyupdate: key " << leaf->key(leaf->slotuse - 1));
2750  myres |= result_t(btree_update_lastkey, leaf->key(leaf->slotuse - 1));
2751  }
2752  else
2753  {
2754  TLX_BTREE_ASSERT(leaf == root_);
2755  }
2756  }
2757  }
2758 
2759  if (leaf->is_underflow() && !(leaf == root_ && leaf->slotuse >= 1))
2760  {
2761  // determine what to do about the underflow
2762 
2763  // case : if this empty leaf is the root, then delete all nodes
2764  // and set root to nullptr.
2765  if (left_leaf == nullptr && right_leaf == nullptr)
2766  {
2767  TLX_BTREE_ASSERT(leaf == root_);
2768  TLX_BTREE_ASSERT(leaf->slotuse == 0);
2769 
2770  free_node(root_);
2771 
2772  root_ = leaf = nullptr;
2773  head_leaf_ = tail_leaf_ = nullptr;
2774 
2775  // will be decremented soon by insert_start()
2779 
2780  return btree_ok;
2781  }
2782  // case : if both left and right leaves would underflow in case
2783  // of a shift, then merging is necessary. choose the more local
2784  // merger with our parent
2785  else if ((left_leaf == nullptr || left_leaf->is_few()) && (right_leaf == nullptr || right_leaf->is_few()))
2786  {
2787  if (left_parent == parent)
2788  myres |= merge_leaves(left_leaf, leaf, left_parent);
2789  else
2790  myres |= merge_leaves(leaf, right_leaf, right_parent);
2791  }
2792  // case : the right leaf has extra data, so balance right with
2793  // current
2794  else if ((left_leaf != nullptr && left_leaf->is_few()) && (right_leaf != nullptr && !right_leaf->is_few()))
2795  {
2796  if (right_parent == parent)
2797  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2798  else
2799  myres |= merge_leaves(left_leaf, leaf, left_parent);
2800  }
2801  // case : the left leaf has extra data, so balance left with
2802  // current
2803  else if ((left_leaf != nullptr && !left_leaf->is_few()) && (right_leaf != nullptr && right_leaf->is_few()))
2804  {
2805  if (left_parent == parent)
2806  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2807  else
2808  myres |= merge_leaves(leaf, right_leaf, right_parent);
2809  }
2810  // case : both the leaf and right leaves have extra data and our
2811  // parent, choose the leaf with more data
2812  else if (left_parent == right_parent)
2813  {
2814  if (left_leaf->slotuse <= right_leaf->slotuse)
2815  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2816  else
2817  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2818  }
2819  else
2820  {
2821  if (left_parent == parent)
2822  shift_right_leaf(left_leaf, leaf, left_parent, parentslot - 1);
2823  else
2824  myres |= shift_left_leaf(leaf, right_leaf, right_parent, parentslot);
2825  }
2826  }
2827 
2828  return myres;
2829  }
2830  else // !curr->is_leafnode()
2831  {
2832  inner_node* inner = static_cast<inner_node*>(curr);
2833  inner_node* left_inner = static_cast<inner_node*>(left);
2834  inner_node* right_inner = static_cast<inner_node*>(right);
2835 
2836  // find first slot below which the searched iterator might be
2837  // located.
2838 
2839  result_t result;
2840  int slot = find_lower(inner, iter.key());
2841 
2842  while (slot <= inner->slotuse)
2843  {
2844  node* myleft, * myright;
2845  inner_node* myleft_parent, * myright_parent;
2846 
2847  if (slot == 0) {
2848  myleft = (left == nullptr) ? nullptr : (static_cast<inner_node*>(left))->childid[left->slotuse - 1];
2849  myleft_parent = left_parent;
2850  }
2851  else {
2852  myleft = inner->childid[slot - 1];
2853  myleft_parent = inner;
2854  }
2855 
2856  if (slot == inner->slotuse) {
2857  myright = (right == nullptr) ? nullptr : (static_cast<inner_node*>(right))->childid[0];
2858  myright_parent = right_parent;
2859  }
2860  else {
2861  myright = inner->childid[slot + 1];
2862  myright_parent = inner;
2863  }
2864 
2865  TLX_BTREE_PRINT("erase_iter_descend into " << inner->childid[slot]);
2866 
2867  result = erase_iter_descend(iter,
2868  inner->childid[slot],
2869  myleft, myright,
2870  myleft_parent, myright_parent,
2871  inner, slot);
2872 
2873  if (!result.has(btree_not_found))
2874  break;
2875 
2876  // continue recursive search for leaf on next slot
2877 
2878  if (slot < inner->slotuse && key_less(inner->slotkey[slot], iter.key()))
2879  return btree_not_found;
2880 
2881  ++slot;
2882  }
2883 
2884  if (slot > inner->slotuse)
2885  return btree_not_found;
2886 
2887  result_t myres = btree_ok;
2888 
2889  if (result.has(btree_update_lastkey))
2890  {
2891  if (parent && parentslot < parent->slotuse)
2892  {
2893  TLX_BTREE_PRINT("Fixing lastkeyupdate: key " << result.lastkey << " into parent " << parent << " at parentslot " << parentslot);
2894 
2895  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2896  parent->slotkey[parentslot] = result.lastkey;
2897  }
2898  else
2899  {
2900  TLX_BTREE_PRINT("Forwarding lastkeyupdate: key " << result.lastkey);
2901  myres |= result_t(btree_update_lastkey, result.lastkey);
2902  }
2903  }
2904 
2905  if (result.has(btree_fixmerge))
2906  {
2907  // either the current node or the next is empty and should be removed
2908  if (inner->childid[slot]->slotuse != 0)
2909  slot++;
2910 
2911  // this is the child slot invalidated by the merge
2912  TLX_BTREE_ASSERT(inner->childid[slot]->slotuse == 0);
2913 
2914  free_node(inner->childid[slot]);
2915 
2916  std::copy(inner->slotkey + slot, inner->slotkey + inner->slotuse,
2917  inner->slotkey + slot - 1);
2918  std::copy(inner->childid + slot + 1, inner->childid + inner->slotuse + 1,
2919  inner->childid + slot);
2920 
2921  inner->slotuse--;
2922 
2923  if (inner->level == 1)
2924  {
2925  // fix split key for children leaves
2926  slot--;
2927  leaf_node* child = static_cast<leaf_node*>(inner->childid[slot]);
2928  inner->slotkey[slot] = child->key(child->slotuse - 1);
2929  }
2930  }
2931 
2932  if (inner->is_underflow() && !(inner == root_ && inner->slotuse >= 1))
2933  {
2934  // case: the inner node is the root and has just one
2935  // child. that child becomes the new root
2936  if (left_inner == nullptr && right_inner == nullptr)
2937  {
2938  TLX_BTREE_ASSERT(inner == root_);
2939  TLX_BTREE_ASSERT(inner->slotuse == 0);
2940 
2941  root_ = inner->childid[0];
2942 
2943  inner->slotuse = 0;
2944  free_node(inner);
2945 
2946  return btree_ok;
2947  }
2948  // case : if both left and right leaves would underflow in case
2949  // of a shift, then merging is necessary. choose the more local
2950  // merger with our parent
2951  else if ((left_inner == nullptr || left_inner->is_few()) && (right_inner == nullptr || right_inner->is_few()))
2952  {
2953  if (left_parent == parent)
2954  myres |= merge_inner(left_inner, inner, left_parent, parentslot - 1);
2955  else
2956  myres |= merge_inner(inner, right_inner, right_parent, parentslot);
2957  }
2958  // case : the right leaf has extra data, so balance right with
2959  // current
2960  else if ((left_inner != nullptr && left_inner->is_few()) && (right_inner != nullptr && !right_inner->is_few()))
2961  {
2962  if (right_parent == parent)
2963  shift_left_inner(inner, right_inner, right_parent, parentslot);
2964  else
2965  myres |= merge_inner(left_inner, inner, left_parent, parentslot - 1);
2966  }
2967  // case : the left leaf has extra data, so balance left with
2968  // current
2969  else if ((left_inner != nullptr && !left_inner->is_few()) && (right_inner != nullptr && right_inner->is_few()))
2970  {
2971  if (left_parent == parent)
2972  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2973  else
2974  myres |= merge_inner(inner, right_inner, right_parent, parentslot);
2975  }
2976  // case : both the leaf and right leaves have extra data and our
2977  // parent, choose the leaf with more data
2978  else if (left_parent == right_parent)
2979  {
2980  if (left_inner->slotuse <= right_inner->slotuse)
2981  shift_left_inner(inner, right_inner, right_parent, parentslot);
2982  else
2983  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2984  }
2985  else
2986  {
2987  if (left_parent == parent)
2988  shift_right_inner(left_inner, inner, left_parent, parentslot - 1);
2989  else
2990  shift_left_inner(inner, right_inner, right_parent, parentslot);
2991  }
2992  }
2993 
2994  return myres;
2995  }
2996  }
2997 
2998  //! Merge two leaf nodes. The function moves all key/data pairs from right
2999  //! to left and sets right's slotuse to zero. The right slot is then removed
3000  //! by the calling parent node.
3001  result_t merge_leaves(leaf_node* left, leaf_node* right,
3002  inner_node* parent) {
3003  TLX_BTREE_PRINT("Merge leaf nodes " << left << " and " << right << " with common parent " << parent << ".");
3004  (void)parent;
3005 
3006  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3007  TLX_BTREE_ASSERT(parent->level == 1);
3008 
3009  TLX_BTREE_ASSERT(left->slotuse + right->slotuse < leaf_slotmax);
3010 
3011  std::copy(right->slotdata, right->slotdata + right->slotuse,
3012  left->slotdata + left->slotuse);
3013 
3014  left->slotuse += right->slotuse;
3015 
3016  left->next_leaf = right->next_leaf;
3017  if (left->next_leaf)
3018  left->next_leaf->prev_leaf = left;
3019  else
3020  tail_leaf_ = left;
3021 
3022  right->slotuse = 0;
3023 
3024  return btree_fixmerge;
3025  }
3026 
3027  //! Merge two inner nodes. The function moves all key/childid pairs from
3028  //! right to left and sets right's slotuse to zero. The right slot is then
3029  //! removed by the calling parent node.
3030  static result_t merge_inner(inner_node* left, inner_node* right,
3031  inner_node* parent, unsigned int parentslot) {
3032  TLX_BTREE_PRINT("Merge inner nodes " << left << " and " << right << " with common parent " << parent << ".");
3033 
3034  TLX_BTREE_ASSERT(left->level == right->level);
3035  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3036 
3037  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3038 
3039  TLX_BTREE_ASSERT(left->slotuse + right->slotuse < inner_slotmax);
3040 
3041  if (self_verify)
3042  {
3043  // find the left node's slot in the parent's children
3044  unsigned int leftslot = 0;
3045  while (leftslot <= parent->slotuse && parent->childid[leftslot] != left)
3046  ++leftslot;
3047 
3048  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3049  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3050  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3051 
3052  TLX_BTREE_ASSERT(parentslot == leftslot);
3053  }
3054 
3055  // retrieve the decision key from parent
3056  left->slotkey[left->slotuse] = parent->slotkey[parentslot];
3057  left->slotuse++;
3058 
3059  // copy over keys and children from right
3060  std::copy(right->slotkey, right->slotkey + right->slotuse,
3061  left->slotkey + left->slotuse);
3062  std::copy(right->childid, right->childid + right->slotuse + 1,
3063  left->childid + left->slotuse);
3064 
3065  left->slotuse += right->slotuse;
3066  right->slotuse = 0;
3067 
3068  return btree_fixmerge;
3069  }
3070 
3071  //! Balance two leaf nodes. The function moves key/data pairs from right to
3072  //! left so that both nodes are equally filled. The parent node is updated
3073  //! if possible.
3074  static result_t shift_left_leaf(leaf_node* left, leaf_node* right,
3075  inner_node* parent, unsigned int parentslot) {
3076  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3077  TLX_BTREE_ASSERT(parent->level == 1);
3078 
3079  TLX_BTREE_ASSERT(left->next_leaf == right);
3080  TLX_BTREE_ASSERT(left == right->prev_leaf);
3081 
3082  TLX_BTREE_ASSERT(left->slotuse < right->slotuse);
3083  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3084 
3085  unsigned int shiftnum = (right->slotuse - left->slotuse) >> 1;
3086 
3087  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum << " entries to left " << left << " from right " << right << " with common parent " << parent << ".");
3088 
3089  TLX_BTREE_ASSERT(left->slotuse + shiftnum < leaf_slotmax);
3090 
3091  // copy the first items from the right node to the last slot in the left node.
3092 
3093  std::copy(right->slotdata, right->slotdata + shiftnum,
3094  left->slotdata + left->slotuse);
3095 
3096  left->slotuse += shiftnum;
3097 
3098  // shift all slots in the right node to the left
3099 
3100  std::copy(right->slotdata + shiftnum, right->slotdata + right->slotuse,
3101  right->slotdata);
3102 
3103  right->slotuse -= shiftnum;
3104 
3105  // fixup parent
3106  if (parentslot < parent->slotuse) {
3107  parent->slotkey[parentslot] = left->key(left->slotuse - 1);
3108  return btree_ok;
3109  }
3110  else { // the update is further up the tree
3111  return result_t(btree_update_lastkey, left->key(left->slotuse - 1));
3112  }
3113  }
3114 
3115  //! Balance two inner nodes. The function moves key/data pairs from right to
3116  //! left so that both nodes are equally filled. The parent node is updated
3117  //! if possible.
3118  static void shift_left_inner(inner_node* left, inner_node* right,
3119  inner_node* parent, unsigned int parentslot) {
3120  TLX_BTREE_ASSERT(left->level == right->level);
3121  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3122 
3123  TLX_BTREE_ASSERT(left->slotuse < right->slotuse);
3124  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3125 
3126  unsigned int shiftnum = (right->slotuse - left->slotuse) >> 1;
3127 
3128  TLX_BTREE_PRINT("Shifting (inner) " << shiftnum << " entries to left " << left << " from right " << right << " with common parent " << parent << ".");
3129 
3130  TLX_BTREE_ASSERT(left->slotuse + shiftnum < inner_slotmax);
3131 
3132  if (self_verify)
3133  {
3134  // find the left node's slot in the parent's children and compare to
3135  // parentslot
3136 
3137  unsigned int leftslot = 0;
3138  while (leftslot <= parent->slotuse && parent->childid[leftslot] != left)
3139  ++leftslot;
3140 
3141  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3142  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3143  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3144 
3145  TLX_BTREE_ASSERT(leftslot == parentslot);
3146  }
3147 
3148  // copy the parent's decision slotkey and childid to the first new key
3149  // on the left
3150  left->slotkey[left->slotuse] = parent->slotkey[parentslot];
3151  left->slotuse++;
3152 
3153  // copy the other items from the right node to the last slots in the
3154  // left node.
3155  std::copy(right->slotkey, right->slotkey + shiftnum - 1,
3156  left->slotkey + left->slotuse);
3157  std::copy(right->childid, right->childid + shiftnum,
3158  left->childid + left->slotuse);
3159 
3160  left->slotuse += shiftnum - 1;
3161 
3162  // fixup parent
3163  parent->slotkey[parentslot] = right->slotkey[shiftnum - 1];
3164 
3165  // shift all slots in the right node
3166  std::copy(right->slotkey + shiftnum, right->slotkey + right->slotuse,
3167  right->slotkey);
3168  std::copy(right->childid + shiftnum, right->childid + right->slotuse + 1,
3169  right->childid);
3170 
3171  right->slotuse -= shiftnum;
3172  }
3173 
3174  //! Balance two leaf nodes. The function moves key/data pairs from left to
3175  //! right so that both nodes are equally filled. The parent node is updated
3176  //! if possible.
3177  static void shift_right_leaf(leaf_node* left, leaf_node* right,
3178  inner_node* parent, unsigned int parentslot) {
3179  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3180  TLX_BTREE_ASSERT(parent->level == 1);
3181 
3182  TLX_BTREE_ASSERT(left->next_leaf == right);
3183  TLX_BTREE_ASSERT(left == right->prev_leaf);
3184  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3185 
3186  TLX_BTREE_ASSERT(left->slotuse > right->slotuse);
3187 
3188  unsigned int shiftnum = (left->slotuse - right->slotuse) >> 1;
3189 
3190  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum << " entries to right " << right << " from left " << left << " with common parent " << parent << ".");
3191 
3192  if (self_verify)
3193  {
3194  // find the left node's slot in the parent's children
3195  unsigned int leftslot = 0;
3196  while (leftslot <= parent->slotuse && parent->childid[leftslot] != left)
3197  ++leftslot;
3198 
3199  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3200  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3201  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3202 
3203  TLX_BTREE_ASSERT(leftslot == parentslot);
3204  }
3205 
3206  // shift all slots in the right node
3207 
3208  TLX_BTREE_ASSERT(right->slotuse + shiftnum < leaf_slotmax);
3209 
3210  std::copy_backward(right->slotdata, right->slotdata + right->slotuse,
3211  right->slotdata + right->slotuse + shiftnum);
3212 
3213  right->slotuse += shiftnum;
3214 
3215  // copy the last items from the left node to the first slot in the right
3216  // node.
3217  std::copy(left->slotdata + left->slotuse - shiftnum, left->slotdata + left->slotuse,
3218  right->slotdata);
3219 
3220  left->slotuse -= shiftnum;
3221 
3222  parent->slotkey[parentslot] = left->key(left->slotuse - 1);
3223  }
3224 
3225  //! Balance two inner nodes. The function moves key/data pairs from left to
3226  //! right so that both nodes are equally filled. The parent node is updated
3227  //! if possible.
3228  static void shift_right_inner(inner_node* left, inner_node* right, inner_node* parent, unsigned int parentslot) {
3229  TLX_BTREE_ASSERT(left->level == right->level);
3230  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3231 
3232  TLX_BTREE_ASSERT(left->slotuse > right->slotuse);
3233  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3234 
3235  unsigned int shiftnum = (left->slotuse - right->slotuse) >> 1;
3236 
3237  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum << " entries to right " << right << " from left " << left << " with common parent " << parent << ".");
3238 
3239  if (self_verify)
3240  {
3241  // find the left node's slot in the parent's children
3242  unsigned int leftslot = 0;
3243  while (leftslot <= parent->slotuse && parent->childid[leftslot] != left)
3244  ++leftslot;
3245 
3246  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3247  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3248  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3249 
3250  TLX_BTREE_ASSERT(leftslot == parentslot);
3251  }
3252 
3253  // shift all slots in the right node
3254 
3255  TLX_BTREE_ASSERT(right->slotuse + shiftnum < inner_slotmax);
3256 
3257  std::copy_backward(right->slotkey, right->slotkey + right->slotuse,
3258  right->slotkey + right->slotuse + shiftnum);
3259  std::copy_backward(right->childid, right->childid + right->slotuse + 1,
3260  right->childid + right->slotuse + 1 + shiftnum);
3261 
3262  right->slotuse += shiftnum;
3263 
3264  // copy the parent's decision slotkey and childid to the last new key on
3265  // the right
3266  right->slotkey[shiftnum - 1] = parent->slotkey[parentslot];
3267 
3268  // copy the remaining last items from the left node to the first slot in
3269  // the right node.
3270  std::copy(left->slotkey + left->slotuse - shiftnum + 1, left->slotkey + left->slotuse,
3271  right->slotkey);
3272  std::copy(left->childid + left->slotuse - shiftnum + 1, left->childid + left->slotuse + 1,
3273  right->childid);
3274 
3275  // copy the first to-be-removed key from the left node to the parent's
3276  // decision slot
3277  parent->slotkey[parentslot] = left->slotkey[left->slotuse - shiftnum];
3278 
3279  left->slotuse -= shiftnum;
3280  }
3281 
3282  //! \}
3283 
3284 #ifdef TLX_BTREE_DEBUG
3285 
3286 public:
3287  //! \name Debug Printing
3288  //! \{
3289 
3290  //! Print out the B+ tree structure with keys onto the given ostream. This
3291  //! function requires that the header is compiled with TLX_BTREE_DEBUG and
3292  //! that key_type is printable via std::ostream.
3293  void print(std::ostream& os) const {
3294  if (root_) {
3295  print_node(os, root_, 0, true);
3296  }
3297  }
3298 
3299  //! Print out only the leaves via the double linked list.
3300  void print_leaves(std::ostream& os) const {
3301  os << "leaves:" << std::endl;
3302 
3303  const leaf_node* n = head_leaf_;
3304 
3305  while (n)
3306  {
3307  os << " " << n << std::endl;
3308 
3309  n = n->next_leaf;
3310  }
3311  }
3312 
3313 private:
3314  //! Recursively descend down the tree and print out nodes.
3315  static void print_node(std::ostream& os, const node* node,
3316  unsigned int depth = 0, bool recursive = false) {
3317  for (unsigned int i = 0; i < depth; i++) os << " ";
3318 
3319  os << "node " << node << " level " << node->level << " slotuse " << node->slotuse << std::endl;
3320 
3321  if (node->is_leafnode())
3322  {
3323  const leaf_node* leafnode = static_cast<const leaf_node*>(node);
3324 
3325  for (unsigned int i = 0; i < depth; i++) os << " ";
3326  os << " leaf prev " << leafnode->prev_leaf << " next " << leafnode->next_leaf << std::endl;
3327 
3328  for (unsigned int i = 0; i < depth; i++) os << " ";
3329 
3330  for (unsigned int slot = 0; slot < leafnode->slotuse; ++slot)
3331  {
3332  os << leafnode->key(slot) << " "; // << "(data: " << leafnode->slotdata[slot] << ") ";
3333  }
3334  os << std::endl;
3335  }
3336  else
3337  {
3338  const inner_node* innernode = static_cast<const inner_node*>(node);
3339 
3340  for (unsigned int i = 0; i < depth; i++) os << " ";
3341 
3342  for (unsigned short slot = 0; slot < innernode->slotuse; ++slot)
3343  {
3344  os << "(" << innernode->childid[slot] << ") " << innernode->slotkey[slot] << " ";
3345  }
3346  os << "(" << innernode->childid[innernode->slotuse] << ")" << std::endl;
3347 
3348  if (recursive)
3349  {
3350  for (unsigned short slot = 0; slot < innernode->slotuse + 1; ++slot)
3351  {
3352  print_node(os, innernode->childid[slot], depth + 1, recursive);
3353  }
3354  }
3355  }
3356  }
3357 
3358  //! \}
3359 #endif
3360 
3361 public:
3362  //! \name Verification of B+ Tree Invariants
3363  //! \{
3364 
3365  //! Run a thorough verification of all B+ tree invariants. The program
3366  //! aborts via die_unless() if something is wrong.
3367  void verify() const {
3368  key_type minkey, maxkey;
3369  tree_stats vstats;
3370 
3371  if (root_)
3372  {
3373  verify_node(root_, &minkey, &maxkey, vstats);
3374 
3375  die_unless(vstats.size == stats_.size);
3376  die_unless(vstats.leaves == stats_.leaves);
3377  die_unless(vstats.inner_nodes == stats_.inner_nodes);
3378 
3379  verify_leaflinks();
3380  }
3381  }
3382 
3383 private:
3384  //! Recursively descend down the tree and verify each node
3385  void verify_node(const node* n, key_type* minkey, key_type* maxkey,
3386  tree_stats& vstats) const {
3387  TLX_BTREE_PRINT("verifynode " << n);
3388 
3389  if (n->is_leafnode())
3390  {
3391  const leaf_node* leaf = static_cast<const leaf_node*>(n);
3392 
3393  die_unless(leaf == root_ || !leaf->is_underflow());
3394  die_unless(leaf->slotuse > 0);
3395 
3396  for (unsigned short slot = 0; slot < leaf->slotuse - 1; ++slot)
3397  {
3398  die_unless(key_lessequal(leaf->key(slot), leaf->key(slot + 1)));
3399  }
3400 
3401  *minkey = leaf->key(0);
3402  *maxkey = leaf->key(leaf->slotuse - 1);
3403 
3404  vstats.leaves++;
3405  vstats.size += leaf->slotuse;
3406  }
3407  else // !n->is_leafnode()
3408  {
3409  const inner_node* inner = static_cast<const inner_node*>(n);
3410  vstats.inner_nodes++;
3411 
3412  die_unless(inner == root_ || !inner->is_underflow());
3413  die_unless(inner->slotuse > 0);
3414 
3415  for (unsigned short slot = 0; slot < inner->slotuse - 1; ++slot)
3416  {
3417  die_unless(key_lessequal(inner->key(slot), inner->key(slot + 1)));
3418  }
3419 
3420  for (unsigned short slot = 0; slot <= inner->slotuse; ++slot)
3421  {
3422  const node* subnode = inner->childid[slot];
3423  key_type subminkey = key_type();
3424  key_type submaxkey = key_type();
3425 
3426  die_unless(subnode->level + 1 == inner->level);
3427  verify_node(subnode, &subminkey, &submaxkey, vstats);
3428 
3429  TLX_BTREE_PRINT("verify subnode " << subnode << ": " << subminkey << " - " << submaxkey);
3430 
3431  if (slot == 0)
3432  *minkey = subminkey;
3433  else
3434  die_unless(key_greaterequal(subminkey, inner->key(slot - 1)));
3435 
3436  if (slot == inner->slotuse)
3437  *maxkey = submaxkey;
3438  else
3439  die_unless(key_equal(inner->key(slot), submaxkey));
3440 
3441  if (inner->level == 1 && slot < inner->slotuse)
3442  {
3443  // children are leaves and must be linked together in the
3444  // correct order
3445  const leaf_node* leafa = static_cast<const leaf_node*>(inner->childid[slot]);
3446  const leaf_node* leafb = static_cast<const leaf_node*>(inner->childid[slot + 1]);
3447 
3448  die_unless(leafa->next_leaf == leafb);
3449  die_unless(leafa == leafb->prev_leaf);
3450  }
3451  if (inner->level == 2 && slot < inner->slotuse)
3452  {
3453  // verify leaf links between the adjacent inner nodes
3454  const inner_node* parenta = static_cast<const inner_node*>(inner->childid[slot]);
3455  const inner_node* parentb = static_cast<const inner_node*>(inner->childid[slot + 1]);
3456 
3457  const leaf_node* leafa = static_cast<const leaf_node*>(parenta->childid[parenta->slotuse]);
3458  const leaf_node* leafb = static_cast<const leaf_node*>(parentb->childid[0]);
3459 
3460  die_unless(leafa->next_leaf == leafb);
3461  die_unless(leafa == leafb->prev_leaf);
3462  }
3463  }
3464  }
3465  }
3466 
3467  //! Verify the double linked list of leaves.
3468  void verify_leaflinks() const {
3469  const leaf_node* n = head_leaf_;
3470 
3471  die_unless(n->level == 0);
3472  die_unless(!n || n->prev_leaf == nullptr);
3473 
3474  unsigned int testcount = 0;
3475 
3476  while (n)
3477  {
3478  die_unless(n->level == 0);
3479  die_unless(n->slotuse > 0);
3480 
3481  for (unsigned short slot = 0; slot < n->slotuse - 1; ++slot)
3482  {
3483  die_unless(key_lessequal(n->key(slot), n->key(slot + 1)));
3484  }
3485 
3486  testcount += n->slotuse;
3487 
3488  if (n->next_leaf)
3489  {
3490  die_unless(key_lessequal(n->key(n->slotuse - 1), n->next_leaf->key(0)));
3491 
3492  die_unless(n == n->next_leaf->prev_leaf);
3493  }
3494  else
3495  {
3496  die_unless(tail_leaf_ == n);
3497  }
3498 
3499  n = n->next_leaf;
3500  }
3501 
3502  die_unless(testcount == size());
3503  }
3504 
3505  //! \}
3506 };
3507 
3508 //! \}
3509 //! \}
3510 
3511 } // namespace tlx
3512 
3513 #endif // !TLX_CONTAINER_BTREE_HEADER
3514 
3515 /******************************************************************************/
bool is_few() const
True if few used entries, less than half full.
Definition: btree.hpp:261
iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:424
iterator(const reverse_iterator &it)
Copy-constructor from a reverse iterator.
Definition: btree.hpp:404
bool operator<=(const btree &other) const
Less-equal relation. Based on operator<.
Definition: btree.hpp:1739
node * childid[inner_slotmax+1]
Pointers to children.
Definition: btree.hpp:243
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:409
bool key_less(const key_type &a, const key_type b) const
True if a < b ? "constructed" from key_less_()
Definition: btree.hpp:1200
key_compare key_comp
Key comparison function from the template parameter.
Definition: btree.hpp:1164
static const bool debug
Definition: btree.hpp:203
Deletion not successful because key was not found.
Definition: btree.hpp:2278
btree::leaf_node * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:710
unsigned short curr_slot
One slot past the current key/data slot referenced.
Definition: btree.hpp:886
bool key_equal(const key_type &a, const key_type &b) const
Definition: btree.hpp:1221
const_iterator begin() const
Definition: btree.hpp:1353
static const bool allow_duplicates
Definition: btree.hpp:150
void verify() const
Definition: btree.hpp:3367
const_reverse_iterator & operator++()
Prefix++ advance the iterator to the previous slot.
Definition: btree.hpp:945
const value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:523
iterator begin()
Definition: btree.hpp:1341
static const int inner_slots
Definition: btree.hpp:93
A small struct containing basic statistics about the B+ tree.
Definition: btree.hpp:1037
std::pair< iterator, iterator > equal_range(const key_type &key)
Searches the B+ tree and returns both lower_bound() and upper_bound().
Definition: btree.hpp:1695
Deletion successful and no fix-ups necessary.
Definition: btree.hpp:2275
bool is_full() const
True if the node's slots are full.
Definition: btree.hpp:298
const_reverse_iterator(const typename btree::leaf_node *l, unsigned short s)
Initializing-Constructor of a const reverse iterator.
Definition: btree.hpp:906
static const bool self_verify
Definition: btree.hpp:198
KeyOfValue_ key_of_value
Third template: key extractor class to pull key_type from value_type.
Definition: btree.hpp:139
const_iterator end() const
Definition: btree.hpp:1359
const_iterator(const typename btree::leaf_node *l, unsigned short s)
Initializing-Constructor of a const iterator.
Definition: btree.hpp:561
#define die_unless(X)
Definition: die.hpp:64
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:351
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:701
double avgfill_leaves() const
Return the average fill of leaves.
Definition: btree.hpp:1065
allocator_type allocator_
Memory allocator.
Definition: btree.hpp:1093
struct node * copy_recursive(const node *n)
Recursively copy nodes from another B+ tree object.
Definition: btree.hpp:1796
const_iterator upper_bound(const key_type &key) const
Definition: btree.hpp:1676
allocator_type get_allocator() const
Return the base node allocator provided during construction.
Definition: btree.hpp:1232
void erase(iterator iter)
Erase the key/data pair referenced by the iterator.
Definition: btree.hpp:2369
bool operator!=(const const_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:673
size_type size() const
Return the number of key/data pairs in the B+ tree.
Definition: btree.hpp:1494
value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:692
static void shift_right_inner(inner_node *left, inner_node *right, inner_node *parent, unsigned int parentslot)
Definition: btree.hpp:3228
bool exists(const key_type &key) const
Definition: btree.hpp:1522
leaf_node * prev_leaf
Double linked list pointers to traverse the leaves.
Definition: btree.hpp:278
const_iterator find(const key_type &key) const
Definition: btree.hpp:1563
btree(const btree &other)
Definition: btree.hpp:1779
void initialize()
Set variables to initial values.
Definition: btree.hpp:287
result_flags_t flags
Merged result flags.
Definition: btree.hpp:2293
size_t size_type
Size type used to count keys.
Definition: btree.hpp:171
value_compare value_comp() const
Definition: btree.hpp:1189
inner_node * allocate_inner(unsigned short level)
Allocate and initialize an inner node.
Definition: btree.hpp:1261
size_type leaves
Number of leaves in the B+ tree.
Definition: btree.hpp:1042
static const unsigned short leaf_slots
Base B+ tree parameter: The number of key/data slots in each leaf.
Definition: btree.hpp:1048
void free_node(node *n)
Definition: btree.hpp:1270
bool key_greaterequal(const key_type &a, const key_type b) const
True if a >= b ? constructed from key_less()
Definition: btree.hpp:1215
void swap(btree &from)
Fast swapping of two identical B+ tree objects.
Definition: btree.hpp:1144
bool erase_one(const key_type &key)
Definition: btree.hpp:2334
btree(const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1103
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:933
value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:345
bool key_greater(const key_type &a, const key_type &b) const
True if a > b ? constructed from key_less()
Definition: btree.hpp:1210
Function class to compare value_type objects. Required by the STL.
Definition: btree.hpp:1160
bool empty() const
Returns true if there is at least one key/data pair in the B+ tree.
Definition: btree.hpp:1499
std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Searches the B+ tree and returns both lower_bound() and upper_bound().
Definition: btree.hpp:1702
const btree::leaf_node * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:538
bool operator()(const value_type &x, const value_type &y) const
Function call "less"-operator resulting in true if x < y.
Definition: btree.hpp:1177
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:757
const_reverse_iterator rend() const
Definition: btree.hpp:1383
btree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:514
const value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:865
void clear()
Frees all key/data pairs and all nodes of the tree.
Definition: btree.hpp:1294
#define TLX_BTREE_MAX(a, b)
The maximum of a and b. Used in some compile-time formulas.
Definition: btree.hpp:60
value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:348
bool key_lessequal(const key_type &a, const key_type b) const
True if a <= b ? constructed from key_less()
Definition: btree.hpp:1205
void bulk_load(Iterator ibegin, Iterator iend)
Definition: btree.hpp:2138
static ssize_t get(const CounterType &a)
const value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:520
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:871
std::pair< iterator, bool > insert_descend(node *n, const key_type &key, const value_type &value, key_type *splitkey, node **splitnode)
Insert an item into the B+ tree.
Definition: btree.hpp:1925
const_reverse_iterator(const reverse_iterator &it)
Copy-constructor from a mutable reverse iterator.
Definition: btree.hpp:922
reverse_iterator rbegin()
Definition: btree.hpp:1365
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:939
btree< key_type, value_type, key_of_value, key_compare, traits, allow_duplicates, allocator_type > btree_self
Typedef of our own type.
Definition: btree.hpp:168
const key_type & key(size_t s) const
Return key in slot s.
Definition: btree.hpp:293
bool operator==(const const_iterator &x) const
Equality of iterators.
Definition: btree.hpp:668
btree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:686
bool is_leafnode() const
True if this is a leaf node.
Definition: btree.hpp:228
leaf_node * allocate_leaf()
Allocate and initialize a leaf node.
Definition: btree.hpp:1253
const_iterator lower_bound(const key_type &key) const
Definition: btree.hpp:1636
size_type inner_nodes
Number of inner nodes in the B+ tree.
Definition: btree.hpp:1045
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:419
reverse_iterator rend()
Definition: btree.hpp:1371
bool is_underflow() const
True if node has too few entries.
Definition: btree.hpp:308
bool operator!=(const const_reverse_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:1022
size_type size
Number of items in the B+ tree.
Definition: btree.hpp:1039
const key_type & key(size_t s) const
Return key in slot s.
Definition: btree.hpp:251
value_type slotdata[leaf_slotmax]
Array of (key, data) pairs.
Definition: btree.hpp:284
key_compare key_less_
Definition: btree.hpp:1090
void split_leaf_node(leaf_node *leaf, key_type *out_newkey, node **out_newleaf)
Definition: btree.hpp:2062
reverse_iterator(typename btree::leaf_node *l, unsigned short s)
Initializing-Constructor of a mutable reverse iterator.
Definition: btree.hpp:741
reverse_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:746
btree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:862
reverse_iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:769
const_iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:632
void set_slot(unsigned short slot, const value_type &value)
Definition: btree.hpp:314
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
#define TLX_BTREE_PRINT(x)
Print out debug information to std::cout if TLX_BTREE_DEBUG is defined.
Definition: btree.hpp:52
size_type count(const key_type &key) const
Definition: btree.hpp:1584
void initialize(const unsigned short l)
Delayed initialisation of constructed node.
Definition: btree.hpp:222
bool operator!=(const btree &other) const
Inequality relation. Based on operator==.
Definition: btree.hpp:1722
list x
Definition: gen_data.py:39
Key_ key_type
Definition: btree.hpp:132
static const unsigned short leaf_slotmax
Base B+ tree parameter: The number of key/data slots in each leaf.
Definition: btree.hpp:180
int value
Definition: gen_data.py:41
const_reverse_iterator(const const_iterator &it)
Copy-constructor from a const iterator.
Definition: btree.hpp:917
btree(InputIterator first, InputIterator last, const key_compare &kcf, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1131
node * root_
Pointer to the B+ tree's root node, either leaf or inner node.
Definition: btree.hpp:1077
void clear_recursive(node *n)
Recursively free up nodes.
Definition: btree.hpp:1311
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:927
int find_upper(const node_type *n, const key_type &key) const
Definition: btree.hpp:1445
unsigned short level
Level in the b-tree, if level == 0 -> leaf node.
Definition: btree.hpp:215
static void shift_left_inner(inner_node *left, inner_node *right, inner_node *parent, unsigned int parentslot)
Definition: btree.hpp:3118
const_reverse_iterator & operator--()
Prefix– backstep the iterator to the next slot.
Definition: btree.hpp:981
btree(InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1120
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:751
leaf_node * tail_leaf_
Pointer to last leaf in the double linked leaf chain.
Definition: btree.hpp:1083
iterator end()
Definition: btree.hpp:1347
static const size_t binsearch_threshold
Definition: btree.hpp:100
static const bool debug
Definition: btree.hpp:84
static void shift_right_leaf(leaf_node *left, leaf_node *right, inner_node *parent, unsigned int parentslot)
Definition: btree.hpp:3177
bool operator!=(const reverse_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:846
static constexpr bool debug
static const unsigned short inner_slotmin
Definition: btree.hpp:194
bool operator==(const const_reverse_iterator &x) const
Equality of iterators.
Definition: btree.hpp:1017
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:529
const struct tree_stats & get_stats() const
Return a const reference to the current statistics.
Definition: btree.hpp:1510
void insert(InputIterator first, InputIterator last)
Definition: btree.hpp:1860
unsigned short slotuse
Definition: btree.hpp:219
btree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:689
iterator()
Default-Constructor of a mutable iterator.
Definition: btree.hpp:394
reverse_iterator()
Default-Constructor of a reverse iterator.
Definition: btree.hpp:736
btree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:342
void verify_leaflinks() const
Verify the double linked list of leaves.
Definition: btree.hpp:3468
void initialize(const unsigned short l)
Set variables to initial values.
Definition: btree.hpp:246
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:414
Alloc_ allocator_type
Seventh template parameter: STL allocator for tree nodes.
Definition: btree.hpp:153
result_t & operator|=(const result_t &other)
Merge two results OR-ing the result flags and overwriting lastkeys.
Definition: btree.hpp:2315
bool operator>(const btree &other) const
Greater relation. Based on operator<.
Definition: btree.hpp:1734
btree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:517
result_t(result_flags_t f=btree_ok)
Definition: btree.hpp:2300
unsigned short curr_slot
Current key/data slot referenced.
Definition: btree.hpp:541
leaf_node * head_leaf_
Pointer to first leaf in the double linked leaf chain.
Definition: btree.hpp:1080
iterator upper_bound(const key_type &key)
Definition: btree.hpp:1656
bool operator==(const iterator &x) const
Equality of iterators.
Definition: btree.hpp:496
btree(const key_compare &kcf, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1110
const_iterator()
Default-Constructor of a const iterator.
Definition: btree.hpp:556
static const unsigned short leaf_slotmin
Definition: btree.hpp:189
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:581
void split_inner_node(inner_node *inner, key_type *out_newkey, node **out_newinner, unsigned int addslot)
Definition: btree.hpp:2097
leaf_node::alloc_type leaf_node_allocator()
Return an allocator for leaf_node objects.
Definition: btree.hpp:1243
bool operator==(const btree &other) const
Definition: btree.hpp:1716
Compare_ key_compare
Fourth template parameter: key_type comparison function object.
Definition: btree.hpp:142
btree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:339
static const unsigned short inner_slotmax
Definition: btree.hpp:184
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:586
btree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:859
bool operator<(const btree &other) const
Definition: btree.hpp:1728
~btree()
Frees up all used B+ tree memory pages.
Definition: btree.hpp:1139
value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:695
const_reverse_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:912
#define TLX_BTREE_ASSERT(x)
Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
Definition: btree.hpp:55
const_iterator(const reverse_iterator &it)
Copy-constructor from a mutable reverse iterator.
Definition: btree.hpp:571
iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:460
iterator lower_bound(const key_type &key)
Definition: btree.hpp:1616
bool operator==(const reverse_iterator &x) const
Equality of iterators.
Definition: btree.hpp:841
const_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:566
leaf_node * next_leaf
Double linked list pointers to traverse the leaves.
Definition: btree.hpp:281
bool is_few() const
True if few used entries, less than half full.
Definition: btree.hpp:303
bool is_underflow() const
True if node has too few entries.
Definition: btree.hpp:266
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:698
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:526
std::pair< iterator, bool > insert(const value_type &x)
Definition: btree.hpp:1846
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:874
Alloc_::template rebind< leaf_node >::other alloc_type
Define an related allocator for the leaf_node structs.
Definition: btree.hpp:275
Generates default traits for a B+ tree used as a set or map.
Definition: btree.hpp:74
value_compare(key_compare kc)
Constructor called from btree::value_comp()
Definition: btree.hpp:1167
result_t erase_one_descend(const key_type &key, node *curr, node *left, node *right, inner_node *left_parent, inner_node *right_parent, inner_node *parent, unsigned int parentslot)
Erase one (the first) key/data pair in the B+ tree matching key.
Definition: btree.hpp:2411
std::pair< iterator, bool > insert_start(const key_type &key, const value_type &value)
Definition: btree.hpp:1878
iterator insert(iterator, const value_type &x)
Definition: btree.hpp:1852
Basic class implementing a B+ tree data structure in memory.
Definition: btree.hpp:124
size_type erase(const key_type &key)
Definition: btree.hpp:2356
key_compare key_comp() const
Constant access to the key comparison object sorting the B+ tree.
Definition: btree.hpp:1183
const_iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:596
static const int leaf_slots
Definition: btree.hpp:88
const value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:868
key_type slotkey[inner_slotmax]
Keys of children or data pointers.
Definition: btree.hpp:240
Value_ value_type
Definition: btree.hpp:136
int find_lower(const node_type *n, const key_type &key) const
Definition: btree.hpp:1398
static const bool self_verify
Definition: btree.hpp:78
iterator(typename btree::leaf_node *l, unsigned short s)
Initializing-Constructor of a mutable iterator.
Definition: btree.hpp:399
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:591
static const unsigned short inner_slots
Base B+ tree parameter: The number of key slots in each inner node.
Definition: btree.hpp:1051
size_type nodes() const
Return the total number of nodes.
Definition: btree.hpp:1060
unsigned short curr_slot
Current key/data slot referenced.
Definition: btree.hpp:366
Alloc_::template rebind< inner_node >::other alloc_type
Define an related allocator for the inner_node structs.
Definition: btree.hpp:237
tree_stats()
Zero initialized.
Definition: btree.hpp:1054
bool operator>=(const btree &other) const
Greater-equal relation. Based on operator<.
Definition: btree.hpp:1744
unsigned short curr_slot
One slot past the current key/data slot referenced.
Definition: btree.hpp:713
btree::leaf_node * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:363
result_t merge_leaves(leaf_node *left, leaf_node *right, inner_node *parent)
Definition: btree.hpp:3001
static result_t merge_inner(inner_node *left, inner_node *right, inner_node *parent, unsigned int parentslot)
Definition: btree.hpp:3030
#define TLX_BTREE_FRIENDS
Definition: btree.hpp:66
void verify_node(const node *n, key_type *minkey, key_type *maxkey, tree_stats &vstats) const
Recursively descend down the tree and verify each node.
Definition: btree.hpp:3385
key_type lastkey
The key to be updated at the parent's slot.
Definition: btree.hpp:2296
tree_stats stats_
Other small statistics about the B+ tree.
Definition: btree.hpp:1086
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:354
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:763
result_t erase_iter_descend(const iterator &iter, node *curr, node *left, node *right, inner_node *left_parent, inner_node *right_parent, inner_node *parent, unsigned int parentslot)
Erase one key/data pair referenced by an iterator in the B+ tree.
Definition: btree.hpp:2700
const_reverse_iterator()
Default-Constructor of a const reverse iterator.
Definition: btree.hpp:901
const_reverse_iterator rbegin() const
Definition: btree.hpp:1377
const btree::leaf_node * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:883
static result_t shift_left_leaf(leaf_node *left, leaf_node *right, inner_node *parent, unsigned int parentslot)
Definition: btree.hpp:3074
const_iterator(const const_reverse_iterator &it)
Copy-constructor from a const reverse iterator.
Definition: btree.hpp:576
result_t(result_flags_t f, const key_type &k)
Constructor with a lastkey value.
Definition: btree.hpp:2305
iterator find(const key_type &key)
Definition: btree.hpp:1542
inner_node::alloc_type inner_node_allocator()
Return an allocator for inner_node objects.
Definition: btree.hpp:1248
btree & operator=(const btree &other)
Assignment operator. All the key/data pairs are copied.
Definition: btree.hpp:1755
bool operator!=(const iterator &x) const
Inequality of iterators.
Definition: btree.hpp:501
Traits_ traits
Definition: btree.hpp:146
bool is_full() const
True if the node's slots are full.
Definition: btree.hpp:256
bool has(result_flags_t f) const
Test if this result object has a given flag set.
Definition: btree.hpp:2310
reverse_iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:805
size_type max_size() const
Definition: btree.hpp:1505