forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colony.h
3517 lines (3007 loc) · 202 KB
/
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
// This software is a modified version of the original.
// The original license is as follows:
// Copyright (c) 2019, Matthew Bentley ([email protected]) 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.
#pragma once
#ifndef CATA_SRC_COLONY_H
#define CATA_SRC_COLONY_H
// Compiler-specific defines used by colony:
#define COLONY_CONSTEXPR
#define COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept
#define COLONY_NOEXCEPT_SWAP(the_allocator) noexcept
// TODO: Switch to these when we move to C++17
// #define COLONY_CONSTEXPR constexpr
// #define COLONY_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits<the_allocator>::is_always_equal::value)
// #define COLONY_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value)
// Note: GCC creates faster code without forcing inline
#if defined(_MSC_VER)
#define COLONY_FORCE_INLINE __forceinline
#else
#define COLONY_FORCE_INLINE
#endif
/* whole GCC 6 family */
#if __GNUC__ == 6
/* GCC 6.5 at least complains about type punning, but nothing else does. */
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
// TODO: get rid of these defines
#define COLONY_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits<the_allocator>::construct(allocator_instance, location, __VA_ARGS__)
#define COLONY_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits<the_allocator>::destroy(allocator_instance, location)
#define COLONY_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits<the_allocator>::allocate(allocator_instance, size, hint)
#define COLONY_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits<the_allocator>::allocate(*this, size, hint)
#define COLONY_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits<the_allocator>::deallocate(allocator_instance, location, size)
#include <algorithm> // std::sort and std::fill_n
#include <cassert> // assert
#include <cstddef> // offsetof, used in blank()
#include <cstring> // memset, memcpy
#include <initializer_list>
#include <iterator> // std::bidirectional_iterator_tag
#include <limits> // std::numeric_limits
#include <memory> // std::allocator
#include <type_traits> // std::is_trivially_destructible, etc
#include <utility> // std::move
namespace cata
{
template <class element_type, class element_allocator_type = std::allocator<element_type>, typename element_skipfield_type = unsigned short >
// Empty base class optimization - inheriting allocator functions
class colony : private element_allocator_type
// Note: unsigned short is equivalent to uint_least16_t i.e. Using 16-bit unsigned integer in best-case scenario, greater-than-16-bit unsigned integer where platform doesn't support 16-bit types
{
public:
// Standard container typedefs:
using value_type = element_type;
using allocator_type = element_allocator_type;
using skipfield_type = element_skipfield_type;
using aligned_element_type = typename std::aligned_storage < sizeof( element_type ),
( alignof( element_type ) > ( sizeof( element_skipfield_type ) * 2 ) ) ? alignof( element_type ) :
( sizeof( element_skipfield_type ) * 2 ) >::type;
using size_type = typename std::allocator_traits<element_allocator_type>::size_type;
using difference_type = typename std::allocator_traits<element_allocator_type>::difference_type;
using reference = element_type&;
using const_reference = const element_type&;
using pointer = typename std::allocator_traits<element_allocator_type>::pointer;
using const_pointer = typename std::allocator_traits<element_allocator_type>::const_pointer;
// Iterator declarations:
template <bool is_const> class colony_iterator;
using iterator = colony_iterator<false>;
using const_iterator = colony_iterator<true>;
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;
using reverse_iterator = colony_reverse_iterator<false>;
using const_reverse_iterator = colony_reverse_iterator<true>;
friend class colony_reverse_iterator<false>;
friend class colony_reverse_iterator<true>;
private:
struct group; // forward declaration for typedefs below
using aligned_element_allocator_type = typename
std::allocator_traits<element_allocator_type>::template rebind_alloc<aligned_element_type>;
using group_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<group>;
using skipfield_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<skipfield_type>;
// Using uchar as the generic allocator type, 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.
using uchar_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<unsigned char>;
// Different typedef to 'pointer' - this is a pointer to the over aligned element type, not the original element type
using aligned_pointer_type = typename
std::allocator_traits<aligned_element_allocator_type>::pointer;
using group_pointer_type = typename std::allocator_traits<group_allocator_type>::pointer;
using skipfield_pointer_type = typename std::allocator_traits<skipfield_allocator_type>::pointer;
using uchar_pointer_type = typename std::allocator_traits<uchar_allocator_type>::pointer;
using pointer_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<pointer>;
// Colony groups:
// Empty base class optimization (EBCO) - inheriting allocator functions
struct group : private uchar_allocator_type {
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
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, 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 intrusive 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 i.e. no erasures have occurred in the group (or if they have, the erased locations have then been reused via insert()).
const skipfield_type
capacity; // The element capacity of this particular group
skipfield_type
number_of_elements; // 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 intrusive singly-linked list of groups with erasures i.e. with active erased-element free lists
size_type
group_number; // Used for comparison (> < >= <=) iterator operators (used by distance function and user)
group( const skipfield_type elements_per_group, group_pointer_type const previous = nullptr ):
last_endpoint( reinterpret_cast<aligned_pointer_type>( COLONY_ALLOCATE_INITIALIZATION(
uchar_allocator_type, ( ( elements_per_group * ( sizeof( aligned_element_type ) ) ) + ( (
elements_per_group + 1u ) * sizeof( skipfield_type ) ) ),
( previous == nullptr ) ? nullptr :
previous->elements ) ) ), /* allocating to here purely because it is first in the struct sequence - actual pointer is elements, last_endpoint is only initialized to element's base value initially, then incremented by one below */
next_group( nullptr ),
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 ),
number_of_elements( 1 ),
erasures_list_next_group( nullptr ),
group_number( ( previous == nullptr ) ? 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 ) * ( elements_per_group +
1u ) ); // &* to avoid problems with non-trivial pointers
}
~group() noexcept {
// Null check not necessary (for copied group as above) as delete will also perform a null check.
COLONY_DEALLOCATE( uchar_allocator_type, ( *this ),
reinterpret_cast<uchar_pointer_type>( elements ),
( capacity * sizeof( aligned_element_type ) ) + ( ( capacity + 1u ) * sizeof( skipfield_type ) ) );
}
};
// Implement const/non-const iterator 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> {
using type = is_true;
};
template <class is_true, class is_false> struct choose<false, is_true, is_false> {
using type = is_false;
};
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:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename colony::value_type;
using difference_type = typename colony::difference_type;
using pointer = typename
choose<is_const, typename colony::const_pointer, typename colony::pointer>::type;
using reference = typename
choose<is_const, typename colony::const_reference, typename colony::reference>::type;
friend class colony;
friend class colony_reverse_iterator<false>;
friend class colony_reverse_iterator<true>;
inline colony_iterator &operator=( const colony_iterator &source ) 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 ) noexcept {
group_pointer = source.group_pointer;
element_pointer = source.element_pointer;
skipfield_pointer = source.skipfield_pointer;
return *this;
}
// Move assignment - only really necessary if the allocator uses non-standard i.e. smart pointers
inline colony_iterator &operator=( colony_iterator &&source )
noexcept { // Move is a copy in this scenario
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 ) 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_FORCE_INLINE bool operator==( const colony_iterator &rh ) const noexcept {
return ( element_pointer == rh.element_pointer );
}
inline COLONY_FORCE_INLINE bool operator==( const colony_iterator < !is_const > &rh ) const
noexcept {
return ( element_pointer == rh.element_pointer );
}
inline COLONY_FORCE_INLINE bool operator!=( const colony_iterator &rh ) const noexcept {
return ( element_pointer != rh.element_pointer );
}
inline COLONY_FORCE_INLINE bool operator!=( const colony_iterator < !is_const > &rh ) const
noexcept {
return ( element_pointer != rh.element_pointer );
}
// may cause exception with uninitialized iterator
inline COLONY_FORCE_INLINE reference operator*() const {
return *( reinterpret_cast<pointer>( element_pointer ) );
}
inline COLONY_FORCE_INLINE pointer operator->() const noexcept {
return reinterpret_cast<pointer>( element_pointer );
}
colony_iterator &operator++() {
// covers uninitialized colony_iterator
assert( group_pointer != nullptr );
// Assert that iterator is not already at end()
assert( !( element_pointer == group_pointer->last_endpoint &&
group_pointer->next_group != nullptr ) );
skipfield_type skip = *( ++skipfield_pointer );
// ie. beyond end of available data
if( ( element_pointer += skip + 1 ) == group_pointer->last_endpoint &&
group_pointer->next_group != nullptr ) {
group_pointer = group_pointer->next_group;
skip = *( group_pointer->skipfield );
element_pointer = group_pointer->elements + skip;
skipfield_pointer = group_pointer->skipfield;
}
skipfield_pointer += skip;
return *this;
}
inline colony_iterator operator++( int ) {
const colony_iterator copy( *this );
++*this;
return copy;
}
private:
inline COLONY_FORCE_INLINE void check_for_end_of_group_and_progress() { // used by erase
if( element_pointer == group_pointer->last_endpoint && group_pointer->next_group != nullptr ) {
group_pointer = group_pointer->next_group;
skipfield_pointer = group_pointer->skipfield;
element_pointer = group_pointer->elements + *skipfield_pointer;
skipfield_pointer += *skipfield_pointer;
}
}
public:
colony_iterator &operator--() {
assert( group_pointer != nullptr );
assert( !( element_pointer == group_pointer->elements &&
group_pointer->previous_group ==
nullptr ) ); // Assert that we are not already at begin() - this is not required to be tested in the code below as we don't need a special condition to progress to begin(), like we do with end() in operator ++
if( element_pointer != group_pointer->elements ) { // i.e. not already at beginning of group
const skipfield_type skip = *( --skipfield_pointer );
skipfield_pointer -= skip;
if( ( element_pointer -= skip + 1 ) != group_pointer->elements -
1 ) { // i.e. 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;
skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1;
element_pointer = ( reinterpret_cast<colony::aligned_pointer_type>( group_pointer->skipfield ) - 1 )
- *skipfield_pointer;
skipfield_pointer -= *skipfield_pointer;
return *this;
}
inline colony_iterator operator--( int ) {
const colony_iterator copy( *this );
--*this;
return copy;
}
inline bool operator>( const colony_iterator &rh ) const 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 );
}
inline bool operator<( const colony_iterator &rh ) const noexcept {
return rh > *this;
}
inline bool operator>=( const colony_iterator &rh ) const noexcept {
return !( rh > *this );
}
inline bool operator<=( const colony_iterator &rh ) const noexcept {
return !( *this > rh );
}
inline bool operator>( const colony_iterator < !is_const > &rh ) const 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 );
}
inline bool operator<( const colony_iterator < !is_const > &rh ) const noexcept {
return rh > *this;
}
inline bool operator>=( const colony_iterator < !is_const > &rh ) const noexcept {
return !( rh > *this );
}
inline bool operator<=( const colony_iterator < !is_const > &rh ) const noexcept {
return !( *this > rh );
}
colony_iterator() noexcept: group_pointer( nullptr ), element_pointer( nullptr ),
skipfield_pointer( nullptr ) {}
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 ) noexcept: group_pointer( group_p ),
element_pointer( element_p ), skipfield_pointer( skipfield_p ) {}
public:
inline colony_iterator( const colony_iterator &source ) noexcept:
group_pointer( source.group_pointer ),
element_pointer( source.element_pointer ),
skipfield_pointer( source.skipfield_pointer ) {}
inline colony_iterator( const colony_iterator < !is_const > &source ) noexcept:
group_pointer( source.group_pointer ),
element_pointer( source.element_pointer ),
skipfield_pointer( source.skipfield_pointer ) {}
// move constructor
inline colony_iterator( colony_iterator &&source ) noexcept:
group_pointer( std::move( source.group_pointer ) ),
element_pointer( std::move( source.element_pointer ) ),
skipfield_pointer( std::move( source.skipfield_pointer ) ) {}
inline colony_iterator( colony_iterator < !is_const > &&source ) noexcept:
group_pointer( std::move( source.group_pointer ) ),
element_pointer( std::move( source.element_pointer ) ),
skipfield_pointer( std::move( source.skipfield_pointer ) ) {}
}; // colony_iterator
// Reverse iterators:
template <bool r_is_const> class colony_reverse_iterator
{
private:
iterator it;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename colony::value_type;
using difference_type = typename colony::difference_type;
using pointer = typename
choose<r_is_const, typename colony::const_pointer, typename colony::pointer>::type;
using reference = typename
choose<r_is_const, typename colony::const_reference, typename colony::reference>::type;
friend class colony;
inline colony_reverse_iterator &operator=( const colony_reverse_iterator &source ) noexcept {
it = source.it;
return *this;
}
// move assignment
inline colony_reverse_iterator &operator=( colony_reverse_iterator &&source ) noexcept {
it = std::move( source.it );
return *this;
}
inline COLONY_FORCE_INLINE bool operator==( const colony_reverse_iterator &rh ) const noexcept {
return ( it == rh.it );
}
inline COLONY_FORCE_INLINE bool operator!=( const colony_reverse_iterator &rh ) const noexcept {
return ( it != rh.it );
}
inline COLONY_FORCE_INLINE reference operator*() const noexcept {
return *( reinterpret_cast<pointer>( it.element_pointer ) );
}
inline COLONY_FORCE_INLINE pointer *operator->() const noexcept {
return reinterpret_cast<pointer>( it.element_pointer );
}
// In this case we have to redefine the algorithm, rather than using the internal iterator's -- operator, in order for the reverse_iterator to be allowed to reach rend() ie. begin_iterator - 1
colony_reverse_iterator &operator++() {
colony::group_pointer_type &group_pointer = it.group_pointer;
colony::aligned_pointer_type &element_pointer = it.element_pointer;
colony::skipfield_pointer_type &skipfield_pointer = it.skipfield_pointer;
assert( group_pointer != nullptr );
assert( !( element_pointer == group_pointer->elements - 1 &&
group_pointer->previous_group == nullptr ) ); // Assert that we are not already at rend()
if( element_pointer != group_pointer->elements ) { // ie. not already at beginning of group
element_pointer -= *( --skipfield_pointer ) + 1;
skipfield_pointer -= *skipfield_pointer;
if( !( element_pointer == group_pointer->elements - 1 &&
group_pointer->previous_group == nullptr ) ) { // i.e. iterator is not == rend()
return *this;
}
}
if( group_pointer->previous_group != nullptr ) { // i.e. not first group in colony
group_pointer = group_pointer->previous_group;
skipfield_pointer = group_pointer->skipfield + group_pointer->capacity - 1;
element_pointer = ( reinterpret_cast<colony::aligned_pointer_type>( group_pointer->skipfield ) - 1 )
- *skipfield_pointer;
skipfield_pointer -= *skipfield_pointer;
} else { // necessary so that reverse_iterator can end up == rend(), if we were already at first element in colony
--element_pointer;
--skipfield_pointer;
}
return *this;
}
inline colony_reverse_iterator operator++( int ) {
const colony_reverse_iterator copy( *this );
++*this;
return copy;
}
inline COLONY_FORCE_INLINE colony_reverse_iterator &operator--() {
// i.e. Check that we are not already at rbegin()
assert( !( it.element_pointer == it.group_pointer->last_endpoint - 1 &&
it.group_pointer->next_group == nullptr ) );
++it;
return *this;
}
inline colony_reverse_iterator operator--( int ) {
const colony_reverse_iterator copy( *this );
--*this;
return copy;
}
inline typename colony::iterator base() const {
return ++( typename colony::iterator( it ) );
}
inline bool operator>( const colony_reverse_iterator &rh ) const noexcept {
return ( rh.it > it );
}
inline bool operator<( const colony_reverse_iterator &rh ) const noexcept {
return ( it > rh.it );
}
inline bool operator>=( const colony_reverse_iterator &rh ) const noexcept {
return !( it > rh.it );
}
inline bool operator<=( const colony_reverse_iterator &rh ) const noexcept {
return !( rh.it > it );
}
inline COLONY_FORCE_INLINE bool operator==( const colony_reverse_iterator < !r_is_const > &rh )
const noexcept {
return ( it == rh.it );
}
inline COLONY_FORCE_INLINE bool operator!=( const colony_reverse_iterator < !r_is_const > &rh )
const noexcept {
return ( it != rh.it );
}
inline bool operator>( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept {
return ( rh.it > it );
}
inline bool operator<( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept {
return ( it > rh.it );
}
inline bool operator>=( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept {
return !( it > rh.it );
}
inline bool operator<=( const colony_reverse_iterator < !r_is_const > &rh ) const noexcept {
return !( rh.it > it );
}
colony_reverse_iterator() noexcept = default;
colony_reverse_iterator( const colony_reverse_iterator &source ) noexcept:
it( source.it ) {}
colony_reverse_iterator( const typename colony::iterator &source ) noexcept:
it( source ) {}
private:
// Used by rend(), etc:
colony_reverse_iterator( const group_pointer_type group_p, const aligned_pointer_type element_p,
const skipfield_pointer_type skipfield_p ) noexcept:
it( group_p, element_p, skipfield_p ) {}
public:
// move constructors
colony_reverse_iterator( colony_reverse_iterator &&source ) noexcept:
it( std::move( source.it ) ) {}
colony_reverse_iterator( typename colony::iterator &&source ) noexcept:
it( std::move( source ) ) {}
}; // colony_reverse_iterator
private:
// Used to prevent fill-insert/constructor calls being mistakenly resolved to range-insert/constructor calls
template <bool condition, class T = void>
struct enable_if_c {
using type = T;
};
template <class T>
struct enable_if_c<false, T> {
};
iterator end_iterator, begin_iterator;
// Head of a singly-linked intrusive list of groups which have erased-element memory locations available for reuse
group_pointer_type groups_with_erasures_list_head;
size_type total_number_of_elements, total_capacity;
// Packaging the element pointer allocator with a lesser-used member variable, for empty-base-class optimization
struct ebco_pair2 : pointer_allocator_type {
skipfield_type min_elements_per_group;
explicit ebco_pair2( const skipfield_type min_elements ) noexcept:
min_elements_per_group( min_elements ) {}
} pointer_allocator_pair;
struct ebco_pair : group_allocator_type {
skipfield_type max_elements_per_group;
explicit ebco_pair( const skipfield_type max_elements ) noexcept:
max_elements_per_group( max_elements ) {}
} group_allocator_pair;
public:
/**
* Default constructor:
* default minimum group size is 8, default maximum group size is
* std::numeric_limits<Skipfield_type>::max() (typically 65535). You cannot set the group
* sizes from the constructor in this scenario, but you can call the change_group_sizes()
* member function after construction has occurred.
*/
colony() noexcept:
element_allocator_type( element_allocator_type() ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( ( sizeof( aligned_element_type ) * 8 > ( sizeof( *this ) + sizeof(
group ) ) * 2 ) ? 8 : ( ( ( sizeof( *this ) + sizeof( group ) ) * 2 ) / sizeof(
aligned_element_type ) ) ),
group_allocator_pair( std::numeric_limits<skipfield_type>::max() ) {
// skipfield type must be of unsigned integer type (uchar, ushort, uint etc)
assert( std::numeric_limits<skipfield_type>::is_integer &
!std::numeric_limits<skipfield_type>::is_signed );
}
/**
* Default constructor, but using a custom memory allocator eg. something other than
* std::allocator.
*/
explicit colony( const element_allocator_type &alloc ):
element_allocator_type( alloc ),
groups_with_erasures_list_head( NULL ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( ( sizeof( aligned_element_type ) * 8 > ( sizeof( *this ) + sizeof(
group ) ) * 2 ) ? 8 : ( ( ( sizeof( *this ) + sizeof( group ) ) * 2 ) / sizeof(
aligned_element_type ) ) ),
group_allocator_pair( std::numeric_limits<skipfield_type>::max() ) {
assert( std::numeric_limits<skipfield_type>::is_integer &
!std::numeric_limits<skipfield_type>::is_signed );
}
/**
* Copy constructor:
* Copy all contents from source colony, removes any empty (erased) element locations in the
* process. Size of groups created is either the total size of the source colony, or the
* maximum group size of the source colony, whichever is the smaller.
*/
colony( const colony &source ):
element_allocator_type( source ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
// Make the first colony group capacity the greater of min_elements_per_group or total_number_of_elements, so long as total_number_of_elements isn't larger than max_elements_per_group
pointer_allocator_pair( static_cast<skipfield_type>( (
source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements ) ?
source.pointer_allocator_pair.min_elements_per_group : ( ( source.total_number_of_elements >
source.group_allocator_pair.max_elements_per_group ) ?
source.group_allocator_pair.max_elements_per_group :
source.total_number_of_elements ) ) ),
group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) {
insert( source.begin_iterator, source.end_iterator );
// reset to correct value for future clear() or erasures
pointer_allocator_pair.min_elements_per_group =
source.pointer_allocator_pair.min_elements_per_group;
}
// Copy constructor (allocator-extended):
colony( const colony &source, const allocator_type &alloc ):
element_allocator_type( alloc ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( static_cast<skipfield_type>( (
source.pointer_allocator_pair.min_elements_per_group > source.total_number_of_elements ) ?
source.pointer_allocator_pair.min_elements_per_group : ( ( source.total_number_of_elements >
source.group_allocator_pair.max_elements_per_group ) ?
source.group_allocator_pair.max_elements_per_group : source.total_number_of_elements ) ) ),
group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) {
insert( source.begin_iterator, source.end_iterator );
pointer_allocator_pair.min_elements_per_group =
source.pointer_allocator_pair.min_elements_per_group;
}
private:
inline void blank() noexcept {
// if all pointer types are trivial, we can just nuke it from orbit with memset (NULL is always 0 in C++):
if COLONY_CONSTEXPR( std::is_trivial<group_pointer_type>::value &&
std::is_trivial<aligned_pointer_type>::value &&
std::is_trivial<skipfield_pointer_type>::value ) {
std::memset( static_cast<void *>( this ), 0, offsetof( colony, pointer_allocator_pair ) );
} else {
end_iterator.group_pointer = nullptr;
end_iterator.element_pointer = nullptr;
end_iterator.skipfield_pointer = nullptr;
begin_iterator.group_pointer = nullptr;
begin_iterator.element_pointer = nullptr;
begin_iterator.skipfield_pointer = nullptr;
groups_with_erasures_list_head = nullptr;
total_number_of_elements = 0;
total_capacity = 0;
}
}
public:
/**
* Move constructor:
* Move all contents from source colony, does not remove any erased element locations or
* alter any of the source group sizes. Source colony is now empty and can be safely
* destructed or otherwise used.
*/
colony( colony &&source ) noexcept:
element_allocator_type( source ),
end_iterator( std::move( source.end_iterator ) ),
begin_iterator( std::move( source.begin_iterator ) ),
groups_with_erasures_list_head( std::move( source.groups_with_erasures_list_head ) ),
total_number_of_elements( source.total_number_of_elements ),
total_capacity( source.total_capacity ),
pointer_allocator_pair( source.pointer_allocator_pair.min_elements_per_group ),
group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) {
source.blank();
}
// Move constructor (allocator-extended):
colony( colony &&source, const allocator_type &alloc ):
element_allocator_type( alloc ),
end_iterator( std::move( source.end_iterator ) ),
begin_iterator( std::move( source.begin_iterator ) ),
groups_with_erasures_list_head( std::move( source.groups_with_erasures_list_head ) ),
total_number_of_elements( source.total_number_of_elements ),
total_capacity( source.total_capacity ),
pointer_allocator_pair( source.pointer_allocator_pair.min_elements_per_group ),
group_allocator_pair( source.group_allocator_pair.max_elements_per_group ) {
source.blank();
}
/**
* Fill constructor with value_type unspecified, so the value_type's default constructor is
* used. n specifies the number of elements to create upon construction. If n is larger than
* min_group_size, the size of the groups created will either be n and max_group_size,
* depending on which is smaller. min_group_size (i.e. the smallest possible number of
* elements which can be stored in a colony group) can be defined, as can the max_group_size.
* Setting the group sizes can be a performance advantage if you know in advance roughly how
* many objects are likely to be stored in your colony long-term - or at least the rough
* scale of storage. If that case, using this can stop many small initial groups being
* allocated (reserve() will achieve a similar result, but structurally at the moment is
* limited to allocating one group).
*/
colony( const size_type fill_number, const element_type &element,
const skipfield_type min_allocation_amount = 0,
const skipfield_type max_allocation_amount = std::numeric_limits<skipfield_type>::max(),
const element_allocator_type &alloc = element_allocator_type() ):
element_allocator_type( alloc ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( ( min_allocation_amount != 0 ) ? min_allocation_amount :
( fill_number > max_allocation_amount ) ? max_allocation_amount :
( fill_number > 8 ) ? static_cast<skipfield_type>( fill_number ) : 8 ),
group_allocator_pair( max_allocation_amount ) {
assert( std::numeric_limits<skipfield_type>::is_integer &
!std::numeric_limits<skipfield_type>::is_signed );
assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) &
( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) );
insert( fill_number, element );
}
// Range constructor:
template<typename iterator_type>
colony( const typename enable_if_c < !std::numeric_limits<iterator_type>::is_integer,
iterator_type >::type &first, const iterator_type &last,
const skipfield_type min_allocation_amount = 8,
const skipfield_type max_allocation_amount = std::numeric_limits<skipfield_type>::max(),
const element_allocator_type &alloc = element_allocator_type() ):
element_allocator_type( alloc ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( min_allocation_amount ),
group_allocator_pair( max_allocation_amount ) {
assert( std::numeric_limits<skipfield_type>::is_integer &
!std::numeric_limits<skipfield_type>::is_signed );
assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) &
( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) );
insert<iterator_type>( first, last );
}
// Initializer-list constructor:
colony( const std::initializer_list<element_type> &element_list,
const skipfield_type min_allocation_amount = 0,
const skipfield_type max_allocation_amount = std::numeric_limits<skipfield_type>::max(),
const element_allocator_type &alloc = element_allocator_type() ):
element_allocator_type( alloc ),
groups_with_erasures_list_head( nullptr ),
total_number_of_elements( 0 ),
total_capacity( 0 ),
pointer_allocator_pair( ( min_allocation_amount != 0 ) ? min_allocation_amount :
( element_list.size() > max_allocation_amount ) ? max_allocation_amount :
( element_list.size() > 8 ) ? static_cast<skipfield_type>( element_list.size() ) : 8 ),
group_allocator_pair( max_allocation_amount ) {
assert( std::numeric_limits<skipfield_type>::is_integer &
!std::numeric_limits<skipfield_type>::is_signed );
assert( ( pointer_allocator_pair.min_elements_per_group > 2 ) &
( pointer_allocator_pair.min_elements_per_group <= group_allocator_pair.max_elements_per_group ) );
insert( element_list );
}
inline COLONY_FORCE_INLINE iterator begin() noexcept {
return begin_iterator;
}
inline COLONY_FORCE_INLINE const iterator &begin() const
noexcept { // To allow for functions which only take const colony & as a source eg. copy constructor
return begin_iterator;
}
inline COLONY_FORCE_INLINE iterator end() noexcept {
return end_iterator;
}
inline COLONY_FORCE_INLINE const iterator &end() const noexcept {
return end_iterator;
}
inline const_iterator cbegin() const noexcept {
return const_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer,
begin_iterator.skipfield_pointer );
}
inline const_iterator cend() const noexcept {
return const_iterator( end_iterator.group_pointer, end_iterator.element_pointer,
end_iterator.skipfield_pointer );
}
inline reverse_iterator rbegin()
const { // May throw exception if colony is empty so end_iterator is uninitialized
return ++reverse_iterator( end_iterator );
}
inline reverse_iterator rend() const noexcept {
return reverse_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer - 1,
begin_iterator.skipfield_pointer - 1 );
}
inline const_reverse_iterator crbegin() const {
return ++const_reverse_iterator( end_iterator );
}
inline const_reverse_iterator crend() const noexcept {
return const_reverse_iterator( begin_iterator.group_pointer, begin_iterator.element_pointer - 1,
begin_iterator.skipfield_pointer - 1 );
}
~colony() noexcept {
destroy_all_data();
}
private:
void destroy_all_data() noexcept {
// Amusingly enough, these changes from && to logical & actually do make a significant difference in debug mode
if( ( total_number_of_elements != 0 ) & !( std::is_trivially_destructible<element_type>::value ) ) {
total_number_of_elements = 0; // to avoid double-destruction
while( true ) {
const aligned_pointer_type end_pointer = begin_iterator.group_pointer->last_endpoint;
do {
COLONY_DESTROY( element_allocator_type, ( *this ),
reinterpret_cast<pointer>( begin_iterator.element_pointer ) );
++begin_iterator.skipfield_pointer;
begin_iterator.element_pointer += *begin_iterator.skipfield_pointer + 1;
begin_iterator.skipfield_pointer += *begin_iterator.skipfield_pointer;
} while( begin_iterator.element_pointer != end_pointer ); // ie. beyond end of available data
const group_pointer_type next_group = begin_iterator.group_pointer->next_group;
COLONY_DESTROY( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer );
COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 );
begin_iterator.group_pointer =
next_group; // required to be before if statement in order for first_group to be NULL and avoid potential double-destruction in future
if( next_group == nullptr ) {
return;
}
begin_iterator.element_pointer = next_group->elements + *( next_group->skipfield );
begin_iterator.skipfield_pointer = next_group->skipfield + *( next_group->skipfield );
}
} else { // Avoid iteration for both empty groups and trivially-destructible types eg. POD, structs, classes with empty destructors
// Technically under a type-traits-supporting compiler total_number_of_elements could be non-zero at this point, but since begin_iterator.group_pointer would already be NULL in the case of double-destruction, it's unnecessary to zero total_number_of_elements
while( begin_iterator.group_pointer != nullptr ) {
const group_pointer_type next_group = begin_iterator.group_pointer->next_group;
COLONY_DESTROY( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer );
COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 );
begin_iterator.group_pointer = next_group;
}
}
}
void initialize( const skipfield_type first_group_size ) {
begin_iterator.group_pointer = COLONY_ALLOCATE( group_allocator_type, group_allocator_pair, 1,
nullptr );
try {
COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer,
first_group_size );
} catch( ... ) {
COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, begin_iterator.group_pointer, 1 );
begin_iterator.group_pointer = nullptr;
throw;
}
end_iterator.group_pointer = begin_iterator.group_pointer;
end_iterator.element_pointer = begin_iterator.element_pointer =
begin_iterator.group_pointer->elements;
end_iterator.skipfield_pointer = begin_iterator.skipfield_pointer =
begin_iterator.group_pointer->skipfield;
total_capacity = first_group_size;
}
public:
/**
* Inserts the element supplied to the colony, using the object's copy-constructor. Will
* insert the element into a previously erased element slot if one exists, otherwise will
* insert to back of colony. Returns iterator to location of inserted element.
*/
iterator insert( const element_type &element ) {
if( end_iterator.element_pointer != nullptr ) {
switch( ( ( groups_with_erasures_list_head != nullptr ) << 1 ) | ( end_iterator.element_pointer ==
reinterpret_cast<aligned_pointer_type>( end_iterator.group_pointer->skipfield ) ) ) {
case 0: { // ie. there are no erased elements and end_iterator is not at end of current final group
// Make copy for return before modifying end_iterator
const iterator return_iterator = end_iterator;
if COLONY_CONSTEXPR( std::is_nothrow_copy_constructible<element_type>::value ) {
// For no good reason this compiles to faster code under GCC:
COLONY_CONSTRUCT( element_allocator_type, ( *this ),
reinterpret_cast<pointer>( end_iterator.element_pointer++ ), element );
end_iterator.group_pointer->last_endpoint = end_iterator.element_pointer;
} else {
COLONY_CONSTRUCT( element_allocator_type, ( *this ),
reinterpret_cast<pointer>( end_iterator.element_pointer ), element );
// Shift the addition to the second operation, avoiding problems if an exception is thrown during construction
end_iterator.group_pointer->last_endpoint = ++end_iterator.element_pointer;
}
++( end_iterator.group_pointer->number_of_elements );
++end_iterator.skipfield_pointer;
++total_number_of_elements;
return return_iterator; // return value before incrementation
}
case 1: { // ie. there are no erased elements and end_iterator is at end of current final group - ie. colony is full - create new group
end_iterator.group_pointer->next_group = COLONY_ALLOCATE( group_allocator_type,
group_allocator_pair, 1, end_iterator.group_pointer );
group &next_group = *( end_iterator.group_pointer->next_group );
const skipfield_type new_group_size = ( total_number_of_elements < static_cast<size_type>
( group_allocator_pair.max_elements_per_group ) ) ? static_cast<skipfield_type>
( total_number_of_elements ) : group_allocator_pair.max_elements_per_group;
try {
COLONY_CONSTRUCT( group_allocator_type, group_allocator_pair, &next_group, new_group_size,
end_iterator.group_pointer );
} catch( ... ) {
COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 );
end_iterator.group_pointer->next_group = nullptr;
throw;
}
if COLONY_CONSTEXPR( std::is_nothrow_copy_constructible<element_type>::value ) {
COLONY_CONSTRUCT( element_allocator_type, ( *this ),
reinterpret_cast<pointer>( next_group.elements ), element );
} else {
try {
COLONY_CONSTRUCT( element_allocator_type, ( *this ),
reinterpret_cast<pointer>( next_group.elements ), element );
} catch( ... ) {
COLONY_DESTROY( group_allocator_type, group_allocator_pair, &next_group );
COLONY_DEALLOCATE( group_allocator_type, group_allocator_pair, &next_group, 1 );
end_iterator.group_pointer->next_group = nullptr;
throw;
}