forked from mattreecebentley/plf_colony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplf_colony.h
4749 lines (3698 loc) · 176 KB
/
plf_colony.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
// Copyright (c) 2021, Matthew Bentley (mattreecebentley@gmail.com) www.plflib.org
// zLib license (https://www.zlib.net/zlib_license.html):
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgement in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
#ifndef PLF_COLONY_H
#define PLF_COLONY_H
// Compiler-specific defines:
#if defined(_MSC_VER)
#define PLF_FORCE_INLINE __forceinline
#if _MSC_VER >= 1600
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_STATIC_ASSERT(check, message) static_assert(check, message)
#else
#define PLF_STATIC_ASSERT(check, message) assert(check)
#endif
#if _MSC_VER >= 1700
#define PLF_TYPE_TRAITS_SUPPORT
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#endif
#if _MSC_VER >= 1800
#define PLF_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if _MSC_VER >= 1900
#define PLF_ALIGNMENT_SUPPORT
#define PLF_NOEXCEPT noexcept
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#else
#define PLF_NOEXCEPT throw()
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)
#define PLF_CONSTEXPR constexpr
#else
#define PLF_CONSTEXPR
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG > 201703L) && _MSC_VER >= 1923
#define PLF_CPP20_SUPPORT
#endif
#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least
#define PLF_FORCE_INLINE // note: GCC and clang create faster code without forcing inline
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_VARIADICS_SUPPORT
#define PLF_STATIC_ASSERT(check, message) static_assert(check, message)
#else
#define PLF_STATIC_ASSERT(check, message) assert(check)
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4
#define PLF_NOEXCEPT noexcept
#else
#define PLF_NOEXCEPT throw()
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || __GNUC__ > 4
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable
#define PLF_TYPE_TRAITS_SUPPORT
#endif
#if __GNUC__ > 6
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#elif defined(__clang__) && !defined(__GLIBCXX__) && !defined(_LIBCPP_CXX03_LANG)
#if __clang_major__ >= 3 // clang versions < 3 don't support __has_feature() or traits
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#define PLF_TYPE_TRAITS_SUPPORT
#if __has_feature(cxx_alignas) && __has_feature(cxx_alignof)
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __has_feature(cxx_noexcept)
#define PLF_NOEXCEPT noexcept
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#else
#define PLF_NOEXCEPT throw()
#endif
#if __has_feature(cxx_rvalue_references) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
#define PLF_MOVE_SEMANTICS_SUPPORT
#endif
#if __has_feature(cxx_static_assert)
#define PLF_STATIC_ASSERT(check, message) static_assert(check, message)
#else
#define PLF_STATIC_ASSERT(check, message) assert(check)
#endif
#if __has_feature(cxx_variadic_templates) && !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_VARIADICS_SUPPORT
#endif
#if (__clang_major__ == 3 && __clang_minor__ >= 1) || __clang_major__ > 3
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#endif
#elif defined(__GLIBCXX__) // Using another compiler type with libstdc++ - we are assuming full c++11 compliance for compiler - which may not be true
#if __GLIBCXX__ >= 20080606
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_VARIADICS_SUPPORT
#define PLF_STATIC_ASSERT(check, message) static_assert(check, message)
#else
#define PLF_STATIC_ASSERT(check, message) assert(check)
#endif
#if __GLIBCXX__ >= 20090421
#define PLF_INITIALIZER_LIST_SUPPORT
#endif
#if __GLIBCXX__ >= 20120322
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#define PLF_NOEXCEPT noexcept
#else
#define PLF_NOEXCEPT throw()
#endif
#if __GLIBCXX__ >= 20130322
#define PLF_ALIGNMENT_SUPPORT
#endif
#if __GLIBCXX__ >= 20150422 // libstdc++ v4.9 and below do not support std::is_trivially_copyable
#define PLF_TYPE_TRAITS_SUPPORT
#endif
#if __GLIBCXX__ >= 20160111
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#elif defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) // Special case for checking C++11 support with libCPP
#define PLF_STATIC_ASSERT(check, message) assert(check)
#define PLF_NOEXCEPT throw()
#if !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_VARIADICS_SUPPORT
#endif
#else // Assume type traits and initializer support for other compilers and standard library implementations
#define PLF_MOVE_SEMANTICS_SUPPORT
#define PLF_STATIC_ASSERT(check, message) static_assert(check, message)
#define PLF_VARIADICS_SUPPORT
#define PLF_TYPE_TRAITS_SUPPORT
#define PLF_ALLOCATOR_TRAITS_SUPPORT
#define PLF_ALIGNMENT_SUPPORT
#define PLF_INITIALIZER_LIST_SUPPORT
#define PLF_NOEXCEPT noexcept
#define PLF_IS_ALWAYS_EQUAL_SUPPORT
#endif
#if __cplusplus >= 201703L && ((defined(__clang__) && ((__clang_major__ == 3 && __clang_minor__ == 9) || __clang_major__ > 3)) || (defined(__GNUC__) && __GNUC__ >= 7) || (!defined(__clang__) && !defined(__GNUC__))) // assume correct C++17 implementation for non-gcc/clang compilers
#define PLF_CONSTEXPR constexpr
#else
#define PLF_CONSTEXPR
#endif
#if __cplusplus > 201703L && ((defined(__clang__) && (__clang_major__ >= 10)) || (defined(__GNUC__) && __GNUC__ >= 10) || (!defined(__clang__) && !defined(__GNUC__)))
#define PLF_CPP20_SUPPORT
#endif
#else
#define PLF_FORCE_INLINE
#define PLF_STATIC_ASSERT(check, message) assert(check)
#define PLF_NOEXCEPT throw()
#define PLF_CONSTEXPR
#endif
#if defined(PLF_IS_ALWAYS_EQUAL_SUPPORT) && defined(PLF_MOVE_SEMANTICS_SUPPORT) && defined(PLF_ALLOCATOR_TRAITS_SUPPORT) && (__cplusplus >= 201703L || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
#define PLF_NOEXCEPT_MOVE_ASSIGN(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#define PLF_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value || std::allocator_traits<the_allocator>::is_always_equal::value)
#define PLF_NOEXCEPT_SPLICE(the_allocator) noexcept(std::allocator_traits<the_allocator>::is_always_equal::value)
#else
#define PLF_NOEXCEPT_MOVE_ASSIGN(the_allocator)
#define PLF_NOEXCEPT_SWAP(the_allocator)
#define PLF_NOEXCEPT_SPLICE(the_allocator)
#endif
#undef PLF_IS_ALWAYS_EQUAL_SUPPORT
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
#ifdef PLF_VARIADICS_SUPPORT
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits<the_allocator>::construct(allocator_instance, location, __VA_ARGS__)
#else
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, data) std::allocator_traits<the_allocator>::construct(allocator_instance, location, data)
#endif
#define PLF_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits<the_allocator>::destroy(allocator_instance, location)
#define PLF_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits<the_allocator>::allocate(allocator_instance, size, hint)
#define PLF_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits<the_allocator>::deallocate(allocator_instance, location, size)
#else
#ifdef PLF_VARIADICS_SUPPORT
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, ...) (allocator_instance).construct(location, __VA_ARGS__)
#else
#define PLF_CONSTRUCT(the_allocator, allocator_instance, location, data) (allocator_instance).construct(location, data)
#endif
#define PLF_DESTROY(the_allocator, allocator_instance, location) (allocator_instance).destroy(location)
#define PLF_ALLOCATE(the_allocator, allocator_instance, size, hint) (allocator_instance).allocate(size, hint)
#define PLF_DEALLOCATE(the_allocator, allocator_instance, location, size) (allocator_instance).deallocate(location, size)
#endif
#include <algorithm> // std::fill_n, std::sort
#include <cassert> // assert
#include <cstring> // memset, memcpy, size_t
#include <limits> // std::numeric_limits
#include <memory> // std::allocator
#include <iterator> // std::bidirectional_iterator_tag, iterator_traits, make_move_iterator
#include <stdexcept> // std::length_error
#ifdef PLF_TYPE_TRAITS_SUPPORT
#include <cstddef> // offsetof, used in blank()
#include <type_traits> // std::is_trivially_destructible, etc
#endif
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
#include <utility> // std::move
#endif
#ifdef PLF_INITIALIZER_LIST_SUPPORT
#include <initializer_list>
#endif
#ifdef PLF_CPP20_SUPPORT
#include <concepts>
#endif
namespace plf
{
struct colony_limits // for use in block_capacity setting/getting functions and constructors
{
size_t min, max;
colony_limits(const size_t minimum, const size_t maximum) PLF_NOEXCEPT : min(minimum), max(maximum) {}
};
enum colony_priority { performance, memory_use };
template <class element_type, class allocator_type = std::allocator<element_type>, plf::colony_priority priority = plf::performance> class colony : private allocator_type // Empty base class optimisation (EBCO) - inheriting allocator functions
{
// Type-switching pattern:
template <bool flag, class is_true, class is_false> struct choose;
template <class is_true, class is_false> struct choose<true, is_true, is_false>
{
typedef is_true type;
};
template <class is_true, class is_false> struct choose<false, is_true, is_false>
{
typedef is_false type;
};
typedef typename choose<priority == plf::performance, unsigned short, unsigned char>::type skipfield_type; // Note: unsigned short is equivalent to uint_least16_t ie. Using 16-bit unsigned integer in best-case scenario, greater-than-16-bit unsigned integer where platform doesn't support 16-bit types. unsigned char is always == 1 byte, as opposed to uint_8, which may not be
public:
// Standard container typedefs:
typedef element_type value_type;
#ifdef PLF_ALIGNMENT_SUPPORT
typedef typename std::aligned_storage<sizeof(element_type), (sizeof(element_type) >= (sizeof(skipfield_type) * 2) || alignof(element_type) >= (sizeof(skipfield_type) * 2)) ? alignof(element_type) : (sizeof(skipfield_type) * 2)>::type aligned_element_type;
#else
typedef element_type aligned_element_type;
#endif
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
typedef typename std::allocator_traits<allocator_type>::size_type size_type;
typedef typename std::allocator_traits<allocator_type>::difference_type difference_type;
typedef element_type & reference;
typedef const element_type & const_reference;
typedef typename std::allocator_traits<allocator_type>::pointer pointer;
typedef typename std::allocator_traits<allocator_type>::const_pointer const_pointer;
#else
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
#endif
// Iterator declarations:
template <bool is_const> class colony_iterator;
typedef colony_iterator<false> iterator;
typedef colony_iterator<true> const_iterator;
friend class colony_iterator<false>; // Using above typedef name here is illegal under C++03
friend class colony_iterator<true>;
template <bool r_is_const> class colony_reverse_iterator;
typedef colony_reverse_iterator<false> reverse_iterator;
typedef colony_reverse_iterator<true> const_reverse_iterator;
friend class colony_reverse_iterator<false>;
friend class colony_reverse_iterator<true>;
private:
#ifdef PLF_ALIGNMENT_SUPPORT
struct alignas(alignof(aligned_element_type)) aligned_allocation_struct
{
char data[alignof(aligned_element_type)]; // Using char as sizeof is always guaranteed to be 1 byte regardless of the number of bits in a byte on given computer, whereas for example, uint8_t would fail on machines where there are more than 8 bits in a byte eg. Texas Instruments C54x DSPs.
};
#define PLF_GROUP_ALIGNED_BLOCK_SIZE(elements_per_group) ((((elements_per_group * (((sizeof(aligned_element_type) >= alignof(aligned_element_type)) ? sizeof(aligned_element_type) : alignof(aligned_element_type)) + sizeof(skipfield_type))) + sizeof(skipfield_type)) + sizeof(aligned_allocation_struct) - 1) / sizeof(aligned_allocation_struct)) // The size of a groups' memory block when expressed in multiples of the value_type's alignment. We also check to see if alignment is larger than sizeof value_type and use alignment size if so.
#else
struct aligned_allocation_struct
{
char data;
};
#define PLF_GROUP_ALIGNED_BLOCK_SIZE(elements_per_group) ((elements_per_group * (sizeof(aligned_element_type) + sizeof(skipfield_type))) + sizeof(skipfield_type)) // The size of a groups' memory block when expressed in bytes, since no alignment available
#endif
// forward declarations for typedefs below
struct group;
struct item_index_tuple; // for use in sort()
#ifdef PLF_ALLOCATOR_TRAITS_SUPPORT
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<aligned_element_type> aligned_element_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<group> group_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<skipfield_type> skipfield_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<aligned_allocation_struct> aligned_struct_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<item_index_tuple> tuple_allocator_type;
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<unsigned char> uchar_allocator_type;
typedef typename std::allocator_traits<aligned_element_allocator_type>::pointer aligned_pointer_type; // pointer to the overaligned element type, not the original element type
typedef typename std::allocator_traits<group_allocator_type>::pointer group_pointer_type;
typedef typename std::allocator_traits<skipfield_allocator_type>::pointer skipfield_pointer_type;
typedef typename std::allocator_traits<aligned_struct_allocator_type>::pointer aligned_struct_pointer_type;
typedef typename std::allocator_traits<tuple_allocator_type>::pointer tuple_pointer_type;
#else
typedef typename allocator_type::template rebind<aligned_element_type>::other aligned_element_allocator_type; // In case compiler supports alignment but not allocator_traits
typedef typename allocator_type::template rebind<group>::other group_allocator_type;
typedef typename allocator_type::template rebind<skipfield_type>::other skipfield_allocator_type;
typedef typename allocator_type::template rebind<char>::other aligned_struct_allocator_type;
typedef typename allocator_type::template rebind<item_index_tuple>::other tuple_allocator_type;
typedef typename allocator_type::template rebind<unsigned char>::other uchar_allocator_type;
typedef typename aligned_element_allocator_type::pointer aligned_pointer_type;
typedef typename group_allocator_type::pointer group_pointer_type;
typedef typename skipfield_allocator_type::pointer skipfield_pointer_type;
typedef typename aligned_struct_allocator_type::pointer aligned_struct_pointer_type;
typedef typename tuple_allocator_type::pointer tuple_pointer_type;
#endif
// Colony groups:
struct group : private aligned_struct_allocator_type // ebco - inherit allocator functions
{
aligned_pointer_type last_endpoint; // The address that is one past the highest cell number that's been used so far in this group - does not change with erase command but may change with insert (if no previously-erased locations are available) - is necessary because an iterator cannot access the colony's end_iterator. Most-used variable in colony use (operator ++, --) so first in struct. If the colony has been completely filled at some point, it will be == reinterpret_cast<aligned_pointer_type>(skipfield)
group_pointer_type next_group; // Next group in the intrusive list of all groups. NULL if no next group
const aligned_pointer_type elements; // Element storage
const skipfield_pointer_type skipfield; // Skipfield storage. The element and skipfield arrays are allocated contiguously in this implementation, hence the skipfield pointer also functions as a 'one-past-end' pointer for the elements array. There will always be one additional skipfield node allocated compared to the number of elements. This is to ensure a faster ++ iterator operation (fewer checks are required when this is present). The extra node is unused and always zero, but checked, and not having it will result in out-of-bounds memory errors.
group_pointer_type previous_group; // previous group in the linked list of all groups. NULL if no preceding group
skipfield_type free_list_head; // The index of the last erased element in the group. The last erased element will, in turn, contain the number of the index of the next erased element, and so on. If this is == maximum skipfield_type value then free_list is empty ie. no erasures have occurred in the group (or if they have, the erased locations have subsequently been reused via insert()).
const skipfield_type capacity; // The element capacity of this particular group - can also be calculated from reinterpret_cast<aligned_pointer_type>(group->skipfield) - group->elements, however this space is effectively free due to struct padding and the default sizeof skipfield_type, and calculating it once is cheaper
skipfield_type size; // indicates total number of active elements in group - changes with insert and erase commands - used to check for empty group in erase function, as an indication to remove the group
group_pointer_type erasures_list_next_group; // The next group in the singly-linked list of groups with erasures ie. with active erased-element free lists
size_type group_number; // Used for comparison (> < >= <= <=>) iterator operators (used by distance function and user)
#ifdef PLF_VARIADICS_SUPPORT
group(const skipfield_type elements_per_group, group_pointer_type const previous):
last_endpoint(reinterpret_cast<aligned_pointer_type>(PLF_ALLOCATE(aligned_struct_allocator_type, *this, PLF_GROUP_ALIGNED_BLOCK_SIZE(elements_per_group), (previous == NULL) ? 0 : previous->elements))),
next_group(NULL),
elements(last_endpoint++),
skipfield(reinterpret_cast<skipfield_pointer_type>(elements + elements_per_group)),
previous_group(previous),
free_list_head(std::numeric_limits<skipfield_type>::max()),
capacity(elements_per_group),
size(1),
erasures_list_next_group(NULL),
group_number((previous == NULL) ? 0 : previous->group_number + 1u)
{
// Static casts to unsigned int from short not necessary as C++ automatically promotes lesser types for arithmetic purposes.
std::memset(&*skipfield, 0, sizeof(skipfield_type) * (static_cast<size_type>(elements_per_group) + 1u)); // &* to avoid problems with non-trivial pointers
}
#else
// This is a hack around the fact that allocator_type::construct only supports copy construction in C++03 and copy elision does not occur on the vast majority of compilers in this circumstance. So to avoid running out of memory (and losing performance) from allocating the same block twice, we're allocating in the 'copy' constructor.
group(const skipfield_type elements_per_group, group_pointer_type const previous) PLF_NOEXCEPT:
elements(NULL),
skipfield(NULL),
previous_group(previous),
capacity(elements_per_group)
{}
// Not a real copy constructor ie. actually a constructor. Only used for allocator.construct in C++03 for reasons stated above:
group(const group &source):
aligned_struct_allocator_type(source),
last_endpoint(reinterpret_cast<aligned_pointer_type>(PLF_ALLOCATE(aligned_struct_allocator_type, *this, PLF_GROUP_ALIGNED_BLOCK_SIZE(source.capacity), (source.previous_group == NULL) ? 0 : source.previous_group->elements))),
next_group(NULL),
elements(last_endpoint++),
skipfield(reinterpret_cast<skipfield_pointer_type>(elements + source.capacity)),
previous_group(source.previous_group),
free_list_head(std::numeric_limits<skipfield_type>::max()),
capacity(source.capacity),
size(1),
erasures_list_next_group(NULL),
group_number((source.previous_group == NULL) ? 0 : source.previous_group->group_number + 1u)
{
std::memset(&*skipfield, 0, sizeof(skipfield_type) * (static_cast<size_type>(capacity) + 1u));
}
#endif
void reset(const skipfield_type increment, const group_pointer_type next, const group_pointer_type previous, const size_type group_num) PLF_NOEXCEPT
{
last_endpoint = elements + increment;
next_group = next;
free_list_head = std::numeric_limits<skipfield_type>::max();
previous_group = previous;
size = increment;
erasures_list_next_group = NULL;
group_number = group_num;
std::memset(&*skipfield, 0, sizeof(skipfield_type) * static_cast<size_type>(capacity)); // capacity + 1 is not necessary here as the end skipfield is never written to after initialization
}
~group() PLF_NOEXCEPT
{
// Null check not necessary (for copied group as above) as delete will also perform a null check.
PLF_DEALLOCATE(aligned_struct_allocator_type, *this, reinterpret_cast<aligned_struct_pointer_type>(elements), PLF_GROUP_ALIGNED_BLOCK_SIZE(capacity));
}
};
public:
// Iterators:
template <bool is_const> class colony_iterator
{
private:
group_pointer_type group_pointer;
aligned_pointer_type element_pointer;
skipfield_pointer_type skipfield_pointer;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename colony::value_type value_type;
typedef typename colony::difference_type difference_type;
typedef typename choose<is_const, typename colony::const_pointer, typename colony::pointer>::type pointer;
typedef typename choose<is_const, typename colony::const_reference, typename colony::reference>::type reference;
friend class colony;
friend class colony_reverse_iterator<false>;
friend class colony_reverse_iterator<true>;
inline colony_iterator & operator = (const colony_iterator &source) PLF_NOEXCEPT
{
group_pointer = source.group_pointer;
element_pointer = source.element_pointer;
skipfield_pointer = source.skipfield_pointer;
return *this;
}
inline colony_iterator & operator = (const colony_iterator<!is_const> &source) PLF_NOEXCEPT
{
group_pointer = source.group_pointer;
element_pointer = source.element_pointer;
skipfield_pointer = source.skipfield_pointer;
return *this;
}
#ifdef PLF_MOVE_SEMANTICS_SUPPORT
// Move assignment - only really necessary if the allocator uses non-standard ie. "smart" pointers
inline colony_iterator & operator = (colony_iterator &&source) PLF_NOEXCEPT
{
assert(&source != this);
group_pointer = std::move(source.group_pointer);
element_pointer = std::move(source.element_pointer);
skipfield_pointer = std::move(source.skipfield_pointer);
return *this;
}
inline colony_iterator & operator = (colony_iterator<!is_const> &&source) PLF_NOEXCEPT
{
group_pointer = std::move(source.group_pointer);
element_pointer = std::move(source.element_pointer);
skipfield_pointer = std::move(source.skipfield_pointer);
return *this;
}
#endif
inline PLF_FORCE_INLINE bool operator == (const colony_iterator &rh) const PLF_NOEXCEPT
{
return (element_pointer == rh.element_pointer);
}
inline PLF_FORCE_INLINE bool operator == (const colony_iterator<!is_const> &rh) const PLF_NOEXCEPT
{
return (element_pointer == rh.element_pointer);
}
inline PLF_FORCE_INLINE bool operator != (const colony_iterator &rh) const PLF_NOEXCEPT
{
return (element_pointer != rh.element_pointer);
}
inline PLF_FORCE_INLINE bool operator != (const colony_iterator<!is_const> &rh) const PLF_NOEXCEPT
{
return (element_pointer != rh.element_pointer);
}
inline PLF_FORCE_INLINE reference operator * () const // may cause exception with uninitialized iterator
{
return *(reinterpret_cast<pointer>(element_pointer));
}
inline PLF_FORCE_INLINE pointer operator -> () const PLF_NOEXCEPT
{
return reinterpret_cast<pointer>(element_pointer);
}
#if defined(_MSC_VER) && _MSC_VER <= 1600 // MSVC 2010 needs a bit of a helping hand when it comes to optimizing
inline PLF_FORCE_INLINE colony_iterator & operator ++ ()
#else
colony_iterator & operator ++ ()
#endif
{
assert(group_pointer != NULL); // covers uninitialised colony_iterator
skipfield_type skip = *(++skipfield_pointer);
if ((element_pointer += static_cast<size_type>(skip) + 1u) == group_pointer->last_endpoint && group_pointer->next_group != NULL) // ie. beyond end of current memory block. Second condition allows iterator to reach end(), which may be 1 past end of block, if block has been fully used and another block is not allocated
{
group_pointer = group_pointer->next_group;
const aligned_pointer_type elements = group_pointer->elements;
const skipfield_pointer_type skipfield = group_pointer->skipfield;
skip = *skipfield;
element_pointer = elements + skip;
skipfield_pointer = skipfield;
}
skipfield_pointer += skip;
return *this;
}
inline colony_iterator operator ++(int)
{
const colony_iterator copy(*this);
++*this;
return copy;
}
public:
colony_iterator & operator -- ()
{
assert(group_pointer != NULL);
if (element_pointer != group_pointer->elements) // ie. not already at beginning of group
{
const skipfield_type skip = *(--skipfield_pointer);
skipfield_pointer -= skip;
if ((element_pointer -= static_cast<size_type>(skip) + 1u) != group_pointer->elements - 1) // ie. iterator was not already at beginning of colony (with some previous consecutive deleted elements), and skipfield does not takes us into the previous group)
{
return *this;
}
}
group_pointer = group_pointer->previous_group;
const skipfield_pointer_type skipfield = group_pointer->skipfield + group_pointer->capacity - 1;
const skipfield_type skip = *skipfield;
element_pointer = (reinterpret_cast<colony::aligned_pointer_type>(group_pointer->skipfield) - 1) - skip;
skipfield_pointer = skipfield - skip;
return *this;
}
inline colony_iterator operator -- (int)
{
const colony_iterator copy(*this);
--*this;
return copy;
}
template <bool is_const_it>
inline bool operator > (const colony_iterator<is_const_it> &rh) const PLF_NOEXCEPT
{
return ((group_pointer == rh.group_pointer) & (element_pointer > rh.element_pointer)) || (group_pointer != rh.group_pointer && group_pointer->group_number > rh.group_pointer->group_number);
}
template <bool is_const_it>
inline bool operator < (const colony_iterator<is_const_it> &rh) const PLF_NOEXCEPT
{
return rh > *this;
}
template <bool is_const_it>
inline bool operator >= (const colony_iterator<is_const_it> &rh) const PLF_NOEXCEPT
{
return !(rh > *this);
}
template <bool is_const_it>
inline bool operator <= (const colony_iterator<is_const_it> &rh) const PLF_NOEXCEPT
{
return !(*this > rh);
}
#ifdef PLF_CPP20_SUPPORT
template <bool is_const_it>
inline int operator <=> (const colony_iterator<is_const_it> &rh) const PLF_NOEXCEPT
{
return (element_pointer == rh.element_pointer) ? 0 : ((*this > rh) ? 1 : -1);
}
#endif
colony_iterator() PLF_NOEXCEPT: group_pointer(NULL), element_pointer(NULL), skipfield_pointer(NULL) {}
private:
// Used by cend(), erase() etc:
colony_iterator(const group_pointer_type group_p, const aligned_pointer_type element_p, const skipfield_pointer_type skipfield_p) PLF_NOEXCEPT: group_pointer(group_p), element_pointer(element_p), skipfield_pointer(skipfield_p) {}
public:
// Friend functions:
template <class distance_type>
friend inline void advance(colony_iterator &it, distance_type distance)
{
it.advance(static_cast<difference_type>(distance));
}
friend inline colony_iterator next(const colony_iterator &it, const difference_type distance)
{
colony_iterator return_iterator(it);
return_iterator.advance(static_cast<difference_type>(distance));
return return_iterator;
}
friend inline colony_iterator prev(const colony_iterator &it, const difference_type distance)
{
colony_iterator return_iterator(it);
return_iterator.advance(-(static_cast<difference_type>(distance)));
return return_iterator;
}
friend inline typename colony_iterator::difference_type distance(const colony_iterator &first, const colony_iterator &last)
{
return first.distance(last);
}
private:
// Advance implementation:
void advance(difference_type distance) // Cannot be noexcept due to the possibility of an uninitialized iterator
{
assert(group_pointer != NULL); // covers uninitialized colony_iterator && empty group
// Now, run code based on the nature of the distance type - negative, positive or zero:
if (distance > 0) // ie. +=
{
// Code explanation:
// For the initial state of the iterator, we don't know how what elements have been erased before that element in that group.
// So for the first group, we follow the following logic:
// 1. If no elements have been erased in the group, we do simple addition to progress either to within the group (if the distance is small enough) or the end of the group and subtract from distance accordingly.
// 2. If any of the first group elements have been erased, we manually iterate, as we don't know whether the erased elements occur before or after the initial iterator position, and we subtract 1 from the distance amount each time. Iteration continues until either distance becomes zero, or we reach the end of the group.
// For all subsequent groups, we follow this logic:
// 1. If distance is larger than the total number of non-erased elements in a group, we skip that group and subtract the number of elements in that group from distance
// 2. If distance is smaller than the total number of non-erased elements in a group, then:
// a. if there're no erased elements in the group we simply add distance to group->elements to find the new location for the iterator
// b. if there are erased elements in the group, we manually iterate and subtract 1 from distance on each iteration, until the new iterator location is found ie. distance = 0
// Note: incrementing element_pointer is avoided until necessary to avoid needless calculations
assert(!(element_pointer == group_pointer->last_endpoint && group_pointer->next_group == NULL)); // Check that we're not already at end()
// Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is)
if (element_pointer != group_pointer->elements + *(group_pointer->skipfield)) // ie. != first non-erased element in group
{
const difference_type distance_from_end = static_cast<difference_type>(group_pointer->last_endpoint - element_pointer);
if (group_pointer->size == static_cast<skipfield_type>(distance_from_end)) // ie. if there are no erasures in the group (using endpoint - elements_start to determine number of elements in group just in case this is the last group of the colony, in which case group->last_endpoint != group->elements + group->capacity)
{
if (distance < distance_from_end)
{
element_pointer += distance;
skipfield_pointer += distance;
return;
}
else if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end()
{
element_pointer = group_pointer->last_endpoint;
skipfield_pointer += distance_from_end;
return;
}
else
{
distance -= distance_from_end;
}
}
else
{
const skipfield_pointer_type endpoint = skipfield_pointer + distance_from_end;
while(true)
{
++skipfield_pointer;
skipfield_pointer += *skipfield_pointer;
--distance;
if (skipfield_pointer == endpoint)
{
break;
}
else if (distance == 0)
{
element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield);
return;
}
}
if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end()
{
element_pointer = group_pointer->last_endpoint;
return;
}
}
group_pointer = group_pointer->next_group;
if (distance == 0)
{
element_pointer = group_pointer->elements + *(group_pointer->skipfield);
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
return;
}
}
// Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be the first non-erased element in the current group:
while (static_cast<difference_type>(group_pointer->size) <= distance)
{
if (group_pointer->next_group == NULL) // either we've reached end() or gone beyond it, so bound to end()
{
element_pointer = group_pointer->last_endpoint;
skipfield_pointer = group_pointer->skipfield + (group_pointer->last_endpoint - group_pointer->elements);
return;
}
else if ((distance -= group_pointer->size) == 0)
{
group_pointer = group_pointer->next_group;
element_pointer = group_pointer->elements + *(group_pointer->skipfield);
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
return;
}
else
{
group_pointer = group_pointer->next_group;
}
}
// Final group (if not already reached):
if (group_pointer->free_list_head == std::numeric_limits<skipfield_type>::max()) // No erasures in this group, use straight pointer addition
{
element_pointer = group_pointer->elements + distance;
skipfield_pointer = group_pointer->skipfield + distance;
return;
}
else // ie. size > distance - safe to ignore endpoint check condition while incrementing:
{
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
do
{
++skipfield_pointer;
skipfield_pointer += *skipfield_pointer;
} while(--distance != 0);
element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield);
return;
}
return;
}
else if (distance < 0) // for negative change
{
// Code logic is very similar to += above
assert(!((element_pointer == group_pointer->elements + *(group_pointer->skipfield)) && group_pointer->previous_group == NULL)); // check that we're not already at begin()
distance = -distance;
// Special case for initial element pointer and initial group (we don't know how far into the group the element pointer is)
if (element_pointer != group_pointer->last_endpoint) // ie. != end()
{
if (group_pointer->free_list_head == std::numeric_limits<skipfield_type>::max()) // ie. no prior erasures have occurred in this group
{
const difference_type distance_from_beginning = static_cast<difference_type>(element_pointer - group_pointer->elements);
if (distance <= distance_from_beginning)
{
element_pointer -= distance;
skipfield_pointer -= distance;
return;
}
else if (group_pointer->previous_group == NULL) // ie. we've gone before begin(), so bound to begin()
{
element_pointer = group_pointer->elements;
skipfield_pointer = group_pointer->skipfield;
return;
}
else
{
distance -= distance_from_beginning;
}
}
else
{
const skipfield_pointer_type beginning_point = group_pointer->skipfield + *(group_pointer->skipfield);
while(skipfield_pointer != beginning_point)
{
--skipfield_pointer;
skipfield_pointer -= *skipfield_pointer;
if (--distance == 0)
{
element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield);
return;
}
}
if (group_pointer->previous_group == NULL)
{
element_pointer = group_pointer->elements + *(group_pointer->skipfield); // This is first group, so bound to begin() (just in case final decrement took us before begin())
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
return;
}
}
group_pointer = group_pointer->previous_group;
}
// Intermediary groups - at the start of this code block and the subsequent block, the position of the iterator is assumed to be either the first non-erased element in the next group over, or end():
while(static_cast<difference_type>(group_pointer->size) < distance)
{
if (group_pointer->previous_group == NULL) // we've gone beyond begin(), so bound to it
{
element_pointer = group_pointer->elements + *(group_pointer->skipfield);
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
return;
}
distance -= group_pointer->size;
group_pointer = group_pointer->previous_group;
}
// Final group (if not already reached):
if (static_cast<difference_type>(group_pointer->size) == distance)
{
element_pointer = group_pointer->elements + *(group_pointer->skipfield);
skipfield_pointer = group_pointer->skipfield + *(group_pointer->skipfield);
return;
}
else if (group_pointer->free_list_head == std::numeric_limits<skipfield_type>::max()) // ie. no erased elements in this group
{
element_pointer = reinterpret_cast<aligned_pointer_type>(group_pointer->skipfield) - distance;
skipfield_pointer = (group_pointer->skipfield + group_pointer->capacity) - distance;
return;
}
else // ie. no more groups to traverse but there are erased elements in this group
{
skipfield_pointer = group_pointer->skipfield + group_pointer->capacity;
do
{
--skipfield_pointer;
skipfield_pointer -= *skipfield_pointer;
} while(--distance != 0);
element_pointer = group_pointer->elements + (skipfield_pointer - group_pointer->skipfield);
return;
}
}
// Only distance == 0 reaches here
}
// distance implementation:
difference_type distance(const colony_iterator &last) const
{
// Code logic:
// If iterators are the same, return 0
// Otherwise, find which iterator is later in colony, copy that to iterator2. Copy the lower to iterator1.
// If they are not pointing to elements in the same group, process the intermediate groups and add distances,
// skipping manual incrementation in all but the initial and final groups.
// In the initial and final groups, manual incrementation must be used to calculate distance, if there have been no prior erasures in those groups.
// If there are no prior erasures in either of those groups, we can use pointer arithmetic to calculate the distances for those groups.
assert(!(group_pointer == NULL) && !(last.group_pointer == NULL)); // Check that they are initialized
if (last.element_pointer == element_pointer)
{
return 0;
}
difference_type distance = 0;
colony_iterator iterator1 = *this, iterator2 = last;
const bool swap = iterator1 > iterator2;
if (swap) // Less common case
{
iterator1 = last;
iterator2 = *this;
}
if (iterator1.group_pointer != iterator2.group_pointer) // if not in same group, process intermediate groups
{