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/core.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 InnerNode or LeafNode.
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 InnerNode : public node {
236  //! Define an related allocator for the InnerNode structs.
237  typedef typename Alloc_::template rebind<InnerNode>::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 LeafNode : public node {
274  //! Define an related allocator for the LeafNode structs.
275  typedef typename Alloc_::template rebind<LeafNode>::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::LeafNode* 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::LeafNode* 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::LeafNode* 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::LeafNode* 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::LeafNode* 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::LeafNode* 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 = 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 = 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  LeafNode* head_leaf_;
1081 
1082  //! Pointer to last leaf in the double linked leaf chain.
1083  LeafNode* 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 LeafNode objects.
1244  return typename LeafNode::alloc_type(allocator_);
1245  }
1246 
1247  //! Return an allocator for InnerNode objects.
1249  return typename InnerNode::alloc_type(allocator_);
1250  }
1251 
1252  //! Allocate and initialize a leaf node
1253  LeafNode * allocate_leaf() {
1254  LeafNode* n = new (leaf_node_allocator().allocate(1)) LeafNode();
1255  n->initialize();
1256  stats_.leaves++;
1257  return n;
1258  }
1259 
1260  //! Allocate and initialize an inner node
1261  InnerNode * allocate_inner(unsigned short level) {
1262  InnerNode* n = new (inner_node_allocator().allocate(1)) InnerNode();
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  LeafNode* ln = static_cast<LeafNode*>(n);
1274  a.destroy(ln);
1275  a.deallocate(ln, 1);
1276  stats_.leaves--;
1277  }
1278  else {
1279  InnerNode* in = static_cast<InnerNode*>(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  LeafNode* leafnode = static_cast<LeafNode*>(n);
1315 
1316  for (unsigned int slot = 0; slot < leafnode->slotuse; ++slot)
1317  {
1318  // data objects are deleted by LeafNode's destructor
1319  }
1320  }
1321  else
1322  {
1323  InnerNode* innernode = static_cast<InnerNode*>(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 LeafNode and InnerNode.
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  //! LeafNode and InnerNode.
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 InnerNode* inner = static_cast<const InnerNode*>(n);
1529  int slot = find_lower(inner, key);
1530 
1531  n = inner->childid[slot];
1532  }
1533 
1534  const LeafNode* leaf = static_cast<const LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1549  int slot = find_lower(inner, key);
1550 
1551  n = inner->childid[slot];
1552  }
1553 
1554  LeafNode* leaf = static_cast<LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1570  int slot = find_lower(inner, key);
1571 
1572  n = inner->childid[slot];
1573  }
1574 
1575  const LeafNode* leaf = static_cast<const LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1591  int slot = find_lower(inner, key);
1592 
1593  n = inner->childid[slot];
1594  }
1595 
1596  const LeafNode* leaf = static_cast<const LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1623  int slot = find_lower(inner, key);
1624 
1625  n = inner->childid[slot];
1626  }
1627 
1628  LeafNode* leaf = static_cast<LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1643  int slot = find_lower(inner, key);
1644 
1645  n = inner->childid[slot];
1646  }
1647 
1648  const LeafNode* leaf = static_cast<const LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1663  int slot = find_upper(inner, key);
1664 
1665  n = inner->childid[slot];
1666  }
1667 
1668  LeafNode* leaf = static_cast<LeafNode*>(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 InnerNode* inner = static_cast<const InnerNode*>(n);
1683  int slot = find_upper(inner, key);
1684 
1685  n = inner->childid[slot];
1686  }
1687 
1688  const LeafNode* leaf = static_cast<const LeafNode*>(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 LeafNode* leaf = static_cast<const LeafNode*>(n);
1800  LeafNode* 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 InnerNode* inner = static_cast<const InnerNode*>(n);
1823  InnerNode* 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  InnerNode* 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  InnerNode* inner = static_cast<InnerNode*>(n);
1932 
1933  key_type newkey = key_type();
1934  node* newchild = nullptr;
1935 
1936  int slot = find_lower(inner, key);
1937 
1939  "BTree::insert_descend into " << inner->childid[slot]);
1940 
1941  std::pair<iterator, bool> r =
1942  insert_descend(inner->childid[slot],
1943  key, value, &newkey, &newchild);
1944 
1945  if (newchild)
1946  {
1947  TLX_BTREE_PRINT("BTree::insert_descend newchild" <<
1948  " with key " << newkey <<
1949  " node " << newchild << " at slot " << slot);
1950 
1951  if (inner->is_full())
1952  {
1953  split_inner_node(inner, splitkey, splitnode, slot);
1954 
1955  TLX_BTREE_PRINT("BTree::insert_descend done split_inner:" <<
1956  " putslot: " << slot <<
1957  " putkey: " << newkey <<
1958  " upkey: " << *splitkey);
1959 
1960 #ifdef TLX_BTREE_DEBUG
1961  if (debug)
1962  {
1963  print_node(std::cout, inner);
1964  print_node(std::cout, *splitnode);
1965  }
1966 #endif
1967 
1968  // check if insert slot is in the split sibling node
1969  TLX_BTREE_PRINT("BTree::insert_descend switch: "
1970  << slot << " > " << inner->slotuse + 1);
1971 
1972  if (slot == inner->slotuse + 1 &&
1973  inner->slotuse < (*splitnode)->slotuse)
1974  {
1975  // special case when the insert slot matches the split
1976  // place between the two nodes, then the insert key
1977  // becomes the split key.
1978 
1979  TLX_BTREE_ASSERT(inner->slotuse + 1 < inner_slotmax);
1980 
1981  InnerNode* split = static_cast<InnerNode*>(*splitnode);
1982 
1983  // move the split key and it's datum into the left node
1984  inner->slotkey[inner->slotuse] = *splitkey;
1985  inner->childid[inner->slotuse + 1] = split->childid[0];
1986  inner->slotuse++;
1987 
1988  // set new split key and move corresponding datum into
1989  // right node
1990  split->childid[0] = newchild;
1991  *splitkey = newkey;
1992 
1993  return r;
1994  }
1995  else if (slot >= inner->slotuse + 1)
1996  {
1997  // in case the insert slot is in the newly create split
1998  // node, we reuse the code below.
1999 
2000  slot -= inner->slotuse + 1;
2001  inner = static_cast<InnerNode*>(*splitnode);
2003  "BTree::insert_descend switching to "
2004  "splitted node " << inner << " slot " << slot);
2005  }
2006  }
2007 
2008  // move items and put pointer to child node into correct slot
2009  TLX_BTREE_ASSERT(slot >= 0 && slot <= inner->slotuse);
2010 
2011  std::copy_backward(
2012  inner->slotkey + slot, inner->slotkey + inner->slotuse,
2013  inner->slotkey + inner->slotuse + 1);
2014  std::copy_backward(
2015  inner->childid + slot, inner->childid + inner->slotuse + 1,
2016  inner->childid + inner->slotuse + 2);
2017 
2018  inner->slotkey[slot] = newkey;
2019  inner->childid[slot + 1] = newchild;
2020  inner->slotuse++;
2021  }
2022 
2023  return r;
2024  }
2025  else // n->is_leafnode() == true
2026  {
2027  LeafNode* leaf = static_cast<LeafNode*>(n);
2028 
2029  int slot = find_lower(leaf, key);
2030 
2031  if (!allow_duplicates &&
2032  slot < leaf->slotuse && key_equal(key, leaf->key(slot))) {
2033  return std::pair<iterator, bool>(iterator(leaf, slot), false);
2034  }
2035 
2036  if (leaf->is_full())
2037  {
2038  split_leaf_node(leaf, splitkey, splitnode);
2039 
2040  // check if insert slot is in the split sibling node
2041  if (slot >= leaf->slotuse)
2042  {
2043  slot -= leaf->slotuse;
2044  leaf = static_cast<LeafNode*>(*splitnode);
2045  }
2046  }
2047 
2048  // move items and put data item into correct data slot
2049  TLX_BTREE_ASSERT(slot >= 0 && slot <= leaf->slotuse);
2050 
2051  std::copy_backward(
2052  leaf->slotdata + slot, leaf->slotdata + leaf->slotuse,
2053  leaf->slotdata + leaf->slotuse + 1);
2054 
2055  leaf->slotdata[slot] = value;
2056  leaf->slotuse++;
2057 
2058  if (splitnode && leaf != *splitnode && slot == leaf->slotuse - 1)
2059  {
2060  // special case: the node was split, and the insert is at the
2061  // last slot of the old node. then the splitkey must be updated.
2062  *splitkey = key;
2063  }
2064 
2065  return std::pair<iterator, bool>(iterator(leaf, slot), true);
2066  }
2067  }
2068 
2069  //! Split up a leaf node into two equally-filled sibling leaves. Returns the
2070  //! new nodes and it's insertion key in the two parameters.
2071  void split_leaf_node(LeafNode* leaf,
2072  key_type* out_newkey, node** out_newleaf) {
2073  TLX_BTREE_ASSERT(leaf->is_full());
2074 
2075  unsigned int mid = (leaf->slotuse >> 1);
2076 
2077  TLX_BTREE_PRINT("BTree::split_leaf_node on " << leaf);
2078 
2079  LeafNode* newleaf = allocate_leaf();
2080 
2081  newleaf->slotuse = leaf->slotuse - mid;
2082 
2083  newleaf->next_leaf = leaf->next_leaf;
2084  if (newleaf->next_leaf == nullptr) {
2085  TLX_BTREE_ASSERT(leaf == tail_leaf_);
2086  tail_leaf_ = newleaf;
2087  }
2088  else {
2089  newleaf->next_leaf->prev_leaf = newleaf;
2090  }
2091 
2092  std::copy(leaf->slotdata + mid, leaf->slotdata + leaf->slotuse,
2093  newleaf->slotdata);
2094 
2095  leaf->slotuse = mid;
2096  leaf->next_leaf = newleaf;
2097  newleaf->prev_leaf = leaf;
2098 
2099  *out_newkey = leaf->key(leaf->slotuse - 1);
2100  *out_newleaf = newleaf;
2101  }
2102 
2103  //! Split up an inner node into two equally-filled sibling nodes. Returns
2104  //! the new nodes and it's insertion key in the two parameters. Requires the
2105  //! slot of the item will be inserted, so the nodes will be the same size
2106  //! after the insert.
2107  void split_inner_node(InnerNode* inner, key_type* out_newkey,
2108  node** out_newinner, unsigned int addslot) {
2109  TLX_BTREE_ASSERT(inner->is_full());
2110 
2111  unsigned int mid = (inner->slotuse >> 1);
2112 
2113  TLX_BTREE_PRINT("BTree::split_inner: mid " << mid <<
2114  " addslot " << addslot);
2115 
2116  // if the split is uneven and the overflowing item will be put into the
2117  // larger node, then the smaller split node may underflow
2118  if (addslot <= mid && mid > inner->slotuse - (mid + 1))
2119  mid--;
2120 
2121  TLX_BTREE_PRINT("BTree::split_inner: mid " << mid <<
2122  " addslot " << addslot);
2123 
2124  TLX_BTREE_PRINT("BTree::split_inner_node on " << inner <<
2125  " into two nodes " << mid << " and " <<
2126  inner->slotuse - (mid + 1) << " sized");
2127 
2128  InnerNode* newinner = allocate_inner(inner->level);
2129 
2130  newinner->slotuse = inner->slotuse - (mid + 1);
2131 
2132  std::copy(inner->slotkey + mid + 1, inner->slotkey + inner->slotuse,
2133  newinner->slotkey);
2134  std::copy(inner->childid + mid + 1, inner->childid + inner->slotuse + 1,
2135  newinner->childid);
2136 
2137  inner->slotuse = mid;
2138 
2139  *out_newkey = inner->key(mid);
2140  *out_newinner = newinner;
2141  }
2142 
2143  //! \}
2144 
2145 public:
2146  //! \name Bulk Loader - Construct Tree from Sorted Sequence
2147  //! \{
2148 
2149  //! Bulk load a sorted range. Loads items into leaves and constructs a
2150  //! B-tree above them. The tree must be empty when calling this function.
2151  template <typename Iterator>
2152  void bulk_load(Iterator ibegin, Iterator iend) {
2154 
2155  stats_.size = iend - ibegin;
2156 
2157  // calculate number of leaves needed, round up.
2158  size_t num_items = iend - ibegin;
2159  size_t num_leaves = (num_items + leaf_slotmax - 1) / leaf_slotmax;
2160 
2161  TLX_BTREE_PRINT("BTree::bulk_load, level 0: " << stats_.size <<
2162  " items into " << num_leaves <<
2163  " leaves with up to " <<
2164  ((iend - ibegin + num_leaves - 1) / num_leaves) <<
2165  " items per leaf.");
2166 
2167  Iterator it = ibegin;
2168  for (size_t i = 0; i < num_leaves; ++i)
2169  {
2170  // allocate new leaf node
2171  LeafNode* leaf = allocate_leaf();
2172 
2173  // copy keys or (key,value) pairs into leaf nodes, uses template
2174  // switch leaf->set_slot().
2175  leaf->slotuse = static_cast<int>(num_items / (num_leaves - i));
2176  for (size_t s = 0; s < leaf->slotuse; ++s, ++it)
2177  leaf->set_slot(s, *it);
2178 
2179  if (tail_leaf_ != nullptr) {
2180  tail_leaf_->next_leaf = leaf;
2181  leaf->prev_leaf = tail_leaf_;
2182  }
2183  else {
2184  head_leaf_ = leaf;
2185  }
2186  tail_leaf_ = leaf;
2187 
2188  num_items -= leaf->slotuse;
2189  }
2190 
2191  TLX_BTREE_ASSERT(it == iend && num_items == 0);
2192 
2193  // if the btree is so small to fit into one leaf, then we're done.
2194  if (head_leaf_ == tail_leaf_) {
2195  root_ = head_leaf_;
2196  return;
2197  }
2198 
2199  TLX_BTREE_ASSERT(stats_.leaves == num_leaves);
2200 
2201  // create first level of inner nodes, pointing to the leaves.
2202  size_t num_parents =
2203  (num_leaves + (inner_slotmax + 1) - 1) / (inner_slotmax + 1);
2204 
2205  TLX_BTREE_PRINT("BTree::bulk_load, level 1: " <<
2206  num_leaves << " leaves in " <<
2207  num_parents << " inner nodes with up to " <<
2208  ((num_leaves + num_parents - 1) / num_parents) <<
2209  " leaves per inner node.");
2210 
2211  // save inner nodes and maxkey for next level.
2212  typedef std::pair<InnerNode*, const key_type*> nextlevel_type;
2213  nextlevel_type* nextlevel = new nextlevel_type[num_parents];
2214 
2215  LeafNode* leaf = head_leaf_;
2216  for (size_t i = 0; i < num_parents; ++i)
2217  {
2218  // allocate new inner node at level 1
2219  InnerNode* n = allocate_inner(1);
2220 
2221  n->slotuse = static_cast<int>(num_leaves / (num_parents - i));
2222  TLX_BTREE_ASSERT(n->slotuse > 0);
2223  // this counts keys, but an inner node has keys+1 children.
2224  --n->slotuse;
2225 
2226  // copy last key from each leaf and set child
2227  for (unsigned short s = 0; s < n->slotuse; ++s)
2228  {
2229  n->slotkey[s] = leaf->key(leaf->slotuse - 1);
2230  n->childid[s] = leaf;
2231  leaf = leaf->next_leaf;
2232  }
2233  n->childid[n->slotuse] = leaf;
2234 
2235  // track max key of any descendant.
2236  nextlevel[i].first = n;
2237  nextlevel[i].second = &leaf->key(leaf->slotuse - 1);
2238 
2239  leaf = leaf->next_leaf;
2240  num_leaves -= n->slotuse + 1;
2241  }
2242 
2243  TLX_BTREE_ASSERT(leaf == nullptr && num_leaves == 0);
2244 
2245  // recursively build inner nodes pointing to inner nodes.
2246  for (int level = 2; num_parents != 1; ++level)
2247  {
2248  size_t num_children = num_parents;
2249  num_parents =
2250  (num_children + (inner_slotmax + 1) - 1) / (inner_slotmax + 1);
2251 
2253  "BTree::bulk_load, level " << level <<
2254  ": " << num_children << " children in " <<
2255  num_parents << " inner nodes with up to " <<
2256  ((num_children + num_parents - 1) / num_parents) <<
2257  " children per inner node.");
2258 
2259  size_t inner_index = 0;
2260  for (size_t i = 0; i < num_parents; ++i)
2261  {
2262  // allocate new inner node at level
2263  InnerNode* n = allocate_inner(level);
2264 
2265  n->slotuse = static_cast<int>(num_children / (num_parents - i));
2266  TLX_BTREE_ASSERT(n->slotuse > 0);
2267  // this counts keys, but an inner node has keys+1 children.
2268  --n->slotuse;
2269 
2270  // copy children and maxkeys from nextlevel
2271  for (unsigned short s = 0; s < n->slotuse; ++s)
2272  {
2273  n->slotkey[s] = *nextlevel[inner_index].second;
2274  n->childid[s] = nextlevel[inner_index].first;
2275  ++inner_index;
2276  }
2277  n->childid[n->slotuse] = nextlevel[inner_index].first;
2278 
2279  // reuse nextlevel array for parents, because we can overwrite
2280  // slots we've already consumed.
2281  nextlevel[i].first = n;
2282  nextlevel[i].second = nextlevel[inner_index].second;
2283 
2284  ++inner_index;
2285  num_children -= n->slotuse + 1;
2286  }
2287 
2288  TLX_BTREE_ASSERT(num_children == 0);
2289  }
2290 
2291  root_ = nextlevel[0].first;
2292  delete[] nextlevel;
2293 
2294  if (self_verify) verify();
2295  }
2296 
2297  //! \}
2298 
2299 private:
2300  //! \name Support Class Encapsulating Deletion Results
2301  //! \{
2302 
2303  //! Result flags of recursive deletion.
2305  //! Deletion successful and no fix-ups necessary.
2307 
2308  //! Deletion not successful because key was not found.
2310 
2311  //! Deletion successful, the last key was updated so parent slotkeys
2312  //! need updates.
2314 
2315  //! Deletion successful, children nodes were merged and the parent needs
2316  //! to remove the empty node.
2318  };
2319 
2320  //! B+ tree recursive deletion has much information which is needs to be
2321  //! passed upward.
2322  struct result_t {
2323  //! Merged result flags
2325 
2326  //! The key to be updated at the parent's slot
2328 
2329  //! Constructor of a result with a specific flag, this can also be used
2330  //! as for implicit conversion.
2332  : flags(f), lastkey()
2333  { }
2334 
2335  //! Constructor with a lastkey value.
2337  : flags(f), lastkey(k)
2338  { }
2339 
2340  //! Test if this result object has a given flag set.
2341  bool has(result_flags_t f) const {
2342  return (flags & f) != 0;
2343  }
2344 
2345  //! Merge two results OR-ing the result flags and overwriting lastkeys.
2346  result_t& operator |= (const result_t& other) {
2347  flags = result_flags_t(flags | other.flags);
2348 
2349  // we overwrite existing lastkeys on purpose
2350  if (other.has(btree_update_lastkey))
2351  lastkey = other.lastkey;
2352 
2353  return *this;
2354  }
2355  };
2356 
2357  //! \}
2358 
2359 public:
2360  //! \name Public Erase Functions
2361  //! \{
2362 
2363  //! Erases one (the first) of the key/data pairs associated with the given
2364  //! key.
2365  bool erase_one(const key_type& key) {
2366  TLX_BTREE_PRINT("BTree::erase_one(" << key <<
2367  ") on btree size " << size());
2368 
2369  if (self_verify) verify();
2370 
2371  if (!root_) return false;
2372 
2373  result_t result = erase_one_descend(
2374  key, root_, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
2375 
2376  if (!result.has(btree_not_found))
2377  --stats_.size;
2378 
2379 #ifdef TLX_BTREE_DEBUG
2380  if (debug) print(std::cout);
2381 #endif
2382  if (self_verify) verify();
2383 
2384  return !result.has(btree_not_found);
2385  }
2386 
2387  //! Erases all the key/data pairs associated with the given key. This is
2388  //! implemented using erase_one().
2389  size_type erase(const key_type& key) {
2390  size_type c = 0;
2391 
2392  while (erase_one(key))
2393  {
2394  ++c;
2395  if (!allow_duplicates) break;
2396  }
2397 
2398  return c;
2399  }
2400 
2401  //! Erase the key/data pair referenced by the iterator.
2402  void erase(iterator iter) {
2403  TLX_BTREE_PRINT("BTree::erase_iter(" << iter.curr_leaf <<
2404  "," << iter.curr_slot << ") on btree size " << size());
2405 
2406  if (self_verify) verify();
2407 
2408  if (!root_) return;
2409 
2410  result_t result = erase_iter_descend(
2411  iter, root_, nullptr, nullptr, nullptr, nullptr, nullptr, 0);
2412 
2413  if (!result.has(btree_not_found))
2414  --stats_.size;
2415 
2416 #ifdef TLX_BTREE_DEBUG
2417  if (debug) print(std::cout);
2418 #endif
2419  if (self_verify) verify();
2420  }
2421 
2422 #ifdef BTREE_TODO
2423  //! Erase all key/data pairs in the range [first,last). This function is
2424  //! currently not implemented by the B+ Tree.
2425  void erase(iterator /* first */, iterator /* last */) {
2426  abort();
2427  }
2428 #endif
2429 
2430  //! \}
2431 
2432 private:
2433  //! \name Private Erase Functions
2434  //! \{
2435 
2436  /*!
2437  * Erase one (the first) key/data pair in the B+ tree matching key.
2438  *
2439  * Descends down the tree in search of key. During the descent the parent,
2440  * left and right siblings and their parents are computed and passed
2441  * down. Once the key/data pair is found, it is removed from the leaf. If
2442  * the leaf underflows 6 different cases are handled. These cases resolve
2443  * the underflow by shifting key/data pairs from adjacent sibling nodes,
2444  * merging two sibling nodes or trimming the tree.
2445  */
2446  result_t erase_one_descend(const key_type& key,
2447  node* curr,
2448  node* left, node* right,
2449  InnerNode* left_parent, InnerNode* right_parent,
2450  InnerNode* parent, unsigned int parentslot) {
2451  if (curr->is_leafnode())
2452  {
2453  LeafNode* leaf = static_cast<LeafNode*>(curr);
2454  LeafNode* left_leaf = static_cast<LeafNode*>(left);
2455  LeafNode* right_leaf = static_cast<LeafNode*>(right);
2456 
2457  int slot = find_lower(leaf, key);
2458 
2459  if (slot >= leaf->slotuse || !key_equal(key, leaf->key(slot)))
2460  {
2461  TLX_BTREE_PRINT("Could not find key " << key << " to erase.");
2462 
2463  return btree_not_found;
2464  }
2465 
2467  "Found key in leaf " << curr << " at slot " << slot);
2468 
2469  std::copy(leaf->slotdata + slot + 1, leaf->slotdata + leaf->slotuse,
2470  leaf->slotdata + slot);
2471 
2472  leaf->slotuse--;
2473 
2474  result_t myres = btree_ok;
2475 
2476  // if the last key of the leaf was changed, the parent is notified
2477  // and updates the key of this leaf
2478  if (slot == leaf->slotuse)
2479  {
2480  if (parent && parentslot < parent->slotuse)
2481  {
2482  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2483  parent->slotkey[parentslot] = leaf->key(leaf->slotuse - 1);
2484  }
2485  else
2486  {
2487  if (leaf->slotuse >= 1)
2488  {
2489  TLX_BTREE_PRINT("Scheduling lastkeyupdate: key " <<
2490  leaf->key(leaf->slotuse - 1));
2491  myres |= result_t(
2492  btree_update_lastkey, leaf->key(leaf->slotuse - 1));
2493  }
2494  else
2495  {
2496  TLX_BTREE_ASSERT(leaf == root_);
2497  }
2498  }
2499  }
2500 
2501  if (leaf->is_underflow() && !(leaf == root_ && leaf->slotuse >= 1))
2502  {
2503  // determine what to do about the underflow
2504 
2505  // case : if this empty leaf is the root, then delete all nodes
2506  // and set root to nullptr.
2507  if (left_leaf == nullptr && right_leaf == nullptr)
2508  {
2509  TLX_BTREE_ASSERT(leaf == root_);
2510  TLX_BTREE_ASSERT(leaf->slotuse == 0);
2511 
2512  free_node(root_);
2513 
2514  root_ = leaf = nullptr;
2515  head_leaf_ = tail_leaf_ = nullptr;
2516 
2517  // will be decremented soon by insert_start()
2521 
2522  return btree_ok;
2523  }
2524  // case : if both left and right leaves would underflow in case
2525  // of a shift, then merging is necessary. choose the more local
2526  // merger with our parent
2527  else if ((left_leaf == nullptr || left_leaf->is_few()) &&
2528  (right_leaf == nullptr || right_leaf->is_few()))
2529  {
2530  if (left_parent == parent)
2531  myres |= merge_leaves(left_leaf, leaf, left_parent);
2532  else
2533  myres |= merge_leaves(leaf, right_leaf, right_parent);
2534  }
2535  // case : the right leaf has extra data, so balance right with
2536  // current
2537  else if ((left_leaf != nullptr && left_leaf->is_few()) &&
2538  (right_leaf != nullptr && !right_leaf->is_few()))
2539  {
2540  if (right_parent == parent)
2541  myres |= shift_left_leaf(
2542  leaf, right_leaf, right_parent, parentslot);
2543  else
2544  myres |= merge_leaves(left_leaf, leaf, left_parent);
2545  }
2546  // case : the left leaf has extra data, so balance left with
2547  // current
2548  else if ((left_leaf != nullptr && !left_leaf->is_few()) &&
2549  (right_leaf != nullptr && right_leaf->is_few()))
2550  {
2551  if (left_parent == parent)
2553  left_leaf, leaf, left_parent, parentslot - 1);
2554  else
2555  myres |= merge_leaves(leaf, right_leaf, right_parent);
2556  }
2557  // case : both the leaf and right leaves have extra data and our
2558  // parent, choose the leaf with more data
2559  else if (left_parent == right_parent)
2560  {
2561  if (left_leaf->slotuse <= right_leaf->slotuse)
2562  myres |= shift_left_leaf(
2563  leaf, right_leaf, right_parent, parentslot);
2564  else
2566  left_leaf, leaf, left_parent, parentslot - 1);
2567  }
2568  else
2569  {
2570  if (left_parent == parent)
2572  left_leaf, leaf, left_parent, parentslot - 1);
2573  else
2574  myres |= shift_left_leaf(
2575  leaf, right_leaf, right_parent, parentslot);
2576  }
2577  }
2578 
2579  return myres;
2580  }
2581  else // !curr->is_leafnode()
2582  {
2583  InnerNode* inner = static_cast<InnerNode*>(curr);
2584  InnerNode* left_inner = static_cast<InnerNode*>(left);
2585  InnerNode* right_inner = static_cast<InnerNode*>(right);
2586 
2587  node* myleft, * myright;
2588  InnerNode* myleft_parent, * myright_parent;
2589 
2590  int slot = find_lower(inner, key);
2591 
2592  if (slot == 0) {
2593  myleft =
2594  (left == nullptr) ? nullptr :
2595  static_cast<InnerNode*>(left)->childid[left->slotuse - 1];
2596  myleft_parent = left_parent;
2597  }
2598  else {
2599  myleft = inner->childid[slot - 1];
2600  myleft_parent = inner;
2601  }
2602 
2603  if (slot == inner->slotuse) {
2604  myright =
2605  (right == nullptr) ? nullptr :
2606  static_cast<InnerNode*>(right)->childid[0];
2607  myright_parent = right_parent;
2608  }
2609  else {
2610  myright = inner->childid[slot + 1];
2611  myright_parent = inner;
2612  }
2613 
2614  TLX_BTREE_PRINT("erase_one_descend into " << inner->childid[slot]);
2615 
2616  result_t result = erase_one_descend(
2617  key,
2618  inner->childid[slot],
2619  myleft, myright,
2620  myleft_parent, myright_parent,
2621  inner, slot);
2622 
2623  result_t myres = btree_ok;
2624 
2625  if (result.has(btree_not_found))
2626  {
2627  return result;
2628  }
2629 
2630  if (result.has(btree_update_lastkey))
2631  {
2632  if (parent && parentslot < parent->slotuse)
2633  {
2634  TLX_BTREE_PRINT("Fixing lastkeyupdate: key " <<
2635  result.lastkey << " into parent " <<
2636  parent << " at parentslot " <<
2637  parentslot);
2638 
2639  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2640  parent->slotkey[parentslot] = result.lastkey;
2641  }
2642  else
2643  {
2645  "Forwarding lastkeyupdate: key " << result.lastkey);
2646  myres |= result_t(btree_update_lastkey, result.lastkey);
2647  }
2648  }
2649 
2650  if (result.has(btree_fixmerge))
2651  {
2652  // either the current node or the next is empty and should be
2653  // removed
2654  if (inner->childid[slot]->slotuse != 0)
2655  slot++;
2656 
2657  // this is the child slot invalidated by the merge
2658  TLX_BTREE_ASSERT(inner->childid[slot]->slotuse == 0);
2659 
2660  free_node(inner->childid[slot]);
2661 
2662  std::copy(
2663  inner->slotkey + slot, inner->slotkey + inner->slotuse,
2664  inner->slotkey + slot - 1);
2665  std::copy(
2666  inner->childid + slot + 1,
2667  inner->childid + inner->slotuse + 1,
2668  inner->childid + slot);
2669 
2670  inner->slotuse--;
2671 
2672  if (inner->level == 1)
2673  {
2674  // fix split key for children leaves
2675  slot--;
2676  LeafNode* child =
2677  static_cast<LeafNode*>(inner->childid[slot]);
2678  inner->slotkey[slot] = child->key(child->slotuse - 1);
2679  }
2680  }
2681 
2682  if (inner->is_underflow() &&
2683  !(inner == root_ && inner->slotuse >= 1))
2684  {
2685  // case: the inner node is the root and has just one child. that
2686  // child becomes the new root
2687  if (left_inner == nullptr && right_inner == nullptr)
2688  {
2689  TLX_BTREE_ASSERT(inner == root_);
2690  TLX_BTREE_ASSERT(inner->slotuse == 0);
2691 
2692  root_ = inner->childid[0];
2693 
2694  inner->slotuse = 0;
2695  free_node(inner);
2696 
2697  return btree_ok;
2698  }
2699  // case : if both left and right leaves would underflow in case
2700  // of a shift, then merging is necessary. choose the more local
2701  // merger with our parent
2702  else if ((left_inner == nullptr || left_inner->is_few()) &&
2703  (right_inner == nullptr || right_inner->is_few()))
2704  {
2705  if (left_parent == parent)
2706  myres |= merge_inner(
2707  left_inner, inner, left_parent, parentslot - 1);
2708  else
2709  myres |= merge_inner(
2710  inner, right_inner, right_parent, parentslot);
2711  }
2712  // case : the right leaf has extra data, so balance right with
2713  // current
2714  else if ((left_inner != nullptr && left_inner->is_few()) &&
2715  (right_inner != nullptr && !right_inner->is_few()))
2716  {
2717  if (right_parent == parent)
2719  inner, right_inner, right_parent, parentslot);
2720  else
2721  myres |= merge_inner(
2722  left_inner, inner, left_parent, parentslot - 1);
2723  }
2724  // case : the left leaf has extra data, so balance left with
2725  // current
2726  else if ((left_inner != nullptr && !left_inner->is_few()) &&
2727  (right_inner != nullptr && right_inner->is_few()))
2728  {
2729  if (left_parent == parent)
2731  left_inner, inner, left_parent, parentslot - 1);
2732  else
2733  myres |= merge_inner(
2734  inner, right_inner, right_parent, parentslot);
2735  }
2736  // case : both the leaf and right leaves have extra data and our
2737  // parent, choose the leaf with more data
2738  else if (left_parent == right_parent)
2739  {
2740  if (left_inner->slotuse <= right_inner->slotuse)
2742  inner, right_inner, right_parent, parentslot);
2743  else
2745  left_inner, inner, left_parent, parentslot - 1);
2746  }
2747  else
2748  {
2749  if (left_parent == parent)
2751  left_inner, inner, left_parent, parentslot - 1);
2752  else
2754  inner, right_inner, right_parent, parentslot);
2755  }
2756  }
2757 
2758  return myres;
2759  }
2760  }
2761 
2762  /*!
2763  * Erase one key/data pair referenced by an iterator in the B+ tree.
2764  *
2765  * Descends down the tree in search of an iterator. During the descent the
2766  * parent, left and right siblings and their parents are computed and passed
2767  * down. The difficulty is that the iterator contains only a pointer to a
2768  * LeafNode, which means that this function must do a recursive depth first
2769  * search for that leaf node in the subtree containing all pairs of the same
2770  * key. This subtree can be very large, even the whole tree, though in
2771  * practice it would not make sense to have so many duplicate keys.
2772  *
2773  * Once the referenced key/data pair is found, it is removed from the leaf
2774  * and the same underflow cases are handled as in erase_one_descend.
2775  */
2776  result_t erase_iter_descend(const iterator& iter,
2777  node* curr,
2778  node* left, node* right,
2779  InnerNode* left_parent, InnerNode* right_parent,
2780  InnerNode* parent, unsigned int parentslot) {
2781  if (curr->is_leafnode())
2782  {
2783  LeafNode* leaf = static_cast<LeafNode*>(curr);
2784  LeafNode* left_leaf = static_cast<LeafNode*>(left);
2785  LeafNode* right_leaf = static_cast<LeafNode*>(right);
2786 
2787  // if this is not the correct leaf, get next step in recursive
2788  // search
2789  if (leaf != iter.curr_leaf)
2790  {
2791  return btree_not_found;
2792  }
2793 
2794  if (iter.curr_slot >= leaf->slotuse)
2795  {
2796  TLX_BTREE_PRINT("Could not find iterator (" <<
2797  iter.curr_leaf << "," << iter.curr_slot <<
2798  ") to erase. Invalid leaf node?");
2799 
2800  return btree_not_found;
2801  }
2802 
2803  int slot = iter.curr_slot;
2804 
2805  TLX_BTREE_PRINT("Found iterator in leaf " <<
2806  curr << " at slot " << slot);
2807 
2808  std::copy(leaf->slotdata + slot + 1, leaf->slotdata + leaf->slotuse,
2809  leaf->slotdata + slot);
2810 
2811  leaf->slotuse--;
2812 
2813  result_t myres = btree_ok;
2814 
2815  // if the last key of the leaf was changed, the parent is notified
2816  // and updates the key of this leaf
2817  if (slot == leaf->slotuse)
2818  {
2819  if (parent && parentslot < parent->slotuse)
2820  {
2821  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
2822  parent->slotkey[parentslot] = leaf->key(leaf->slotuse - 1);
2823  }
2824  else
2825  {
2826  if (leaf->slotuse >= 1)
2827  {
2828  TLX_BTREE_PRINT("Scheduling lastkeyupdate: key " <<
2829  leaf->key(leaf->slotuse - 1));
2830  myres |= result_t(
2831  btree_update_lastkey, leaf->key(leaf->slotuse - 1));
2832  }
2833  else
2834  {
2835  TLX_BTREE_ASSERT(leaf == root_);
2836  }
2837  }
2838  }
2839 
2840  if (leaf->is_underflow() && !(leaf == root_ && leaf->slotuse >= 1))
2841  {
2842  // determine what to do about the underflow
2843 
2844  // case : if this empty leaf is the root, then delete all nodes
2845  // and set root to nullptr.
2846  if (left_leaf == nullptr && right_leaf == nullptr)
2847  {
2848  TLX_BTREE_ASSERT(leaf == root_);
2849  TLX_BTREE_ASSERT(leaf->slotuse == 0);
2850 
2851  free_node(root_);
2852 
2853  root_ = leaf = nullptr;
2854  head_leaf_ = tail_leaf_ = nullptr;
2855 
2856  // will be decremented soon by insert_start()
2860 
2861  return btree_ok;
2862  }
2863  // case : if both left and right leaves would underflow in case
2864  // of a shift, then merging is necessary. choose the more local
2865  // merger with our parent
2866  else if ((left_leaf == nullptr || left_leaf->is_few()) &&
2867  (right_leaf == nullptr || right_leaf->is_few()))
2868  {
2869  if (left_parent == parent)
2870  myres |= merge_leaves(left_leaf, leaf, left_parent);
2871  else
2872  myres |= merge_leaves(leaf, right_leaf, right_parent);
2873  }
2874  // case : the right leaf has extra data, so balance right with
2875  // current
2876  else if ((left_leaf != nullptr && left_leaf->is_few()) &&
2877  (right_leaf != nullptr && !right_leaf->is_few()))
2878  {
2879  if (right_parent == parent) {
2880  myres |= shift_left_leaf(
2881  leaf, right_leaf, right_parent, parentslot);
2882  }
2883  else {
2884  myres |= merge_leaves(left_leaf, leaf, left_parent);
2885  }
2886  }
2887  // case : the left leaf has extra data, so balance left with
2888  // current
2889  else if ((left_leaf != nullptr && !left_leaf->is_few()) &&
2890  (right_leaf != nullptr && right_leaf->is_few()))
2891  {
2892  if (left_parent == parent) {
2894  left_leaf, leaf, left_parent, parentslot - 1);
2895  }
2896  else {
2897  myres |= merge_leaves(leaf, right_leaf, right_parent);
2898  }
2899  }
2900  // case : both the leaf and right leaves have extra data and our
2901  // parent, choose the leaf with more data
2902  else if (left_parent == right_parent)
2903  {
2904  if (left_leaf->slotuse <= right_leaf->slotuse) {
2905  myres |= shift_left_leaf(
2906  leaf, right_leaf, right_parent, parentslot);
2907  }
2908  else {
2910  left_leaf, leaf, left_parent, parentslot - 1);
2911  }
2912  }
2913  else
2914  {
2915  if (left_parent == parent) {
2917  left_leaf, leaf, left_parent, parentslot - 1);
2918  }
2919  else {
2920  myres |= shift_left_leaf(
2921  leaf, right_leaf, right_parent, parentslot);
2922  }
2923  }
2924  }
2925 
2926  return myres;
2927  }
2928  else // !curr->is_leafnode()
2929  {
2930  InnerNode* inner = static_cast<InnerNode*>(curr);
2931  InnerNode* left_inner = static_cast<InnerNode*>(left);
2932  InnerNode* right_inner = static_cast<InnerNode*>(right);
2933 
2934  // find first slot below which the searched iterator might be
2935  // located.
2936 
2937  result_t result;
2938  int slot = find_lower(inner, iter.key());
2939 
2940  while (slot <= inner->slotuse)
2941  {
2942  node* myleft, * myright;
2943  InnerNode* myleft_parent, * myright_parent;
2944 
2945  if (slot == 0) {
2946  myleft = (left == nullptr) ? nullptr
2947  : static_cast<InnerNode*>(left)->childid[
2948  left->slotuse - 1];
2949  myleft_parent = left_parent;
2950  }
2951  else {
2952  myleft = inner->childid[slot - 1];
2953  myleft_parent = inner;
2954  }
2955 
2956  if (slot == inner->slotuse) {
2957  myright = (right == nullptr) ? nullptr
2958  : static_cast<InnerNode*>(right)->childid[0];
2959  myright_parent = right_parent;
2960  }
2961  else {
2962  myright = inner->childid[slot + 1];
2963  myright_parent = inner;
2964  }
2965 
2966  TLX_BTREE_PRINT("erase_iter_descend into " <<
2967  inner->childid[slot]);
2968 
2969  result = erase_iter_descend(iter,
2970  inner->childid[slot],
2971  myleft, myright,
2972  myleft_parent, myright_parent,
2973  inner, slot);
2974 
2975  if (!result.has(btree_not_found))
2976  break;
2977 
2978  // continue recursive search for leaf on next slot
2979 
2980  if (slot < inner->slotuse &&
2981  key_less(inner->slotkey[slot], iter.key()))
2982  return btree_not_found;
2983 
2984  ++slot;
2985  }
2986 
2987  if (slot > inner->slotuse)
2988  return btree_not_found;
2989 
2990  result_t myres = btree_ok;
2991 
2992  if (result.has(btree_update_lastkey))
2993  {
2994  if (parent && parentslot < parent->slotuse)
2995  {
2996  TLX_BTREE_PRINT("Fixing lastkeyupdate: key " <<
2997  result.lastkey << " into parent " <<
2998  parent << " at parentslot " << parentslot);
2999 
3000  TLX_BTREE_ASSERT(parent->childid[parentslot] == curr);
3001  parent->slotkey[parentslot] = result.lastkey;
3002  }
3003  else
3004  {
3006  "Forwarding lastkeyupdate: key " << result.lastkey);
3007  myres |= result_t(btree_update_lastkey, result.lastkey);
3008  }
3009  }
3010 
3011  if (result.has(btree_fixmerge))
3012  {
3013  // either the current node or the next is empty and should be
3014  // removed
3015  if (inner->childid[slot]->slotuse != 0)
3016  slot++;
3017 
3018  // this is the child slot invalidated by the merge
3019  TLX_BTREE_ASSERT(inner->childid[slot]->slotuse == 0);
3020 
3021  free_node(inner->childid[slot]);
3022 
3023  std::copy(
3024  inner->slotkey + slot, inner->slotkey + inner->slotuse,
3025  inner->slotkey + slot - 1);
3026  std::copy(
3027  inner->childid + slot + 1,
3028  inner->childid + inner->slotuse + 1,
3029  inner->childid + slot);
3030 
3031  inner->slotuse--;
3032 
3033  if (inner->level == 1)
3034  {
3035  // fix split key for children leaves
3036  slot--;
3037  LeafNode* child =
3038  static_cast<LeafNode*>(inner->childid[slot]);
3039  inner->slotkey[slot] = child->key(child->slotuse - 1);
3040  }
3041  }
3042 
3043  if (inner->is_underflow() &&
3044  !(inner == root_ && inner->slotuse >= 1))
3045  {
3046  // case: the inner node is the root and has just one
3047  // child. that child becomes the new root
3048  if (left_inner == nullptr && right_inner == nullptr)
3049  {
3050  TLX_BTREE_ASSERT(inner == root_);
3051  TLX_BTREE_ASSERT(inner->slotuse == 0);
3052 
3053  root_ = inner->childid[0];
3054 
3055  inner->slotuse = 0;
3056  free_node(inner);
3057 
3058  return btree_ok;
3059  }
3060  // case : if both left and right leaves would underflow in case
3061  // of a shift, then merging is necessary. choose the more local
3062  // merger with our parent
3063  else if ((left_inner == nullptr || left_inner->is_few()) &&
3064  (right_inner == nullptr || right_inner->is_few()))
3065  {
3066  if (left_parent == parent) {
3067  myres |= merge_inner(
3068  left_inner, inner, left_parent, parentslot - 1);
3069  }
3070  else {
3071  myres |= merge_inner(
3072  inner, right_inner, right_parent, parentslot);
3073  }
3074  }
3075  // case : the right leaf has extra data, so balance right with
3076  // current
3077  else if ((left_inner != nullptr && left_inner->is_few()) &&
3078  (right_inner != nullptr && !right_inner->is_few()))
3079  {
3080  if (right_parent == parent) {
3082  inner, right_inner, right_parent, parentslot);
3083  }
3084  else {
3085  myres |= merge_inner(
3086  left_inner, inner, left_parent, parentslot - 1);
3087  }
3088  }
3089  // case : the left leaf has extra data, so balance left with
3090  // current
3091  else if ((left_inner != nullptr && !left_inner->is_few()) &&
3092  (right_inner != nullptr && right_inner->is_few()))
3093  {
3094  if (left_parent == parent) {
3096  left_inner, inner, left_parent, parentslot - 1);
3097  }
3098  else {
3099  myres |= merge_inner(
3100  inner, right_inner, right_parent, parentslot);
3101  }
3102  }
3103  // case : both the leaf and right leaves have extra data and our
3104  // parent, choose the leaf with more data
3105  else if (left_parent == right_parent)
3106  {
3107  if (left_inner->slotuse <= right_inner->slotuse) {
3109  inner, right_inner, right_parent, parentslot);
3110  }
3111  else {
3113  left_inner, inner, left_parent, parentslot - 1);
3114  }
3115  }
3116  else
3117  {
3118  if (left_parent == parent) {
3120  left_inner, inner, left_parent, parentslot - 1);
3121  }
3122  else {
3124  inner, right_inner, right_parent, parentslot);
3125  }
3126  }
3127  }
3128 
3129  return myres;
3130  }
3131  }
3132 
3133  //! Merge two leaf nodes. The function moves all key/data pairs from right
3134  //! to left and sets right's slotuse to zero. The right slot is then removed
3135  //! by the calling parent node.
3136  result_t merge_leaves(LeafNode* left, LeafNode* right,
3137  InnerNode* parent) {
3138  TLX_BTREE_PRINT("Merge leaf nodes " << left << " and " << right <<
3139  " with common parent " << parent << ".");
3140  (void)parent;
3141 
3142  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3143  TLX_BTREE_ASSERT(parent->level == 1);
3144 
3145  TLX_BTREE_ASSERT(left->slotuse + right->slotuse < leaf_slotmax);
3146 
3147  std::copy(right->slotdata, right->slotdata + right->slotuse,
3148  left->slotdata + left->slotuse);
3149 
3150  left->slotuse += right->slotuse;
3151 
3152  left->next_leaf = right->next_leaf;
3153  if (left->next_leaf)
3154  left->next_leaf->prev_leaf = left;
3155  else
3156  tail_leaf_ = left;
3157 
3158  right->slotuse = 0;
3159 
3160  return btree_fixmerge;
3161  }
3162 
3163  //! Merge two inner nodes. The function moves all key/childid pairs from
3164  //! right to left and sets right's slotuse to zero. The right slot is then
3165  //! removed by the calling parent node.
3166  static result_t merge_inner(InnerNode* left, InnerNode* right,
3167  InnerNode* parent, unsigned int parentslot) {
3168  TLX_BTREE_PRINT("Merge inner nodes " << left << " and " << right <<
3169  " with common parent " << parent << ".");
3170 
3171  TLX_BTREE_ASSERT(left->level == right->level);
3172  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3173 
3174  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3175 
3176  TLX_BTREE_ASSERT(left->slotuse + right->slotuse < inner_slotmax);
3177 
3178  if (self_verify)
3179  {
3180  // find the left node's slot in the parent's children
3181  unsigned int leftslot = 0;
3182  while (leftslot <= parent->slotuse &&
3183  parent->childid[leftslot] != left)
3184  ++leftslot;
3185 
3186  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3187  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3188  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3189 
3190  TLX_BTREE_ASSERT(parentslot == leftslot);
3191  }
3192 
3193  // retrieve the decision key from parent
3194  left->slotkey[left->slotuse] = parent->slotkey[parentslot];
3195  left->slotuse++;
3196 
3197  // copy over keys and children from right
3198  std::copy(right->slotkey, right->slotkey + right->slotuse,
3199  left->slotkey + left->slotuse);
3200  std::copy(right->childid, right->childid + right->slotuse + 1,
3201  left->childid + left->slotuse);
3202 
3203  left->slotuse += right->slotuse;
3204  right->slotuse = 0;
3205 
3206  return btree_fixmerge;
3207  }
3208 
3209  //! Balance two leaf nodes. The function moves key/data pairs from right to
3210  //! left so that both nodes are equally filled. The parent node is updated
3211  //! if possible.
3212  static result_t shift_left_leaf(
3213  LeafNode* left, LeafNode* right,
3214  InnerNode* parent, unsigned int parentslot) {
3215 
3216  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3217  TLX_BTREE_ASSERT(parent->level == 1);
3218 
3219  TLX_BTREE_ASSERT(left->next_leaf == right);
3220  TLX_BTREE_ASSERT(left == right->prev_leaf);
3221 
3222  TLX_BTREE_ASSERT(left->slotuse < right->slotuse);
3223  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3224 
3225  unsigned int shiftnum = (right->slotuse - left->slotuse) >> 1;
3226 
3227  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum << " entries to left " <<
3228  left << " from right " << right <<
3229  " with common parent " << parent << ".");
3230 
3231  TLX_BTREE_ASSERT(left->slotuse + shiftnum < leaf_slotmax);
3232 
3233  // copy the first items from the right node to the last slot in the left
3234  // node.
3235 
3236  std::copy(right->slotdata, right->slotdata + shiftnum,
3237  left->slotdata + left->slotuse);
3238 
3239  left->slotuse += shiftnum;
3240 
3241  // shift all slots in the right node to the left
3242 
3243  std::copy(right->slotdata + shiftnum, right->slotdata + right->slotuse,
3244  right->slotdata);
3245 
3246  right->slotuse -= shiftnum;
3247 
3248  // fixup parent
3249  if (parentslot < parent->slotuse) {
3250  parent->slotkey[parentslot] = left->key(left->slotuse - 1);
3251  return btree_ok;
3252  }
3253  else { // the update is further up the tree
3254  return result_t(btree_update_lastkey, left->key(left->slotuse - 1));
3255  }
3256  }
3257 
3258  //! Balance two inner nodes. The function moves key/data pairs from right to
3259  //! left so that both nodes are equally filled. The parent node is updated
3260  //! if possible.
3261  static void shift_left_inner(InnerNode* left, InnerNode* right,
3262  InnerNode* parent, unsigned int parentslot) {
3263  TLX_BTREE_ASSERT(left->level == right->level);
3264  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3265 
3266  TLX_BTREE_ASSERT(left->slotuse < right->slotuse);
3267  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3268 
3269  unsigned int shiftnum = (right->slotuse - left->slotuse) >> 1;
3270 
3271  TLX_BTREE_PRINT("Shifting (inner) " << shiftnum <<
3272  " entries to left " << left <<
3273  " from right " << right <<
3274  " with common parent " << parent << ".");
3275 
3276  TLX_BTREE_ASSERT(left->slotuse + shiftnum < inner_slotmax);
3277 
3278  if (self_verify)
3279  {
3280  // find the left node's slot in the parent's children and compare to
3281  // parentslot
3282 
3283  unsigned int leftslot = 0;
3284  while (leftslot <= parent->slotuse &&
3285  parent->childid[leftslot] != left)
3286  ++leftslot;
3287 
3288  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3289  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3290  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3291 
3292  TLX_BTREE_ASSERT(leftslot == parentslot);
3293  }
3294 
3295  // copy the parent's decision slotkey and childid to the first new key
3296  // on the left
3297  left->slotkey[left->slotuse] = parent->slotkey[parentslot];
3298  left->slotuse++;
3299 
3300  // copy the other items from the right node to the last slots in the
3301  // left node.
3302  std::copy(right->slotkey, right->slotkey + shiftnum - 1,
3303  left->slotkey + left->slotuse);
3304  std::copy(right->childid, right->childid + shiftnum,
3305  left->childid + left->slotuse);
3306 
3307  left->slotuse += shiftnum - 1;
3308 
3309  // fixup parent
3310  parent->slotkey[parentslot] = right->slotkey[shiftnum - 1];
3311 
3312  // shift all slots in the right node
3313  std::copy(
3314  right->slotkey + shiftnum, right->slotkey + right->slotuse,
3315  right->slotkey);
3316  std::copy(
3317  right->childid + shiftnum, right->childid + right->slotuse + 1,
3318  right->childid);
3319 
3320  right->slotuse -= shiftnum;
3321  }
3322 
3323  //! Balance two leaf nodes. The function moves key/data pairs from left to
3324  //! right so that both nodes are equally filled. The parent node is updated
3325  //! if possible.
3326  static void shift_right_leaf(LeafNode* left, LeafNode* right,
3327  InnerNode* parent, unsigned int parentslot) {
3328  TLX_BTREE_ASSERT(left->is_leafnode() && right->is_leafnode());
3329  TLX_BTREE_ASSERT(parent->level == 1);
3330 
3331  TLX_BTREE_ASSERT(left->next_leaf == right);
3332  TLX_BTREE_ASSERT(left == right->prev_leaf);
3333  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3334 
3335  TLX_BTREE_ASSERT(left->slotuse > right->slotuse);
3336 
3337  unsigned int shiftnum = (left->slotuse - right->slotuse) >> 1;
3338 
3339  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum <<
3340  " entries to right " << right <<
3341  " from left " << left <<
3342  " with common parent " << parent << ".");
3343 
3344  if (self_verify)
3345  {
3346  // find the left node's slot in the parent's children
3347  unsigned int leftslot = 0;
3348  while (leftslot <= parent->slotuse &&
3349  parent->childid[leftslot] != left)
3350  ++leftslot;
3351 
3352  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3353  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3354  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3355 
3356  TLX_BTREE_ASSERT(leftslot == parentslot);
3357  }
3358 
3359  // shift all slots in the right node
3360 
3361  TLX_BTREE_ASSERT(right->slotuse + shiftnum < leaf_slotmax);
3362 
3363  std::copy_backward(right->slotdata, right->slotdata + right->slotuse,
3364  right->slotdata + right->slotuse + shiftnum);
3365 
3366  right->slotuse += shiftnum;
3367 
3368  // copy the last items from the left node to the first slot in the right
3369  // node.
3370  std::copy(left->slotdata + left->slotuse - shiftnum,
3371  left->slotdata + left->slotuse,
3372  right->slotdata);
3373 
3374  left->slotuse -= shiftnum;
3375 
3376  parent->slotkey[parentslot] = left->key(left->slotuse - 1);
3377  }
3378 
3379  //! Balance two inner nodes. The function moves key/data pairs from left to
3380  //! right so that both nodes are equally filled. The parent node is updated
3381  //! if possible.
3382  static void shift_right_inner(InnerNode* left, InnerNode* right,
3383  InnerNode* parent, unsigned int parentslot) {
3384  TLX_BTREE_ASSERT(left->level == right->level);
3385  TLX_BTREE_ASSERT(parent->level == left->level + 1);
3386 
3387  TLX_BTREE_ASSERT(left->slotuse > right->slotuse);
3388  TLX_BTREE_ASSERT(parent->childid[parentslot] == left);
3389 
3390  unsigned int shiftnum = (left->slotuse - right->slotuse) >> 1;
3391 
3392  TLX_BTREE_PRINT("Shifting (leaf) " << shiftnum <<
3393  " entries to right " << right <<
3394  " from left " << left <<
3395  " with common parent " << parent << ".");
3396 
3397  if (self_verify)
3398  {
3399  // find the left node's slot in the parent's children
3400  unsigned int leftslot = 0;
3401  while (leftslot <= parent->slotuse &&
3402  parent->childid[leftslot] != left)
3403  ++leftslot;
3404 
3405  TLX_BTREE_ASSERT(leftslot < parent->slotuse);
3406  TLX_BTREE_ASSERT(parent->childid[leftslot] == left);
3407  TLX_BTREE_ASSERT(parent->childid[leftslot + 1] == right);
3408 
3409  TLX_BTREE_ASSERT(leftslot == parentslot);
3410  }
3411 
3412  // shift all slots in the right node
3413 
3414  TLX_BTREE_ASSERT(right->slotuse + shiftnum < inner_slotmax);
3415 
3416  std::copy_backward(
3417  right->slotkey, right->slotkey + right->slotuse,
3418  right->slotkey + right->slotuse + shiftnum);
3419  std::copy_backward(
3420  right->childid, right->childid + right->slotuse + 1,
3421  right->childid + right->slotuse + 1 + shiftnum);
3422 
3423  right->slotuse += shiftnum;
3424 
3425  // copy the parent's decision slotkey and childid to the last new key on
3426  // the right
3427  right->slotkey[shiftnum - 1] = parent->slotkey[parentslot];
3428 
3429  // copy the remaining last items from the left node to the first slot in
3430  // the right node.
3431  std::copy(left->slotkey + left->slotuse - shiftnum + 1,
3432  left->slotkey + left->slotuse,
3433  right->slotkey);
3434  std::copy(left->childid + left->slotuse - shiftnum + 1,
3435  left->childid + left->slotuse + 1,
3436  right->childid);
3437 
3438  // copy the first to-be-removed key from the left node to the parent's
3439  // decision slot
3440  parent->slotkey[parentslot] = left->slotkey[left->slotuse - shiftnum];
3441 
3442  left->slotuse -= shiftnum;
3443  }
3444 
3445  //! \}
3446 
3447 #ifdef TLX_BTREE_DEBUG
3448 
3449 public:
3450  //! \name Debug Printing
3451  //! \{
3452 
3453  //! Print out the B+ tree structure with keys onto the given ostream. This
3454  //! function requires that the header is compiled with TLX_BTREE_DEBUG and
3455  //! that key_type is printable via std::ostream.
3456  void print(std::ostream& os) const {
3457  if (root_) {
3458  print_node(os, root_, 0, true);
3459  }
3460  }
3461 
3462  //! Print out only the leaves via the double linked list.
3463  void print_leaves(std::ostream& os) const {
3464  os << "leaves:" << std::endl;
3465 
3466  const LeafNode* n = head_leaf_;
3467 
3468  while (n)
3469  {
3470  os << " " << n << std::endl;
3471 
3472  n = n->next_leaf;
3473  }
3474  }
3475 
3476 private:
3477  //! Recursively descend down the tree and print out nodes.
3478  static void print_node(std::ostream& os, const node* node,
3479  unsigned int depth = 0, bool recursive = false) {
3480  for (unsigned int i = 0; i < depth; i++) os << " ";
3481 
3482  os << "node " << node << " level " << node->level <<
3483  " slotuse " << node->slotuse << std::endl;
3484 
3485  if (node->is_leafnode())
3486  {
3487  const LeafNode* leafnode = static_cast<const LeafNode*>(node);
3488 
3489  for (unsigned int i = 0; i < depth; i++) os << " ";
3490  os << " leaf prev " << leafnode->prev_leaf <<
3491  " next " << leafnode->next_leaf << std::endl;
3492 
3493  for (unsigned int i = 0; i < depth; i++) os << " ";
3494 
3495  for (unsigned int slot = 0; slot < leafnode->slotuse; ++slot)
3496  {
3497  // os << leafnode->key(slot) << " "
3498  // << "(data: " << leafnode->slotdata[slot] << ") ";
3499  os << leafnode->key(slot) << " ";
3500  }
3501  os << std::endl;
3502  }
3503  else
3504  {
3505  const InnerNode* innernode = static_cast<const InnerNode*>(node);
3506 
3507  for (unsigned int i = 0; i < depth; i++) os << " ";
3508 
3509  for (unsigned short slot = 0; slot < innernode->slotuse; ++slot)
3510  {
3511  os << "(" << innernode->childid[slot] << ") "
3512  << innernode->slotkey[slot] << " ";
3513  }
3514  os << "(" << innernode->childid[innernode->slotuse] << ")"
3515  << std::endl;
3516 
3517  if (recursive)
3518  {
3519  for (unsigned short slot = 0;
3520  slot < innernode->slotuse + 1; ++slot)
3521  {
3522  print_node(
3523  os, innernode->childid[slot], depth + 1, recursive);
3524  }
3525  }
3526  }
3527  }
3528 
3529  //! \}
3530 #endif
3531 
3532 public:
3533  //! \name Verification of B+ Tree Invariants
3534  //! \{
3535 
3536  //! Run a thorough verification of all B+ tree invariants. The program
3537  //! aborts via tlx_die_unless() if something is wrong.
3538  void verify() const {
3539  key_type minkey, maxkey;
3540  tree_stats vstats;
3541 
3542  if (root_)
3543  {
3544  verify_node(root_, &minkey, &maxkey, vstats);
3545 
3546  tlx_die_unless(vstats.size == stats_.size);
3547  tlx_die_unless(vstats.leaves == stats_.leaves);
3548  tlx_die_unless(vstats.inner_nodes == stats_.inner_nodes);
3549 
3550  verify_leaflinks();
3551  }
3552  }
3553 
3554 private:
3555  //! Recursively descend down the tree and verify each node
3556  void verify_node(const node* n, key_type* minkey, key_type* maxkey,
3557  tree_stats& vstats) const {
3558  TLX_BTREE_PRINT("verifynode " << n);
3559 
3560  if (n->is_leafnode())
3561  {
3562  const LeafNode* leaf = static_cast<const LeafNode*>(n);
3563 
3564  tlx_die_unless(leaf == root_ || !leaf->is_underflow());
3565  tlx_die_unless(leaf->slotuse > 0);
3566 
3567  for (unsigned short slot = 0; slot < leaf->slotuse - 1; ++slot)
3568  {
3570  key_lessequal(leaf->key(slot), leaf->key(slot + 1)));
3571  }
3572 
3573  *minkey = leaf->key(0);
3574  *maxkey = leaf->key(leaf->slotuse - 1);
3575 
3576  vstats.leaves++;
3577  vstats.size += leaf->slotuse;
3578  }
3579  else // !n->is_leafnode()
3580  {
3581  const InnerNode* inner = static_cast<const InnerNode*>(n);
3582  vstats.inner_nodes++;
3583 
3584  tlx_die_unless(inner == root_ || !inner->is_underflow());
3585  tlx_die_unless(inner->slotuse > 0);
3586 
3587  for (unsigned short slot = 0; slot < inner->slotuse - 1; ++slot)
3588  {
3590  key_lessequal(inner->key(slot), inner->key(slot + 1)));
3591  }
3592 
3593  for (unsigned short slot = 0; slot <= inner->slotuse; ++slot)
3594  {
3595  const node* subnode = inner->childid[slot];
3596  key_type subminkey = key_type();
3597  key_type submaxkey = key_type();
3598 
3599  tlx_die_unless(subnode->level + 1 == inner->level);
3600  verify_node(subnode, &subminkey, &submaxkey, vstats);
3601 
3602  TLX_BTREE_PRINT("verify subnode " << subnode <<
3603  ": " << subminkey <<
3604  " - " << submaxkey);
3605 
3606  if (slot == 0)
3607  *minkey = subminkey;
3608  else
3610  key_greaterequal(subminkey, inner->key(slot - 1)));
3611 
3612  if (slot == inner->slotuse)
3613  *maxkey = submaxkey;
3614  else
3615  tlx_die_unless(key_equal(inner->key(slot), submaxkey));
3616 
3617  if (inner->level == 1 && slot < inner->slotuse)
3618  {
3619  // children are leaves and must be linked together in the
3620  // correct order
3621  const LeafNode* leafa = static_cast<const LeafNode*>(
3622  inner->childid[slot]);
3623  const LeafNode* leafb = static_cast<const LeafNode*>(
3624  inner->childid[slot + 1]);
3625 
3626  tlx_die_unless(leafa->next_leaf == leafb);
3627  tlx_die_unless(leafa == leafb->prev_leaf);
3628  }
3629  if (inner->level == 2 && slot < inner->slotuse)
3630  {
3631  // verify leaf links between the adjacent inner nodes
3632  const InnerNode* parenta = static_cast<const InnerNode*>(
3633  inner->childid[slot]);
3634  const InnerNode* parentb = static_cast<const InnerNode*>(
3635  inner->childid[slot + 1]);
3636 
3637  const LeafNode* leafa = static_cast<const LeafNode*>(
3638  parenta->childid[parenta->slotuse]);
3639  const LeafNode* leafb = static_cast<const LeafNode*>(
3640  parentb->childid[0]);
3641 
3642  tlx_die_unless(leafa->next_leaf == leafb);
3643  tlx_die_unless(leafa == leafb->prev_leaf);
3644  }
3645  }
3646  }
3647  }
3648 
3649  //! Verify the double linked list of leaves.
3650  void verify_leaflinks() const {
3651  const LeafNode* n = head_leaf_;
3652 
3653  tlx_die_unless(n->level == 0);
3654  tlx_die_unless(!n || n->prev_leaf == nullptr);
3655 
3656  unsigned int testcount = 0;
3657 
3658  while (n)
3659  {
3660  tlx_die_unless(n->level == 0);
3661  tlx_die_unless(n->slotuse > 0);
3662 
3663  for (unsigned short slot = 0; slot < n->slotuse - 1; ++slot)
3664  {
3665  tlx_die_unless(key_lessequal(n->key(slot), n->key(slot + 1)));
3666  }
3667 
3668  testcount += n->slotuse;
3669 
3670  if (n->next_leaf)
3671  {
3672  tlx_die_unless(key_lessequal(n->key(n->slotuse - 1),
3673  n->next_leaf->key(0)));
3674 
3675  tlx_die_unless(n == n->next_leaf->prev_leaf);
3676  }
3677  else
3678  {
3679  tlx_die_unless(tail_leaf_ == n);
3680  }
3681 
3682  n = n->next_leaf;
3683  }
3684 
3685  tlx_die_unless(testcount == size());
3686  }
3687 
3688  //! \}
3689 };
3690 
3691 //! \}
3692 //! \}
3693 
3694 } // namespace tlx
3695 
3696 #endif // !TLX_CONTAINER_BTREE_HEADER
3697 
3698 /******************************************************************************/
iterator begin()
Definition: btree.hpp:1341
iterator find(const key_type &key)
Definition: btree.hpp:1542
bool key_equal(const key_type &a, const key_type &b) const
Definition: btree.hpp:1221
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
tree_stats()
Zero initialized.
Definition: btree.hpp:1054
static const bool allow_duplicates
Definition: btree.hpp:150
void swap(BTree &from)
Fast swapping of two identical B+ tree objects.
Definition: btree.hpp:1144
bool operator!=(const iterator &x) const
Inequality of iterators.
Definition: btree.hpp:501
reverse_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:746
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:874
Alloc_::template rebind< LeafNode >::other alloc_type
Define an related allocator for the LeafNode structs.
Definition: btree.hpp:275
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:581
iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:424
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:757
result_flags_t flags
Merged result flags.
Definition: btree.hpp:2324
const_reverse_iterator & operator--()
Prefix– backstep the iterator to the next slot.
Definition: btree.hpp:981
const BTree::LeafNode * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:538
void erase(iterator iter)
Erase the key/data pair referenced by the iterator.
Definition: btree.hpp:2402
BTree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:514
size_type max_size() const
Definition: btree.hpp:1505
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:751
struct node * copy_recursive(const node *n)
Recursively copy nodes from another B+ tree object.
Definition: btree.hpp:1796
LeafNode * prev_leaf
Double linked list pointers to traverse the leaves.
Definition: btree.hpp:278
static const int inner_slots
Definition: btree.hpp:93
static const bool debug
Definition: btree.hpp:203
bool operator==(const const_iterator &x) const
Equality of iterators.
Definition: btree.hpp:668
const_iterator(const typename BTree::LeafNode *l, unsigned short s)
Initializing-Constructor of a const iterator.
Definition: btree.hpp:561
const value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:523
BTree(const key_compare &kcf, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1110
void verify() const
Definition: btree.hpp:3538
const_iterator begin() const
Definition: btree.hpp:1353
BTree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:686
InnerNode * allocate_inner(unsigned short level)
Allocate and initialize an inner node.
Definition: btree.hpp:1261
const_iterator find(const key_type &key) const
Definition: btree.hpp:1563
static const bool self_verify
Definition: btree.hpp:198
size_type inner_nodes
Number of inner nodes in the B+ tree.
Definition: btree.hpp:1045
bool operator<=(const BTree &other) const
Less-equal relation. Based on operator<.
Definition: btree.hpp:1739
BTree & operator=(const BTree &other)
Assignment operator. All the key/data pairs are copied.
Definition: btree.hpp:1755
BTree(const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1103
void initialize()
Set variables to initial values.
Definition: btree.hpp:287
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 upper_bound(const key_type &key) const
Definition: btree.hpp:1676
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:351
allocator_type allocator_
Memory allocator.
Definition: btree.hpp:1093
BTree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:339
bool erase_one(const key_type &key)
Definition: btree.hpp:2365
void free_node(node *n)
Definition: btree.hpp:1270
allocator_type get_allocator() const
Return the base node allocator provided during construction.
Definition: btree.hpp:1232
bool operator==(const reverse_iterator &x) const
Equality of iterators.
Definition: btree.hpp:841
Alloc_::template rebind< InnerNode >::other alloc_type
Define an related allocator for the InnerNode structs.
Definition: btree.hpp:237
A small struct containing basic statistics about the B+ tree.
Definition: btree.hpp:1037
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:939
void initialize(const unsigned short l)
Delayed initialisation of constructed node.
Definition: btree.hpp:222
BTree(InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1120
size_type size() const
Return the number of key/data pairs in the B+ tree.
Definition: btree.hpp:1494
const_reverse_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:912
static const unsigned short inner_slots
Base B+ tree parameter: The number of key slots in each inner node.
Definition: btree.hpp:1051
value_compare value_comp() const
Definition: btree.hpp:1189
const value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:868
const_iterator(const iterator &it)
Copy-constructor from a mutable iterator.
Definition: btree.hpp:566
bool exists(const key_type &key) const
Definition: btree.hpp:1522
size_t size_type
Size type used to count keys.
Definition: btree.hpp:171
static void shift_left_inner(InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
Definition: btree.hpp:3261
bool empty() const
Returns true if there is at least one key/data pair in the B+ tree.
Definition: btree.hpp:1499
node * childid[inner_slotmax+1]
Pointers to children.
Definition: btree.hpp:243
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:763
const BTree::LeafNode * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:883
const_reverse_iterator rend() const
Definition: btree.hpp:1383
unsigned short level
Level in the b-tree, if level == 0 -> leaf node.
Definition: btree.hpp:215
const_iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:596
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:591
unsigned short curr_slot
One slot past the current key/data slot referenced.
Definition: btree.hpp:886
unsigned short slotuse
Definition: btree.hpp:219
bool operator==(const BTree &other) const
Definition: btree.hpp:1716
value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:345
bool is_full() const
True if the node's slots are full.
Definition: btree.hpp:256
bool is_underflow() const
True if node has too few entries.
Definition: btree.hpp:266
const_iterator(const reverse_iterator &it)
Copy-constructor from a mutable reverse iterator.
Definition: btree.hpp:571
bool operator>(const BTree &other) const
Greater relation. Based on operator<.
Definition: btree.hpp:1734
#define TLX_BTREE_MAX(a, b)
The maximum of a and b. Used in some compile-time formulas.
Definition: btree.hpp:60
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
bool key_greater(const key_type &a, const key_type &b) const
True if a > b ? constructed from key_less()
Definition: btree.hpp:1210
const value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:520
void split_leaf_node(LeafNode *leaf, key_type *out_newkey, node **out_newleaf)
Definition: btree.hpp:2071
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
static ssize_t get(const CounterType &a)
result_t & operator|=(const result_t &other)
Merge two results OR-ing the result flags and overwriting lastkeys.
Definition: btree.hpp:2346
BTree(InputIterator first, InputIterator last, const key_compare &kcf, const allocator_type &alloc=allocator_type())
Definition: btree.hpp:1131
void bulk_load(Iterator ibegin, Iterator iend)
Definition: btree.hpp:2152
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:701
void clear()
Frees all key/data pairs and all nodes of the tree.
Definition: btree.hpp:1294
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:698
value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:348
InnerNode::alloc_type inner_node_allocator()
Return an allocator for InnerNode objects.
Definition: btree.hpp:1248
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_iterator lower_bound(const key_type &key) const
Definition: btree.hpp:1636
reverse_iterator rend()
Definition: btree.hpp:1371
std::vector< std::string > split(char sep, const std::string &str, std::string::size_type limit)
Split the given string at each separator character into distinct substrings.
Definition: split.cpp:20
bool operator>=(const BTree &other) const
Greater-equal relation. Based on operator<.
Definition: btree.hpp:1744
void initialize(const unsigned short l)
Set variables to initial values.
Definition: btree.hpp:246
LeafNode * allocate_leaf()
Allocate and initialize a leaf node.
Definition: btree.hpp:1253
key_type slotkey[inner_slotmax]
Keys of children or data pointers.
Definition: btree.hpp:240
reverse_iterator rbegin()
Definition: btree.hpp:1365
BTree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:342
size_type count(const key_type &key) const
Definition: btree.hpp:1584
double avgfill_leaves() const
Return the average fill of leaves.
Definition: btree.hpp:1065
reverse_iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:805
static void shift_right_leaf(LeafNode *left, LeafNode *right, InnerNode *parent, unsigned int parentslot)
Definition: btree.hpp:3326
result_t erase_one_descend(const key_type &key, node *curr, node *left, node *right, InnerNode *left_parent, InnerNode *right_parent, InnerNode *parent, unsigned int parentslot)
Erase one (the first) key/data pair in the B+ tree matching key.
Definition: btree.hpp:2446
BTree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:689
static result_t shift_left_leaf(LeafNode *left, LeafNode *right, InnerNode *parent, unsigned int parentslot)
Definition: btree.hpp:3212
bool is_underflow() const
True if node has too few entries.
Definition: btree.hpp:308
bool is_few() const
True if few used entries, less than half full.
Definition: btree.hpp:261
Deletion not successful because key was not found.
Definition: btree.hpp:2309
const key_type & key() const
Key of the current slot.
Definition: btree.hpp:419
node * root_
Pointer to the B+ tree's root node, either leaf or inner node.
Definition: btree.hpp:1077
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 leaves
Number of leaves in the B+ tree.
Definition: btree.hpp:1042
size_type nodes() const
Return the total number of nodes.
Definition: btree.hpp:1060
list x
Definition: gen_data.py:39
LeafNode::alloc_type leaf_node_allocator()
Return an allocator for LeafNode objects.
Definition: btree.hpp:1243
int value
Definition: gen_data.py:41
BTree::LeafNode * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:710
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:529
Deletion successful and no fix-ups necessary.
Definition: btree.hpp:2306
key_compare key_less_
Definition: btree.hpp:1090
static void shift_right_inner(InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
Definition: btree.hpp:3382
result_t(result_flags_t f=btree_ok)
Definition: btree.hpp:2331
bool key_greaterequal(const key_type &a, const key_type &b) const
True if a >= b ? constructed from key_less()
Definition: btree.hpp:1215
iterator end()
Definition: btree.hpp:1347
iterator(typename BTree::LeafNode *l, unsigned short s)
Initializing-Constructor of a mutable iterator.
Definition: btree.hpp:399
iterator()
Default-Constructor of a mutable iterator.
Definition: btree.hpp:394
const_iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:632
bool is_few() const
True if few used entries, less than half full.
Definition: btree.hpp:303
LeafNode * next_leaf
Double linked list pointers to traverse the leaves.
Definition: btree.hpp:281
value_type * pointer
Pointer to the value_type. STL required.
Definition: btree.hpp:695
static const unsigned short leaf_slotmax
Base B+ tree parameter: The number of key/data slots in each leaf.
Definition: btree.hpp:180
static const size_t binsearch_threshold
Definition: btree.hpp:100
static const bool debug
Definition: btree.hpp:84
Key_ key_type
Definition: btree.hpp:132
const key_type & key(size_t s) const
Return key in slot s.
Definition: btree.hpp:251
bool key_lessequal(const key_type &a, const key_type &b) const
True if a <= b ? constructed from key_less()
Definition: btree.hpp:1205
static constexpr bool debug
int find_upper(const node_type *n, const key_type &key) const
Definition: btree.hpp:1445
static const unsigned short leaf_slotmin
Definition: btree.hpp:189
void clear_recursive(node *n)
Recursively free up nodes.
Definition: btree.hpp:1311
LeafNode * head_leaf_
Pointer to first leaf in the double linked leaf chain.
Definition: btree.hpp:1080
Basic class implementing a B+ tree data structure in memory.
Definition: btree.hpp:124
Function class to compare value_type objects. Required by the STL.
Definition: btree.hpp:1160
iterator upper_bound(const key_type &key)
Definition: btree.hpp:1656
const key_type & key(size_t s) const
Return key in slot s.
Definition: btree.hpp:293
Compare_ key_compare
Fourth template parameter: key_type comparison function object.
Definition: btree.hpp:142
result_t(result_flags_t f, const key_type &k)
Constructor with a lastkey value.
Definition: btree.hpp:2336
const_iterator()
Default-Constructor of a const iterator.
Definition: btree.hpp:556
static const unsigned short inner_slotmin
Definition: btree.hpp:194
unsigned short curr_slot
One slot past the current key/data slot referenced.
Definition: btree.hpp:713
const struct tree_stats & get_stats() const
Return a const reference to the current statistics.
Definition: btree.hpp:1510
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:414
bool operator!=(const const_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:673
bool operator!=(const const_reverse_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:1022
const_reverse_iterator & operator++()
Prefix++ advance the iterator to the previous slot.
Definition: btree.hpp:945
reverse_iterator & operator++()
Prefix++ advance the iterator to the next slot.
Definition: btree.hpp:769
unsigned short curr_slot
Current key/data slot referenced.
Definition: btree.hpp:541
void insert(InputIterator first, InputIterator last)
Definition: btree.hpp:1860
void verify_leaflinks() const
Verify the double linked list of leaves.
Definition: btree.hpp:3650
reverse_iterator(typename BTree::LeafNode *l, unsigned short s)
Initializing-Constructor of a mutable reverse iterator.
Definition: btree.hpp:741
bool operator==(const iterator &x) const
Equality of iterators.
Definition: btree.hpp:496
Alloc_ allocator_type
Seventh template parameter: STL allocator for tree nodes.
Definition: btree.hpp:153
BTree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:862
LeafNode * tail_leaf_
Pointer to last leaf in the double linked leaf chain.
Definition: btree.hpp:1083
value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:692
std::pair< iterator, bool > insert(const value_type &x)
Definition: btree.hpp:1846
static const unsigned short inner_slotmax
Definition: btree.hpp:184
size_type size
Number of items in the B+ tree.
Definition: btree.hpp:1039
result_t erase_iter_descend(const iterator &iter, node *curr, node *left, node *right, InnerNode *left_parent, InnerNode *right_parent, InnerNode *parent, unsigned int parentslot)
Erase one key/data pair referenced by an iterator in the B+ tree.
Definition: btree.hpp:2776
#define TLX_BTREE_ASSERT(x)
Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
Definition: btree.hpp:55
key_compare key_comp() const
Constant access to the key comparison object sorting the B+ tree.
Definition: btree.hpp:1183
int find_lower(const node_type *n, const key_type &key) const
Definition: btree.hpp:1398
Value_ value_type
Definition: btree.hpp:136
bool operator<(const BTree &other) const
Definition: btree.hpp:1728
reverse_iterator()
Default-Constructor of a reverse iterator.
Definition: btree.hpp:736
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:927
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:933
const_reverse_iterator()
Default-Constructor of a const reverse iterator.
Definition: btree.hpp:901
key_type lastkey
The key to be updated at the parent's slot.
Definition: btree.hpp:2327
pointer operator->() const
Dereference the iterator.
Definition: btree.hpp:586
iterator insert(iterator, const value_type &x)
Definition: btree.hpp:1852
~BTree()
Frees up all used B+ tree memory pages.
Definition: btree.hpp:1139
size_type erase(const key_type &key)
Definition: btree.hpp:2389
Generates default traits for a B+ tree used as a set or map.
Definition: btree.hpp:74
iterator lower_bound(const key_type &key)
Definition: btree.hpp:1616
bool operator!=(const BTree &other) const
Inequality relation. Based on operator==.
Definition: btree.hpp:1722
static const int leaf_slots
Definition: btree.hpp:88
iterator & operator--()
Prefix– backstep the iterator to the last slot.
Definition: btree.hpp:460
void split_inner_node(InnerNode *inner, key_type *out_newkey, node **out_newinner, unsigned int addslot)
Definition: btree.hpp:2107
ptrdiff_t difference_type
STL-magic.
Definition: btree.hpp:354
result_t merge_leaves(LeafNode *left, LeafNode *right, InnerNode *parent)
Definition: btree.hpp:3136
static const bool self_verify
Definition: btree.hpp:78
value_compare(key_compare kc)
Constructor called from BTree::value_comp()
Definition: btree.hpp:1167
BTree< key_type, value_type, key_of_value, key_compare, traits, allow_duplicates, allocator_type > Self
Typedef of our own type.
Definition: btree.hpp:168
static const unsigned short leaf_slots
Base B+ tree parameter: The number of key/data slots in each leaf.
Definition: btree.hpp:1048
const value_type & reference
Reference to the value_type. STL required.
Definition: btree.hpp:865
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:526
bool is_full() const
True if the node's slots are full.
Definition: btree.hpp:298
const_reverse_iterator rbegin() const
Definition: btree.hpp:1377
bool key_less(const key_type &a, const key_type &b) const
True if a < b ? "constructed" from key_less_()
Definition: btree.hpp:1200
bool operator==(const const_reverse_iterator &x) const
Equality of iterators.
Definition: btree.hpp:1017
std::pair< iterator, bool > insert_start(const key_type &key, const value_type &value)
Definition: btree.hpp:1878
BTree::value_type value_type
The value type of the btree. Returned by operator*().
Definition: btree.hpp:517
static result_t merge_inner(InnerNode *left, InnerNode *right, InnerNode *parent, unsigned int parentslot)
Definition: btree.hpp:3166
#define TLX_BTREE_FRIENDS
Definition: btree.hpp:66
const_reverse_iterator(const const_iterator &it)
Copy-constructor from a const iterator.
Definition: btree.hpp:917
Traits_ traits
Definition: btree.hpp:146
BTree::LeafNode * curr_leaf
The currently referenced leaf node of the tree.
Definition: btree.hpp:363
value_type slotdata[leaf_slotmax]
Array of (key, data) pairs.
Definition: btree.hpp:284
iterator(const reverse_iterator &it)
Copy-constructor from a reverse iterator.
Definition: btree.hpp:404
bool operator!=(const reverse_iterator &x) const
Inequality of iterators.
Definition: btree.hpp:846
unsigned short curr_slot
Current key/data slot referenced.
Definition: btree.hpp:366
void set_slot(unsigned short slot, const value_type &value)
Definition: btree.hpp:314
BTree(const BTree &other)
Definition: btree.hpp:1779
const_reverse_iterator(const typename BTree::LeafNode *l, unsigned short s)
Initializing-Constructor of a const reverse iterator.
Definition: btree.hpp:906
reference operator*() const
Dereference the iterator.
Definition: btree.hpp:409
bool has(result_flags_t f) const
Test if this result object has a given flag set.
Definition: btree.hpp:2341
key_compare key_comp
Key comparison function from the template parameter.
Definition: btree.hpp:1164
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:3556
#define tlx_die_unless(X)
Definition: core.hpp:64
BTree::key_type key_type
The key type of the btree. Returned by key().
Definition: btree.hpp:859
bool is_leafnode() const
True if this is a leaf node.
Definition: btree.hpp:228
tree_stats stats_
Other small statistics about the B+ tree.
Definition: btree.hpp:1086
std::bidirectional_iterator_tag iterator_category
STL-magic iterator category.
Definition: btree.hpp:871
const_iterator(const const_reverse_iterator &it)
Copy-constructor from a const reverse iterator.
Definition: btree.hpp:576
const_reverse_iterator(const reverse_iterator &it)
Copy-constructor from a mutable reverse iterator.
Definition: btree.hpp:922