-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
1696 lines (1498 loc) · 50 KB
/
map.hpp
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
#ifndef MAP_HPP
#define MAP_HPP
# include <functional>
# include <memory>
# include "utility/pair.hpp"
# include "traits/iterator.hpp"
# include "algorithm/algorithm.hpp"
namespace ft
{
/**
* Forward declarations
*
*/
template<class T>
struct rb_iterator;
template<class T>
struct rb_const_iterator;
template<class T>
struct rb_reverse_iterator;
template<class T>
struct rb_const_reverse_iterator;
enum rb_color { RB_COLOR_BLACK = 1, RB_COLOR_RED, RB_COLOR_SENTINEL, RB_COLOR_NULL };
/**
* Internal struct representing a binary tree node.
*
*/
template<class T, class Alloc = std::allocator<T> >
struct rb_node {
typedef T value_type;
typedef typename ft::rb_node<T> node_type;
typedef Alloc allocator_type;
typedef typename std::allocator<node_type> node_allocator_type;
typedef typename node_allocator_type::reference reference;
typedef typename node_allocator_type::const_reference const_reference;
typedef typename node_allocator_type::pointer pointer;
typedef typename node_allocator_type::const_pointer const_pointer;
typedef typename std::size_t size_type;
value_type data;
pointer parent;
pointer left;
pointer right;
int color;
/**
* Default constructor
*
* Constructs a new empty node with default value_type() and all
* pointers to NULL.
*
* Color is always set to RED as it will always be inserted as a RED
* node.
*
*/
rb_node()
: data( value_type() ), parent( NULL ), left( NULL ), right( NULL ), color( RB_COLOR_RED ) { }
/**
* Data constructor
*
* Constructs a new node with data and all pointers to NULL.
*
* Color is always set to RED as it will always be inserted as a RED
* node.
*
*/
rb_node( const value_type &__data )
: data( __data ), parent( NULL ), left( NULL ), right( NULL ), color( RB_COLOR_RED ) { }
/**
* Data-Parent constructor
*
* Constructs a new node with data and a parent, right and left
* pointers are set to NULL.
*
* Color is always set to RED as it will always be inserted as a RED
* node.
*
*/
rb_node( const value_type &__data, pointer __parent )
: data( __data ), parent( __parent ), left( NULL ), right( NULL ), color( RB_COLOR_RED ) { }
/**
* Get the grand parent
*
* Returns the grand parent of the current node.
*
*/
pointer grand_parent() {
if ( this->parent == NULL )
return NULL;
return this->parent->parent;
}
/**
* Get uncle node
*
* Returns the uncle node of the current node.
*
*/
pointer uncle() {
pointer gp = this->grand_parent();
if ( gp == NULL )
return NULL;
return this->parent->sibling();
}
/**
* Get the sibling node
* rb_node
* Returns the sibling node.
*
*/
pointer sibling() {
if ( this->parent == NULL )
return NULL;
if ( this == this->parent->left )
return this->parent->right;
return this->parent->left;
}
bool is_sentinel() const {
return color == RB_COLOR_SENTINEL;
}
bool is_left() const {
if ( this->parent == NULL )
return false;
return this->parent->left == this;
}
bool is_right() const {
if ( this->parent == NULL )
return false;
return this->parent->right == this;
}
bool is_null_node() const {
return this->color == RB_COLOR_NULL;
}
void set_left( pointer node ){
this->left = node;
node->parent = this;
}
void set_right( pointer node ) {
this->right = node;
node->parent = this;
}
void detach() {
if ( this->parent ){
if ( this->is_left() ){
this->parent->left = NULL;
} else {
this->parent->right = NULL;
}
this->parent = NULL;
}
}
void assign( pointer node ){
if ( node ){
this->parent = node->parent;
this->color = node->color;
this->set_left(node->left);
this->set_right(node->right);
}
}
size_type max_size() const {
return node_allocator_type().max_size();
}
class Compare {
private:
pointer m_node;
public:
Compare(pointer node) : m_node(node) { }
Compare(const Compare &comp) : m_node( comp.m_node ) { }
Compare &operator=(const Compare &comp) {
m_node = comp.m_node;
return *this;
}
bool operator()(pointer node) const {
return m_node == node;
}
};
static Compare create_compare( pointer node ){
return Compare(node);
}
static pointer create_node( node_allocator_type alloc = node_allocator_type() ){
pointer node = alloc.allocate( 1 );
alloc.construct( node, rb_node() );
return node;
}
static pointer create_node( const value_type &data, node_allocator_type alloc = node_allocator_type() ){
pointer node = alloc.allocate( 1 );
alloc.construct( node, data );
return node;
}
static pointer create_node( const value_type &data, pointer parent, node_allocator_type alloc = node_allocator_type() ){
pointer node = alloc.allocate( 1 );
alloc.construct( node, rb_node( data, parent ) );
return node;
}
static pointer create_sentinel_node( node_allocator_type alloc = node_allocator_type() ){
pointer node = rb_node::create_node( alloc );
node->color = RB_COLOR_SENTINEL;
return node;
}
static pointer create_null_node(pointer parent, node_allocator_type alloc = node_allocator_type()){
pointer node = rb_node::create_node( alloc );
node->color = RB_COLOR_BLACK;
node->parent = parent;
node->left = NULL;
node->right = NULL;
return node;
}
static pointer create_null_node( node_allocator_type alloc = node_allocator_type() ){
pointer node = rb_node::create_node( alloc );
node->color = RB_COLOR_BLACK;
return node;
}
/**
* Destroy node
*
* Call the delete operator of the node and deallocate memory.
*/
static void destroy_node( pointer node, node_allocator_type alloc = node_allocator_type() ){
if ( node != NULL ){
alloc.destroy( node );
alloc.deallocate( node, 1 );
node = NULL;
}
}
};
/**
*
* Map
*
* Maps are associative containers that store elements formed by a combination of a
* key value and a mapped value, following a specific order.
*
* In a map, the key values are generally used to sort and uniquely identify the elements,
* while the mapped values store the content associated to this key. The types of key and
* mapped value may differ, and are grouped together in member type value_type, which is
* a pair type combining both:
*
* typedef pair<const Key, T> value_type;
*
* Internally, the elements in a map are always sorted by its key following a specific strict
* weak ordering criterion indicated by its internal comparison object (of type Compare).
*
* map containers are generally slower than unordered_map containers to access individual
* elements by their key, but they allow the direct iteration on subsets based on their order.
*
* The mapped values in a map can be accessed directly by their corresponding
* key using the bracket operator ((operator[]).
*
* Maps are typically implemented as binary search trees.
*
*/
template<
class Key, // map::key_type
class T, // map::mapped_type
class Compare = std::less<Key>, // map::key_compare
class Alloc = std::allocator<ft::pair<const Key, T> > // map::allocator_type
>
class map {
public:
typedef Key key_type;
typedef T mapped_type;
typedef ft::pair<const key_type, mapped_type> value_type;
typedef Compare key_compare;
typedef Alloc allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type;
typedef rb_iterator<value_type> iterator;
typedef rb_const_iterator<value_type> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
class value_compare : std::binary_function<value_type, value_type, bool> {
friend class map;
protected:
Compare m_comp;
value_compare(Compare comp) : m_comp(comp) {}
public:
typedef bool result_type;
typedef value_type first_argument_type;
typedef value_type second_argument_type;
bool operator()(const value_type &x, const value_type &y) const {
return m_comp(x.first, y.first);
}
};
private:
typedef rb_node<value_type> node_type;
typedef typename node_type::pointer node_pointer;
/**
* Member variables
*/
private:
node_pointer m_root;
size_type m_size;
key_compare m_comp;
allocator_type m_alloc;
node_pointer m_right_sentinel;
node_pointer m_left_sentinel;
node_pointer m_null;
typename node_type::Compare m_is_null_node;
/**
* Public member functions.
*
*/
public:
/**
* Construct map
*
* (1) empty container constructor (default constructor)
*
* Constructs an empty container, with no elements.
*/
explicit map( const key_compare &comp = key_compare(), const allocator_type &alloc = allocator_type() )
: m_root( NULL ),
m_size( 0 ),
m_comp( comp ),
m_alloc( alloc ),
m_right_sentinel( node_type::create_sentinel_node() ),
m_left_sentinel( node_type::create_sentinel_node() ),
m_null( node_type::create_null_node() ),
m_is_null_node( node_type::create_compare( m_null ) )
{
}
/**
* Construct map
*
* (2) range constructor
*
* Constructs a container with as many elements as the range [first,last), with each element constructed from its
* corresponding element in that range.
*/
template<class InputIterator>
map(InputIterator first, InputIterator last, const key_compare &comp = key_compare(), const allocator_type &alloc = allocator_type())
: m_root( NULL ),
m_size( 0 ),
m_comp( comp ),
m_alloc( alloc ),
m_right_sentinel( node_type::create_sentinel_node() ),
m_left_sentinel( node_type::create_sentinel_node() ),
m_null( node_type::create_null_node() ),
m_is_null_node( node_type::create_compare( m_null ) )
{
for ( ; first != last ; ++first ){
insert(*first);
}
}
/**
* Constructor map
*
* (3) copy constructor
*
* Constructs a container with a copy of each of the elements in x.
*/
map(const map &x) :
m_root(NULL),
m_size(0),
m_comp(x.m_comp),
m_alloc(x.m_alloc),
m_right_sentinel(node_type::create_sentinel_node()),
m_left_sentinel(node_type::create_sentinel_node()),
m_null(node_type::create_null_node()),
m_is_null_node(node_type::create_compare(m_null))
{
// Copy with breath first search, to get rid of rebalancing ?
// When x is copied, the traversal will be in order, that will cause
// rebalancing.
this->insert(x.begin(), x.end());
}
/**
* Copy container content
*
* Assigns new contents to the container, replacing its current content.
*
* Copies all the elements from x into the container, changing its size accordingly.
*
* The container preserves its current allocator, which is used to allocate additional storage if needed.
*/
map &operator=(const map &x){
if ( this == &x ){
return *this;
}
// Delete previous
this->clear();
// Copy
m_comp = x.m_comp;
this->insert(x.begin(), x.end());
return *this;
}
~map() {
clear();
node_type::destroy_node(m_right_sentinel);
node_type::destroy_node(m_left_sentinel);
node_type::destroy_node(m_null);
}
/**
* Insert elements
*
* Extends the container by inserting new elements, effectively increasing the container
* size by the number of elements inserted.
*
* Because element keys in a map are unique, the insertion operation checks whether each
* inserted element has a key equivalent to the one of an element already in the container,
* and if so, the element is not inserted, returning an iterator to this existing element
* (if the function returns a value).
*
* For a similar container allowing for duplicate elements, see multimap.
*
* An alternative way to insert elements in a map is by using member function map::operator[].
*
* Internally, map containers keep all their elements sorted by their key following the criterion
* specified by its comparison object. The elements are always inserted in its respective position
* following this ordering.
*
*/
ft::pair<iterator, bool> insert(const value_type &val){
ft::pair<iterator, bool> ret;
if ( m_root == NULL ){
m_size++;
m_root = node_type::create_node( val );
m_root->set_right(m_right_sentinel);
m_root->set_left(m_left_sentinel);
return ft::pair<iterator, bool>( iterator( m_root ), true );
}
ret = insert_recursive_(m_root, val);
if ( ret.second ){
m_size++;
this->rb_insert_fix_tree_(ret.first.m_ptr);
}
return ret;
}
template<class InputIterator>
void insert(InputIterator first, InputIterator last){
for ( ; first != last ; ++first ){
this->insert(*first);
}
}
iterator insert(iterator position, const value_type &val){
iterator curr = position;
iterator next = position;
ft::pair<iterator, bool> result;
// If the position point to the end, insert normally
if ( size() == 0 || position == this->end() || position == (--this->begin()) ){
return this->insert(val).first;
}
// Otherwise, try another strategy, check that the val is within the range (position, position + 1)
// until a valid range is found, where node will be inserted.
// Check if greater or lower to determine the direction
if ( is_equal_val_(*position, val) ){
return position;
}
if ( this->value_comp()(*position, val) ){
// Go to right
while ( next != this->end() ) {
curr = next++;
// Not found, insert at the end
if ( next == this->end() )
break;
if ( is_equal_val_(*curr, val) )
return curr;
if ( this->value_comp()(*curr, val) && this->value_comp()(val, *next) )
break;
}
} else {
// Go to left
while ( next != this->begin() ){
curr = next--;
// Not found, insert at the end
if ( next == this->begin() )
break;
if ( is_equal_val_(*curr, val) )
return curr;
if ( this->value_comp()(val, *curr) && this->value_comp()(*next, val) )
break;
}
}
result = insert_recursive_(curr.m_ptr, val);
if ( result.second )
m_size++;
return result.first;
}
/**
* Access element
*
* If k matches the key of an element in the container, the function returns a reference to its mapped value.
* If k does not match the key of any element in the container, the function inserts a new element with that
* key and returns a reference to its mapped value. Notice that this always increases the container size by
* one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
* A similar member function, map::at, has the same behavior when an element with the key exists,
* but throws an exception when it does not.
*
*/
mapped_type& operator[](const key_type& k) {
return (*((this->insert(ft::make_pair(k,mapped_type()))).first)).second;
}
/**
* Clear content
*
* Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.
*/
void clear() {
if ( m_root ){
clear_recursive_(m_root);
m_root = NULL;
}
m_size = 0;
}
/**
* Get iterator to element
*
* Searches the container for an element with a key equivalent to k and returns an iterator to it if found,
* otherwise it returns an iterator to map::end.
*
* Two keys are considered equivalent if the container's comparison object returns false reflexively
* (i.e., no matter the order in which the elements are passed as arguments).
* Another member function, map::count, can be used to just check whether a particular key exists.
*/
iterator find(const key_type &k){
iterator found = this->lower_bound(k);
return ( found == this->end() || (m_comp(k, (*found).first))) ? this->end() : found;
}
/**
* Get iterator to element
*
* Searches the container for an element with a key equivalent to k and returns an iterator to it if found,
* otherwise it returns an iterator to map::end.
*
* Two keys are considered equivalent if the container's comparison object returns false reflexively
* (i.e., no matter the order in which the elements are passed as arguments).
* Another member function, map::count, can be used to just check whether a particular key exists.
*/
const_iterator find(const key_type &k) const {
const_iterator found = this->lower_bound(k);
return ( found == this->end() || (m_comp(k, (*found).first))) ? this->end() : found;
}
/**
* Erase elements
*
* Removes from the map container either a single element or a range of elements ([first,last)).
*
* This effectively reduces the container size by the number of elements removed, which are destroyed.
*/
void erase(iterator position){
if ( position != this->end() ){
node_pointer target = position.m_ptr;
node_pointer successor;
int prev_color = target->color;
bool is_root = target == m_root;
if ( target->left != NULL && target->right != NULL ){
// Case 3, node has children
// Go to the right subtree, then find the min (go to leftmost)
if ( target->left->is_sentinel() && target->right->is_sentinel() ){
m_root = NULL;
successor = NULL;
} else {
ft::pair<node_pointer, int> ret = detach_node_(target);
prev_color = ret.second;
successor = (!is_root) ? ret.first : m_root;
}
}
else if ( target->left != NULL || target->right != NULL){
// Case 2, target has one child
successor = (target->right != NULL) ? target->right : target->left;
if ( target->is_left() ){
target->parent->set_left(successor);
} else {
target->parent->set_right(successor);
}
if ( successor->is_sentinel() )
successor = target->parent;
}
else {
// Case 1, target has no children
m_null->parent = target->parent;
successor = m_null;
if ( target->is_left() ){
target->parent->left = m_null;
} else {
target->parent->right = m_null;
}
}
node_type::destroy_node( target );
m_size--;
if ( m_root != NULL && !m_root->left->is_sentinel() && !m_root->right->is_sentinel() &&
prev_color == RB_COLOR_BLACK ){
this->rb_erase_fix_(successor);
}
m_null->detach();
}
}
/**
* Erase elements
*
* Removes from the map container either a single element or a range of elements ([first,last)).
*
* This effectively reduces the container size by the number of elements removed, which are destroyed.
*/
size_type erase(const key_type &k){
iterator it = this->find( k );
if ( it == this->end() )
return 0;
this->erase( it );
return 1;
}
/**
* Erase elements
*
* Removes from the map container either a single element or a range of elements ([first,last)).
*
* This effectively reduces the container size by the number of elements removed, which are destroyed.
*/
void erase(iterator first, iterator last){
while ( first != last ){
this->erase((*first++).first);
}
}
/**
* Swap content
*
* Exchanges the content of the container by the content of x, which is another map of the same type.
* Sizes may differ.
*
* After the call to this member function, the elements in this container are those which were in x
* before the call, and the elements of x are those which were in this.
* All iterators, references and pointers remain valid for the swapped objects.
*
* Notice that a non-member function exists with the same name, swap, overloading that algorithm
* with an optimization that behaves like this member function.
*
*/
void swap(map &x){
node_pointer tmp_root = x.m_root;
size_type tmp_size = x.m_size;
key_compare tmp_comp = x.m_comp;
allocator_type tmp_alloc = x.m_alloc;
node_pointer tmp_right_sentinel = x.m_right_sentinel;
node_pointer tmp_left_sentinel = x.m_left_sentinel;
typename node_type::Compare tmp_is_null_node = x.m_is_null_node;
x.m_root = this->m_root;
x.m_size = this->m_size;
x.m_comp = this->m_comp;
x.m_alloc = this->m_alloc;
x.m_right_sentinel = this->m_right_sentinel;
x.m_left_sentinel = this->m_left_sentinel;
x.m_is_null_node = this->m_is_null_node;
this->m_root = tmp_root;
this->m_size = tmp_size;
this->m_comp = tmp_comp;
this->m_alloc = tmp_alloc;
this->m_right_sentinel = tmp_right_sentinel;
this->m_left_sentinel = tmp_left_sentinel;
this->m_is_null_node = tmp_is_null_node;
}
/**
* Return container size
*
* Returns the number of elements in the map container.
*/
size_type size() const {
return m_size;
}
/**
* Test whether container is empty
*
* Returns whether the map container is empty (i.e. whether its size is 0).
*
* This function does not modify the container in any way. To clear the content of a map container,
* see map::clear.
*/
bool empty() const {
return m_size == 0;
}
/**
* Return maximum size
*
* Returns the maximum number of elements that the map container can hold.
*
* This is the maximum potential size the container can reach due to known
* system or library implementation limitations, but the container is by
* no means guaranteed to be able to reach that size: it can still fail to
* allocate storage at any point before that size is reached.
*/
size_type max_size() const {
return ( node_type().max_size() );
}
/**
* Return iterator to begining
*
* Returns an iterator referring to the first element in the map container
* Because map containers keep their elements ordered at all times, begin points to the element that goes
* first following the container's sorting criterion
* If the container is empty, the returned iterator value shall not be dereferenced.
*
*/
iterator begin(){
if ( m_root == NULL ){
return end();
}
return iterator( m_left_sentinel->parent );
}
/**
* Return iterator to begining
*
* Returns an iterator referring to the first element in the map container
* Because map containers keep their elements ordered at all times, begin points to the element that goes
* first following the container's sorting criterion
* If the container is empty, the returned iterator value shall not be dereferenced.
*
*/
const_iterator begin() const {
if ( m_root == NULL ){
return end();
}
return const_iterator( m_left_sentinel->parent );
}
/**
* Return reverse iterator to reverse beginning
*
* Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning).
*
* Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container.
*
* rbegin points to the element preceding the one that would be pointed to by member end.
*/
reverse_iterator rbegin() {
if ( m_root == NULL ){
return rend();
}
return reverse_iterator( end() );
}
/**
* Return reverse iterator to reverse beginning
*
* Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning).
*
* Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container.
*
* rbegin points to the element preceding the one that would be pointed to by member end.
*/
const_reverse_iterator rbegin() const {
if ( m_root == NULL ){
return rend();
}
return const_reverse_iterator( end() );
}
/**
* Return iterator to end
*
* Returns an iterator referring to the past-the-end element in the map container.
*
* The past-the-end element is the theoretical element that would follow the last
* element in the map container. It does not point to any element, and thus shall not be dereferenced.
*
* Because the ranges used by functions of the standard library do not include the element pointed
* by their closing iterator, this function is often used in combination with map::begin to specify
* a range including all the elements in the container.
*
* If the container is empty, this function returns the same as map::begin.
*/
iterator end() {
return iterator( m_right_sentinel );
}
/**
* Return iterator to end
*
* Returns an iterator referring to the past-the-end element in the map container.
*
* The past-the-end element is the theoretical element that would follow the last
* element in the map container. It does not point to any element, and thus shall not be dereferenced.
*
* Because the ranges used by functions of the standard library do not include the element pointed
* by their closing iterator, this function is often used in combination with map::begin to specify
* a range including all the elements in the container.
*
* If the container is empty, this function returns the same as map::begin.
*/
const_iterator end() const {
return const_iterator( m_right_sentinel );
}
/**
* Return reverse iterator to reverse end
*
* Returns a reverse iterator pointing to the theoretical element right before the first element in the map container
* (which is considered its reverse end).
* The range between map::rbegin and map::rend contains all the elements of the container (in reverse order).
*/
reverse_iterator rend() {
return ( reverse_iterator( begin() ) );
}
/**
* Return reverse iterator to reverse end
*
* Returns a reverse iterator pointing to the theoretical element right before the first element in the map container
* (which is considered its reverse end).
* The range between map::rbegin and map::rend contains all the elements of the container (in reverse order).
*/
const_reverse_iterator rend() const {
return ( const_reverse_iterator( begin() ) );
}
/**
* Return iterator to upper bound
*
* Returns an iterator pointing to the first element in the container whose key is considered to go after k.
*
* The function uses its internal comparison object (key_comp) to determine this, returning an iterator to
* the first element for which key_comp(k,element_key) would return true.
*
* If the map class is instantiated with the default comparison type (less), the function returns an iterator
* to the first element whose key is greater than k.
*
* A similar member function, lower_bound, has the same behavior as upper_bound, except in the case that the
* map contains an element with a key equivalent to k: In this case lower_bound returns an iterator pointing
* to that element, whereas upper_bound returns an iterator pointing to the next element.
*/
iterator upper_bound(const key_type &k){
node_pointer y = NULL;
node_pointer x = m_root;
while ( x != NULL && !x->is_sentinel() ){
if ( m_comp(k, x->data.first) ){
y = x;
x = x->left;
} else {
x = x->right;
}
}
return (y == NULL) ? this->end() : iterator(y);
}
const_iterator upper_bound(const key_type &k) const {
node_pointer y = NULL;
node_pointer x = m_root;
while ( x != NULL && !x->is_sentinel() ){
if ( m_comp(k, x->data.first) ){
y = x;
x = x->left;
} else {
x = x->right;
}
}
return (y == NULL) ? this->end() : const_iterator(y);
}
/**
* Return iterator to lower bound
*
* Returns an iterator pointing to the first element in the container whose key is not considered to go before
* k (i.e., either it is equivalent or goes after).
*
* The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the
* first element for which key_comp(element_key,k) would return false.
* If the map class is instantiated with the default comparison type (less), the function returns an iterator to the first element whose key is not less than k.
* A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the map contains an element with a key equivalent to k: In this case, lower_bound returns an iterator pointing to that element, whereas upper_bound returns an iterator pointing to the next element.
*/
iterator lower_bound(const key_type &k){
node_pointer y = NULL;
node_pointer x = m_root;
while ( x != NULL && !x->is_sentinel() ){
if ( !m_comp(x->data.first, k) ){
y = x;
x = x->left;
} else {
x = x->right;
}
}
return (y == NULL) ? this->end() : iterator(y);
}
const_iterator lower_bound(const key_type &k) const {
node_pointer y = NULL;
node_pointer x = m_root;
while ( x != NULL && !x->is_sentinel() ){
if ( !m_comp(x->data.first, k) ){
y = x;
x = x->left;
} else {
x = x->right;
}
}
return (y == NULL) ? this->end() : const_iterator(y);
}
/**
* Get range of equal elements
*
* Returns the bounds of a range that includes all the elements in the container which have a key equivalent to k.
*
* Because the elements in a map container have unique keys, the range returned will contain a single element at most.
*
* If no matches are found, the range returned has a length of zero, with both iterators pointing to the first element
* that has a key considered to go after k according to the container's internal comparison object (key_comp).
*
* Two keys are considered equivalent if the container's comparison object returns false reflexively
* (i.e., no matter the order in which the keys are passed as arguments).
*/
ft::pair<iterator, iterator> equal_range(const key_type& k) {
return ft::pair<iterator, iterator>( this->lower_bound(k), this->upper_bound(k) );
}
ft::pair<const_iterator, const_iterator> equal_range(const key_type& k) const {
return ft::pair<const_iterator, const_iterator>( this->lower_bound(k), this->upper_bound(k) );
}
/**
* Count elements with a specific key
*
* Searches the container for elements with a key equivalent to k and returns the number of matches.
*
* Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).
* Two keys are considered equivalent if the container's comparison object returns false reflexively
* (i.e., no matter the order in which the keys are passed as arguments).
*/
size_type count(const key_type &k) const {