-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsbtree.h
1855 lines (1775 loc) · 53 KB
/
sbtree.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
#pragma once
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <memory>
#include <stdexcept>
#include <tuple>
template<class config_t>
class size_balanced_tree
{
public:
typedef typename config_t::key_type key_type;
typedef typename config_t::mapped_type mapped_type;
typedef typename config_t::value_type value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef typename config_t::key_compare key_compare;
typedef typename config_t::allocator_type allocator_type;
typedef value_type &reference;
typedef value_type const &const_reference;
typedef value_type *pointer;
typedef value_type const *const_pointer;
protected:
struct node_t
{
node_t *parent;
node_t *left;
node_t *right;
size_t size;
};
struct value_node_t : public node_t
{
value_node_t(value_type const &v) : value(v)
{
}
template<class ...args_t> value_node_t(args_t &&...args) : value(std::forward<args_t>(args)...)
{
}
value_type value;
};
typedef typename allocator_type::template rebind<value_node_t>::other node_allocator_t;
struct root_node_t : public node_t, public key_compare, public node_allocator_t
{
template<class any_key_compare, class any_allocator_t> root_node_t(any_key_compare &&comp, any_allocator_t &&alloc) : key_compare(std::forward<any_key_compare>(comp)), node_allocator_t(std::forward<any_allocator_t>(alloc))
{
}
};
typedef typename allocator_type::template rebind<root_node_t>::other root_allocator_t;
struct head_t : public root_allocator_t
{
template<class any_allocator_t> head_t(any_allocator_t &&alloc) : root_allocator_t(std::forward<any_allocator_t>(alloc))
{
}
root_node_t *root;
};
public:
class iterator
{
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename size_balanced_tree::value_type value_type;
typedef typename size_balanced_tree::difference_type difference_type;
typedef typename size_balanced_tree::reference reference;
typedef typename size_balanced_tree::pointer pointer;
public:
explicit iterator(node_t *in_node) : node(in_node)
{
}
iterator(iterator const &) = default;
iterator &operator += (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, diff);
return *this;
}
iterator &operator -= (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, -diff);
return *this;
}
iterator operator + (difference_type diff) const
{
return iterator(size_balanced_tree::sbt_advance_(node, diff));
}
iterator operator - (difference_type diff) const
{
return iterator(size_balanced_tree::sbt_advance_(node, -diff));
}
difference_type operator - (iterator const &other) const
{
return static_cast<difference_type>(size_balanced_tree::sbt_rank_(node)) - static_cast<difference_type>(size_balanced_tree::sbt_rank_(other.node));
}
iterator &operator++()
{
node = size_balanced_tree::bst_move_<true>(node);
return *this;
}
iterator &operator--()
{
node = size_balanced_tree::bst_move_<false>(node);
return *this;
}
iterator operator++(int)
{
iterator save(*this);
++*this;
return save;
}
iterator operator--(int)
{
iterator save(*this);
--*this;
return save;
}
reference operator *() const
{
return static_cast<value_node_t *>(node)->value;
}
pointer operator->() const
{
return &static_cast<value_node_t *>(node)->value;
}
reference operator[](difference_type index) const
{
return *(*this + index);
}
bool operator > (iterator const &other) const
{
return *this - other > 0;
}
bool operator < (iterator const &other) const
{
return *this - other < 0;
}
bool operator >= (iterator const &other) const
{
return *this - other >= 0;
}
bool operator <= (iterator const &other) const
{
return *this - other <= 0;
}
bool operator == (iterator const &other) const
{
return node == other.node;
}
bool operator != (iterator const &other) const
{
return node != other.node;
}
private:
friend class size_balanced_tree;
node_t *node;
};
class const_iterator
{
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename size_balanced_tree::value_type value_type;
typedef typename size_balanced_tree::difference_type difference_type;
typedef typename size_balanced_tree::reference reference;
typedef typename size_balanced_tree::const_reference const_reference;
typedef typename size_balanced_tree::pointer pointer;
typedef typename size_balanced_tree::const_pointer const_pointer;
public:
explicit const_iterator(node_t *in_node) : node(in_node)
{
}
const_iterator(iterator const &other) : node(other.node)
{
}
const_iterator(const_iterator const &) = default;
const_iterator &operator += (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, diff);
return *this;
}
const_iterator &operator -= (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, -diff);
return *this;
}
const_iterator operator + (difference_type diff) const
{
return const_iterator(size_balanced_tree::sbt_advance_(node, diff));
}
const_iterator operator - (difference_type diff) const
{
return const_iterator(size_balanced_tree::sbt_advance_(node, -diff));
}
difference_type operator - (const_iterator const &other) const
{
return static_cast<difference_type>(size_balanced_tree::sbt_rank_(node)) - static_cast<difference_type>(size_balanced_tree::sbt_rank_(other.node));
}
const_iterator &operator++()
{
node = size_balanced_tree::bst_move_<true>(node);
return *this;
}
const_iterator &operator--()
{
node = size_balanced_tree::bst_move_<false>(node);
return *this;
}
const_iterator operator++(int)
{
const_iterator save(*this);
++*this;
return save;
}
const_iterator operator--(int)
{
const_iterator save(*this);
--*this;
return save;
}
const_reference operator *() const
{
return static_cast<value_node_t *>(node)->value;
}
const_pointer operator->() const
{
return &static_cast<value_node_t *>(node)->value;
}
const_reference operator[](difference_type index) const
{
return *(*this + index);
}
bool operator > (const_iterator const &other) const
{
return *this - other > 0;
}
bool operator < (const_iterator const &other) const
{
return *this - other < 0;
}
bool operator >= (const_iterator const &other) const
{
return *this - other >= 0;
}
bool operator <= (const_iterator const &other) const
{
return *this - other <= 0;
}
bool operator == (const_iterator const &other) const
{
return node == other.node;
}
bool operator != (const_iterator const &other) const
{
return node != other.node;
}
private:
friend class size_balanced_tree;
node_t *node;
};
class reverse_iterator
{
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename size_balanced_tree::value_type value_type;
typedef typename size_balanced_tree::difference_type difference_type;
typedef typename size_balanced_tree::reference reference;
typedef typename size_balanced_tree::pointer pointer;
public:
explicit reverse_iterator(node_t *in_node) : node(in_node)
{
}
explicit reverse_iterator(iterator const &other) : node(other.node)
{
++*this;
}
reverse_iterator(reverse_iterator const &) = default;
reverse_iterator &operator += (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, -diff);
return *this;
}
reverse_iterator &operator -= (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, diff);
return *this;
}
reverse_iterator operator + (difference_type diff) const
{
return reverse_iterator(size_balanced_tree::sbt_advance_(node, -diff));
}
reverse_iterator operator - (difference_type diff) const
{
return reverse_iterator(size_balanced_tree::sbt_advance_(node, diff));
}
difference_type operator - (reverse_iterator const &other) const
{
return static_cast<difference_type>(size_balanced_tree::sbt_rank_(other.node)) - static_cast<difference_type>(size_balanced_tree::sbt_rank_(node));
}
reverse_iterator &operator++()
{
node = size_balanced_tree::bst_move_<false>(node);
return *this;
}
reverse_iterator &operator--()
{
node = size_balanced_tree::bst_move_<true>(node);
return *this;
}
reverse_iterator operator++(int)
{
reverse_iterator save(*this);
++*this;
return save;
}
reverse_iterator operator--(int)
{
reverse_iterator save(*this);
--*this;
return save;
}
reference operator *() const
{
return static_cast<value_node_t *>(node)->value;
}
pointer operator->() const
{
return &static_cast<value_node_t *>(node)->value;
}
reference operator[](difference_type index) const
{
return *(*this + index);
}
bool operator > (reverse_iterator const &other) const
{
return *this - other > 0;
}
bool operator < (reverse_iterator const &other) const
{
return *this - other < 0;
}
bool operator >= (reverse_iterator const &other) const
{
return *this - other >= 0;
}
bool operator <= (reverse_iterator const &other) const
{
return *this - other <= 0;
}
bool operator == (reverse_iterator const &other) const
{
return node == other.node;
}
bool operator != (reverse_iterator const &other) const
{
return node != other.node;
}
iterator base() const
{
return ++iterator(node);
}
private:
friend class size_balanced_tree;
node_t *node;
};
class const_reverse_iterator
{
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename size_balanced_tree::value_type value_type;
typedef typename size_balanced_tree::difference_type difference_type;
typedef typename size_balanced_tree::reference reference;
typedef typename size_balanced_tree::const_reference const_reference;
typedef typename size_balanced_tree::pointer pointer;
typedef typename size_balanced_tree::const_pointer const_pointer;
public:
explicit const_reverse_iterator(node_t *in_node) : node(in_node)
{
}
explicit const_reverse_iterator(const_iterator const &other) : node(other.node)
{
++*this;
}
const_reverse_iterator(reverse_iterator const &other) : node(other.node)
{
}
const_reverse_iterator(const_reverse_iterator const &) = default;
const_reverse_iterator &operator += (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, -diff);
return *this;
}
const_reverse_iterator &operator -= (difference_type diff)
{
node = size_balanced_tree::sbt_advance_(node, diff);
return *this;
}
const_reverse_iterator operator + (difference_type diff) const
{
return const_reverse_iterator(size_balanced_tree::sbt_advance_(node, -diff));
}
const_reverse_iterator operator - (difference_type diff) const
{
return const_reverse_iterator(size_balanced_tree::sbt_advance_(node, diff));
}
difference_type operator - (const_reverse_iterator const &other) const
{
return static_cast<difference_type>(size_balanced_tree::sbt_rank_(other.node)) - static_cast<difference_type>(size_balanced_tree::sbt_rank_(node));
}
const_reverse_iterator &operator++()
{
node = size_balanced_tree::bst_move_<false>(node);
return *this;
}
const_reverse_iterator &operator--()
{
node = size_balanced_tree::bst_move_<true>(node);
return *this;
}
const_reverse_iterator operator++(int)
{
const_reverse_iterator save(*this);
++*this;
return save;
}
const_reverse_iterator operator--(int)
{
const_reverse_iterator save(*this);
--*this;
return save;
}
const_reference operator *() const
{
return static_cast<value_node_t *>(node)->value;
}
const_pointer operator->() const
{
return &static_cast<value_node_t *>(node)->value;
}
const_reference operator[](difference_type index) const
{
return *(*this + index);
}
bool operator > (const_reverse_iterator const &other) const
{
return *this - other > 0;
}
bool operator < (const_reverse_iterator const &other) const
{
return *this - other < 0;
}
bool operator >= (const_reverse_iterator const &other) const
{
return *this - other >= 0;
}
bool operator <= (const_reverse_iterator const &other) const
{
return *this - other <= 0;
}
bool operator == (const_reverse_iterator const &other) const
{
return node == other.node;
}
bool operator != (const_reverse_iterator const &other) const
{
return node != other.node;
}
const_iterator base() const
{
return ++iterator(node);
}
private:
friend class size_balanced_tree;
node_t *node;
};
protected:
//full
template<class in_root_allocator_t, class in_node_allocator_t> size_balanced_tree(key_compare const &comp, in_root_allocator_t &&root_alloc, in_node_allocator_t &&node_alloc) : head_(std::forward<in_root_allocator_t>(root_alloc))
{
head_.root = get_root_allocator_().allocate(1);
get_root_allocator_().construct(head_.root, comp, std::forward<in_node_allocator_t>(node_alloc));
set_size_(nil_(), 0);
set_root_(nil_());
set_most_left_(nil_());
set_most_right_(nil_());
}
public:
//empty
size_balanced_tree() : size_balanced_tree(key_compare(), allocator_type())
{
}
//empty
explicit size_balanced_tree(key_compare const &comp, allocator_type const &alloc = allocator_type()) : size_balanced_tree(comp, alloc, alloc)
{
}
//empty
explicit size_balanced_tree(allocator_type const &alloc) : size_balanced_tree(key_compare(), alloc, alloc)
{
}
//range
template <class iterator_t> size_balanced_tree(iterator_t begin, iterator_t end, key_compare const &comp = key_compare(), allocator_type const &alloc = allocator_type()) : size_balanced_tree(comp, alloc, alloc)
{
insert(begin, end);
}
//range
template <class iterator_t> size_balanced_tree(iterator_t begin, iterator_t end, allocator_type const &alloc = allocator_type()) : size_balanced_tree(key_compare(), alloc, alloc)
{
insert(begin, end);
}
//copy
size_balanced_tree(size_balanced_tree const &other) : size_balanced_tree(other.get_comparator_(), other.get_root_allocator_(), other.get_node_allocator_())
{
sbt_copy_<std::false_type>(nullptr, other.get_root_());
}
//copy
size_balanced_tree(size_balanced_tree const &other, allocator_type const &alloc) : size_balanced_tree(other.get_comparator_(), alloc, alloc)
{
sbt_copy_<std::false_type>(nullptr, other.get_root_());
}
//move
size_balanced_tree(size_balanced_tree &&other) : size_balanced_tree(key_compare(), other.get_root_allocator_(), node_allocator_t())
{
std::swap(get_root_allocator_(), other.get_root_allocator_());
std::swap(head_.root, other.head_.root);
}
//move
size_balanced_tree(size_balanced_tree &&other, allocator_type const &alloc) : size_balanced_tree(key_compare(), alloc, alloc)
{
sbt_copy_<std::true_type>(nullptr, other.get_root_());
}
//initializer list
size_balanced_tree(std::initializer_list<value_type> il, key_compare const &comp = key_compare(), allocator_type const &alloc = allocator_type()) : size_balanced_tree(il.begin(), il.end(), comp, alloc)
{
}
//initializer list
size_balanced_tree(std::initializer_list<value_type> il, allocator_type const &alloc) : size_balanced_tree(il.begin(), il.end(), key_compare(), alloc)
{
}
//destructor
~size_balanced_tree()
{
clear();
get_root_allocator_().destroy(head_.root);
get_root_allocator_().deallocate(head_.root, 1);
}
//copy
size_balanced_tree &operator = (size_balanced_tree const &other)
{
if(this == &other)
{
return *this;
}
if(get_node_allocator_() == other.get_node_allocator_())
{
size_balanced_tree tree_memory(get_comparator_(), get_root_allocator_(), get_node_allocator_());
std::swap(head_.root, tree_memory.head_.root);
get_comparator_() = other.get_comparator_();
get_node_allocator_() = other.get_node_allocator_();
sbt_copy_<std::false_type>(&tree_memory, other.get_root_());
}
else
{
clear();
get_comparator_() = other.get_comparator_();
get_node_allocator_() = other.get_node_allocator_();
sbt_copy_<std::false_type>(nullptr, other.get_root_());
}
return *this;
}
//move
size_balanced_tree &operator = (size_balanced_tree &&other)
{
if(this == &other)
{
return *this;
}
std::swap(head_, other.head_);
return *this;
}
//initializer list
size_balanced_tree &operator = (std::initializer_list<value_type> il)
{
size_balanced_tree tree_memory(get_comparator_(), get_root_allocator_(), get_node_allocator_());
std::swap(head_.root, tree_memory.head_.root);
typename std::initializer_list<value_type>::iterator it = il.begin();
while(!tree_memory.empty())
{
if(it == il.end())
{
return *this;
}
value_node_t *node = static_cast<value_node_t *>(tree_memory.get_root_());
tree_memory.sbt_erase_<true>(node);
get_node_allocator_().destroy(node);
get_node_allocator_().construct(node, *it++);
sbt_insert_hint_(nil_(), node);
}
insert(it, il.end());
return *this;
}
allocator_type get_allocator() const
{
return *head_.root;
}
void swap(size_balanced_tree &other)
{
std::swap(head_, other.head_);
}
typedef std::pair<iterator, iterator> pair_ii_t;
typedef std::pair<const_iterator, const_iterator> pair_cici_t;
//single element
iterator insert(value_type const &value)
{
check_max_size_();
return iterator(sbt_insert_<false>(sbt_create_node_(value)));
}
//single element
template<class in_value_t> typename std::enable_if<std::is_convertible<in_value_t, value_type>::value, iterator>::type insert(in_value_t &&value)
{
check_max_size_();
return iterator(sbt_insert_<false>(sbt_create_node_(std::forward<in_value_t>(value))));
}
//with hint
iterator insert(const_iterator hint, value_type const &value)
{
check_max_size_();
return iterator(sbt_insert_hint_(hint.node, sbt_create_node_(value)));
}
//with hint
template<class in_value_t> typename std::enable_if<std::is_convertible<in_value_t, value_type>::value, iterator>::type insert(const_iterator hint, in_value_t &&value)
{
check_max_size_();
return iterator(sbt_insert_hint_(hint.node, sbt_create_node_(std::forward<in_value_t>(value))));
}
//range
template<class iterator_t> void insert(iterator_t begin, iterator_t end)
{
for(; begin != end; ++begin)
{
emplace_hint(cend(), *begin);
}
}
//initializer list
void insert(std::initializer_list<value_type> il)
{
insert(il.begin(), il.end());
}
//single element
template<class ...args_t> iterator emplace(args_t &&...args)
{
check_max_size_();
return iterator(sbt_insert_<false>(sbt_create_node_(std::forward<args_t>(args)...)));
}
//with hint
template<class ...args_t> iterator emplace_hint(const_iterator hint, args_t &&...args)
{
check_max_size_();
return iterator(sbt_insert_hint_(hint.node, sbt_create_node_(std::forward<args_t>(args)...)));
}
iterator find(key_type const &key)
{
node_t *where = bst_lower_bound_(key);
return (is_nil_(where) || get_comparator_()(key, get_key_(where))) ? iterator(nil_()) : iterator(where);
}
const_iterator find(key_type const &key) const
{
node_t *where = bst_lower_bound_(key);
return (is_nil_(where) || get_comparator_()(key, get_key_(where))) ? const_iterator(nil_()) : const_iterator(where);
}
iterator erase(const_iterator where)
{
const_iterator pos = std::next(where);
sbt_erase_<false>(where.node);
sbt_destroy_node_(where.node);
return iterator(pos.node);
}
size_type erase(key_type const &key)
{
size_type erase_count = 0;
node_t *where = bst_lower_bound_(key);
while(!is_nil_(where) && !get_comparator_()(key, get_key_(where)))
{
node_t *next = bst_move_<true>(where);
erase(iterator(where));
where = next;
++erase_count;
}
return erase_count;
}
iterator erase(const_iterator erase_begin, const_iterator erase_end)
{
if(erase_begin == cbegin() && erase_end == cend())
{
clear();
return begin();
}
else
{
while(erase_begin != erase_end)
{
erase(erase_begin++);
}
return iterator(erase_begin.node);
}
}
size_type count(key_type const &key) const
{
pair_cici_t range = equal_range(key);
return std::distance(range.first, range.second);
}
size_type count(key_type const &min, key_type const &max) const
{
if(get_comparator_()(max, min))
{
return 0;
}
return sbt_rank_(bst_upper_bound_(max)) - sbt_rank_(bst_lower_bound_(min));
}
pair_ii_t range(key_type const &min, key_type const &max)
{
if(get_comparator_()(max, min))
{
return pair_ii_t(end(), end());
}
return pair_ii_t(iterator(bst_lower_bound_(min)), iterator(bst_upper_bound_(max)));
}
pair_cici_t range(key_type const &min, key_type const &max) const
{
if(get_comparator_()(max, min))
{
return pair_cici_t(cend(), cend());
}
return pair_cici_t(const_iterator(bst_lower_bound_(min)), const_iterator(bst_upper_bound_(max)));
}
//reverse index when index < 0
pair_ii_t slice(difference_type slice_begin = 0, difference_type slice_end = 0)
{
difference_type s_size = size();
if(slice_begin < 0)
{
slice_begin = std::max<difference_type>(s_size + slice_begin, 0);
}
if(slice_end <= 0)
{
slice_end = s_size + slice_end;
}
if(slice_begin > slice_end || slice_begin >= s_size)
{
return pair_ii_t(end(), end());
}
return pair_ii_t(at(slice_begin), at(slice_end));
}
//reverse index when index < 0
pair_cici_t slice(difference_type slice_begin = 0, difference_type slice_end = 0) const
{
difference_type s_size = size();
if(slice_begin < 0)
{
slice_begin = std::max<difference_type>(s_size + slice_begin, 0);
}
if(slice_end <= 0)
{
slice_end = s_size + slice_end;
}
if(slice_begin > slice_end || slice_begin >= s_size)
{
return pair_cici_t(cend(), cend());
}
return pair_cici_t(at(slice_begin), at(slice_end));
}
iterator lower_bound(key_type const &key)
{
return iterator(bst_lower_bound_(key));
}
const_iterator lower_bound(key_type const &key) const
{
return const_iterator(bst_lower_bound_(key));
}
iterator upper_bound(key_type const &key)
{
return iterator(bst_upper_bound_(key));
}
const_iterator upper_bound(key_type const &key) const
{
return const_iterator(bst_upper_bound_(key));
}
pair_ii_t equal_range(key_type const &key)
{
node_t *lower, *upper;
std::tie(lower, upper) = bst_equal_range_(key);
return pair_ii_t(iterator(lower), iterator(upper));
}
pair_cici_t equal_range(key_type const &key) const
{
node_t *lower, *upper;
std::tie(lower, upper) = bst_equal_range_(key);
return pair_cici_t(const_iterator(lower), const_iterator(upper));
}
iterator begin()
{
return iterator(get_most_left_());
}
iterator end()
{
return iterator(nil_());
}
const_iterator begin() const
{
return const_iterator(get_most_left_());
}
const_iterator end() const
{
return const_iterator(nil_());
}
const_iterator cbegin() const
{
return const_iterator(get_most_left_());
}
const_iterator cend() const
{
return const_iterator(nil_());
}
reverse_iterator rbegin()
{
return reverse_iterator(get_most_right_());
}
reverse_iterator rend()
{
return reverse_iterator(nil_());
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(get_most_right_());
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(nil_());
}
const_reverse_iterator crbegin() const
{
return const_reverse_iterator(get_most_right_());
}
const_reverse_iterator crend() const
{
return const_reverse_iterator(nil_());
}
reference front()
{
return static_cast<value_node_t *>(get_most_left_())->value;
}
reference back()
{
return static_cast<value_node_t *>(get_most_right_())->value;
}
const_reference front() const
{
return static_cast<value_node_t *>(get_most_left_())->value;
}
const_reference back() const
{
return static_cast<value_node_t *>(get_most_right_())->value;
}
bool empty() const
{
return is_nil_(get_root_());
}
void clear()
{
sbt_clear_(get_root_());
set_root_(nil_());
}
size_type size() const
{
return get_size_(get_root_());
}
size_type max_size() const
{
return node_allocator_t(get_node_allocator_()).max_size();
}
//if(index >= size) return end
iterator at(size_type index)
{
return iterator(sbt_at_(index));
}
//if(index >= size) return end
const_iterator at(size_type index) const
{
return const_iterator(sbt_at_(index));
}
//rank(begin) == 0, key rank
size_type rank(key_type const &key) const
{
return bst_lower_rank_(key);
}
//rank(begin) == 0, rank of iterator
static size_type rank(const_iterator where)
{
return sbt_rank_(where.node);
}
//rank(begin) == 0, key rank current best
size_type lower_rank(key_type const &key) const
{
return bst_lower_rank_(key);
}
//rank(begin) == 0, key rank when insert
size_type upper_rank(key_type const &key) const
{
return bst_upper_rank_(key);
}
protected:
head_t head_;
protected:
key_compare &get_comparator_()
{
return *head_.root;
}
key_compare const &get_comparator_() const
{
return *head_.root;
}
root_allocator_t &get_root_allocator_()
{
return head_;
}
root_allocator_t const &get_root_allocator_() const
{
return head_;
}
node_allocator_t &get_node_allocator_()
{
return *head_.root;
}
node_allocator_t const &get_node_allocator_() const
{
return *head_.root;
}
node_t *nil_() const
{
return head_.root;
}
node_t *get_root_() const
{
return get_parent_(nil_());
}
void set_root_(node_t *root)
{
set_parent_(nil_(), root);
}
node_t *get_most_left_() const
{
return get_left_(nil_());
}
void set_most_left_(node_t *left)
{
set_left_(nil_(), left);
}
node_t *get_most_right_() const
{
return get_right_(nil_());
}
void set_most_right_(node_t *right)
{
set_right_(nil_(), right);
}
static key_type const &get_key_(node_t *node)
{
return config_t::get_key(static_cast<value_node_t const *>(node)->value);
}