-
Notifications
You must be signed in to change notification settings - Fork 1
/
containers.h
1767 lines (1588 loc) · 69.9 KB
/
containers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============================================================================
// Mini Spin-X Library
//
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#ifndef PFC_CORE_CONTAINERS_H
#define PFC_CORE_CONTAINERS_H
//----------------------------------------------------------------------------
//============================================================================
// interface
//============================================================================
// external
#include "utils.h"
#include "iterators.h"
namespace pfc
{
// new
struct compare_predicate;
template<typename> class owner_array;
template<typename> class array;
template<typename, usize_t capacity> class sarray;
template<typename> class deque;
template<typename> class list;
template<typename T, class CmpPred=compare_predicate> class set;
template<typename K, typename T, class CmpPred=compare_predicate> class map;
template<typename T> class hash_table_array;
template<typename T, usize_t capacity> class hash_table_sarray;
template<typename T> class hash_table_deque;
template<class Func, template<class> class Table=hash_table_array> struct hash_config;
struct hash_table_info;
template<typename T, class Config=hash_config<hash_func<T> > > class hash_set;
template<typename K, typename T, class Config=hash_config<hash_func<K> > > class hash_map;
template<typename K, typename T, class KConfig=hash_config<hash_func<K> >, class VConfig=hash_config<hash_func<T> > > class hash_bimap;
class str_pool;
// swap functions
template<typename T> PFC_INLINE void swap(owner_array<T>&, owner_array<T>&);
template<typename T> PFC_INLINE void swap(array<T>&, array<T>&);
template<typename T, usize_t capacity> PFC_INLINE void swap(sarray<T, capacity>&, sarray<T, capacity>&);
template<typename T> PFC_INLINE void swap(deque<T>&, deque<T>&);
template<typename T> PFC_INLINE void swap(list<T>&, list<T>&);
template<typename T, class CmpPred> PFC_INLINE void swap(set<T, CmpPred>&, set<T, CmpPred>&);
template<typename K, typename T, class CmpPred> PFC_INLINE void swap(map<K, T, CmpPred>&, map<K, T, CmpPred>&);
template<typename T, class Config> PFC_INLINE void swap(hash_set<T, Config>&, hash_set<T, Config>&);
template<typename K, typename T, class Config> PFC_INLINE void swap(hash_map<K, T, Config>&, hash_map<K, T, Config>&);
template<typename K, typename T, class KConfig, class VConfig> PFC_INLINE void swap(hash_bimap<K, T, KConfig, VConfig>&, hash_bimap<K, T, KConfig, VConfig>&);
PFC_INLINE void swap(str_pool&, str_pool&);
//----------------------------------------------------------------------------
//============================================================================
// compare_predicate
//============================================================================
struct compare_predicate
{
template<typename T, typename U> PFC_INLINE bool before(const T&, const U&) const;
template<typename T, typename U> PFC_INLINE bool equal(const T&, const U&) const;
};
//----------------------------------------------------------------------------
//============================================================================
// owner_array
//============================================================================
template<typename T>
class owner_array
{ PFC_MONO(owner_array) PFC_INTROSPEC_DECL;
public:
// construction
PFC_INLINE owner_array();
PFC_INLINE owner_array(T*, usize_t size_);
PFC_INLINE owner_array(const owner_array&);
PFC_INLINE void set(T*, usize_t size_);
PFC_INLINE void operator=(const owner_array&);
PFC_INLINE ~owner_array();
PFC_INLINE void clear();
PFC_INLINE void swap(owner_array&);
owner_data steal_data();
//--------------------------------------------------------------------------
// accessors
PFC_INLINE friend bool is_valid(const owner_array &arr_) {return arr_.data!=0;}
PFC_INLINE friend void *ptr(const owner_array &arr_) {return arr_.data;}
//--------------------------------------------------------------------------
mutable usize_t size;
mutable T *data;
//--------------------------------------------------------------------------
private:
template<class PE> void stream_array(PE&, meta_case<0> is_type_void_);
template<class PE> void stream_array(PE&, meta_case<1> default_);
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, owner_array<T>, is_type_pod_def_ctor, true);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, owner_array<T>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// array
//============================================================================
template<typename T>
class array
{ PFC_MONO(array) PFC_INTROSPEC_DECL;
public:
// nested types
typedef const T *const_iterator;
typedef T *iterator;
typedef array_iterator<typename add_const<T>::res, false> const_reverse_iterator;
typedef array_iterator<T, false> reverse_iterator;
//--------------------------------------------------------------------------
// construction
array(memory_allocator_base *alloc_=0);
array(const array&);
array(const array&, memory_allocator_base*);
explicit array(usize_t size_, memory_allocator_base *alloc_=0);
array(usize_t size_, const T&, memory_allocator_base *alloc_=0);
PFC_INLINE ~array();
void operator=(const array&);
void set_allocator(memory_allocator_base*);
void clear();
void reset_size(usize_t size_);
void reset_size(usize_t size_, const T&);
void resize(usize_t size_);
void resize(usize_t size_, const T&);
void resize_to_zero();
void reserve(usize_t capacity_);
void trim(usize_t permitted_slack_=0);
PFC_INLINE void swap(array&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
PFC_INLINE usize_t capacity() const;
PFC_INLINE const T &operator[](usize_t idx_) const;
PFC_INLINE T &operator[](usize_t idx_);
PFC_INLINE const T &front() const;
PFC_INLINE T &front();
PFC_INLINE const T &back() const;
PFC_INLINE T &back();
PFC_INLINE const T *data() const;
PFC_INLINE T *data();
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_reverse_iterator rbegin() const;
PFC_INLINE reverse_iterator rbegin();
PFC_INLINE const_iterator last() const;
PFC_INLINE iterator last();
PFC_INLINE const_reverse_iterator rlast() const;
PFC_INLINE reverse_iterator rlast();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
PFC_INLINE const_reverse_iterator rend() const;
PFC_INLINE reverse_iterator rend();
PFC_INLINE void get(usize_t start_idx_, T*, usize_t num_items_) const;
PFC_INLINE void set(usize_t start_idx_, const T*, usize_t num_items_);
PFC_INLINE void set(usize_t start_idx_, const T&, usize_t num_items_);
PFC_INLINE void push_front(const T&);
PFC_INLINE T &push_front();
PFC_INLINE void push_back(const T&);
PFC_INLINE T &push_back();
PFC_INLINE void insert_front(usize_t num_items_);
PFC_INLINE void insert_front(usize_t num_items_, const T&);
PFC_INLINE void insert_front(usize_t num_items_, const T*);
PFC_INLINE void insert_back(usize_t num_items_);
PFC_INLINE void insert_back(usize_t num_items_, const T&);
PFC_INLINE void insert_back(usize_t num_items_, const T*);
PFC_INLINE void pop_front();
PFC_INLINE void pop_back();
PFC_INLINE void remove_front(usize_t num_items_);
PFC_INLINE void remove_back(usize_t num_items_);
PFC_INLINE void remove(T&);
PFC_INLINE void remove_unordered(T&);
PFC_INLINE void remove_at(usize_t idx_);
PFC_INLINE void remove_at(usize_t idx_, usize_t num_items_);
PFC_INLINE void remove_at_unordered(usize_t idx_);
owner_array<T> steal_data();
//--------------------------------------------------------------------------
private:
void reserve(usize_t capacity_, usize_t offset_);
//--------------------------------------------------------------------------
memory_allocator_base *m_allocator;
usize_t m_size;
usize_t m_capacity;
T *m_data;
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, array<T>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// sarray
//============================================================================
template<typename T, usize_t cap>
class sarray
{ PFC_MONO(sarray) PFC_INTROSPEC_DECL;
public:
// nested types
typedef const T *const_iterator;
typedef T *iterator;
typedef array_iterator<typename add_const<T>::res, false> const_reverse_iterator;
typedef array_iterator<T, false> reverse_iterator;
//--------------------------------------------------------------------------
// construction
sarray();
sarray(const sarray&);
template<usize_t cap2> sarray(const sarray<T, cap2>&);
explicit sarray(usize_t size_);
sarray(usize_t size_, const T&);
PFC_INLINE ~sarray();
void operator=(const sarray&);
template<usize_t cap2> void operator=(const sarray<T, cap2>&);
void clear();
void reset_size(usize_t size_);
void reset_size(usize_t size_, const T&);
void resize(usize_t size_);
void resize(usize_t size_, const T&);
void resize_to_zero();
PFC_INLINE void swap(sarray&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE usize_t size() const;
PFC_INLINE const T &operator[](usize_t idx_) const;
PFC_INLINE T &operator[](usize_t idx_);
PFC_INLINE const T &front() const;
PFC_INLINE T &front();
PFC_INLINE const T &back() const;
PFC_INLINE T &back();
PFC_INLINE const T *data() const;
PFC_INLINE T *data();
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_reverse_iterator rbegin() const;
PFC_INLINE reverse_iterator rbegin();
PFC_INLINE const_iterator last() const;
PFC_INLINE iterator last();
PFC_INLINE const_reverse_iterator rlast() const;
PFC_INLINE reverse_iterator rlast();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
PFC_INLINE const_reverse_iterator rend() const;
PFC_INLINE reverse_iterator rend();
PFC_INLINE void get(usize_t start_idx_, T*, usize_t num_items_) const;
PFC_INLINE void set(usize_t start_idx_, const T*, usize_t num_items_);
PFC_INLINE void set(usize_t start_idx_, const T&, usize_t num_items_);
PFC_INLINE void push_front(const T&);
PFC_INLINE T &push_front();
PFC_INLINE void push_back(const T&);
PFC_INLINE T &push_back();
PFC_INLINE void insert_front(usize_t num_items_);
PFC_INLINE void insert_front(usize_t num_items_, const T&);
PFC_INLINE void insert_front(usize_t num_items_, const T*);
PFC_INLINE void insert_back(usize_t num_items_);
PFC_INLINE void insert_back(usize_t num_items_, const T&);
PFC_INLINE void insert_back(usize_t num_items_, const T*);
PFC_INLINE void pop_front();
PFC_INLINE void pop_back();
PFC_INLINE void remove_front(usize_t num_items_);
PFC_INLINE void remove_back(usize_t num_items_);
//--------------------------------------------------------------------------
private:
usize_t m_size;
typename meta_storage<T>::res m_data[cap];
};
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, usize_t cap, sarray<T, cap>, is_type_pod, is_type_pod<T>::res);
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, usize_t cap, sarray<T, cap>, is_type_pod_def_ctor, is_type_pod_def_ctor<T>::res);
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, usize_t cap, sarray<T, cap>, is_type_pod_dtor, is_type_pod_dtor<T>::res);
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, usize_t cap, sarray<T, cap>, is_type_pod_move, is_type_pod_move<T>::res);
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, usize_t cap, sarray<T, cap>, is_type_pod_copy, is_type_pod_copy<T>::res);
//----------------------------------------------------------------------------
//============================================================================
// deque
//============================================================================
template<typename T>
struct deque_traits
{
// properties
enum {sshift=meta_max<meta_log2<1024/sizeof(T)>::res, 4>::res,
ssize=1<<sshift,
smask=ssize-1,
block_alloc_size=ssize*sizeof(T),
block_alloc_align=meta_alignof<T>::res};
};
//----------------------------------------------------------------------------
template<typename T>
class deque
{ PFC_MONO(deque) PFC_INTROSPEC_DECL;
template<bool is_forward> class const_iterator_impl;
template<bool is_forward> class iterator_impl;
public:
// nested types
typedef const_iterator_impl<true> const_iterator;
typedef const_iterator_impl<false> const_reverse_iterator;
typedef iterator_impl<true> iterator;
typedef iterator_impl<false> reverse_iterator;
//--------------------------------------------------------------------------
// construction
deque(memory_allocator_base *alloc_=0);
deque(const deque&);
deque(const deque&, memory_allocator_base*);
explicit deque(usize_t size_, memory_allocator_base *alloc_=0);
deque(usize_t size_, const T&, memory_allocator_base *alloc_=0);
PFC_INLINE ~deque();
void operator=(const deque&);
void set_allocator(memory_allocator_base*);
void clear();
void resize(usize_t size_);
void resize(usize_t size_, const T&);
PFC_INLINE void swap(deque&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
PFC_INLINE const T &operator[](usize_t idx_) const;
PFC_INLINE T &operator[](usize_t idx_);
PFC_INLINE const T &front() const;
PFC_INLINE T &front();
PFC_INLINE const T &back() const;
PFC_INLINE T &back();
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_reverse_iterator rbegin() const;
PFC_INLINE reverse_iterator rbegin();
PFC_INLINE const_iterator last() const;
PFC_INLINE iterator last();
PFC_INLINE const_reverse_iterator rlast() const;
PFC_INLINE reverse_iterator rlast();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
PFC_INLINE const_reverse_iterator rend() const;
PFC_INLINE reverse_iterator rend();
PFC_INLINE void get(usize_t start_idx_, T*, usize_t num_items_) const;
PFC_INLINE void set(usize_t start_idx_, const T*, usize_t num_items_);
PFC_INLINE void set(usize_t start_idx_, const T&, usize_t num_items_);
PFC_INLINE void push_front(const T&);
PFC_INLINE T &push_front();
PFC_INLINE void push_back(const T&);
PFC_INLINE T &push_back();
PFC_INLINE void insert_front(usize_t num_items_);
PFC_INLINE void insert_front(usize_t num_items_, const T&);
PFC_INLINE void insert_front(usize_t num_items_, const T*);
PFC_INLINE void insert_back(usize_t num_items_);
PFC_INLINE void insert_back(usize_t num_items_, const T&);
PFC_INLINE void insert_back(usize_t num_items_, const T*);
PFC_INLINE void pop_front();
PFC_INLINE void pop_back();
PFC_INLINE void remove_front(usize_t num_items_);
PFC_INLINE void remove_back(usize_t num_items_);
//--------------------------------------------------------------------------
private:
void cctor(const deque&);
void alloc_stride_front();
void alloc_stride_back();
void dealloc_stride_front();
void dealloc_stride_back();
//--------------------------------------------------------------------------
memory_allocator_base *m_allocator;
usize_t m_size;
usize_t m_offset;
T *m_first, *m_last, *m_stride_last;
array<T*> m_strides;
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, deque<T>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// deque::const_iterator_impl
//============================================================================
template<typename T>
template<bool is_forward>
class deque<T>::const_iterator_impl
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE const_iterator_impl();
PFC_INLINE const_iterator_impl(const iterator_impl<is_forward>&);
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const const_iterator_impl &it_) {return it_.m_deque && it_.m_index<it_.m_deque->size();}
PFC_INLINE bool operator==(const const_iterator_impl&) const;
PFC_INLINE bool operator==(const iterator_impl<is_forward>&) const;
PFC_INLINE bool operator!=(const const_iterator_impl&) const;
PFC_INLINE bool operator!=(const iterator_impl<is_forward>&) const;
PFC_INLINE const_iterator_impl &operator++();
PFC_INLINE const_iterator_impl &operator--();
PFC_INLINE const_iterator_impl &operator+=(ssize_t offset_);
PFC_INLINE const_iterator_impl &operator+=(const const_iterator_impl&);
PFC_INLINE const_iterator_impl &operator+=(const iterator_impl<is_forward>&);
PFC_INLINE const_iterator_impl &operator-=(ssize_t offset_);
PFC_INLINE const_iterator_impl &operator-=(const const_iterator_impl&);
PFC_INLINE const_iterator_impl &operator-=(const iterator_impl<is_forward>&);
PFC_INLINE const_iterator_impl operator+(ssize_t offset_) const;
PFC_INLINE const_iterator_impl operator+(const const_iterator_impl&) const;
PFC_INLINE const_iterator_impl operator+(const iterator_impl<is_forward>&) const;
PFC_INLINE const_iterator_impl operator-(ssize_t offset_) const;
PFC_INLINE const_iterator_impl operator-(const const_iterator_impl&) const;
PFC_INLINE const_iterator_impl operator-(const iterator_impl<is_forward>&) const;
PFC_INLINE const T &operator[](usize_t index_) const;
PFC_INLINE const T &operator*() const;
PFC_INLINE const T *operator->() const;
PFC_INLINE friend const T *ptr(const const_iterator_impl &it_) {return it_.m_deque?&(*it_.m_deque)[it_.m_index]:0;}
PFC_INLINE usize_t index() const;
//--------------------------------------------------------------------------
private:
friend class deque<T>;
friend class iterator_impl<is_forward>;
PFC_INLINE const_iterator_impl(const deque<T>&, usize_t index_);
//--------------------------------------------------------------------------
const deque<T> *m_deque;
usize_t m_index;
};
//----------------------------------------------------------------------------
//============================================================================
// deque::iterator_impl
//============================================================================
template<typename T>
template<bool is_forward>
class deque<T>::iterator_impl
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE iterator_impl();
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const iterator_impl &it_) {return it_.m_deque && it_.m_index<it_.m_deque->size();}
PFC_INLINE bool operator==(const const_iterator_impl<is_forward>&) const;
PFC_INLINE bool operator==(const iterator_impl&) const;
PFC_INLINE bool operator!=(const const_iterator_impl<is_forward>&) const;
PFC_INLINE bool operator!=(const iterator_impl&) const;
PFC_INLINE iterator_impl &operator++();
PFC_INLINE iterator_impl &operator--();
PFC_INLINE iterator_impl &operator+=(ssize_t offset_);
PFC_INLINE iterator_impl &operator+=(const const_iterator_impl<is_forward>&);
PFC_INLINE iterator_impl &operator+=(const iterator_impl&);
PFC_INLINE iterator_impl &operator-=(ssize_t offset_);
PFC_INLINE iterator_impl &operator-=(const const_iterator_impl<is_forward>&);
PFC_INLINE iterator_impl &operator-=(const iterator_impl&);
PFC_INLINE iterator_impl operator+(ssize_t offset_) const;
PFC_INLINE iterator_impl operator+(const const_iterator_impl<is_forward>&) const;
PFC_INLINE iterator_impl operator+(const iterator_impl&) const;
PFC_INLINE iterator_impl operator-(ssize_t offset_) const;
PFC_INLINE iterator_impl operator-(const const_iterator_impl<is_forward>&) const;
PFC_INLINE iterator_impl operator-(const iterator_impl&) const;
PFC_INLINE T &operator[](usize_t index_) const;
PFC_INLINE T &operator*() const;
PFC_INLINE T *operator->() const;
PFC_INLINE friend T *ptr(const iterator_impl &it_) {return it_.m_deque?&(*it_.m_deque)[it_.m_index]:0;}
PFC_INLINE usize_t index() const;
//--------------------------------------------------------------------------
private:
friend class deque<T>;
friend class const_iterator_impl<is_forward>;
PFC_INLINE iterator_impl(deque<T>&, usize_t index_);
PFC_INLINE friend void move_construct(const iterator_impl &dst_, const iterator_impl &src_, usize_t count_) {move_construct_impl(dst_, src_, count_);}
static void move_construct_impl(const iterator_impl &dst_, const iterator_impl &src_, usize_t count_);
PFC_INLINE friend void rotate_sequence_left(const iterator_impl &first_, const iterator_impl &last_) {rotate_sequence_left_impl(first_, last_);}
static void rotate_sequence_left_impl(const iterator_impl &first_, const iterator_impl &last_);
PFC_INLINE friend void rotate_sequence_right(const iterator_impl &first_, const iterator_impl &last_) {rotate_sequence_right_impl(first_, last_);}
static void rotate_sequence_right_impl(const iterator_impl &first_, const iterator_impl &last_);
//--------------------------------------------------------------------------
deque<T> *m_deque;
usize_t m_index;
};
//----------------------------------------------------------------------------
//============================================================================
// list
//============================================================================
template<typename T>
struct list_traits
{
// properties
enum {block_alloc_size=sizeof(typename list<T>::item),
block_alloc_align=meta_alignof<typename list<T>::item>::res};
};
//----------------------------------------------------------------------------
template<typename T>
class list
{ PFC_MONO(list) PFC_INTROSPEC_DECL;
template<bool is_forward> class const_iterator_impl;
template<bool is_forward> class iterator_impl;
public:
// nested types
typedef const_iterator_impl<true> const_iterator;
typedef const_iterator_impl<false> const_reverse_iterator;
typedef iterator_impl<true> iterator;
typedef iterator_impl<false> reverse_iterator;
//--------------------------------------------------------------------------
// construction
list(memory_allocator_base *alloc_=0);
list(const list&);
list(const list&, memory_allocator_base*);
explicit list(usize_t size_, memory_allocator_base *alloc_=0);
list(usize_t size_, const T&, memory_allocator_base *alloc_=0);
PFC_INLINE ~list();
void operator=(const list&);
void set_allocator(memory_allocator_base*);
void clear();
void resize(usize_t size_);
void resize(usize_t size_, const T&);
PFC_INLINE void swap(list&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
PFC_INLINE const T &front() const;
PFC_INLINE T &front();
PFC_INLINE const T &back() const;
PFC_INLINE T &back();
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_reverse_iterator rbegin() const;
PFC_INLINE reverse_iterator rbegin();
PFC_INLINE const_iterator last() const;
PFC_INLINE iterator last();
PFC_INLINE const_reverse_iterator rlast() const;
PFC_INLINE reverse_iterator rlast();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
PFC_INLINE const_reverse_iterator rend() const;
PFC_INLINE reverse_iterator rend();
PFC_INLINE void push_front(const T&);
PFC_INLINE T &push_front();
PFC_INLINE void push_back(const T&);
PFC_INLINE T &push_back();
PFC_INLINE void insert_front(usize_t num_items_);
PFC_INLINE void insert_front(usize_t num_items_, const T&);
PFC_INLINE void insert_front(usize_t num_items_, const T*);
PFC_INLINE void insert_front(const list<T>&);
PFC_INLINE void insert_back(usize_t num_items_);
PFC_INLINE void insert_back(usize_t num_items_, const T&);
PFC_INLINE void insert_back(usize_t num_items_, const T*);
PFC_INLINE void insert_back(const list<T>&);
PFC_INLINE void insert_before(iterator&, const T&);
PFC_INLINE T &insert_before(iterator&);
PFC_INLINE void insert_after(iterator&, const T&);
PFC_INLINE T &insert_after(iterator&);
PFC_INLINE void splice_front(list&);
PFC_INLINE void splice_front(iterator&);
PFC_INLINE void splice_back(list&);
PFC_INLINE void splice_back(iterator&);
PFC_INLINE void pop_front();
PFC_INLINE void pop_back();
PFC_INLINE void remove_front(usize_t num_items_);
PFC_INLINE void remove_back(usize_t num_items_);
template<bool is_forward> PFC_INLINE void erase(iterator_impl<is_forward> &it_);
//--------------------------------------------------------------------------
private:
void cctor(const list&);
template<bool is_forward> PFC_INLINE void detach(iterator_impl<is_forward>&);
//--------------------------------------------------------------------------
//==========================================================================
// list::item
//==========================================================================
class item
{
public:
T data;
item *prev, *next;
//------------------------------------------------------------------------
private:
item(); // not implemented
void operator=(const item&); // not implemented
};
//--------------------------------------------------------------------------
friend struct list_traits<T>;
memory_allocator_base *m_allocator;
usize_t m_size;
item *m_first, *m_last;
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, list<T>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// list::const_iterator_impl
//============================================================================
template<typename T>
template<bool is_forward>
class list<T>::const_iterator_impl
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE const_iterator_impl();
PFC_INLINE const_iterator_impl(const iterator_impl<is_forward>&);
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const const_iterator_impl &it_) {return it_.m_item!=0;}
PFC_INLINE bool operator==(const const_iterator_impl&) const;
PFC_INLINE bool operator==(const iterator_impl<is_forward>&) const;
PFC_INLINE bool operator!=(const const_iterator_impl&) const;
PFC_INLINE bool operator!=(const iterator_impl<is_forward>&) const;
PFC_INLINE const_iterator_impl &operator++();
PFC_INLINE const_iterator_impl &operator--();
PFC_INLINE const T &operator*() const;
PFC_INLINE const T *operator->() const;
PFC_INLINE friend const T *ptr(const const_iterator_impl &it_) {return it_.m_item?&it_.m_item->data:0;}
//--------------------------------------------------------------------------
private:
friend class list<T>;
friend class iterator_impl<is_forward>;
PFC_INLINE const_iterator_impl(item*);
//--------------------------------------------------------------------------
item *m_item;
};
//----------------------------------------------------------------------------
//============================================================================
// list::iterator_impl
//============================================================================
template<typename T>
template<bool is_forward>
class list<T>::iterator_impl
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE iterator_impl();
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const iterator_impl &it_) {return it_.m_item!=0;}
PFC_INLINE bool operator==(const const_iterator_impl<is_forward>&) const;
PFC_INLINE bool operator==(const iterator_impl&) const;
PFC_INLINE bool operator!=(const const_iterator_impl<is_forward>&) const;
PFC_INLINE bool operator!=(const iterator_impl&) const;
PFC_INLINE iterator_impl &operator++();
PFC_INLINE iterator_impl &operator--();
PFC_INLINE T &operator*() const;
PFC_INLINE T *operator->() const;
PFC_INLINE friend T *ptr(const iterator_impl &it_) {return it_.m_item?&it_.m_item->data:0;}
//--------------------------------------------------------------------------
private:
friend class list<T>;
friend class const_iterator_impl<is_forward>;
PFC_INLINE iterator_impl(list&, item*);
PFC_INLINE void detach();
PFC_INLINE friend void rotate_sequence_left(iterator_impl &first_, iterator_impl &last_) {rotate_sequence_left_impl(first_, last_);}
static void rotate_sequence_left_impl(iterator_impl &first_, iterator_impl &last_);
PFC_INLINE friend void rotate_sequence_right(iterator_impl &first_, iterator_impl &last_) {rotate_sequence_right_impl(first_, last_);}
static void rotate_sequence_right_impl(iterator_impl &first_, iterator_impl &last_);
//--------------------------------------------------------------------------
list *m_list;
item *m_item;
};
//----------------------------------------------------------------------------
//============================================================================
// set
//============================================================================
template<typename T, class CmpPred=compare_predicate>
struct set_traits
{
// properties
enum {block_alloc_size=sizeof(typename set<T, CmpPred>::node),
block_alloc_align=meta_alignof<typename set<T, CmpPred>::node>::res};
};
//----------------------------------------------------------------------------
template<typename T, class CmpPred>
class set
{ PFC_MONO(set) PFC_INTROSPEC_DECL;
public:
// nested types
class const_iterator;
class iterator;
struct inserter {iterator it; bool is_new;};
//--------------------------------------------------------------------------
// construction
set(const CmpPred &cmp_pred_=CmpPred(), memory_allocator_base *alloc_=0);
set(const set&);
set(const set&, memory_allocator_base*);
PFC_INLINE ~set();
void operator=(const set&);
void set_allocator(memory_allocator_base*);
void clear();
PFC_INLINE void swap(set&);
void validate_rb_state() const;
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
template<typename K> PFC_INLINE const_iterator find(const K&) const;
template<typename K> PFC_INLINE iterator find(const K&);
template<typename K> PFC_INLINE const_iterator lower_bound(const K&) const;
template<typename K> PFC_INLINE iterator lower_bound(const K&);
template<typename K> PFC_INLINE const_iterator upper_bound(const K&) const;
template<typename K> PFC_INLINE iterator upper_bound(const K&);
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
template<typename K> inserter insert(const K&, bool replace_=true);
void erase(iterator&);
//--------------------------------------------------------------------------
private:
class node;
void cctor(const set&);
void copy_subtree(node&, node *parent_, node *&child_ptr_);
void destroy_subtree(node&);
void rotate_subtree(node&, bool rotate_right_);
void restore_rb_property_after_insert(node*);
void restore_rb_property_after_remove(node*, node *parent_, bool is_left_child_);
void validate_rb_state(node *n_, unsigned depth_, unsigned &max_depth_) const;
//--------------------------------------------------------------------------
//==========================================================================
// set::node
//==========================================================================
class node
{
public:
T data;
char color; // 0=black, 1=red
node *parent;
node *children[2]; // 0=left, 1=right
//------------------------------------------------------------------------
private:
node(); // not implemented
void operator=(const node&); // not implemented
};
//--------------------------------------------------------------------------
friend struct set_traits<T, CmpPred>;
const CmpPred m_cmp_pred;
memory_allocator_base *m_allocator;
usize_t m_size;
node *m_root;
};
PFC_SET_TYPE_TRAIT_PARTIAL2(typename T, class CmpPred, set<T, CmpPred>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// set::const_iterator
//============================================================================
template<typename T, class CmpPred>
class set<T, CmpPred>::const_iterator
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE const_iterator();
PFC_INLINE const_iterator(const iterator&);
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const const_iterator &it_) {return it_.m_node!=0;}
PFC_INLINE bool operator==(const const_iterator&) const;
PFC_INLINE bool operator==(const iterator&) const;
PFC_INLINE bool operator!=(const const_iterator&) const;
PFC_INLINE bool operator!=(const iterator&) const;
PFC_INLINE const_iterator &operator++();
PFC_INLINE const T &operator*() const;
PFC_INLINE const T *operator->() const;
PFC_INLINE friend const T *ptr(const const_iterator &it_) {return it_.m_node?&it_.m_node->data:0;}
//--------------------------------------------------------------------------
private:
friend class set<T, CmpPred>;
PFC_INLINE const_iterator(const node*);
//--------------------------------------------------------------------------
const node *m_node;
};
//----------------------------------------------------------------------------
//============================================================================
// set::iterator
//============================================================================
template<typename T, class CmpPred>
class set<T, CmpPred>::iterator
{
public:
// nested types
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE iterator();
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const iterator &it_) {return it_.m_node!=0;}
PFC_INLINE bool operator==(const const_iterator&) const;
PFC_INLINE bool operator==(const iterator&) const;
PFC_INLINE bool operator!=(const const_iterator&) const;
PFC_INLINE bool operator!=(const iterator&) const;
PFC_INLINE iterator &operator++();
PFC_INLINE const T &operator*() const;
PFC_INLINE const T *operator->() const;
PFC_INLINE friend const T *ptr(const iterator &it_) {return it_.m_node?&it_.m_node->data:0;}
//--------------------------------------------------------------------------
private:
friend class set<T, CmpPred>;
friend class const_iterator;
PFC_INLINE iterator(node*);
//--------------------------------------------------------------------------
node *m_node;
};
//----------------------------------------------------------------------------
//============================================================================
// map
//============================================================================
template<typename K, typename T, class CmpPred=compare_predicate>
struct map_traits
{
// properties
enum {block_alloc_size=sizeof(typename map<K, T, CmpPred>::node),
block_alloc_align=meta_alignof<typename map<K, T, CmpPred>::node>::res};
};
//----------------------------------------------------------------------------
template<typename K, typename T, class CmpPred>
class map
{ PFC_MONO(map) PFC_INTROSPEC_DECL;
public:
// nested types
class const_iterator;
class iterator;
struct inserter {iterator it; bool is_new;};
//--------------------------------------------------------------------------
// construction
map(const CmpPred &cmp_pred_=CmpPred(), memory_allocator_base *alloc_=0);
map(const map&);
map(const map&, memory_allocator_base*);
PFC_INLINE ~map();
void operator=(const map&);
void set_allocator(memory_allocator_base*);
void clear();
PFC_INLINE void swap(map&);
//--------------------------------------------------------------------------
// accessors and mutators
PFC_INLINE memory_allocator_base &allocator() const;
PFC_INLINE usize_t size() const;
template<typename K1> PFC_INLINE const T &operator[](const K1&) const;
template<typename K1> PFC_INLINE T &operator[](const K1&);
template<typename K1> PFC_INLINE const_iterator find(const K1&) const;
template<typename K1> PFC_INLINE iterator find(const K1&);
template<typename K1> PFC_INLINE const_iterator lower_bound(const K1&) const;
template<typename K1> PFC_INLINE iterator lower_bound(const K1&);
template<typename K1> PFC_INLINE const_iterator upper_bound(const K1&) const;
template<typename K1> PFC_INLINE iterator upper_bound(const K1&);
PFC_INLINE const_iterator begin() const;
PFC_INLINE iterator begin();
PFC_INLINE const_iterator end() const;
PFC_INLINE iterator end();
inserter insert(const K&, const T&, bool replace_=true);
inserter insert(const K&, bool replace_=true);
void erase(iterator&);
//--------------------------------------------------------------------------
private:
class node;
void cctor(const map&);
void copy_subtree(node&, node *parent_, node *&child_ptr_);
void destroy_subtree(node&);
void rotate_subtree(node&, bool rotate_right_);
void restore_rb_property_after_insert(node*);
void restore_rb_property_after_remove(node*, node *parent_, bool is_left_child_);
//--------------------------------------------------------------------------
//==========================================================================
// map::node
//==========================================================================
class node
{ PFC_MONO(node) {PFC_VAR2(key, val);}
public:
K key;
T val;
char color; // 0=black, 1=red
node *parent;
node *children[2]; // 0=left, 1=right
//------------------------------------------------------------------------
private:
node(); // not implemented
void operator=(const node&); // not implemented
};
//--------------------------------------------------------------------------
friend struct map_traits<K, T, CmpPred>;
const CmpPred m_cmp_pred;
memory_allocator_base *m_allocator;
usize_t m_size;
node *m_root;
};
PFC_SET_TYPE_TRAIT_PARTIAL3(typename T, typename K, class CmpPred, map<K, T, CmpPred>, is_type_pod_move, true);
//----------------------------------------------------------------------------
//============================================================================
// map::const_iterator
//============================================================================
template<typename K, typename T, class CmpPred>
class map<K, T, CmpPred>::const_iterator
{
public:
// nested types
typedef K key_t;
typedef T value_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE const_iterator();
PFC_INLINE const_iterator(const iterator&);
PFC_INLINE void reset();
//--------------------------------------------------------------------------
// iteration
PFC_INLINE friend bool is_valid(const const_iterator &it_) {return it_.m_node!=0;}
PFC_INLINE bool operator==(const const_iterator&) const;
PFC_INLINE bool operator==(const iterator&) const;
PFC_INLINE bool operator!=(const const_iterator&) const;
PFC_INLINE bool operator!=(const iterator&) const;
PFC_INLINE const_iterator &operator++();
PFC_INLINE const T &operator*() const;
PFC_INLINE const T *operator->() const;
PFC_INLINE friend const T *ptr(const const_iterator &it_) {return it_.m_node?&it_.m_node->val:0;}
PFC_INLINE const K &key() const;
//--------------------------------------------------------------------------
private:
friend class map<K, T, CmpPred>;
PFC_INLINE const_iterator(const node*);
//--------------------------------------------------------------------------
const node *m_node;
};
//----------------------------------------------------------------------------