forked from Haozh20/dsrw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.h
3410 lines (2695 loc) · 109 KB
/
btree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* tlx/container/btree.hpp
*
* Part of tlx - http://panthema.net/tlx
*
* Copyright (C) 2008-2017 Timo Bingmann <[email protected]>
*
* All rights reserved. Published under the Boost Software License, Version 1.0
******************************************************************************/
#ifndef TLX_CONTAINER_BTREE_HEADER
#define TLX_CONTAINER_BTREE_HEADER
#include "die.h"
// *** Required Headers from the STL
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <functional>
#include <istream>
#include <memory>
#include <ostream>
#include <utility>
namespace tlx {
//! \addtogroup tlx_container
//! \{
//! \defgroup tlx_container_btree B+ Trees
//! B+ tree variants
//! \{
// *** Debugging Macros
#ifdef TLX_BTREE_DEBUG
#include <iostream>
//! Print out debug information to std::cout if TLX_BTREE_DEBUG is defined.
#define TLX_BTREE_PRINT(x) \
do { \
if (debug) \
(std::cout << x << std::endl); \
} while (0)
//! Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
#define TLX_BTREE_ASSERT(x) \
do { \
assert(x); \
} while (0)
#else
//! Print out debug information to std::cout if TLX_BTREE_DEBUG is defined.
#define TLX_BTREE_PRINT(x) \
do { \
} while (0)
//! Assertion only if TLX_BTREE_DEBUG is defined. This is not used in verify().
#define TLX_BTREE_ASSERT(x) \
do { \
} while (0)
#endif
//! The maximum of a and b. Used in some compile-time formulas.
#define TLX_BTREE_MAX(a, b) ((a) < (b) ? (b) : (a))
#ifndef TLX_BTREE_FRIENDS
//! The macro TLX_BTREE_FRIENDS can be used by outside class to access the B+
//! tree internals. This was added for wxBTreeDemo to be able to draw the
//! tree.
#define TLX_BTREE_FRIENDS friend class btree_friend
#endif
/*!
* Generates default traits for a B+ tree used as a set or map. It estimates
* leaf and inner node sizes by assuming a cache line multiple of 256 bytes.
*/
template <typename Key, typename Value> struct btree_default_traits {
//! If true, the tree will self verify its invariants after each insert() or
//! erase(). The header must have been compiled with TLX_BTREE_DEBUG
//! defined.
static const bool self_verify = false;
//! If true, the tree will print out debug information and a tree dump
//! during insert() or erase() operation. The header must have been
//! compiled with TLX_BTREE_DEBUG defined and key_type must be std::ostream
//! printable.
static const bool debug = false;
//! Number of slots in each leaf of the tree. Estimated so that each node
//! has a size of about 256 bytes.
static const int leaf_slots = TLX_BTREE_MAX(8, 256 / (sizeof(Value)));
//! Number of slots in each inner node of the tree. Estimated so that each
//! node has a size of about 256 bytes.
static const int inner_slots =
TLX_BTREE_MAX(8, 256 / (sizeof(Key) + sizeof(void *)));
//! As of stx-btree-0.9, the code does linear search in find_lower() and
//! find_upper() instead of binary_search, unless the node size is larger
//! than this threshold. See notes at
//! http://panthema.net/2013/0504-STX-B+Tree-Binary-vs-Linear-Search
static const size_t binsearch_threshold = 256;
};
/*!
* Basic class implementing a B+ tree data structure in memory.
*
* The base implementation of an in-memory B+ tree. It is based on the
* implementation in Cormen's Introduction into Algorithms, Jan Jannink's paper
* and other algorithm resources. Almost all STL-required function calls are
* implemented. The asymptotic time requirements of the STL are not always
* fulfilled in theory, however, in practice this B+ tree performs better than a
* red-black tree and almost always uses less memory. The insertion function
* splits the nodes on the recursion unroll. Erase is largely based on Jannink's
* ideas.
*
* This class is specialized into btree_set, btree_multiset, btree_map and
* btree_multimap using default template parameters and facade functions.
*/
template <typename Key, typename Value, typename KeyOfValue,
typename Compare = std::less<Key>,
typename Traits = btree_default_traits<Key, Value>,
bool Duplicates = false, typename Allocator = std::allocator<Value>>
class BTree {
public:
//! \name Template Parameter Types
//! \{
//! First template parameter: The key type of the B+ tree. This is stored in
//! inner nodes.
typedef Key key_type;
//! Second template parameter: Composition pair of key and data types, or
//! just the key for set containers. This data type is stored in the leaves.
typedef Value value_type;
//! Third template: key extractor class to pull key_type from value_type.
typedef KeyOfValue key_of_value;
//! Fourth template parameter: key_type comparison function object
typedef Compare key_compare;
//! Fifth template parameter: Traits object used to define more parameters
//! of the B+ tree
typedef Traits traits;
//! Sixth template parameter: Allow duplicate keys in the B+ tree. Used to
//! implement multiset and multimap.
static const bool allow_duplicates = Duplicates;
//! Seventh template parameter: STL allocator for tree nodes
typedef Allocator allocator_type;
//! \}
// The macro TLX_BTREE_FRIENDS can be used by outside class to access the B+
// tree internals. This was added for wxBTreeDemo to be able to draw the
// tree.
TLX_BTREE_FRIENDS;
public:
//! \name Constructed Types
//! \{
//! Typedef of our own type
typedef BTree<key_type, value_type, key_of_value, key_compare, traits,
allow_duplicates, allocator_type>
Self;
//! Size type used to count keys
typedef size_t size_type;
//! \}
public:
//! \name Static Constant Options and Values of the B+ Tree
//! \{
//! Base B+ tree parameter: The number of key/data slots in each leaf
static const unsigned short leaf_slotmax = traits::leaf_slots;
//! Base B+ tree parameter: The number of key slots in each inner node,
//! this can differ from slots in each leaf.
static const unsigned short inner_slotmax = traits::inner_slots;
//! Computed B+ tree parameter: The minimum number of key/data slots used
//! in a leaf. If fewer slots are used, the leaf will be merged or slots
//! shifted from it's siblings.
static const unsigned short leaf_slotmin = (leaf_slotmax / 2);
//! Computed B+ tree parameter: The minimum number of key slots used
//! in an inner node. If fewer slots are used, the inner node will be
//! merged or slots shifted from it's siblings.
static const unsigned short inner_slotmin = (inner_slotmax / 2);
//! Debug parameter: Enables expensive and thorough checking of the B+ tree
//! invariants after each insert/erase operation.
static const bool self_verify = traits::self_verify;
//! Debug parameter: Prints out lots of debug information about how the
//! algorithms change the tree. Requires the header file to be compiled
//! with TLX_BTREE_DEBUG and the key type must be std::ostream printable.
static const bool debug = traits::debug;
//! \}
private:
//! \name Node Classes for In-Memory Nodes
//! \{
//! The header structure of each node in-memory. This structure is extended
//! by InnerNode or LeafNode.
struct node {
//! Level in the b-tree, if level == 0 -> leaf node
unsigned short level;
unsigned int subtsize;
//! Number of key slotuse use, so the number of valid children or data
//! pointers
unsigned short slotuse;
//! Delayed initialisation of constructed node.
void initialize(const unsigned short l) {
level = l;
slotuse = 0;
subtsize = 0;
}
//! True if this is a leaf node.
bool is_leafnode() const { return (level == 0); }
};
//! Extended structure of a inner node in-memory. Contains only keys and no
//! data items.
struct InnerNode : public node {
//! Define an related allocator for the InnerNode structs.
typedef typename Allocator::template rebind<InnerNode>::other alloc_type;
//! Keys of children or data pointers
key_type slotkey[inner_slotmax]; // NOLINT
//! Pointers to children
node *childid[inner_slotmax + 1]; // NOLINT
//! Set variables to initial values.
void initialize(const unsigned short l) { node::initialize(l); }
//! Return key in slot s
const key_type &key(size_t s) const { return slotkey[s]; }
//! True if the node's slots are full.
bool is_full() const { return (node::slotuse == inner_slotmax); }
//! True if few used entries, less than half full.
bool is_few() const { return (node::slotuse <= inner_slotmin); }
//! True if node has too few entries.
bool is_underflow() const { return (node::slotuse < inner_slotmin); }
void update_subtsize(){
node::subtsize=0;
for(int i=0;i<=node::slotuse;++i){
node::subtsize+=childid[i]->subtsize;
}
}
};
//! Extended structure of a leaf node in memory. Contains pairs of keys and
//! data items. Key and data slots are kept together in value_type.
struct LeafNode : public node {
//! Define an related allocator for the LeafNode structs.
typedef typename Allocator::template rebind<LeafNode>::other alloc_type;
//! Double linked list pointers to traverse the leaves
LeafNode *prev_leaf;
//! Double linked list pointers to traverse the leaves
LeafNode *next_leaf;
//! Array of (key, data) pairs
value_type slotdata[leaf_slotmax]; // NOLINT
//! Set variables to initial values
void initialize() {
node::initialize(0);
prev_leaf = next_leaf = nullptr;
}
//! Return key in slot s.
const key_type &key(size_t s) const {
return key_of_value::get(slotdata[s]);
}
//! True if the node's slots are full.
bool is_full() const { return (node::slotuse == leaf_slotmax); }
//! True if few used entries, less than half full.
bool is_few() const { return (node::slotuse <= leaf_slotmin); }
//! True if node has too few entries.
bool is_underflow() const { return (node::slotuse < leaf_slotmin); }
//! Set the (key,data) pair in slot. Overloaded function used by
//! bulk_load().
void set_slot(unsigned short slot, const value_type &value) {
TLX_BTREE_ASSERT(slot < node::slotuse);
slotdata[slot] = value;
}
};
//! \}
public:
//! \name Iterators and Reverse Iterators
//! \{
class iterator;
class const_iterator;
class reverse_iterator;
class const_reverse_iterator;
//! STL-like iterator object for B+ tree items. The iterator points to a
//! specific slot number in a leaf.
class iterator {
public:
// *** Types
//! The key type of the btree. Returned by key().
typedef typename BTree::key_type key_type;
//! The value type of the btree. Returned by operator*().
typedef typename BTree::value_type value_type;
//! Reference to the value_type. STL required.
typedef value_type &reference;
//! Pointer to the value_type. STL required.
typedef value_type *pointer;
//! STL-magic iterator category
typedef std::bidirectional_iterator_tag iterator_category;
//! STL-magic
typedef ptrdiff_t difference_type;
//! Our own type
typedef iterator self;
private:
// *** Members
//! The currently referenced leaf node of the tree
typename BTree::LeafNode *curr_leaf;
//! Current key/data slot referenced
unsigned short curr_slot;
//! Friendly to the const_iterator, so it may access the two data items
//! directly.
friend class const_iterator;
//! Also friendly to the reverse_iterator, so it may access the two
//! data items directly.
friend class reverse_iterator;
//! Also friendly to the const_reverse_iterator, so it may access the
//! two data items directly.
friend class const_reverse_iterator;
//! Also friendly to the base btree class, because erase_iter() needs
//! to read the curr_leaf and curr_slot values directly.
friend class BTree<key_type, value_type, key_of_value, key_compare, traits,
allow_duplicates, allocator_type>;
// The macro TLX_BTREE_FRIENDS can be used by outside class to access
// the B+ tree internals. This was added for wxBTreeDemo to be able to
// draw the tree.
TLX_BTREE_FRIENDS;
public:
// *** Methods
//! Default-Constructor of a mutable iterator
iterator() : curr_leaf(nullptr), curr_slot(0) {}
//! Initializing-Constructor of a mutable iterator
iterator(typename BTree::LeafNode *l, unsigned short s)
: curr_leaf(l), curr_slot(s) {}
//! Copy-constructor from a reverse iterator
iterator(const reverse_iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Dereference the iterator.
reference operator*() const { return curr_leaf->slotdata[curr_slot]; }
//! Dereference the iterator.
pointer operator->() const { return &curr_leaf->slotdata[curr_slot]; }
//! Key of the current slot.
const key_type &key() const { return curr_leaf->key(curr_slot); }
//! Prefix++ advance the iterator to the next slot.
iterator &operator++() {
if (curr_slot + 1u < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 0;
} else {
// this is end()
curr_slot = curr_leaf->slotuse;
}
return *this;
}
//! Postfix++ advance the iterator to the next slot.
iterator operator++(int) {
iterator tmp = *this; // copy ourselves
if (curr_slot + 1u < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 0;
} else {
// this is end()
curr_slot = curr_leaf->slotuse;
}
return tmp;
}
//! Prefix-- backstep the iterator to the last slot.
iterator &operator--() {
if (curr_slot > 0) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse - 1;
} else {
// this is begin()
curr_slot = 0;
}
return *this;
}
//! Postfix-- backstep the iterator to the last slot.
iterator operator--(int) {
iterator tmp = *this; // copy ourselves
if (curr_slot > 0) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse - 1;
} else {
// this is begin()
curr_slot = 0;
}
return tmp;
}
//! Equality of iterators.
bool operator==(const iterator &x) const {
return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
}
//! Inequality of iterators.
bool operator!=(const iterator &x) const {
return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
}
};
//! STL-like read-only iterator object for B+ tree items. The iterator
//! points to a specific slot number in a leaf.
class const_iterator {
public:
// *** Types
//! The key type of the btree. Returned by key().
typedef typename BTree::key_type key_type;
//! The value type of the btree. Returned by operator*().
typedef typename BTree::value_type value_type;
//! Reference to the value_type. STL required.
typedef const value_type &reference;
//! Pointer to the value_type. STL required.
typedef const value_type *pointer;
//! STL-magic iterator category
typedef std::bidirectional_iterator_tag iterator_category;
//! STL-magic
typedef ptrdiff_t difference_type;
//! Our own type
typedef const_iterator self;
private:
// *** Members
//! The currently referenced leaf node of the tree
const typename BTree::LeafNode *curr_leaf;
//! Current key/data slot referenced
unsigned short curr_slot;
//! Friendly to the reverse_const_iterator, so it may access the two
//! data items directly
friend class const_reverse_iterator;
// The macro TLX_BTREE_FRIENDS can be used by outside class to access
// the B+ tree internals. This was added for wxBTreeDemo to be able to
// draw the tree.
TLX_BTREE_FRIENDS;
public:
// *** Methods
//! Default-Constructor of a const iterator
const_iterator() : curr_leaf(nullptr), curr_slot(0) {}
//! Initializing-Constructor of a const iterator
const_iterator(const typename BTree::LeafNode *l, unsigned short s)
: curr_leaf(l), curr_slot(s) {}
//! Copy-constructor from a mutable iterator
const_iterator(const iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Copy-constructor from a mutable reverse iterator
const_iterator(const reverse_iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Copy-constructor from a const reverse iterator
const_iterator(const const_reverse_iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Dereference the iterator.
reference operator*() const { return curr_leaf->slotdata[curr_slot]; }
//! Dereference the iterator.
pointer operator->() const { return &curr_leaf->slotdata[curr_slot]; }
//! Key of the current slot.
const key_type &key() const { return curr_leaf->key(curr_slot); }
//! Prefix++ advance the iterator to the next slot.
const_iterator &operator++() {
if (curr_slot + 1u < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 0;
} else {
// this is end()
curr_slot = curr_leaf->slotuse;
}
return *this;
}
//! Postfix++ advance the iterator to the next slot.
const_iterator operator++(int) {
const_iterator tmp = *this; // copy ourselves
if (curr_slot + 1u < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 0;
} else {
// this is end()
curr_slot = curr_leaf->slotuse;
}
return tmp;
}
//! Prefix-- backstep the iterator to the last slot.
const_iterator &operator--() {
if (curr_slot > 0) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse - 1;
} else {
// this is begin()
curr_slot = 0;
}
return *this;
}
//! Postfix-- backstep the iterator to the last slot.
const_iterator operator--(int) {
const_iterator tmp = *this; // copy ourselves
if (curr_slot > 0) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse - 1;
} else {
// this is begin()
curr_slot = 0;
}
return tmp;
}
//! Equality of iterators.
bool operator==(const const_iterator &x) const {
return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
}
//! Inequality of iterators.
bool operator!=(const const_iterator &x) const {
return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
}
};
//! STL-like mutable reverse iterator object for B+ tree items. The
//! iterator points to a specific slot number in a leaf.
class reverse_iterator {
public:
// *** Types
//! The key type of the btree. Returned by key().
typedef typename BTree::key_type key_type;
//! The value type of the btree. Returned by operator*().
typedef typename BTree::value_type value_type;
//! Reference to the value_type. STL required.
typedef value_type &reference;
//! Pointer to the value_type. STL required.
typedef value_type *pointer;
//! STL-magic iterator category
typedef std::bidirectional_iterator_tag iterator_category;
//! STL-magic
typedef ptrdiff_t difference_type;
//! Our own type
typedef reverse_iterator self;
private:
// *** Members
//! The currently referenced leaf node of the tree
typename BTree::LeafNode *curr_leaf;
//! One slot past the current key/data slot referenced.
unsigned short curr_slot;
//! Friendly to the const_iterator, so it may access the two data items
//! directly
friend class iterator;
//! Also friendly to the const_iterator, so it may access the two data
//! items directly
friend class const_iterator;
//! Also friendly to the const_iterator, so it may access the two data
//! items directly
friend class const_reverse_iterator;
// The macro TLX_BTREE_FRIENDS can be used by outside class to access
// the B+ tree internals. This was added for wxBTreeDemo to be able to
// draw the tree.
TLX_BTREE_FRIENDS;
public:
// *** Methods
//! Default-Constructor of a reverse iterator
reverse_iterator() : curr_leaf(nullptr), curr_slot(0) {}
//! Initializing-Constructor of a mutable reverse iterator
reverse_iterator(typename BTree::LeafNode *l, unsigned short s)
: curr_leaf(l), curr_slot(s) {}
//! Copy-constructor from a mutable iterator
reverse_iterator(const iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Dereference the iterator.
reference operator*() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return curr_leaf->slotdata[curr_slot - 1];
}
//! Dereference the iterator.
pointer operator->() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return &curr_leaf->slotdata[curr_slot - 1];
}
//! Key of the current slot.
const key_type &key() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return curr_leaf->key(curr_slot - 1);
}
//! Prefix++ advance the iterator to the next slot.
reverse_iterator &operator++() {
if (curr_slot > 1) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse;
} else {
// this is begin() == rend()
curr_slot = 0;
}
return *this;
}
//! Postfix++ advance the iterator to the next slot.
reverse_iterator operator++(int) {
reverse_iterator tmp = *this; // copy ourselves
if (curr_slot > 1) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse;
} else {
// this is begin() == rend()
curr_slot = 0;
}
return tmp;
}
//! Prefix-- backstep the iterator to the last slot.
reverse_iterator &operator--() {
if (curr_slot < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 1;
} else {
// this is end() == rbegin()
curr_slot = curr_leaf->slotuse;
}
return *this;
}
//! Postfix-- backstep the iterator to the last slot.
reverse_iterator operator--(int) {
reverse_iterator tmp = *this; // copy ourselves
if (curr_slot < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 1;
} else {
// this is end() == rbegin()
curr_slot = curr_leaf->slotuse;
}
return tmp;
}
//! Equality of iterators.
bool operator==(const reverse_iterator &x) const {
return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
}
//! Inequality of iterators.
bool operator!=(const reverse_iterator &x) const {
return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
}
};
//! STL-like read-only reverse iterator object for B+ tree items. The
//! iterator points to a specific slot number in a leaf.
class const_reverse_iterator {
public:
// *** Types
//! The key type of the btree. Returned by key().
typedef typename BTree::key_type key_type;
//! The value type of the btree. Returned by operator*().
typedef typename BTree::value_type value_type;
//! Reference to the value_type. STL required.
typedef const value_type &reference;
//! Pointer to the value_type. STL required.
typedef const value_type *pointer;
//! STL-magic iterator category
typedef std::bidirectional_iterator_tag iterator_category;
//! STL-magic
typedef ptrdiff_t difference_type;
//! Our own type
typedef const_reverse_iterator self;
private:
// *** Members
//! The currently referenced leaf node of the tree
const typename BTree::LeafNode *curr_leaf;
//! One slot past the current key/data slot referenced.
unsigned short curr_slot;
//! Friendly to the const_iterator, so it may access the two data items
//! directly.
friend class reverse_iterator;
// The macro TLX_BTREE_FRIENDS can be used by outside class to access
// the B+ tree internals. This was added for wxBTreeDemo to be able to
// draw the tree.
TLX_BTREE_FRIENDS;
public:
// *** Methods
//! Default-Constructor of a const reverse iterator.
const_reverse_iterator() : curr_leaf(nullptr), curr_slot(0) {}
//! Initializing-Constructor of a const reverse iterator.
const_reverse_iterator(const typename BTree::LeafNode *l, unsigned short s)
: curr_leaf(l), curr_slot(s) {}
//! Copy-constructor from a mutable iterator.
const_reverse_iterator(const iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Copy-constructor from a const iterator.
const_reverse_iterator(const const_iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Copy-constructor from a mutable reverse iterator.
const_reverse_iterator(const reverse_iterator &it) // NOLINT
: curr_leaf(it.curr_leaf), curr_slot(it.curr_slot) {}
//! Dereference the iterator.
reference operator*() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return curr_leaf->slotdata[curr_slot - 1];
}
//! Dereference the iterator.
pointer operator->() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return &curr_leaf->slotdata[curr_slot - 1];
}
//! Key of the current slot.
const key_type &key() const {
TLX_BTREE_ASSERT(curr_slot > 0);
return curr_leaf->key(curr_slot - 1);
}
//! Prefix++ advance the iterator to the previous slot.
const_reverse_iterator &operator++() {
if (curr_slot > 1) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse;
} else {
// this is begin() == rend()
curr_slot = 0;
}
return *this;
}
//! Postfix++ advance the iterator to the previous slot.
const_reverse_iterator operator++(int) {
const_reverse_iterator tmp = *this; // copy ourselves
if (curr_slot > 1) {
--curr_slot;
} else if (curr_leaf->prev_leaf != nullptr) {
curr_leaf = curr_leaf->prev_leaf;
curr_slot = curr_leaf->slotuse;
} else {
// this is begin() == rend()
curr_slot = 0;
}
return tmp;
}
//! Prefix-- backstep the iterator to the next slot.
const_reverse_iterator &operator--() {
if (curr_slot < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 1;
} else {
// this is end() == rbegin()
curr_slot = curr_leaf->slotuse;
}
return *this;
}
//! Postfix-- backstep the iterator to the next slot.
const_reverse_iterator operator--(int) {
const_reverse_iterator tmp = *this; // copy ourselves
if (curr_slot < curr_leaf->slotuse) {
++curr_slot;
} else if (curr_leaf->next_leaf != nullptr) {
curr_leaf = curr_leaf->next_leaf;
curr_slot = 1;
} else {
// this is end() == rbegin()
curr_slot = curr_leaf->slotuse;
}
return tmp;
}
//! Equality of iterators.
bool operator==(const const_reverse_iterator &x) const {
return (x.curr_leaf == curr_leaf) && (x.curr_slot == curr_slot);
}
//! Inequality of iterators.
bool operator!=(const const_reverse_iterator &x) const {
return (x.curr_leaf != curr_leaf) || (x.curr_slot != curr_slot);
}
};
//! \}
public:
//! \name Small Statistics Structure
//! \{
/*!
* A small struct containing basic statistics about the B+ tree. It can be
* fetched using get_stats().
*/
struct tree_stats {
//! Number of items in the B+ tree
size_type size;
//! Number of leaves in the B+ tree
size_type leaves;
//! Number of inner nodes in the B+ tree
size_type inner_nodes;
//! Base B+ tree parameter: The number of key/data slots in each leaf
static const unsigned short leaf_slots = Self::leaf_slotmax;
//! Base B+ tree parameter: The number of key slots in each inner node.
static const unsigned short inner_slots = Self::inner_slotmax;
//! Zero initialized
tree_stats() : size(0), leaves(0), inner_nodes(0) {}
//! Return the total number of nodes
size_type nodes() const { return inner_nodes + leaves; }
//! Return the average fill of leaves
double avgfill_leaves() const {
return static_cast<double>(size) / (leaves * leaf_slots);
}
};
//! \}
private:
//! \name Tree Object Data Members
//! \{
//! Pointer to the B+ tree's root node, either leaf or inner node.
node *root_;
//! Pointer to first leaf in the double linked leaf chain.
LeafNode *head_leaf_;