-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.hpp
750 lines (663 loc) · 24.2 KB
/
bst.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
#pragma once
#include <memory>
#include "algorithm.hpp"
#include "functional.hpp"
#include "iterator.hpp"
#include "type_traits.hpp"
#include "utility.hpp"
#include "utils.hpp"
namespace ft {
// KeyOfValue is a unary function object that returns
// the first type of a pair
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
class bst {
protected:
struct bst_node;
typedef typename Allocator::template rebind<bst_node>::other
bst_node_allocator_type;
typedef typename bst_node_allocator_type::pointer bst_node_pointer;
static bst_node_allocator_type bst_node_allocator;
public:
// types:
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef typename bst_node_allocator_type::size_type size_type;
typedef typename bst_node_allocator_type::difference_type difference_type;
typedef Key key_type;
typedef Value value_type;
typedef Allocator allocator_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef Compare key_compare;
template <bool isconst = false>
class bst_iterator {
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef Value value_type;
typedef
typename bst_node_allocator_type::difference_type difference_type;
typedef typename bst_node_allocator_type::size_type size_type;
typedef typename ft::choose<isconst, const Value &, Value &>::type
reference;
typedef
typename ft::choose<isconst, const Value *, Value *>::type pointer;
bst_iterator(bst_node_pointer current = 0, bst_node_pointer root = 0)
: current(current), root(root) {}
bst_iterator(const bst_iterator<false> &rhs)
: current(rhs.base()), root(rhs.getRoot()) {}
reference operator*() const {
return current->value;
}
pointer operator->() const {
return &(current->value);
}
bst_iterator &operator++() {
if (current->right != NULL) {
current = current->right;
while (current->left != NULL) {
current = current->left;
}
} else {
bst_node_pointer trailing = current->parent;
while (trailing != NULL && current == trailing->right) {
current = trailing;
trailing = trailing->parent;
}
current = trailing;
}
return *this;
}
bst_iterator operator++(int) {
bst_iterator tmp = *this;
++*this;
return tmp;
}
bst_iterator &operator--() {
if (current == NULL && root) {
// Case when current == end()
// For the reverse iterator where rbegin() == end()
current = root;
while (current->right) {
current = current->right;
}
} else if (current->left != NULL) {
current = current->left;
while (current->right != NULL) {
current = current->right;
}
} else {
bst_node_pointer trailing = current->parent;
while (trailing != NULL && current == trailing->left) {
current = trailing;
trailing = trailing->parent;
}
current = trailing;
}
return *this;
}
bst_iterator operator--(int) {
bst_iterator tmp = *this;
--*this;
return tmp;
}
friend bool operator==(const bst_iterator &x, const bst_iterator &y) {
return x.current == y.current;
}
friend bool operator!=(const bst_iterator &x, const bst_iterator &y) {
return !(x.current == y.current);
}
bst_node_pointer base() const {
return current;
}
bst_node_pointer getRoot() const {
return root;
}
protected:
bst_node_pointer current;
bst_node_pointer root;
};
typedef bst_iterator<false> iterator;
typedef bst_iterator<true> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
// construct/copy/destroy
explicit bst(const Compare &comp = Compare(),
const Allocator &allocator = Allocator());
template <class InputIterator>
bst(typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last, const Compare &comp = Compare(),
const Allocator &allocator = Allocator());
bst(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x);
~bst();
bst<Key, Value, KeyOfValue, Compare, Allocator> &operator=(
const bst<Key, Value, KeyOfValue, Compare, Allocator> &x);
allocator_type get_allocator() const;
// iterators:
iterator begin();
iterator begin() const;
iterator end();
iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
// capacity:
bool empty() const;
size_type size() const;
size_type max_size() const;
// modifiers:
ft::pair<iterator, bool> insert(const value_type &x);
iterator insert(iterator position, const value_type &x);
template <class InputIterator>
void insert(typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last);
void erase(iterator position);
size_type erase(const key_type &x);
void erase(iterator first, iterator last);
void swap(bst<Key, Value, KeyOfValue, Compare, Allocator> &x);
void clear();
// observers:
Compare key_comp() const;
// operations:
iterator find(const key_type &x);
const_iterator find(const key_type &x) const;
size_type count(const key_type &x) const;
iterator lower_bound(const key_type &x);
const_iterator lower_bound(const key_type &x) const;
iterator upper_bound(const key_type &x);
const_iterator upper_bound(const key_type &x) const;
ft::pair<iterator, iterator> equal_range(const key_type &x);
ft::pair<const_iterator, const_iterator> equal_range(
const key_type &x) const;
protected:
allocator_type m_allocator;
size_type m_size;
bst_node_pointer m_root;
Compare m_key_compare;
bst_node_pointer m_allocate_bst_node() const;
void m_transplant(bst_node_pointer u, bst_node_pointer v);
bool m_equal_keys(const value_type &x, const value_type &y) const;
bst_node_pointer m_copy(bst_node_pointer src, bst_node_pointer dst_parent);
static bst_node_pointer m_minimum(bst_node_pointer x);
static bst_node_pointer m_maximum(bst_node_pointer x);
};
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
struct bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node {
typedef bst<Key, Value, KeyOfValue, Compare, Allocator> self_type;
self_type::bst_node_pointer parent;
self_type::bst_node_pointer left;
self_type::bst_node_pointer right;
// Pair of key, mapped_type
Value value;
};
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node_pointer
bst<Key, Value, KeyOfValue, Compare, Allocator>::m_minimum(bst_node_pointer x) {
while (x != NULL && x->left) {
x = x->left;
}
return x;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node_pointer
bst<Key, Value, KeyOfValue, Compare, Allocator>::m_maximum(bst_node_pointer x) {
while (x != NULL && x->right) {
x = x->right;
}
return x;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool bst<Key, Value, KeyOfValue, Compare, Allocator>::m_equal_keys(
const value_type &x, const value_type &y) const {
return !m_key_compare(KeyOfValue()(x), KeyOfValue()(y)) &&
!m_key_compare(KeyOfValue()(y), KeyOfValue()(x));
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node_pointer
bst<Key, Value, KeyOfValue, Compare, Allocator>::m_copy(
bst_node_pointer src, bst_node_pointer dst_parent) {
if (src == NULL) {
return NULL;
}
bst_node_pointer dst = m_allocate_bst_node();
m_allocator.construct(m_allocator.address(dst->value), src->value);
dst->parent = dst_parent;
dst->left = m_copy(src->left, dst);
dst->right = m_copy(src->right, dst);
return dst;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare,
Allocator>::bst_node_allocator_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node_allocator;
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::bst_node_pointer
bst<Key, Value, KeyOfValue, Compare, Allocator>::m_allocate_bst_node() const {
bst_node_pointer node = bst_node_allocator.allocate(1, this);
node->parent = NULL;
node->left = NULL;
node->right = NULL;
return node;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::bst(
const Compare &comp, const Allocator &allocator)
: m_allocator(allocator), m_size(0), m_root(NULL), m_key_compare(comp) {}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
template <class InputIterator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::bst(
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last, const Compare &comp, const Allocator &allocator)
: m_allocator(allocator), m_size(0), m_root(NULL), m_key_compare(comp) {
insert(first, last);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::bst(
const bst<Key, Value, KeyOfValue, Compare, Allocator> &x)
: m_allocator(x.get_allocator()), m_size(0), m_root(NULL),
m_key_compare(x.m_key_compare) {
m_root = m_copy(x.m_root, NULL);
m_size = x.m_size;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::~bst() {
erase(begin(), end());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bst<Key, Value, KeyOfValue, Compare, Allocator> &
bst<Key, Value, KeyOfValue, Compare, Allocator>::operator=(
const bst<Key, Value, KeyOfValue, Compare, Allocator> &x) {
if (this != &x) {
erase(begin(), end());
m_root = m_copy(x.m_root, NULL);
m_size = x.m_size;
}
return *this;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::allocator_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::get_allocator() const {
return m_allocator;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::begin() {
return iterator(m_minimum(m_root), m_root);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::begin() const {
return iterator(m_minimum(m_root), m_root);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::end() {
return iterator(NULL, m_root);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::end() const {
return iterator(NULL, m_root);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::reverse_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::rbegin() {
return reverse_iterator(end());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_reverse_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::rbegin() const {
return reverse_iterator(end());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::reverse_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::rend() {
return reverse_iterator(begin());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_reverse_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::rend() const {
return reverse_iterator(begin());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool bst<Key, Value, KeyOfValue, Compare, Allocator>::empty() const {
return m_size == 0;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::size_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::size() const {
return m_size;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::size_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::max_size() const {
return bst_node_allocator.max_size();
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
ft::pair<typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator,
bool>
bst<Key, Value, KeyOfValue, Compare, Allocator>::insert(
const value_type &value) {
bst_node_pointer traverse = m_root;
bst_node_pointer trailing = NULL;
while (traverse != NULL && !m_equal_keys(value, traverse->value)) {
trailing = traverse;
bool comp =
m_key_compare(KeyOfValue()(value), KeyOfValue()(traverse->value));
traverse = comp ? traverse->left : traverse->right;
}
if (traverse != NULL) {
return ft::make_pair(iterator(traverse, m_root), false);
}
bst_node_pointer node = m_allocate_bst_node();
m_allocator.construct(m_allocator.address(node->value), value);
node->parent = trailing;
++m_size;
if (trailing == NULL) {
m_root = node;
} else if (m_key_compare(
KeyOfValue()(value), KeyOfValue()(trailing->value))) {
trailing->left = node;
} else {
trailing->right = node;
}
return ft::make_pair(iterator(node, m_root), true);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::insert(
iterator position, const value_type &x) {
// TODO Optimize insert based on position
(void)position;
return insert(x).first;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
template <class InputIterator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::insert(
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last) {
while (first != last) {
insert(*first++);
}
}
// Make current->parent point to descendent
// And descendent->parent point to current->parent;
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::m_transplant(
bst_node_pointer current, bst_node_pointer descendent) {
if (current->parent == NULL) {
m_root = descendent;
} else if (current == current->parent->left) {
current->parent->left = descendent;
} else {
current->parent->right = descendent;
}
if (descendent != NULL) {
descendent->parent = current->parent;
}
}
// From https://www.cs.dartmouth.edu/~thc/cs10/lectures/0428/0428.html
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::erase(iterator position) {
bst_node_pointer node = position.base();
if (node->left == NULL) {
m_transplant(node, node->right);
} else if (node->right == NULL) {
m_transplant(node, node->left);
} else {
bst_node_pointer successor = m_minimum(node->right);
if (successor->parent != node) {
m_transplant(successor, successor->right);
successor->right = node->right;
successor->right->parent = successor;
}
m_transplant(node, successor);
successor->left = node->left;
successor->left->parent = successor;
}
m_allocator.destroy(m_allocator.address(node->value));
bst_node_allocator.deallocate(node, 1);
--m_size;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::size_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::erase(const key_type &x) {
iterator it = find(x);
if (it == end()) {
return 0;
}
erase(it);
return 1;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::erase(
iterator first, iterator last) {
while (first != last) {
erase(first++);
}
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::swap(
bst<Key, Value, KeyOfValue, Compare, Allocator> &x) {
ft::swap(m_allocator, x.m_allocator);
ft::swap(m_size, x.m_size);
ft::swap(m_root, x.m_root);
ft::swap(m_key_compare, x.m_key_compare);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void bst<Key, Value, KeyOfValue, Compare, Allocator>::clear() {
erase(begin(), end());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::key_compare
bst<Key, Value, KeyOfValue, Compare, Allocator>::key_comp() const {
return m_key_compare;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::find(const key_type &key) {
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
const bool comp = m_key_compare(key, KeyOfValue()(traverse->value));
const bool equalKeys =
!comp && !m_key_compare(KeyOfValue()(traverse->value), key);
if (equalKeys) {
break;
}
traverse = comp ? traverse->left : traverse->right;
}
return iterator(traverse);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::find(
const key_type &key) const {
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
const bool comp = m_key_compare(key, KeyOfValue()(traverse->value));
const bool equalKeys =
!comp && !m_key_compare(KeyOfValue()(traverse->value), key);
if (equalKeys) {
break;
}
traverse = comp ? traverse->left : traverse->right;
}
return const_iterator(traverse);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::size_type
bst<Key, Value, KeyOfValue, Compare, Allocator>::count(
const key_type &key) const {
return find(key) == end() ? 0 : 1;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::lower_bound(
const key_type &key) {
bst_node_pointer trailing = NULL;
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
if (!m_key_compare(KeyOfValue()(traverse->value), key)) {
trailing = traverse;
traverse = traverse->left;
} else {
traverse = traverse->right;
}
}
return iterator(trailing);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::lower_bound(
const key_type &key) const {
bst_node_pointer trailing = NULL;
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
if (!m_key_compare(KeyOfValue()(traverse->value), key)) {
trailing = traverse;
traverse = traverse->left;
} else {
traverse = traverse->right;
}
}
return const_iterator(trailing);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::upper_bound(
const key_type &key) {
bst_node_pointer trailing = NULL;
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
if (m_key_compare(key, KeyOfValue()(traverse->value))) {
trailing = traverse;
traverse = traverse->left;
} else {
traverse = traverse->right;
}
}
return iterator(trailing);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_iterator
bst<Key, Value, KeyOfValue, Compare, Allocator>::upper_bound(
const key_type &key) const {
bst_node_pointer trailing = NULL;
bst_node_pointer traverse = m_root;
while (traverse != NULL) {
if (m_key_compare(key, KeyOfValue()(traverse->value))) {
trailing = traverse;
traverse = traverse->left;
} else {
traverse = traverse->right;
}
}
return const_iterator(trailing);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
ft::pair<typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator,
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::iterator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::equal_range(
const key_type &key) {
return ft::make_pair(lower_bound(key), upper_bound(key));
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
ft::pair<
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_iterator,
typename bst<Key, Value, KeyOfValue, Compare, Allocator>::const_iterator>
bst<Key, Value, KeyOfValue, Compare, Allocator>::equal_range(
const key_type &key) const {
return ft::make_pair(lower_bound(key), upper_bound(key));
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator==(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return x.size() == y.size() && ft::equal(x.begin(), x.end(), y.begin());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator!=(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return !(x == y);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator<(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return ft::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator>(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return y < x;
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator<=(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return !(y < x);
}
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
bool operator>=(const bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
const bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
return !(x < y);
}
// specialized algorithms:
template <class Key, class Value, class KeyOfValue, class Compare,
class Allocator>
void swap(bst<Key, Value, KeyOfValue, Compare, Allocator> &x,
bst<Key, Value, KeyOfValue, Compare, Allocator> &y) {
x.swap(y);
}
} // namespace ft