forked from JGRennison/cpp-btree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.h
2398 lines (2157 loc) · 80.8 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
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// A btree implementation of the STL set and map interfaces. A btree is both
// smaller and faster than STL set/map. The red-black tree implementation of
// STL set/map has an overhead of 3 pointers (left, right and parent) plus the
// node color information for each stored value. So a set<int32> consumes 20
// bytes for each value stored. This btree implementation stores multiple
// values on fixed size nodes (usually 256 bytes) and doesn't store child
// pointers for leaf nodes. The result is that a btree_set<int32> may use much
// less memory per stored value. For the random insertion benchmark in
// btree_test.cc, a btree_set<int32> with node-size of 256 uses 4.9 bytes per
// stored value.
//
// The packing of multiple values on to each node of a btree has another effect
// besides better space utilization: better cache locality due to fewer cache
// lines being accessed. Better cache locality translates into faster
// operations.
//
// CAVEATS
//
// Insertions and deletions on a btree can cause splitting, merging or
// rebalancing of btree nodes. And even without these operations, insertions
// and deletions on a btree will move values around within a node. In both
// cases, the result is that insertions and deletions can invalidate iterators
// pointing to values other than the one being inserted/deleted. This is
// notably different from STL set/map which takes care to not invalidate
// iterators on insert/erase except, of course, for iterators pointing to the
// value being erased. A partial workaround when erasing is available:
// erase() returns an iterator pointing to the item just after the one that was
// erased (or end() if none exists). See also safe_btree.
// PERFORMANCE
//
// btree_bench --benchmarks=. 2>&1 | ./benchmarks.awk
//
// Run on pmattis-warp.nyc (4 X 2200 MHz CPUs); 2010/03/04-15:23:06
// Benchmark STL(ns) B-Tree(ns) @ <size>
// --------------------------------------------------------
// BM_set_int32_insert 1516 608 +59.89% <256> [40.0, 5.2]
// BM_set_int32_lookup 1160 414 +64.31% <256> [40.0, 5.2]
// BM_set_int32_fulllookup 960 410 +57.29% <256> [40.0, 4.4]
// BM_set_int32_delete 1741 528 +69.67% <256> [40.0, 5.2]
// BM_set_int32_queueaddrem 3078 1046 +66.02% <256> [40.0, 5.5]
// BM_set_int32_mixedaddrem 3600 1384 +61.56% <256> [40.0, 5.3]
// BM_set_int32_fifo 227 113 +50.22% <256> [40.0, 4.4]
// BM_set_int32_fwditer 158 26 +83.54% <256> [40.0, 5.2]
// BM_map_int32_insert 1551 636 +58.99% <256> [48.0, 10.5]
// BM_map_int32_lookup 1200 508 +57.67% <256> [48.0, 10.5]
// BM_map_int32_fulllookup 989 487 +50.76% <256> [48.0, 8.8]
// BM_map_int32_delete 1794 628 +64.99% <256> [48.0, 10.5]
// BM_map_int32_queueaddrem 3189 1266 +60.30% <256> [48.0, 11.6]
// BM_map_int32_mixedaddrem 3822 1623 +57.54% <256> [48.0, 10.9]
// BM_map_int32_fifo 151 134 +11.26% <256> [48.0, 8.8]
// BM_map_int32_fwditer 161 32 +80.12% <256> [48.0, 10.5]
// BM_set_int64_insert 1546 636 +58.86% <256> [40.0, 10.5]
// BM_set_int64_lookup 1200 512 +57.33% <256> [40.0, 10.5]
// BM_set_int64_fulllookup 971 487 +49.85% <256> [40.0, 8.8]
// BM_set_int64_delete 1745 616 +64.70% <256> [40.0, 10.5]
// BM_set_int64_queueaddrem 3163 1195 +62.22% <256> [40.0, 11.6]
// BM_set_int64_mixedaddrem 3760 1564 +58.40% <256> [40.0, 10.9]
// BM_set_int64_fifo 146 103 +29.45% <256> [40.0, 8.8]
// BM_set_int64_fwditer 162 31 +80.86% <256> [40.0, 10.5]
// BM_map_int64_insert 1551 720 +53.58% <256> [48.0, 20.7]
// BM_map_int64_lookup 1214 612 +49.59% <256> [48.0, 20.7]
// BM_map_int64_fulllookup 994 592 +40.44% <256> [48.0, 17.2]
// BM_map_int64_delete 1778 764 +57.03% <256> [48.0, 20.7]
// BM_map_int64_queueaddrem 3189 1547 +51.49% <256> [48.0, 20.9]
// BM_map_int64_mixedaddrem 3779 1887 +50.07% <256> [48.0, 21.6]
// BM_map_int64_fifo 147 145 +1.36% <256> [48.0, 17.2]
// BM_map_int64_fwditer 162 41 +74.69% <256> [48.0, 20.7]
// BM_set_string_insert 1989 1966 +1.16% <256> [64.0, 44.5]
// BM_set_string_lookup 1709 1600 +6.38% <256> [64.0, 44.5]
// BM_set_string_fulllookup 1573 1529 +2.80% <256> [64.0, 35.4]
// BM_set_string_delete 2520 1920 +23.81% <256> [64.0, 44.5]
// BM_set_string_queueaddrem 4706 4309 +8.44% <256> [64.0, 48.3]
// BM_set_string_mixedaddrem 5080 4654 +8.39% <256> [64.0, 46.7]
// BM_set_string_fifo 318 512 -61.01% <256> [64.0, 35.4]
// BM_set_string_fwditer 182 93 +48.90% <256> [64.0, 44.5]
// BM_map_string_insert 2600 2227 +14.35% <256> [72.0, 55.8]
// BM_map_string_lookup 2068 1730 +16.34% <256> [72.0, 55.8]
// BM_map_string_fulllookup 1859 1618 +12.96% <256> [72.0, 44.0]
// BM_map_string_delete 3168 2080 +34.34% <256> [72.0, 55.8]
// BM_map_string_queueaddrem 5840 4701 +19.50% <256> [72.0, 59.4]
// BM_map_string_mixedaddrem 6400 5200 +18.75% <256> [72.0, 57.8]
// BM_map_string_fifo 398 596 -49.75% <256> [72.0, 44.0]
// BM_map_string_fwditer 243 113 +53.50% <256> [72.0, 55.8]
#ifndef UTIL_BTREE_BTREE_H__
#define UTIL_BTREE_BTREE_H__
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <type_traits>
#include <new>
#include <ostream>
#include <string>
#include <utility>
namespace btree {
// Inside a btree method, if we just call swap(), it will choose the
// btree::swap method, which we don't want. And we can't say ::swap
// because then MSVC won't pickup any std::swap() implementations. We
// can't just use std::swap() directly because then we don't get the
// specialization for types outside the std namespace. So the solution
// is to have a special swap helper function whose name doesn't
// collide with other swap functions defined by the btree classes.
template <typename T>
inline void btree_swap_helper(T &a, T &b) {
using std::swap;
swap(a, b);
}
// A template helper used to select A or B based on a condition.
template<bool cond, typename A, typename B>
struct if_{
typedef A type;
};
template<typename A, typename B>
struct if_<false, A, B> {
typedef B type;
};
// Types small_ and big_ are promise that sizeof(small_) < sizeof(big_)
typedef char small_;
struct big_ {
char dummy[2];
};
// A compile-time assertion.
#define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
// A helper type used to indicate that a key-compare-to functor has been
// provided. A user can specify a key-compare-to functor by doing:
//
// struct MyStringComparer
// : public util::btree::btree_key_compare_to_tag {
// int operator()(const string &a, const string &b) const {
// return a.compare(b);
// }
// };
//
// Note that the return type is an int and not a bool. There is a
// COMPILE_ASSERT which enforces this return type.
struct btree_key_compare_to_tag {
};
// A helper class that indicates if the Compare parameter is derived from
// btree_key_compare_to_tag.
template <typename Compare>
struct btree_is_key_compare_to
: public std::is_convertible<Compare, btree_key_compare_to_tag> {
};
// A helper class to convert a boolean comparison into a three-way
// "compare-to" comparison that returns a negative value to indicate
// less-than, zero to indicate equality and a positive value to
// indicate greater-than. This helper class is specialized for
// less<string> and greater<string>. The btree_key_compare_to_adapter
// class is provided so that btree users automatically get the more
// efficient compare-to code when using common google string types
// with common comparison functors.
template <typename Compare>
struct btree_key_compare_to_adapter : Compare {
btree_key_compare_to_adapter() { }
btree_key_compare_to_adapter(const Compare &c) : Compare(c) { }
btree_key_compare_to_adapter(const btree_key_compare_to_adapter<Compare> &c)
: Compare(c) {
}
};
template <>
struct btree_key_compare_to_adapter<std::less<std::string> >
: public btree_key_compare_to_tag {
btree_key_compare_to_adapter() {}
btree_key_compare_to_adapter(const std::less<std::string>&) {}
btree_key_compare_to_adapter(
const btree_key_compare_to_adapter<std::less<std::string> >&) {}
int operator()(const std::string &a, const std::string &b) const {
return a.compare(b);
}
};
template <>
struct btree_key_compare_to_adapter<std::greater<std::string> >
: public btree_key_compare_to_tag {
btree_key_compare_to_adapter() {}
btree_key_compare_to_adapter(const std::greater<std::string>&) {}
btree_key_compare_to_adapter(
const btree_key_compare_to_adapter<std::greater<std::string> >&) {}
int operator()(const std::string &a, const std::string &b) const {
return b.compare(a);
}
};
// A helper class that allows a compare-to functor to behave like a plain
// compare functor. This specialization is used when we do not have a
// compare-to functor.
template <typename Key, typename Compare, bool HaveCompareTo>
struct btree_key_comparer {
btree_key_comparer() {}
btree_key_comparer(Compare c) : comp(c) {}
static bool bool_compare(const Compare &comp, const Key &x, const Key &y) {
return comp(x, y);
}
bool operator()(const Key &x, const Key &y) const {
return bool_compare(comp, x, y);
}
Compare comp;
};
// A specialization of btree_key_comparer when a compare-to functor is
// present. We need a plain (boolean) comparison in some parts of the btree
// code, such as insert-with-hint.
template <typename Key, typename Compare>
struct btree_key_comparer<Key, Compare, true> {
btree_key_comparer() {}
btree_key_comparer(Compare c) : comp(c) {}
static bool bool_compare(const Compare &comp, const Key &x, const Key &y) {
return comp(x, y) < 0;
}
bool operator()(const Key &x, const Key &y) const {
return bool_compare(comp, x, y);
}
Compare comp;
};
// A helper function to compare to keys using the specified compare
// functor. This dispatches to the appropriate btree_key_comparer comparison,
// depending on whether we have a compare-to functor or not (which depends on
// whether Compare is derived from btree_key_compare_to_tag).
template <typename Key, typename Compare>
static bool btree_compare_keys(
const Compare &comp, const Key &x, const Key &y) {
typedef btree_key_comparer<Key, Compare,
btree_is_key_compare_to<Compare>::value> key_comparer;
return key_comparer::bool_compare(comp, x, y);
}
template <typename Key, typename Compare,
typename Alloc, int TargetNodeSize, int ValueSize>
struct btree_common_params {
// If Compare is derived from btree_key_compare_to_tag then use it as the
// key_compare type. Otherwise, use btree_key_compare_to_adapter<> which will
// fall-back to Compare if we don't have an appropriate specialization.
typedef typename if_<
btree_is_key_compare_to<Compare>::value,
Compare, btree_key_compare_to_adapter<Compare> >::type key_compare;
// A type which indicates if we have a key-compare-to functor or a plain old
// key-compare functor.
typedef btree_is_key_compare_to<key_compare> is_key_compare_to;
typedef Alloc allocator_type;
typedef Key key_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
enum {
kTargetNodeSize = TargetNodeSize,
// Available space for values. This is largest for leaf nodes,
// which has overhead no fewer than two pointers.
kNodeValueSpace = TargetNodeSize - 2 * sizeof(void*),
};
// This is an integral type large enough to hold as many
// ValueSize-values as will fit a node of TargetNodeSize bytes.
typedef typename if_<
(kNodeValueSpace / ValueSize) >= 256,
uint16_t,
uint8_t>::type node_count_type;
};
// A parameters structure for holding the type parameters for a btree_map.
template <typename Key, typename Data, typename Compare,
typename Alloc, int TargetNodeSize>
struct btree_map_params
: public btree_common_params<Key, Compare, Alloc, TargetNodeSize,
sizeof(Key) + sizeof(Data)> {
typedef Data data_type;
typedef Data mapped_type;
typedef std::pair<const Key, data_type> value_type;
typedef std::pair<Key, data_type> mutable_value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
enum {
kValueSize = sizeof(Key) + sizeof(data_type),
};
static const Key& key(const value_type &x) { return x.first; }
static const Key& key(const mutable_value_type &x) { return x.first; }
static void swap(mutable_value_type *a, mutable_value_type *b) {
btree_swap_helper(a->first, b->first);
btree_swap_helper(a->second, b->second);
}
};
// A parameters structure for holding the type parameters for a btree_set.
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize>
struct btree_set_params
: public btree_common_params<Key, Compare, Alloc, TargetNodeSize,
sizeof(Key)> {
typedef std::false_type data_type;
typedef std::false_type mapped_type;
typedef Key value_type;
typedef value_type mutable_value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
enum {
kValueSize = sizeof(Key),
};
static const Key& key(const value_type &x) { return x; }
static void swap(mutable_value_type *a, mutable_value_type *b) {
btree_swap_helper<mutable_value_type>(*a, *b);
}
};
// An adapter class that converts a lower-bound compare into an upper-bound
// compare.
template <typename Key, typename Compare>
struct btree_upper_bound_adapter : public Compare {
btree_upper_bound_adapter(Compare c) : Compare(c) {}
bool operator()(const Key &a, const Key &b) const {
return !static_cast<const Compare&>(*this)(b, a);
}
};
template <typename Key, typename CompareTo>
struct btree_upper_bound_compare_to_adapter : public CompareTo {
btree_upper_bound_compare_to_adapter(CompareTo c) : CompareTo(c) {}
int operator()(const Key &a, const Key &b) const {
return static_cast<const CompareTo&>(*this)(b, a);
}
};
// Dispatch helper class for using linear search with plain compare.
template <typename K, typename N, typename Compare>
struct btree_linear_search_plain_compare {
static int lower_bound(const K &k, const N &n, Compare comp) {
return n.linear_search_plain_compare(k, 0, n.count(), comp);
}
static int upper_bound(const K &k, const N &n, Compare comp) {
typedef btree_upper_bound_adapter<K, Compare> upper_compare;
return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
}
};
// Dispatch helper class for using linear search with compare-to
template <typename K, typename N, typename CompareTo>
struct btree_linear_search_compare_to {
static int lower_bound(const K &k, const N &n, CompareTo comp) {
return n.linear_search_compare_to(k, 0, n.count(), comp);
}
static int upper_bound(const K &k, const N &n, CompareTo comp) {
typedef btree_upper_bound_adapter<K,
btree_key_comparer<K, CompareTo, true> > upper_compare;
return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
}
};
// Dispatch helper class for using binary search with plain compare.
template <typename K, typename N, typename Compare>
struct btree_binary_search_plain_compare {
static int lower_bound(const K &k, const N &n, Compare comp) {
return n.binary_search_plain_compare(k, 0, n.count(), comp);
}
static int upper_bound(const K &k, const N &n, Compare comp) {
typedef btree_upper_bound_adapter<K, Compare> upper_compare;
return n.binary_search_plain_compare(k, 0, n.count(), upper_compare(comp));
}
};
// Dispatch helper class for using binary search with compare-to.
template <typename K, typename N, typename CompareTo>
struct btree_binary_search_compare_to {
static int lower_bound(const K &k, const N &n, CompareTo comp) {
return n.binary_search_compare_to(k, 0, n.count(), CompareTo());
}
static int upper_bound(const K &k, const N &n, CompareTo comp) {
typedef btree_upper_bound_adapter<K,
btree_key_comparer<K, CompareTo, true> > upper_compare;
return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
}
};
// A node in the btree holding. The same node type is used for both internal
// and leaf nodes in the btree, though the nodes are allocated in such a way
// that the children array is only valid in internal nodes.
template <typename Params>
class btree_node {
public:
typedef Params params_type;
typedef btree_node<Params> self_type;
typedef typename Params::key_type key_type;
typedef typename Params::data_type data_type;
typedef typename Params::value_type value_type;
typedef typename Params::mutable_value_type mutable_value_type;
typedef typename Params::pointer pointer;
typedef typename Params::const_pointer const_pointer;
typedef typename Params::reference reference;
typedef typename Params::const_reference const_reference;
typedef typename Params::key_compare key_compare;
typedef typename Params::size_type size_type;
typedef typename Params::difference_type difference_type;
// Typedefs for the various types of node searches.
typedef btree_linear_search_plain_compare<
key_type, self_type, key_compare> linear_search_plain_compare_type;
typedef btree_linear_search_compare_to<
key_type, self_type, key_compare> linear_search_compare_to_type;
typedef btree_binary_search_plain_compare<
key_type, self_type, key_compare> binary_search_plain_compare_type;
typedef btree_binary_search_compare_to<
key_type, self_type, key_compare> binary_search_compare_to_type;
// If we have a valid key-compare-to type, use linear_search_compare_to,
// otherwise use linear_search_plain_compare.
typedef typename if_<
Params::is_key_compare_to::value,
linear_search_compare_to_type,
linear_search_plain_compare_type>::type linear_search_type;
// If we have a valid key-compare-to type, use binary_search_compare_to,
// otherwise use binary_search_plain_compare.
typedef typename if_<
Params::is_key_compare_to::value,
binary_search_compare_to_type,
binary_search_plain_compare_type>::type binary_search_type;
// If the key is an integral or floating point type, use linear search which
// is faster than binary search for such types. Might be wise to also
// configure linear search based on node-size.
typedef typename if_<
std::is_integral<key_type>::value ||
std::is_floating_point<key_type>::value,
linear_search_type, binary_search_type>::type search_type;
struct base_fields {
typedef typename Params::node_count_type field_type;
// A boolean indicating whether the node is a leaf or not.
bool leaf;
// The position of the node in the node's parent.
field_type position;
// The maximum number of values the node can hold.
field_type max_count;
// The count of the number of values in the node.
field_type count;
// A pointer to the node's parent.
btree_node *parent;
};
enum {
kValueSize = params_type::kValueSize,
kTargetNodeSize = params_type::kTargetNodeSize,
// Compute how many values we can fit onto a leaf node.
kNodeTargetValues = (kTargetNodeSize - sizeof(base_fields)) / kValueSize,
// We need a minimum of 3 values per internal node in order to perform
// splitting (1 value for the two nodes involved in the split and 1 value
// propagated to the parent as the delimiter for the split).
kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3,
kExactMatch = 1 << 30,
kMatchMask = kExactMatch - 1,
};
struct leaf_fields : public base_fields {
// The array of values. Only the first count of these values have been
// constructed and are valid.
mutable_value_type values[kNodeValues];
};
struct internal_fields : public leaf_fields {
// The array of child pointers. The keys in children_[i] are all less than
// key(i). The keys in children_[i + 1] are all greater than key(i). There
// are always count + 1 children.
btree_node *children[kNodeValues + 1];
};
struct root_fields : public internal_fields {
btree_node *rightmost;
size_type size;
};
public:
// Getter/setter for whether this is a leaf node or not. This value doesn't
// change after the node is created.
bool leaf() const { return fields_.leaf; }
// Getter for the position of this node in its parent.
int position() const { return fields_.position; }
void set_position(int v) { fields_.position = static_cast<typename Params::node_count_type>(v); }
// Getter/setter for the number of values stored in this node.
int count() const { return fields_.count; }
void set_count(int v) { fields_.count = static_cast<typename Params::node_count_type>(v); }
int max_count() const { return fields_.max_count; }
// Getter for the parent of this node.
btree_node* parent() const { return fields_.parent; }
// Getter for whether the node is the root of the tree. The parent of the
// root of the tree is the leftmost node in the tree which is guaranteed to
// be a leaf.
bool is_root() const { return parent()->leaf(); }
void make_root() {
assert(parent()->is_root());
fields_.parent = fields_.parent->parent();
}
// Getter for the rightmost root node field. Only valid on the root node.
btree_node* rightmost() const { return fields_.rightmost; }
btree_node** mutable_rightmost() { return &fields_.rightmost; }
// Getter for the size root node field. Only valid on the root node.
size_type size() const { return fields_.size; }
size_type* mutable_size() { return &fields_.size; }
// Getters for the key/value at position i in the node.
const key_type& key(int i) const {
return params_type::key(fields_.values[i]);
}
reference value(int i) {
// Have to cast via a char* to avoid strict aliasing warnings in gcc, possibly others.
char* p = reinterpret_cast<char*>(&(fields_.values[i]));
return reinterpret_cast<reference>(*p);
}
const_reference value(int i) const {
// Have to cast via a char* to avoid strict aliasing warnings in gcc, possibly others.
const char* p = reinterpret_cast<const char*>(&(fields_.values[i]));
return reinterpret_cast<const_reference>(*p);
}
mutable_value_type* mutable_value(int i) {
return &fields_.values[i];
}
// Swap value i in this node with value j in node x.
void value_swap(int i, btree_node *x, int j) {
params_type::swap(mutable_value(i), x->mutable_value(j));
}
// Getters/setter for the child at position i in the node.
btree_node* child(int i) const { return fields_.children[i]; }
btree_node** mutable_child(int i) { return &fields_.children[i]; }
void set_child(int i, btree_node *c) {
*mutable_child(i) = c;
c->fields_.parent = this;
c->fields_.position = static_cast<typename Params::node_count_type>(i);
}
// Returns the position of the first value whose key is not less than k.
template <typename Compare>
int lower_bound(const key_type &k, const Compare &comp) const {
return search_type::lower_bound(k, *this, comp);
}
// Returns the position of the first value whose key is greater than k.
template <typename Compare>
int upper_bound(const key_type &k, const Compare &comp) const {
return search_type::upper_bound(k, *this, comp);
}
// Returns the position of the first value whose key is not less than k using
// linear search performed using plain compare.
template <typename Compare>
int linear_search_plain_compare(
const key_type &k, int s, int e, const Compare &comp) const {
while (s < e) {
if (!btree_compare_keys(comp, key(s), k)) {
break;
}
++s;
}
return s;
}
// Returns the position of the first value whose key is not less than k using
// linear search performed using compare-to.
template <typename Compare>
int linear_search_compare_to(
const key_type &k, int s, int e, const Compare &comp) const {
while (s < e) {
int c = comp(key(s), k);
if (c == 0) {
return s | kExactMatch;
} else if (c > 0) {
break;
}
++s;
}
return s;
}
// Returns the position of the first value whose key is not less than k using
// binary search performed using plain compare.
template <typename Compare>
int binary_search_plain_compare(
const key_type &k, int s, int e, const Compare &comp) const {
while (s != e) {
int mid = (s + e) / 2;
if (btree_compare_keys(comp, key(mid), k)) {
s = mid + 1;
} else {
e = mid;
}
}
return s;
}
// Returns the position of the first value whose key is not less than k using
// binary search performed using compare-to.
template <typename CompareTo>
int binary_search_compare_to(
const key_type &k, int s, int e, const CompareTo &comp) const {
while (s != e) {
int mid = (s + e) / 2;
int c = comp(key(mid), k);
if (c < 0) {
s = mid + 1;
} else if (c > 0) {
e = mid;
} else {
// Need to return the first value whose key is not less than k, which
// requires continuing the binary search. Note that we are guaranteed
// that the result is an exact match because if "key(mid-1) < k" the
// call to binary_search_compare_to() will return "mid".
s = binary_search_compare_to(k, s, mid, comp);
return s | kExactMatch;
}
}
return s;
}
// Inserts the value x at position i, shifting all existing values and
// children at positions >= i to the right by 1.
void insert_value(int i, const value_type &x);
// Removes the value at position i, shifting all existing values and children
// at positions > i to the left by 1.
void remove_value(int i);
// Rebalances a node with its right sibling.
void rebalance_right_to_left(btree_node *sibling, int to_move);
void rebalance_left_to_right(btree_node *sibling, int to_move);
// Splits a node, moving a portion of the node's values to its right sibling.
void split(btree_node *sibling, int insert_position);
// Merges a node with its right sibling, moving all of the values and the
// delimiting key in the parent node onto itself.
void merge(btree_node *sibling);
// Swap the contents of "this" and "src".
void swap(btree_node *src);
// Node allocation/deletion routines.
static btree_node* init_leaf(
leaf_fields *f, btree_node *parent, int max_count) {
btree_node *n = reinterpret_cast<btree_node*>(f);
f->leaf = 1;
f->position = 0;
f->max_count = static_cast<typename base_fields::field_type>(max_count);
f->count = 0;
f->parent = parent;
#ifdef BTREE_DEBUG
memset(&f->values, 0, max_count * sizeof(value_type));
#endif
return n;
}
static btree_node* init_internal(internal_fields *f, btree_node *parent) {
btree_node *n = init_leaf(f, parent, kNodeValues);
f->leaf = 0;
#ifdef BTREE_DEBUG
memset(f->children, 0, sizeof(f->children));
#endif
return n;
}
static btree_node* init_root(root_fields *f, btree_node *parent) {
btree_node *n = init_internal(f, parent);
f->rightmost = parent;
f->size = parent->count();
return n;
}
void destroy() {
for (int i = 0; i < count(); ++i) {
value_destroy(i);
}
}
private:
void value_init(int i) {
new (&fields_.values[i]) mutable_value_type;
}
void value_init(int i, const value_type &x) {
new (&fields_.values[i]) mutable_value_type(x);
}
void value_destroy(int i) {
fields_.values[i].~mutable_value_type();
}
private:
root_fields fields_;
private:
btree_node(const btree_node&);
void operator=(const btree_node&);
};
template <typename Node, typename Reference, typename Pointer>
struct btree_iterator {
typedef typename Node::key_type key_type;
typedef typename Node::size_type size_type;
typedef typename Node::difference_type difference_type;
typedef typename Node::params_type params_type;
typedef Node node_type;
typedef typename std::remove_const<Node>::type normal_node;
typedef const Node const_node;
typedef typename params_type::value_type value_type;
typedef typename params_type::pointer normal_pointer;
typedef typename params_type::reference normal_reference;
typedef typename params_type::const_pointer const_pointer;
typedef typename params_type::const_reference const_reference;
typedef Pointer pointer;
typedef Reference reference;
typedef std::bidirectional_iterator_tag iterator_category;
typedef btree_iterator<
normal_node, normal_reference, normal_pointer> iterator;
typedef btree_iterator<
const_node, const_reference, const_pointer> const_iterator;
typedef btree_iterator<Node, Reference, Pointer> self_type;
btree_iterator()
: node(NULL),
position(-1) {
}
btree_iterator(Node *n, int p)
: node(n),
position(p) {
}
btree_iterator(const iterator &x)
: node(x.node),
position(x.position) {
}
// Increment/decrement the iterator.
void increment() {
if (node->leaf() && ++position < node->count()) {
return;
}
increment_slow();
}
void increment_by(int count);
void increment_slow();
void decrement() {
if (node->leaf() && --position >= 0) {
return;
}
decrement_slow();
}
void decrement_slow();
bool operator==(const const_iterator &x) const {
return node == x.node && position == x.position;
}
bool operator!=(const const_iterator &x) const {
return node != x.node || position != x.position;
}
btree_iterator& operator=(const btree_iterator &x) noexcept {
node = x.node;
position = x.position;
return *this;
}
// Accessors for the key/value the iterator is pointing at.
const key_type& key() const {
return node->key(position);
}
reference operator*() const {
return node->value(position);
}
pointer operator->() const {
return &node->value(position);
}
self_type& operator++() {
increment();
return *this;
}
self_type& operator--() {
decrement();
return *this;
}
self_type operator++(int) {
self_type tmp = *this;
++*this;
return tmp;
}
self_type operator--(int) {
self_type tmp = *this;
--*this;
return tmp;
}
// The node in the tree the iterator is pointing at.
Node *node;
// The position within the node of the tree the iterator is pointing at.
int position;
};
// Dispatch helper class for using btree::internal_locate with plain compare.
struct btree_internal_locate_plain_compare {
template <typename K, typename T, typename Iter>
static std::pair<Iter, int> dispatch(const K &k, const T &t, Iter iter) {
return t.internal_locate_plain_compare(k, iter);
}
};
// Dispatch helper class for using btree::internal_locate with compare-to.
struct btree_internal_locate_compare_to {
template <typename K, typename T, typename Iter>
static std::pair<Iter, int> dispatch(const K &k, const T &t, Iter iter) {
return t.internal_locate_compare_to(k, iter);
}
};
template <typename Params>
class btree : public Params::key_compare {
typedef btree<Params> self_type;
typedef btree_node<Params> node_type;
typedef typename node_type::base_fields base_fields;
typedef typename node_type::leaf_fields leaf_fields;
typedef typename node_type::internal_fields internal_fields;
typedef typename node_type::root_fields root_fields;
typedef typename Params::is_key_compare_to is_key_compare_to;
friend struct btree_internal_locate_plain_compare;
friend struct btree_internal_locate_compare_to;
typedef typename if_<
is_key_compare_to::value,
btree_internal_locate_compare_to,
btree_internal_locate_plain_compare>::type internal_locate_type;
enum {
kNodeValues = node_type::kNodeValues,
kMinNodeValues = kNodeValues / 2,
kValueSize = node_type::kValueSize,
kExactMatch = node_type::kExactMatch,
kMatchMask = node_type::kMatchMask,
};
// A helper class to get the empty base class optimization for 0-size
// allocators. Base is internal_allocator_type.
// (e.g. empty_base_handle<internal_allocator_type, node_type*>). If Base is
// 0-size, the compiler doesn't have to reserve any space for it and
// sizeof(empty_base_handle) will simply be sizeof(Data). Google [empty base
// class optimization] for more details.
template <typename Base, typename Data>
struct empty_base_handle : public Base {
empty_base_handle(const Base &b, const Data &d)
: Base(b),
data(d) {
}
Data data;
};
struct node_stats {
node_stats(size_t l, size_t i)
: leaf_nodes(l),
internal_nodes(i) {
}
node_stats& operator+=(const node_stats &x) {
leaf_nodes += x.leaf_nodes;
internal_nodes += x.internal_nodes;
return *this;
}
size_t leaf_nodes;
size_t internal_nodes;
};
public:
typedef Params params_type;
typedef typename Params::key_type key_type;
typedef typename Params::data_type data_type;
typedef typename Params::mapped_type mapped_type;
typedef typename Params::value_type value_type;
typedef typename Params::key_compare key_compare;
typedef typename Params::pointer pointer;
typedef typename Params::const_pointer const_pointer;
typedef typename Params::reference reference;
typedef typename Params::const_reference const_reference;
typedef typename Params::size_type size_type;
typedef typename Params::difference_type difference_type;
typedef btree_iterator<node_type, reference, pointer> iterator;
typedef typename iterator::const_iterator const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef typename Params::allocator_type allocator_type;
using internal_allocator_type = typename std::allocator_traits<allocator_type>::template rebind_alloc<char>;
public:
// Default constructor.
btree(const key_compare &comp, const allocator_type &alloc);
// Copy constructor.
btree(const self_type &x);
// Destructor.
~btree() {
clear();
}
// Iterator routines.
iterator begin() {
return iterator(leftmost(), 0);
}
const_iterator begin() const {
return const_iterator(leftmost(), 0);
}
iterator end() {
node_type* const right = rightmost();
return iterator(right, right ? right->count() : 0);
}
const_iterator end() const {
const node_type* const right = rightmost();
return const_iterator(right, right ? right->count() : 0);
}
reverse_iterator rbegin() {
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
// Finds the first element whose key is not less than key.
iterator lower_bound(const key_type &key) {
return internal_end(
internal_lower_bound(key, iterator(root(), 0)));
}
const_iterator lower_bound(const key_type &key) const {
return internal_end(
internal_lower_bound(key, const_iterator(root(), 0)));
}
// Finds the first element whose key is greater than key.
iterator upper_bound(const key_type &key) {
return internal_end(
internal_upper_bound(key, iterator(root(), 0)));
}
const_iterator upper_bound(const key_type &key) const {
return internal_end(
internal_upper_bound(key, const_iterator(root(), 0)));
}
// Finds the range of values which compare equal to key. The first member of
// the returned pair is equal to lower_bound(key). The second member pair of
// the pair is equal to upper_bound(key).
std::pair<iterator,iterator> equal_range(const key_type &key) {
return std::make_pair(lower_bound(key), upper_bound(key));
}
std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
return std::make_pair(lower_bound(key), upper_bound(key));
}