-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbtree.h
3994 lines (3233 loc) · 137 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
/*******************************************************************************
* include/stx/btree.h
*
* STX B+ Tree Template Classes v0.9
* Copyright (C) 2008-2013 Timo Bingmann <[email protected]>
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer, must
* be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are
* solely in the form of machine-executable object code generated by a source
* language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************/
#ifndef STX_STX_BTREE_H_HEADER
#define STX_STX_BTREE_H_HEADER
/**
* \file include/stx/btree.h
* Contains the main B+ tree implementation template class btree.
*/
// *** Required Headers from the STL
#include <algorithm>
#include <functional>
#include <istream>
#include <ostream>
#include <memory>
#include <cstddef>
#include <cassert>
// *** Debugging Macros
#ifdef BTREE_DEBUG
#include <iostream>
/// Print out debug information to std::cout if BTREE_DEBUG is defined.
#define BTREE_PRINT(x) do { if (debug) (std::cout << x << std::endl); } while (0)
/// Assertion only if BTREE_DEBUG is defined. This is not used in verify().
#define BTREE_ASSERT(x) do { assert(x); } while (0)
#else
/// Print out debug information to std::cout if BTREE_DEBUG is defined.
#define BTREE_PRINT(x) do { } while (0)
/// Assertion only if BTREE_DEBUG is defined. This is not used in verify().
#define BTREE_ASSERT(x) do { } while (0)
#endif
/// The maximum of a and b. Used in some compile-time formulas.
#define BTREE_MAX(a, b) ((a) < (b) ? (b) : (a))
#ifndef BTREE_FRIENDS
/// The macro 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 BTREE_FRIENDS friend class btree_friend;
#endif
/// STX - Some Template Extensions namespace
namespace stx {
/** Generates default traits for a B+ tree used as a set. It estimates leaf and
* inner node sizes by assuming a cache line size of 256 bytes. */
template <typename _Key>
class btree_default_set_traits
{
public:
/// If true, the tree will self verify it's invariants after each insert()
/// or erase(). The header must have been compiled with BTREE_DEBUG defined.
static const bool selfverify = 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 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 leafslots = BTREE_MAX(8, 256 / (sizeof(_Key)));
/// 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 innerslots = 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;
};
/** Generates default traits for a B+ tree used as a map. It estimates leaf and
* inner node sizes by assuming a cache line size of 256 bytes. */
template <typename _Key, typename _Data>
class btree_default_map_traits
{
public:
/// If true, the tree will self verify it's invariants after each insert()
/// or erase(). The header must have been compiled with BTREE_DEBUG defined.
static const bool selfverify = 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 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 leafslots = BTREE_MAX(8, 256 / (sizeof(_Key) + sizeof(_Data)));
/// 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 innerslots = 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;
};
/** @brief Basic class implementing a base B+ tree data structure in memory.
*
* The base implementation of a 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 by using more 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 _Data,
typename _Value = std::pair<_Key, _Data>,
typename _Compare = std::less<_Key>,
typename _Traits = btree_default_map_traits<_Key, _Data>,
bool _Duplicates = false,
typename _Alloc = std::allocator<_Value>,
bool _UsedAsSet = false>
class btree
{
public:
// *** Template Parameter Types
/// First template parameter: The key type of the B+ tree. This is stored
/// in inner nodes and leaves
typedef _Key key_type;
/// Second template parameter: The data type associated with each
/// key. Stored in the B+ tree's leaves
typedef _Data data_type;
/// Third template parameter: Composition pair of key and data types, this
/// is required by the STL standard. The B+ tree does not store key and
/// data together. If value_type == key_type then the B+ tree implements a
/// set.
typedef _Value value_type;
/// Fourth template parameter: Key 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 _Alloc allocator_type;
/// Eighth template parameter: boolean indicator whether the btree is used
/// as a set. In this case all operations on the data arrays are
/// omitted. This flag is kind of hacky, but required because
/// sizeof(empty_struct) = 1 due to the C standard. Without the flag, lots
/// of superfluous copying would occur.
static const bool used_as_set = _UsedAsSet;
// The macro 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.
BTREE_FRIENDS
public:
// *** Constructed Types
/// Typedef of our own type
typedef btree<key_type, data_type, value_type, key_compare,
traits, allow_duplicates, allocator_type, used_as_set> self_type;
/// Size type used to count keys
typedef size_t size_type;
/// The pair of key_type and data_type, this may be different from
/// value_type.
typedef std::pair<key_type, data_type> pair_type;
public:
// *** 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 leafslotmax = traits::leafslots;
/// 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 innerslotmax = traits::innerslots;
/// 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 minleafslots = (leafslotmax / 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 mininnerslots = (innerslotmax / 2);
/// Debug parameter: Enables expensive and thorough checking of the B+ tree
/// invariants after each insert/erase operation.
static const bool selfverify = traits::selfverify;
/// Debug parameter: Prints out lots of debug information about how the
/// algorithms change the tree. Requires the header file to be compiled
/// with BTREE_DEBUG and the key type must be std::ostream printable.
static const bool debug = traits::debug;
private:
// *** Node Classes for In-Memory Nodes
/// The header structure of each node in-memory. This structure is extended
/// by inner_node or leaf_node.
struct node
{
/// Level in the b-tree, if level == 0 -> leaf node
unsigned short level;
/// Number of key slotuse use, so number of valid children or data
/// pointers
unsigned short slotuse;
/// Delayed initialisation of constructed node
inline node(const unsigned short l, const unsigned short s = 0)
: level(l), slotuse(s)
{ }
/// True if this is a leaf node
inline bool isleafnode() const
{
return (level == 0);
}
};
/// Extended structure of a inner node in-memory. Contains only keys and no
/// data items.
struct inner_node : public node
{
/// Define an related allocator for the inner_node structs.
typedef typename _Alloc::template rebind<inner_node>::other alloc_type;
/// Keys of children or data pointers
key_type slotkey[innerslotmax];
/// Pointers to children
node * childid[innerslotmax + 1];
/// Set variables to initial values
inline inner_node(const unsigned short l)
: node(l)
{ }
/// Construction during restore from node top
inline inner_node(const node& top)
: node(top.level, top.slotuse)
{ }
/// True if the node's slots are full
inline bool isfull() const
{
return (node::slotuse == innerslotmax);
}
/// True if few used entries, less than half full
inline bool isfew() const
{
return (node::slotuse <= mininnerslots);
}
/// True if node has too few entries
inline bool isunderflow() const
{
return (node::slotuse < mininnerslots);
}
};
/// Extended structure of a leaf node in memory. Contains pairs of keys and
/// data items. Key and data slots are kept in separate arrays, because the
/// key array is traversed very often compared to accessing the data items.
struct leaf_node : public node
{
/// Define an related allocator for the leaf_node structs.
typedef typename _Alloc::template rebind<leaf_node>::other alloc_type;
/// Double linked list pointers to traverse the leaves
leaf_node * prevleaf;
/// Double linked list pointers to traverse the leaves
leaf_node * nextleaf;
/// Keys of children or data pointers
key_type slotkey[leafslotmax];
/// Array of data
data_type slotdata[used_as_set ? 1 : leafslotmax];
/// Set variables to initial values
inline leaf_node()
: node(0), prevleaf(NULL), nextleaf(NULL)
{ }
/// Construction during restore from node top
inline leaf_node(const node& top)
: node(top.level, top.slotuse), prevleaf(NULL), nextleaf(NULL)
{ }
/// True if the node's slots are full
inline bool isfull() const
{
return (node::slotuse == leafslotmax);
}
/// True if few used entries, less than half full
inline bool isfew() const
{
return (node::slotuse <= minleafslots);
}
/// True if node has too few entries
inline bool isunderflow() const
{
return (node::slotuse < minleafslots);
}
/// Set the (key,data) pair in slot. Overloaded function used by
/// bulk_load().
inline void set_slot(unsigned short slot, const pair_type& value)
{
BTREE_ASSERT(used_as_set == false);
BTREE_ASSERT(slot < node::slotuse);
slotkey[slot] = value.first;
slotdata[slot] = value.second;
}
/// Set the key pair in slot. Overloaded function used by
/// bulk_load().
inline void set_slot(unsigned short slot, const key_type& key)
{
BTREE_ASSERT(used_as_set == true);
BTREE_ASSERT(slot < node::slotuse);
slotkey[slot] = key;
}
};
private:
// *** Template Magic to Convert a pair or key/data types to a value_type
/// For sets the second pair_type is an empty struct, so the value_type
/// should only be the first.
template <typename value_type, typename pair_type>
struct btree_pair_to_value
{
/// Convert a fake pair type to just the first component
inline value_type operator () (pair_type& p) const
{
return p.first;
}
/// Convert a fake pair type to just the first component
inline value_type operator () (const pair_type& p) const
{
return p.first;
}
};
/// For maps value_type is the same as the pair_type
template <typename value_type>
struct btree_pair_to_value<value_type, value_type>
{
/// Identity "convert" a real pair type to just the first component
inline value_type operator () (pair_type& p) const
{
return p;
}
/// Identity "convert" a real pair type to just the first component
inline value_type operator () (const pair_type& p) const
{
return p;
}
};
/// Using template specialization select the correct converter used by the
/// iterators
typedef btree_pair_to_value<value_type, pair_type> pair_to_value_type;
public:
// *** 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 data type of the btree. Returned by data().
typedef typename btree::data_type data_type;
/// The value type of the btree. Returned by operator*().
typedef typename btree::value_type value_type;
/// The pair type of the btree.
typedef typename btree::pair_type pair_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;
private:
// *** Members
/// The currently referenced leaf node of the tree
typename btree::leaf_node * currnode;
/// Current key/data slot referenced
unsigned short currslot;
/// 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 currnode and currslot values directly.
friend class btree<key_type, data_type, value_type, key_compare,
traits, allow_duplicates, allocator_type, used_as_set>;
/// Evil! A temporary value_type to STL-correctly deliver operator* and
/// operator->
mutable value_type temp_value;
// The macro 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.
BTREE_FRIENDS
public:
// *** Methods
/// Default-Constructor of a mutable iterator
inline iterator()
: currnode(NULL), currslot(0)
{ }
/// Initializing-Constructor of a mutable iterator
inline iterator(typename btree::leaf_node* l, unsigned short s)
: currnode(l), currslot(s)
{ }
/// Copy-constructor from a reverse iterator
inline iterator(const reverse_iterator& it) // NOLINT
: currnode(it.currnode), currslot(it.currslot)
{ }
/// Dereference the iterator, this is not a value_type& because key and
/// value are not stored together
inline reference operator * () const
{
temp_value = pair_to_value_type()(pair_type(key(), data()));
return temp_value;
}
/// Dereference the iterator. Do not use this if possible, use key()
/// and data() instead. The B+ tree does not stored key and data
/// together.
inline pointer operator -> () const
{
temp_value = pair_to_value_type()(pair_type(key(), data()));
return &temp_value;
}
/// Key of the current slot
inline const key_type & key() const
{
return currnode->slotkey[currslot];
}
/// Writable reference to the current data object
inline data_type & data() const
{
return currnode->slotdata[used_as_set ? 0 : currslot];
}
/// Prefix++ advance the iterator to the next slot
inline iterator& operator ++ ()
{
if (currslot + 1 < currnode->slotuse) {
++currslot;
}
else if (currnode->nextleaf != NULL) {
currnode = currnode->nextleaf;
currslot = 0;
}
else {
// this is end()
currslot = currnode->slotuse;
}
return *this;
}
/// Postfix++ advance the iterator to the next slot
inline iterator operator ++ (int)
{
iterator tmp = *this; // copy ourselves
if (currslot + 1 < currnode->slotuse) {
++currslot;
}
else if (currnode->nextleaf != NULL) {
currnode = currnode->nextleaf;
currslot = 0;
}
else {
// this is end()
currslot = currnode->slotuse;
}
return tmp;
}
/// Prefix-- backstep the iterator to the last slot
inline iterator& operator -- ()
{
if (currslot > 0) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse - 1;
}
else {
// this is begin()
currslot = 0;
}
return *this;
}
/// Postfix-- backstep the iterator to the last slot
inline iterator operator -- (int)
{
iterator tmp = *this; // copy ourselves
if (currslot > 0) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse - 1;
}
else {
// this is begin()
currslot = 0;
}
return tmp;
}
/// Equality of iterators
inline bool operator == (const iterator& x) const
{
return (x.currnode == currnode) && (x.currslot == currslot);
}
/// Inequality of iterators
inline bool operator != (const iterator& x) const
{
return (x.currnode != currnode) || (x.currslot != currslot);
}
};
/// 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 data type of the btree. Returned by data().
typedef typename btree::data_type data_type;
/// The value type of the btree. Returned by operator*().
typedef typename btree::value_type value_type;
/// The pair type of the btree.
typedef typename btree::pair_type pair_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;
private:
// *** Members
/// The currently referenced leaf node of the tree
const typename btree::leaf_node * currnode;
/// Current key/data slot referenced
unsigned short currslot;
/// Friendly to the reverse_const_iterator, so it may access the two
/// data items directly
friend class const_reverse_iterator;
/// Evil! A temporary value_type to STL-correctly deliver operator* and
/// operator->
mutable value_type temp_value;
// The macro 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.
BTREE_FRIENDS
public:
// *** Methods
/// Default-Constructor of a const iterator
inline const_iterator()
: currnode(NULL), currslot(0)
{ }
/// Initializing-Constructor of a const iterator
inline const_iterator(const typename btree::leaf_node* l, unsigned short s)
: currnode(l), currslot(s)
{ }
/// Copy-constructor from a mutable iterator
inline const_iterator(const iterator& it) // NOLINT
: currnode(it.currnode), currslot(it.currslot)
{ }
/// Copy-constructor from a mutable reverse iterator
inline const_iterator(const reverse_iterator& it) // NOLINT
: currnode(it.currnode), currslot(it.currslot)
{ }
/// Copy-constructor from a const reverse iterator
inline const_iterator(const const_reverse_iterator& it) // NOLINT
: currnode(it.currnode), currslot(it.currslot)
{ }
/// Dereference the iterator. Do not use this if possible, use key()
/// and data() instead. The B+ tree does not stored key and data
/// together.
inline reference operator * () const
{
temp_value = pair_to_value_type()(pair_type(key(), data()));
return temp_value;
}
/// Dereference the iterator. Do not use this if possible, use key()
/// and data() instead. The B+ tree does not stored key and data
/// together.
inline pointer operator -> () const
{
temp_value = pair_to_value_type()(pair_type(key(), data()));
return &temp_value;
}
/// Key of the current slot
inline const key_type & key() const
{
return currnode->slotkey[currslot];
}
/// Read-only reference to the current data object
inline const data_type & data() const
{
return currnode->slotdata[used_as_set ? 0 : currslot];
}
/// Prefix++ advance the iterator to the next slot
inline const_iterator& operator ++ ()
{
if (currslot + 1 < currnode->slotuse) {
++currslot;
}
else if (currnode->nextleaf != NULL) {
currnode = currnode->nextleaf;
currslot = 0;
}
else {
// this is end()
currslot = currnode->slotuse;
}
return *this;
}
/// Postfix++ advance the iterator to the next slot
inline const_iterator operator ++ (int)
{
const_iterator tmp = *this; // copy ourselves
if (currslot + 1 < currnode->slotuse) {
++currslot;
}
else if (currnode->nextleaf != NULL) {
currnode = currnode->nextleaf;
currslot = 0;
}
else {
// this is end()
currslot = currnode->slotuse;
}
return tmp;
}
/// Prefix-- backstep the iterator to the last slot
inline const_iterator& operator -- ()
{
if (currslot > 0) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse - 1;
}
else {
// this is begin()
currslot = 0;
}
return *this;
}
/// Postfix-- backstep the iterator to the last slot
inline const_iterator operator -- (int)
{
const_iterator tmp = *this; // copy ourselves
if (currslot > 0) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse - 1;
}
else {
// this is begin()
currslot = 0;
}
return tmp;
}
/// Equality of iterators
inline bool operator == (const const_iterator& x) const
{
return (x.currnode == currnode) && (x.currslot == currslot);
}
/// Inequality of iterators
inline bool operator != (const const_iterator& x) const
{
return (x.currnode != currnode) || (x.currslot != currslot);
}
};
/// 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 data type of the btree. Returned by data().
typedef typename btree::data_type data_type;
/// The value type of the btree. Returned by operator*().
typedef typename btree::value_type value_type;
/// The pair type of the btree.
typedef typename btree::pair_type pair_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;
private:
// *** Members
/// The currently referenced leaf node of the tree
typename btree::leaf_node * currnode;
/// One slot past the current key/data slot referenced.
unsigned short currslot;
/// 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;
/// Evil! A temporary value_type to STL-correctly deliver operator* and
/// operator->
mutable value_type temp_value;
// The macro 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.
BTREE_FRIENDS
public:
// *** Methods
/// Default-Constructor of a reverse iterator
inline reverse_iterator()
: currnode(NULL), currslot(0)
{ }
/// Initializing-Constructor of a mutable reverse iterator
inline reverse_iterator(typename btree::leaf_node* l, unsigned short s)
: currnode(l), currslot(s)
{ }
/// Copy-constructor from a mutable iterator
inline reverse_iterator(const iterator& it) // NOLINT
: currnode(it.currnode), currslot(it.currslot)
{ }
/// Dereference the iterator, this is not a value_type& because key and
/// value are not stored together
inline reference operator * () const
{
BTREE_ASSERT(currslot > 0);
temp_value = pair_to_value_type()(pair_type(key(), data()));
return temp_value;
}
/// Dereference the iterator. Do not use this if possible, use key()
/// and data() instead. The B+ tree does not stored key and data
/// together.
inline pointer operator -> () const
{
BTREE_ASSERT(currslot > 0);
temp_value = pair_to_value_type()(pair_type(key(), data()));
return &temp_value;
}
/// Key of the current slot
inline const key_type & key() const
{
BTREE_ASSERT(currslot > 0);
return currnode->slotkey[currslot - 1];
}
/// Writable reference to the current data object
inline data_type & data() const
{
BTREE_ASSERT(currslot > 0);
return currnode->slotdata[used_as_set ? 0 : currslot - 1];
}
/// Prefix++ advance the iterator to the next slot
inline reverse_iterator& operator ++ ()
{
if (currslot > 1) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse;
}
else {
// this is begin() == rend()
currslot = 0;
}
return *this;
}
/// Postfix++ advance the iterator to the next slot
inline reverse_iterator operator ++ (int)
{
reverse_iterator tmp = *this; // copy ourselves
if (currslot > 1) {
--currslot;
}
else if (currnode->prevleaf != NULL) {
currnode = currnode->prevleaf;
currslot = currnode->slotuse;
}
else {
// this is begin() == rend()
currslot = 0;
}
return tmp;
}
/// Prefix-- backstep the iterator to the last slot
inline reverse_iterator& operator -- ()
{
if (currslot < currnode->slotuse) {
++currslot;
}
else if (currnode->nextleaf != NULL) {
currnode = currnode->nextleaf;
currslot = 1;
}