-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.hpp
1711 lines (1664 loc) · 85 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
// Map implementation -*- C++ -*-
// Date: 2019.29.10.
// Author: Vardan Grigoryan.
//
// This library is free software; you can redistribute it and/or
// modify it as it published by the author.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
/**
* @file nmap.hpp
* @brief Map implementation -*- C++ -*-
* @detail This library is free software. You can redistribute it and/or modify it as it published by the author
* @author Vardan Grigoryan
*/
#include <cassert>
#include <memory>
#include <vector>
namespace nstd {
typedef void default_type;
template <typename KeyType,
typename DataType,
template <typename> class comparator,
template <typename T, typename ALLOC> class CONT,
typename AllocatorType>
class map;
template <typename iter_type>
struct generic_iterator_traits;
template <>
struct generic_iterator_traits<default_type> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return nullptr;
}
};
struct inorder_iterator_traits;
template <>
struct generic_iterator_traits<inorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->inorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->inorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->inorder_predecessor(_node_);
}
};
struct preorder_iterator_traits;
template <>
struct generic_iterator_traits<preorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->preorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->preorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->preorder_predecessor(_node_);
}
};
struct postorder_iterator_traits;
template <>
struct generic_iterator_traits<postorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->postorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->postorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->postorder_predecessor(_node_);
}
};
template <typename iter_type>
struct const_generic_iterator_traits;
template <>
struct const_generic_iterator_traits<default_type> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return nullptr;
}
};
template <>
struct const_generic_iterator_traits<inorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->inorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->inorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->inorder_predecessor(_node_);
}
};
template <>
struct const_generic_iterator_traits<preorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->preorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->preorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->preorder_predecessor(_node_);
}
};
template <>
struct const_generic_iterator_traits<postorder_iterator_traits> {
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
start(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_) {
return _map_->postorder_start_helper();
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
next(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->postorder_successor(_node_);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
static const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*
prev(map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_) {
return _map_->postorder_predecessor(_node_);
}
};
template <typename iter_type>
struct base_iterator {};
/**
* @class uncopyable
* @brief A base class with private/deleted copy/assignement
* @detail Inherit privately from this base class in order to make derived classes uncopyable
*/
class uncopyable
{
protected:
uncopyable() {}
~uncopyable() {}
private:
uncopyable(const uncopyable&);
uncopyable& operator=(const uncopyable&);
};
/**
* @class map
* @tparam KeyType The type of key
* @tparam DataType The type of data
* @tparam comparator A template template parameter which indicates which comparator should be used during comparisions
* @tparam CONT A default container which will be used to sore the ordered datas which are results of different ways of tree traversals
* @param AllocatorType Indicates an allocator which could be defined in order to make class map allocate its inner nodes using this allocator
* @brief Key/value based associative data structure
* @detail Container like implementation of dictionary using AVL tree data structure
*/
template <typename KeyType,
typename DataType,
template <typename> class comparator = std::less,
template <typename T, typename ALLOC = std::allocator<T> > class CONT = std::vector,
typename AllocatorType = std::allocator<std::pair<const KeyType, DataType>>>
class map : private uncopyable {
private:
class Node;
typedef AllocatorType _value_allocator;
typedef std::allocator_traits<_value_allocator> _value_allocator_traits;
typedef typename _value_allocator_traits::template rebind_traits<Node> _node_allocator_traits;
typedef typename _node_allocator_traits::allocator_type _node_allocator_type;
_node_allocator_type alloc;
class Node {
public:
Node* parent;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
std::pair<KeyType, DataType> entry;
public:
Node() noexcept: parent(nullptr),
left(nullptr),
right(nullptr) {
}
explicit Node(const KeyType& key) noexcept : parent(nullptr),
left(nullptr),
right(nullptr) {
entry.first = key;
}
Node(const KeyType& key, const DataType& data): parent(nullptr),
left(nullptr),
right(nullptr),
entry(std::make_pair(key, data)) {
}
explicit Node(const std::pair<KeyType, DataType>& _pair_): parent(nullptr),
left(nullptr),
right(nullptr),
entry(_pair_) {
}
std::pair<KeyType, DataType>& get_entry() {
return entry;
}
};
private:
int height(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* get_min(const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* get_max(const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
void insert(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
void insert_helper(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* parent, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*& node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* find(const KeyType& key, void*);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* find_helper(const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* parent_node, const KeyType key);
void transplant(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* second_node);
std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node> left_rotate(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node> right_rotate(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node> left_right_rotate(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node> right_left_rotate(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
void check_balance(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node> rebalance(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* inorder_successor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* preorder_successor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* postorder_successor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* inorder_predecessor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* postorder_predecessor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* preorder_predecessor(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node);
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* inorder_end_helper();
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* preorder_end_helper();
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* postorder_end_helper();
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* inorder_start_helper();
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* preorder_start_helper();
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* postorder_start_helper();
void traverse_inorder_helper(std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node>& node,
std::vector<KeyType>& out);
void traverse_preorder_helper(std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node>& node,
std::vector<KeyType>& out);
void traverse_postorder_helper(std::unique_ptr<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node>& node,
std::vector<KeyType>& out);
public:
/**
* class map
* @fn map
* @brief Default constructor
* @param _alloc A default parameter takes allocator type
* @return no return value
*/
explicit map(const _node_allocator_type& _alloc = AllocatorType()) : alloc(_alloc),
current_size(0),
root(nullptr) {}
/**
* class map
* @fn ~map
* @brief destructor
* @return no return value
*/
virtual ~map() {
if(root) {
alloc.destroy(root);
alloc.deallocate(root, 1);
}
}
/**
* class map
* @fn operator[]
* @brief Returns the associated value with the given key
* @param key A key
* @return DataType&
*/
DataType& operator[](const KeyType& key) {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = find(key);
return node->get_entry().second;
}
/**
* class map
* @fn max_height
* @brief Returns the height of tree started from root
* @return size_t
*/
const size_t max_height();
/**
* class map
* @fn insert
* @brief Inserts a key with associated default value 0
* @param key A key
* @return void
*/
void insert(const KeyType& key);
/**
* class map
* @fn insert
* @brief Inserts a key with associated value
* @param key A key
* @param data A data associated with key
* @return void
*/
void insert(const KeyType& key, const DataType& data);
/**
* class map
* @fn remove
* @brief Removes a node associated with given key
* @param key A key
* @return void
*/
void remove(const KeyType& key);
/**
* class map
* @fn destroy
* @brief This function is for explicitely destroying the tree.
* The implementation is not present in current version of source code.
* @param key A key
* @return void
*/
void destroy();
/**
* class map
* @fn traverse_inorder
* @brief Returns inorder traversal
* @param out An output std:vector in which traversal results are saved
* @return void
*/
void traverse_inorder(std::vector<KeyType>& out);
/**
* class map
* @fn traverse_preorder
* @brief Returns preorder traversal
* @param out An output std:vector in which traversal results are saved
* @return void
*/
void traverse_preorder(std::vector<KeyType>& out);
/**
* class map
* @fn traverse_postorder
* @brief Returns postorder traversal
* @param out An output std:vector in which traversal results are saved
* @return void
*/
void traverse_postorder(std::vector<KeyType>& out);
/**
* class map
* @fn size
* @brief Returns the count of elements into map
* @return size_t
*/
size_t size() const {
return current_size;
}
/**
* class map
* @fn empty
* @brief Returns a boolean that indicates whether the map is empty
* @return bool
*/
bool empty() const {
return !(static_cast<bool>(current_size));
}
public:
/**
* class generic_iterator
* @tparam TraitsType The concreate type of iterator
* can take one of these traits types: inorder_iterator_traits,
* preorder_iterator_traits, postorder_iterator_traits.
* @tparam Traits is template argument which is using to specialize an internal generic_iterator_traits class
* @brief A nested public generic template class
* @detail Provides an interface and functionality to iterate over map in specific order
*/
template <typename TraitsType, typename Traits=generic_iterator_traits<TraitsType>>
class generic_iterator: public base_iterator<TraitsType> {
private:
friend class generic_iterator<inorder_iterator_traits>;
friend class generic_iterator<preorder_iterator_traits>;
friend class generic_iterator<postorder_iterator_traits>;
private:
map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_;
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_;
private:
void increment() {
if(!this->_node_) {
this->_node_ = Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_);
}
this->_node_ = Traits::template next<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_, this->_node_);
}
void decrement() {
if(this->_node_) {
this->_node_ = Traits::template prev<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_, this->_node_);
}
}
public:
/**
* class generic_iterator
* @fn generic_iterator
* @brief An expicit constructor with one argument
* @param An instance of outer template class map
*/
explicit generic_iterator(map<KeyType, DataType, comparator, CONT, AllocatorType>* map_instance) {
if(map_instance) {
this->_map_ = map_instance;
}
this->_node_ = nullptr;
}
/**
* class generic_iterator
* @fn generic_iterator
* @brief constructor
* @param map_instance An instance of the outer class map
*/
generic_iterator(map<KeyType, DataType, comparator, CONT, AllocatorType>* map_instance, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node) {
if (map_instance) {
this->_map_ = map_instance;
}
this->_node_ = node;
}
/**
* class generic_iterator
* @fn generic_iterator
* @brief copy constructor
* @param rhs Another instance of the class with the same type
*/
/*
* Here for copy/assignements we are performing a shalow copy and this is because of the following two reasons:
* 1. First of all we need to have a different iterators pointing to the same actual node
* and any change with one of them is required to be in coherence with all of the remaining
* iterators. For an exmaple an increment of any separate iterator should cause an
* appropriate increment of others too.
* 2. Performing a deep copy will require a memory allocations for new nodes. This in turn will require to
* deep-copy all the mamber unique_ptr's. But from the other hand std::unique_ptr unlike shared_ptr haven't
* standard interface to provide an allocator which will force us to implement make_unique which will take
* allocator as an argument which is an aditional overhead we want to escape. Besides performing a deep-copy
* of unique_ptr will break the concept of uniqueness of pointer.
* */
generic_iterator(const generic_iterator<default_type>& rhs) {
this->_map_ = rhs._map_;
if(!rhs._node_) {
this->_node_ = Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_);
} else {
this->_node_ = rhs._node_;
}
}
/**
* class generic_iterator
* @fn operator=
* @brief An assignement operator
* @param rhs Another instance of the class with the same type
*/
generic_iterator& operator=(const generic_iterator<default_type>& rhs) noexcept {
if((void*)this != (void*)&rhs) {
this->_map_ = rhs._map_;
if(!rhs._node_) {
this->_node_ = Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_);
} else {
this->_node_ = rhs._node_;
}
}
return *this;
}
/**
* class generic_iterator
* @fn operator++
* @brief Preincrement operator
* @return generic_iterator
*/
generic_iterator& operator++() {
increment();
return *this;
}
/**
* class generic_iterator
* @fn operator++
* @brief Postincrement operator
* @return generic_iterator
*/
generic_iterator operator++(int) {
const generic_iterator temp(*this);
increment();
return temp;
}
/**
* class generic_iterator
* @fn operator--
* @brief Predecrement operator
* @return generic_iterator
*/
generic_iterator& operator--() {
decrement();
return *this;
}
/**
* class generic_iterator
* @fn operator--
* @brief Postdecrement operator
* @return generic_iterator
*/
generic_iterator operator--(int) {
const generic_iterator temp(*this);
decrement();
return temp;
}
/**
* class generic_iterator
* @fn operator*
* @brief operator dereference
* @return std::pair<const KeyType&, DataType&>
*/
std::pair<const KeyType&, DataType&> operator*() noexcept {
if(!this->_node_) {
this->_node_ = Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_);
return {this->_node_->entry.first, this->_node_->entry.second};
}
if(this->_node_) {
return {this->_node_->entry.first, this->_node_->entry.second};
}
KeyType key;
DataType data;
return {key, data};
}
/**
* class generic_iterator
* @fn operator==
* @brief boolean equal operator
* @return bool indicates whether the two given instances are equal
*/
template <typename iter_type>
bool operator==(const generic_iterator<iter_type>& rhs) {
return this->_node_ == rhs._node_;
}
/**
* class generic_iterator
* @fn operator!=
* @brief boolean non-equal operator
* @return bool indicates whether the two given instances are equal
*/
template <typename iter_type>
bool operator!=(const generic_iterator<iter_type>& rhs) {
return !((*this) == rhs);
}
};
/**
* @class const_generic_iterator
* @tparam TraitsType The concreate type of iterator
* can take one of these traits types: inorder_iterator_traits,
* preorder_iterator_traits, postorder_iterator_traits.
* @tparam Traits is template argument which is using to specialize an internal const_generic_iterator_traits class
* @brief A nested public generic template class
* @detail Provides an interface and functionality to iterate over map in specific order
*/
template <typename TraitsType, typename Traits = const_generic_iterator_traits<TraitsType>>
class const_generic_iterator : public base_iterator<TraitsType> {
private:
friend class const_generic_iterator<inorder_iterator_traits>;
friend class const_generic_iterator<preorder_iterator_traits>;
friend class const_generic_iterator<postorder_iterator_traits>;
private:
map<KeyType, DataType, comparator, CONT, AllocatorType>* _map_;
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* _node_;
private:
void increment() {
if (!this->_node_) {
this->_node_ = const_cast<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*>(Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_));
}
this->_node_ = const_cast<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*>(Traits::template next<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_, this->_node_));
}
void decrement() {
if (this->_node_) {
this->_node_ = const_cast<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*>(Traits::template prev<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_, this->_node_));
}
}
public:
/**
* class const_generic_iterator
* @fn const_generic_iterator
* @param An instance of outer template class map
* @brief An expicit constructor with one argument
*/
explicit const_generic_iterator(map<KeyType, DataType, comparator, CONT, AllocatorType>* map_instance) {
if (map_instance) {
this->_map_ = map_instance;
}
this->_node_ = nullptr;
}
/**
* class const_generic_iterator
* @fn const_generic_iterator
* @brief constructor
* @param map_instance An instance of the outer class map
*/
const_generic_iterator(map<KeyType, DataType, comparator, CONT, AllocatorType>* map_instance, typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node) {
if (map_instance) {
this->_map_ = map_instance;
}
this->_node_ = node;
}
/**
* class const_generic_iterator
* @fn const_generic_iterator
* @brief copy constructor
* @param rhs Another instance of the class with the same type
*/
const_generic_iterator(const const_generic_iterator<default_type>& rhs) {
this->_map_ = rhs._map_;
if (!rhs._node_) {
this->_node_ = const_cast<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*>(Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_));
}
else {
this->_node_ = rhs._node_;
}
}
/**
* class const_generic_iterator
* @fn operator=
* @brief An assignement operator
* @param rhs Another instance of the class with the same type
*/
const_generic_iterator& operator=(const const_generic_iterator<default_type>& rhs) noexcept {
if ((void*)this != (void*)&rhs) {
this->_map_ = rhs._map_;
if (!rhs._node_) {
this->_node_ = Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_);
}
else {
this->_node_ = rhs._node_;
}
}
return *this;
}
/**
* class const_generic_iterator
* @fn operator++
* @brief Preincrement operator
* @return const_generic_iterator
*/
const_generic_iterator& operator++() {
increment();
return *this;
}
/**
* class const_generic_iterator
* @fn operator++
* @brief Postincrement operator
* @return const_generic_iterator
*/
const_generic_iterator operator++(int) {
const_generic_iterator temp(*this);
increment();
return temp;
}
/**
* class const_generic_iterator
* @fn operator--
* @brief Predecrement operator
* @return const_generic_iterator
*/
const_generic_iterator& operator--() {
decrement();
return *this;
}
/**
* class const_generic_iterator
* @fn operator--
* @brief Postdecrement operator
* @return const_generic_iterator
*/
const_generic_iterator operator--(int) {
const_generic_iterator temp(*this);
decrement();
return temp;
}
/**
* class const_generic_iterator
* @fn operator*
* @brief operator dereference
* @return std::pair<const KeyType&, DataType&>
*/
const std::pair<const KeyType&, const DataType&> operator*() noexcept {
if (!this->_node_) {
this->_node_ = const_cast<typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node*>(Traits::template start<KeyType, DataType, comparator, CONT, AllocatorType>(this->_map_));
return { this->_node_->entry.first, this->_node_->entry.second };
}
if (this->_node_) {
return { this->_node_->entry.first, this->_node_->entry.second };
}
KeyType key;
DataType data;
return { key, data };
}
/**
* class const_generic_iterator
* @fn operator==
* @brief boolean equal operator
* @return bool indicates whether or not the given two instances are equal
*/
template <typename iter_type>
bool operator==(const const_generic_iterator<iter_type>& rhs) {
return this->_node_ == rhs._node_;
}
/**
* class const_generic_iterator
* @fn operator!=
* @brief boolean non-equal operator
* @return bool indicates whether or not the given two instances are equal
*/
template <typename iter_type>
bool operator!=(const const_generic_iterator<iter_type>& rhs) {
return !((*this) == rhs);
}
};
typedef generic_iterator<inorder_iterator_traits> inorder_iterator;
typedef generic_iterator<preorder_iterator_traits> preorder_iterator;
typedef generic_iterator<postorder_iterator_traits> postorder_iterator;
typedef const_generic_iterator<inorder_iterator_traits> const_inorder_iterator;
typedef const_generic_iterator<preorder_iterator_traits> const_preorder_iterator;
typedef const_generic_iterator<postorder_iterator_traits> const_postorder_iterator;
/**
* class map
* @fn begin()
* @brief Returns an iterator to the starting element of the map
* @return generic_iterator<void> which is converting to the concreate iterator type(inorder_iterator, preorder_iterator, postorder_iterator)
*/
generic_iterator<default_type> begin() {
generic_iterator<default_type> git(this);
return git;
}
/**
* class map
* @fn end()
* @brief Returns an iterator to the ending element of the map
* @return generic_iterator<void> which is converting to the concreate iterator type(inorder_iterator, preorder_iterator, postorder_iterator)
*/
generic_iterator<default_type> end() {
generic_iterator<default_type> git(nullptr);
return git;
}
/**
* class map
* @fn cbegin()
* @brief Returns a const iterator to the starting element of the map
* @return const_generic_iterator<void> which is converting to the concreate iterator type(const_inorder_iterator, const_preorder_iterator, const_postorder_iterator)
*/
const const_generic_iterator<default_type> cbegin() {
const const_generic_iterator<default_type> git(this);
return git;
}
/**
* class map
* @fn cend()
* @brief Returns a const iterator to the ending element of the map
* @return const_generic_iterator<void> which is converting to the concreate iterator type(const_inorder_iterator, const_preorder_iterator, const_postorder_iterator)
*/
const const_generic_iterator<default_type> cend() {
const const_generic_iterator<default_type> git(nullptr);
return git;
}
/**
* class map
* @fn cfind()
* @brief Finds an element by the given key.
* @return generic_iterator<void> which is converting to the concreate iterator type(inorder_iterator, preorder_iterator, postorder_iterator)
*/
generic_iterator<default_type> find(const KeyType& key) {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = find_helper(root, key);
generic_iterator<default_type> git(this, node);
return git;
}
/**
* class map
* @fn cfind()
* @brief Finds an element by the given key.
* @return const_generic_iterator<void> which is converting to the concreate iterator type(inorder_iterator, preorder_iterator, postorder_iterator)
*/
generic_iterator<default_type> cfind(const KeyType& key) {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = find_helper(root, key);
generic_iterator<default_type> git(this, node);
return git;
}
/**
* class map
* @fn inorder_end()
* @brief Returns the last valid element in the inorder traversal.
* @return inorder_iterator to the last valid element in map
*/
inorder_iterator inorder_end() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = inorder_end_helper();
inorder_iterator it(this, node);
return it;
}
/**
* class map
* @fn preorder_end()
* @brief Returns the last valid element in the preorder traversal.
* @return preorder_iterator to the last valid element in map
*/
preorder_iterator preorder_end() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = preorder_end_helper();
preorder_iterator pit(this, node);
return pit;
}
/**
* class map
* @fn postorder_end()
* @brief Returns the last valid element in the postorder traversal.
* @return postorder_iterator to the last valid element in map
*/
postorder_iterator postorder_end() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = postorder_end_helper();
postorder_iterator pit(this, node);
return pit;
}
/**
* class map
* @fn inorder_start()
* @brief Returns the first valid element in the inorder traversal.
* @return inorder_iterator to the first valid element in map
*/
inorder_iterator inorder_start() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = inorder_start_helper();
inorder_iterator it(this, node);
return it;
}
/**
* class map
* @fn preorder_start()
* @brief Returns the first valid element in the preorder traversal.
* @return preorder_iterator to the first valid element in map
*/
preorder_iterator preorder_start() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = preorder_start_helper();
preorder_iterator pit(this, node);
return pit;
}
/**
* class map
* @fn postorder_start()
* @brief Returns the first valid element in the postorder traversal.
* @return postorder_iterator to the first valid element in map
*/
postorder_iterator postorder_start() {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node = postorder_start_helper();
postorder_iterator pit(this, node);
return pit;
}
private:
size_t current_size;
map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* root;
template <typename> friend struct generic_iterator_traits;
template <typename, typename> friend class generic_iterator;
template <typename> friend struct const_generic_iterator_traits;
template <typename, typename> friend class const_generic_iterator;
};
template <typename KeyType,
typename DataType,
template <typename> class comparator,
template <typename T, typename ALLOC = std::allocator<T> > class CONT,
typename AllocatorType>
inline int map<KeyType, DataType, comparator, CONT, AllocatorType>::height(typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node) {
if(!node) {
return 0;
}
int left = height(node->left.get());
int right = height(node->right.get());
return std::max(left, right) + 1;
}
template <typename KeyType,
typename DataType,
template <typename> class comparator,
template <typename T, typename ALLOC = std::allocator<T> > class CONT,
typename AllocatorType>
inline const size_t map<KeyType, DataType, comparator, CONT, AllocatorType>::max_height() {
return height(root);
}
template <typename KeyType,
typename DataType,
template <typename> class comparator,
template <typename T, typename ALLOC = std::allocator<T> > class CONT,
typename AllocatorType>
inline typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* map<KeyType, DataType, comparator, CONT, AllocatorType>::get_min(const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node) {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* temp = node->right.get();
while(temp->left) {
temp = temp->left.get();
}
return temp;
}
template <typename KeyType,
typename DataType,
template <typename> class comparator,
template <typename T, typename ALLOC = std::allocator<T> > class CONT,
typename AllocatorType>
inline typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* map<KeyType, DataType, comparator, CONT, AllocatorType>::get_max(const typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* node) {
typename map<KeyType, DataType, comparator, CONT, AllocatorType>::Node* temp = node->left.get();
while(temp->right) {
temp = temp->right.get();