forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
2478 lines (2037 loc) · 121 KB
/
list.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_LIST_H
#define CATA_SRC_LIST_H
#define LIST_BLOCK_MIN static_cast<group_size_type>((sizeof(node) * 8 > (sizeof(*this) + sizeof(group)) * 2) ? 8 : (((sizeof(*this) + sizeof(group)) * 2) / sizeof(node)) + 1)
#define LIST_BLOCK_MAX 2048
#define LIST_CONSTEXPR
#define LIST_NOEXCEPT_SWAP(the_allocator) noexcept
#define LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept
// TODO: Switch to these when we move to C++17
// #define LIST_CONSTEXPR constexpr
// #define LIST_NOEXCEPT_SWAP(the_allocator) noexcept(std::allocator_traits<the_allocator>::propagate_on_container_swap::value)
// #define LIST_NOEXCEPT_MOVE_ASSIGNMENT(the_allocator) noexcept(std::allocator_traits<the_allocator>::is_always_equal::value)
// Note: GCC creates faster code without forcing inline
#if defined(_MSC_VER)
#define LIST_FORCE_INLINE __forceinline
#else
#define LIST_FORCE_INLINE
#endif
// TODO: get rid of these defines
#define LIST_CONSTRUCT(the_allocator, allocator_instance, location, ...) std::allocator_traits<the_allocator>::construct(allocator_instance, location, __VA_ARGS__)
#define LIST_DESTROY(the_allocator, allocator_instance, location) std::allocator_traits<the_allocator>::destroy(allocator_instance, location)
#define LIST_ALLOCATE(the_allocator, allocator_instance, size, hint) std::allocator_traits<the_allocator>::allocate(allocator_instance, size, hint)
#define LIST_ALLOCATE_INITIALIZATION(the_allocator, size, hint) std::allocator_traits<the_allocator>::allocate(*this, size, hint)
#define LIST_DEALLOCATE(the_allocator, allocator_instance, location, size) std::allocator_traits<the_allocator>::deallocate(allocator_instance, location, size)
#include <algorithm> // std::sort
#include <cstring> // memmove, memcpy
#include <initializer_list>
#include <iterator> // std::bidirectional_iterator_tag
#include <limits> // std::numeric_limits
#include <memory> // std::uninitialized_copy, std::allocator
#include <type_traits> // std::is_trivially_destructible, etc
#include <utility> // std::move
#include "cata_assert.h"
namespace cata
{
template <class element_type, class element_allocator_type = std::allocator<element_type> > class
list : private element_allocator_type
{
public:
// Standard container typedefs:
using value_type = element_type;
using allocator_type = element_allocator_type;
using group_size_type = unsigned short;
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 list_iterator;
using iterator = list_iterator<false>;
using const_iterator = list_iterator<true>;
friend class list_iterator<false>; // Using 'iterator' typedef name here is illegal under C++03
friend class list_iterator<true>;
template <bool is_const> class list_reverse_iterator;
using reverse_iterator = list_reverse_iterator<false>;
using const_reverse_iterator = list_reverse_iterator<true>;
friend class list_reverse_iterator<false>;
friend class list_reverse_iterator<true>;
private:
struct group; // forward declarations for typedefs below
struct node;
using group_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<group>;
using node_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<node>;
using group_pointer_type = typename std::allocator_traits<group_allocator_type>::pointer;
using node_pointer_type = typename std::allocator_traits<node_allocator_type>::pointer;
using node_pointer_allocator_type = typename std::allocator_traits<element_allocator_type>::template
rebind_alloc<node_pointer_type>;
struct node_base {
node_pointer_type next, previous;
node_base() = default;
node_base( const node_pointer_type &n, const node_pointer_type &p ):
next( n ),
previous( p )
{}
node_base( node_pointer_type &&n, node_pointer_type &&p ) noexcept:
next( std::move( n ) ),
previous( std::move( p ) )
{}
};
struct node : public node_base {
element_type element;
node( const node_pointer_type next, const node_pointer_type previous, const element_type &source ):
node_base( next, previous ),
element( source )
{}
node( node_pointer_type &&next, node_pointer_type &&previous, element_type &&source ) noexcept:
node_base( std::move( next ), std::move( previous ) ),
element( std::move( source ) )
{}
template<typename... arguments>
node( node_pointer_type const next, node_pointer_type const previous, arguments &&... parameters ):
node_base( next, previous ),
element( std::forward<arguments>( parameters ) ... )
{}
};
struct group : public node_allocator_type {
node_pointer_type nodes;
node_pointer_type free_list_head;
node_pointer_type beyond_end;
group_size_type number_of_elements;
group() noexcept:
nodes( nullptr ),
free_list_head( nullptr ),
beyond_end( nullptr ),
number_of_elements( 0 )
{}
explicit group( const group_size_type group_size, node_pointer_type const previous = nullptr ):
nodes( LIST_ALLOCATE_INITIALIZATION( node_allocator_type, group_size, previous ) ),
free_list_head( nullptr ),
beyond_end( nodes + group_size ),
number_of_elements( 0 )
{}
// Actually a move operator, used by c++03 in group_vector's remove, expand_capacity and append
group &operator=( const group &source ) noexcept {
nodes = source.nodes;
free_list_head = source.free_list_head;
beyond_end = source.beyond_end;
number_of_elements = source.number_of_elements;
return *this;
}
group( group &&source ) noexcept:
node_allocator_type( source ),
nodes( std::move( source.nodes ) ),
free_list_head( std::move( source.free_list_head ) ),
beyond_end( std::move( source.beyond_end ) ),
number_of_elements( source.number_of_elements ) {
source.nodes = nullptr;
source.beyond_end = nullptr;
}
group &operator=( group &&source ) noexcept {
nodes = std::move( source.nodes );
free_list_head = std::move( source.free_list_head );
beyond_end = std::move( source.beyond_end );
number_of_elements = std::move( source.number_of_elements );
source.nodes = nullptr;
source.beyond_end = nullptr;
return *this;
}
~group() noexcept {
LIST_DEALLOCATE( node_allocator_type, ( *this ), nodes,
static_cast<size_type>( beyond_end - nodes ) );
}
};
class group_vector : private node_pointer_allocator_type
{
public:
// last_endpoint_group is the last -active- group in the block. Other -inactive- (previously used, now empty of elements) groups may be stored after this group for future usage (to reduce deallocation/reallocation of nodes). block_pointer + size - 1 == the last group in the block, regardless of whether or not the group is active.
group_pointer_type last_endpoint_group, block_pointer, last_searched_group;
size_type size;
struct ebco_pair2 : allocator_type { // empty-base-class optimization
size_type capacity; // Total element capacity of all initialized groups
explicit ebco_pair2( const size_type number_of_elements ) noexcept: capacity(
number_of_elements ) {}
} element_allocator_pair;
struct ebco_pair : group_allocator_type {
size_type capacity; // Total group capacity
explicit ebco_pair( const size_type number_of_groups ) noexcept: capacity( number_of_groups ) {}
} group_allocator_pair;
group_vector() noexcept:
node_pointer_allocator_type( node_pointer_allocator_type() ),
last_endpoint_group( nullptr ),
block_pointer( nullptr ),
last_searched_group( nullptr ),
size( 0 ),
element_allocator_pair( 0 ),
group_allocator_pair( 0 )
{}
inline LIST_FORCE_INLINE void blank() noexcept {
if LIST_CONSTEXPR( std::is_trivial<group_pointer_type>::value ) {
std::memset( static_cast<void *>( this ), 0, sizeof( group_vector ) );
} else {
last_endpoint_group = nullptr;
block_pointer = nullptr;
last_searched_group = nullptr;
size = 0;
element_allocator_pair.capacity = 0;
group_allocator_pair.capacity = 0;
}
}
group_vector( group_vector &&source ) noexcept:
last_endpoint_group( std::move( source.last_endpoint_group ) ),
block_pointer( std::move( source.block_pointer ) ),
last_searched_group( std::move( source.last_searched_group ) ),
size( source.size ),
element_allocator_pair( source.element_allocator_pair.capacity ),
group_allocator_pair( source.group_allocator_pair.capacity ) {
source.blank();
}
group_vector &operator = ( group_vector &&source ) noexcept {
if LIST_CONSTEXPR( std::is_trivial<group_pointer_type>::value ) {
// NOLINTNEXTLINE(bugprone-undefined-memory-manipulation)
std::memcpy( static_cast<void *>( this ), &source, sizeof( group_vector ) );
} else {
last_endpoint_group = std::move( source.last_endpoint_group );
block_pointer = std::move( source.block_pointer );
last_searched_group = std::move( source.last_searched_group );
size = source.size;
element_allocator_pair.capacity = source.element_allocator_pair.capacity;
group_allocator_pair.capacity = source.group_allocator_pair.capacity;
}
source.blank();
return *this;
}
~group_vector() noexcept = default;
void destroy_all_data( const node_pointer_type last_endpoint_node ) noexcept {
if( block_pointer == nullptr ) {
return;
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ||
!std::is_trivially_destructible<node_pointer_type>::value ) {
clear( last_endpoint_node ); // If clear has already been called, last_endpoint_node will already be == block_pointer->nodes, so no work will occur
}
const group_pointer_type end_group = block_pointer + size;
for( group_pointer_type current_group = block_pointer; current_group != end_group;
++current_group ) {
LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group );
}
LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, block_pointer,
group_allocator_pair.capacity );
blank();
}
void clear( const node_pointer_type last_endpoint_node ) noexcept {
for( group_pointer_type current_group = block_pointer; current_group != last_endpoint_group;
++current_group ) {
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ||
!std::is_trivially_destructible<node_pointer_type>::value ) {
const node_pointer_type end = current_group->beyond_end;
if( ( end - current_group->nodes ) != current_group->number_of_elements ) {
// If there are erased nodes present in the group
for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) {
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ) {
if( current_node->next != nullptr ) { // ie. is not part of free list
LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) );
}
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<node_pointer_type>::value ) {
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) );
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) );
}
}
} else {
for( node_pointer_type current_node = current_group->nodes; current_node != end; ++current_node ) {
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ) {
LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) );
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<node_pointer_type>::value ) {
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) );
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) );
}
}
}
}
current_group->free_list_head = nullptr;
current_group->number_of_elements = 0;
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ||
!std::is_trivially_destructible<node_pointer_type>::value ) {
if( ( last_endpoint_node - last_endpoint_group->nodes ) !=
last_endpoint_group->number_of_elements ) {
// If there are erased nodes present in the group
for( node_pointer_type current_node = last_endpoint_group->nodes;
current_node != last_endpoint_node; ++current_node ) {
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ) {
if( current_node->next != nullptr ) {
// is not part of free list ie. element has not already had it's destructor called
LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) );
}
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<node_pointer_type>::value ) {
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) );
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) );
}
}
} else {
for( node_pointer_type current_node = last_endpoint_group->nodes;
current_node != last_endpoint_node; ++current_node ) {
if LIST_CONSTEXPR( !std::is_trivially_destructible<element_type>::value ) {
LIST_DESTROY( element_allocator_type, element_allocator_pair, &( current_node->element ) );
}
if LIST_CONSTEXPR( !std::is_trivially_destructible<node_pointer_type>::value ) {
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->next ) );
LIST_DESTROY( node_pointer_allocator_type, ( *this ), &( current_node->previous ) );
}
}
}
}
last_endpoint_group->free_list_head = nullptr;
last_endpoint_group->number_of_elements = 0;
last_searched_group = last_endpoint_group = block_pointer;
}
void expand_capacity( const size_type new_capacity ) { // used by add_new and append
group_pointer_type const old_block = block_pointer;
block_pointer = LIST_ALLOCATE( group_allocator_type, group_allocator_pair, new_capacity, nullptr );
if LIST_CONSTEXPR( std::is_trivially_copyable<node_pointer_type>::value &&
std::is_trivially_destructible<node_pointer_type>::value ) {
// Dereferencing here in order to deal with smart pointer situations i.e. obtaining the raw pointer from the smart pointer
// reinterpret_cast necessary to deal with GCC 8 warnings
std::memcpy( static_cast<void *>( &*block_pointer ), static_cast<void *>( &*old_block ),
sizeof( group ) * size );
} else if LIST_CONSTEXPR( std::is_move_constructible<node_pointer_type>::value ) {
std::uninitialized_copy( std::make_move_iterator( old_block ),
std::make_move_iterator( old_block + size ), block_pointer );
} else {
// If allocator supplies non-trivial pointers it becomes necessary to destroy the group. uninitialized_copy will not work in this context as the copy constructor for "group" is overridden in C++03/98. The = operator for "group" has been overridden to make the following work:
const group_pointer_type beyond_end = old_block + size;
group_pointer_type current_new_group = block_pointer;
for( group_pointer_type current_group = old_block; current_group != beyond_end; ++current_group ) {
*( current_new_group++ ) = *current_group;
current_group->nodes = nullptr;
current_group->beyond_end = nullptr;
LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group );
}
}
// correct pointer post-reallocation
last_searched_group = block_pointer + ( last_searched_group - old_block );
LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, old_block,
group_allocator_pair.capacity );
group_allocator_pair.capacity = new_capacity;
}
void add_new( const group_size_type group_size ) {
if( group_allocator_pair.capacity == size ) {
expand_capacity( group_allocator_pair.capacity * 2 );
}
last_endpoint_group = block_pointer + size - 1;
LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, last_endpoint_group + 1, group_size,
last_endpoint_group->nodes );
++last_endpoint_group; // Doing this here instead of pre-construct to avoid need for a try-catch block
element_allocator_pair.capacity += group_size;
++size;
}
// For adding first group *only* when group vector is completely empty and block_pointer is NULL
void initialize( const group_size_type group_size ) {
last_endpoint_group = block_pointer = last_searched_group = LIST_ALLOCATE( group_allocator_type,
group_allocator_pair, 1, nullptr );
group_allocator_pair.capacity = 1;
LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, last_endpoint_group, group_size );
size = 1; // Doing these here instead of pre-construct to avoid need for a try-catch block
element_allocator_pair.capacity = group_size;
}
void remove( group_pointer_type const group_to_erase ) noexcept {
if( last_searched_group >= group_to_erase && last_searched_group != block_pointer ) {
--last_searched_group;
}
element_allocator_pair.capacity -= static_cast<size_type>( group_to_erase->beyond_end -
group_to_erase->nodes );
LIST_DESTROY( group_allocator_type, group_allocator_pair, group_to_erase );
if LIST_CONSTEXPR( std::is_trivially_copyable<node_pointer_type>::value &&
std::is_trivially_destructible<node_pointer_type>::value ) {
// Dereferencing here in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer
std::memmove( static_cast<void *>( &*group_to_erase ), static_cast<void *>( &*group_to_erase + 1 ),
sizeof( group ) * ( --size - static_cast<size_type>( &*group_to_erase - &*block_pointer ) ) );
} else if LIST_CONSTEXPR( std::is_move_constructible<node_pointer_type>::value ) {
std::move( group_to_erase + 1, block_pointer + size--, group_to_erase );
} else {
group_pointer_type back = block_pointer + size--;
std::copy( group_to_erase + 1, back--, group_to_erase );
back->nodes = nullptr;
back->beyond_end = nullptr;
LIST_DESTROY( group_allocator_type, group_allocator_pair, back );
}
}
void move_to_back( group_pointer_type const group_to_erase ) {
if( last_searched_group >= group_to_erase && last_searched_group != block_pointer ) {
--last_searched_group;
}
group *temp_group = LIST_ALLOCATE( group_allocator_type, group_allocator_pair, 1, nullptr );
if LIST_CONSTEXPR( std::is_trivially_copyable<node_pointer_type>::value &&
std::is_trivially_destructible<node_pointer_type>::value ) {
std::memcpy( static_cast<void *>( &*temp_group ), static_cast<void *>( &*group_to_erase ),
sizeof( group ) );
std::memmove( static_cast<void *>( &*group_to_erase ), static_cast<void *>( &*group_to_erase + 1 ),
sizeof( group ) * ( ( size - 1 ) - static_cast<size_type>( &*group_to_erase - &*block_pointer ) ) );
std::memcpy( static_cast<void *>( &*( block_pointer + size - 1 ) ),
static_cast<void *>( &*temp_group ), sizeof( group ) );
} else if LIST_CONSTEXPR( std::is_move_constructible<node_pointer_type>::value ) {
LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, temp_group,
std::move( *group_to_erase ) );
std::move( group_to_erase + 1, block_pointer + size, group_to_erase );
*( block_pointer + size - 1 ) = std::move( *temp_group );
if LIST_CONSTEXPR( !std::is_trivially_destructible<node_pointer_type>::value ) {
LIST_DESTROY( group_allocator_type, group_allocator_pair, temp_group );
}
} else {
LIST_CONSTRUCT( group_allocator_type, group_allocator_pair, temp_group, group() );
*temp_group = *group_to_erase;
std::copy( group_to_erase + 1, block_pointer + size, group_to_erase );
*( block_pointer + --size ) = *temp_group;
temp_group->nodes = nullptr;
LIST_DESTROY( group_allocator_type, group_allocator_pair, temp_group );
}
LIST_DEALLOCATE( group_allocator_type, group_allocator_pair, temp_group, 1 );
}
// In working implementation this cannot throw
group_pointer_type get_nearest_freelist_group( const node_pointer_type location_node ) noexcept {
const group_pointer_type beyond_end_group = last_endpoint_group + 1;
group_pointer_type left = last_searched_group - 1;
group_pointer_type right = last_searched_group + 1;
group_pointer_type freelist_group = nullptr;
bool right_not_beyond_back = right < beyond_end_group;
bool left_not_beyond_front = left >= block_pointer;
// ie. location is within last_search_group
if( location_node >= last_searched_group->nodes &&
location_node < last_searched_group->beyond_end ) {
// if last_searched_group has previously-erased nodes
if( last_searched_group->free_list_head != nullptr ) {
return last_searched_group;
}
} else { // search for the node group which location_node is located within, using last_searched_group as a starting point and searching left and right. Try and find the closest node group with reusable erased-element locations along the way:
group_pointer_type closest_freelist_left = ( last_searched_group->free_list_head == nullptr ) ?
nullptr :
last_searched_group;
group_pointer_type closest_freelist_right = ( last_searched_group->free_list_head == nullptr ) ?
nullptr : last_searched_group;
while( true ) {
if( right_not_beyond_back ) {
// location_node's group is found
if( ( location_node < right->beyond_end ) && ( location_node >= right->nodes ) ) {
// group has erased nodes, reuse them:
if( right->free_list_head != nullptr ) {
last_searched_group = right;
return right;
}
difference_type left_distance;
if( closest_freelist_right != nullptr ) {
last_searched_group = right;
left_distance = right - closest_freelist_right;
if( left_distance <= 2 ) { // ie. this group is close enough to location_node's group
return closest_freelist_right;
}
freelist_group = closest_freelist_right;
} else {
last_searched_group = right;
left_distance = right - left;
}
// Otherwise find closest group with freelist - check an equal distance on the right to the distance we've checked on the left:
const group_pointer_type end_group = ( ( right + left_distance ) > beyond_end_group ) ?
beyond_end_group : ( right + left_distance - 1 );
while( ++right != end_group ) {
if( right->free_list_head != nullptr ) {
return right;
}
}
if( freelist_group != nullptr ) {
return freelist_group;
}
right_not_beyond_back = ( right < beyond_end_group );
break; // group with reusable erased nodes not found yet, continue searching in loop below
}
// location_node's group not found, but a reusable location found
if( right->free_list_head != nullptr ) {
if( ( closest_freelist_right == nullptr ) & ( closest_freelist_left == nullptr ) ) {
closest_freelist_left = right;
}
closest_freelist_right = right;
}
right_not_beyond_back = ( ++right < beyond_end_group );
}
if( left_not_beyond_front ) {
if( ( location_node >= left->nodes ) && ( location_node < left->beyond_end ) ) {
if( left->free_list_head != nullptr ) {
last_searched_group = left;
return left;
}
difference_type right_distance;
if( closest_freelist_left != nullptr ) {
last_searched_group = left;
right_distance = closest_freelist_left - left;
if( right_distance <= 2 ) {
return closest_freelist_left;
}
freelist_group = closest_freelist_left;
} else {
last_searched_group = left;
right_distance = right - left;
}
// Otherwise find closest group with freelist:
const group_pointer_type end_group = ( ( left - right_distance ) < block_pointer ) ? block_pointer
- 1 : ( left - right_distance ) + 1;
while( --left != end_group ) {
if( left->free_list_head != nullptr ) {
return left;
}
}
if( freelist_group != nullptr ) {
return freelist_group;
}
left_not_beyond_front = ( left >= block_pointer );
break;
}
if( left->free_list_head != nullptr ) {
if( ( closest_freelist_left == nullptr ) & ( closest_freelist_right == nullptr ) ) {
closest_freelist_right = left;
}
closest_freelist_left = left;
}
left_not_beyond_front = ( --left >= block_pointer );
}
}
}
// The node group which location_node is located within, is known at this point. Continue searching outwards from this group until a group is found with a reusable location:
while( true ) {
if( right_not_beyond_back ) {
if( right->free_list_head != nullptr ) {
return right;
}
right_not_beyond_back = ( ++right < beyond_end_group );
}
if( left_not_beyond_front ) {
if( left->free_list_head != nullptr ) {
return left;
}
left_not_beyond_front = ( --left >= block_pointer );
}
}
// Will never reach here on functioning implementations
}
void swap( group_vector &source ) LIST_NOEXCEPT_SWAP( group_allocator_type ) {
if LIST_CONSTEXPR(
std::is_trivial<group_pointer_type>::value ) { // if all pointer types are trivial we can just copy using memcpy - faster - avoids constructors/destructors etc
char temp[sizeof( group_vector )];
std::memcpy( static_cast<void *>( &temp ), static_cast<void *>( this ), sizeof( group_vector ) );
std::memcpy( static_cast<void *>( this ), static_cast<void *>( &source ), sizeof( group_vector ) );
std::memcpy( static_cast<void *>( &source ), static_cast<void *>( &temp ), sizeof( group_vector ) );
} else {
const group_pointer_type swap_last_endpoint_group = last_endpoint_group;
const group_pointer_type swap_block_pointer = block_pointer;
const group_pointer_type swap_last_searched_group = last_searched_group;
const size_type swap_size = size;
const size_type swap_element_capacity = element_allocator_pair.capacity;
const size_type swap_capacity = group_allocator_pair.capacity;
last_endpoint_group = source.last_endpoint_group;
block_pointer = source.block_pointer;
last_searched_group = source.last_searched_group;
size = source.size;
element_allocator_pair.capacity = source.element_allocator_pair.capacity;
group_allocator_pair.capacity = source.group_allocator_pair.capacity;
source.last_endpoint_group = swap_last_endpoint_group;
source.block_pointer = swap_block_pointer;
source.last_searched_group = swap_last_searched_group;
source.size = swap_size;
source.element_allocator_pair.capacity = swap_element_capacity;
source.group_allocator_pair.capacity = swap_capacity;
}
}
void trim_trailing_groups() noexcept {
const group_pointer_type beyond_last = block_pointer + size;
for( group_pointer_type current_group = last_endpoint_group + 1; current_group != beyond_last;
++current_group ) {
element_allocator_pair.capacity -= static_cast<size_type>( current_group->beyond_end -
current_group->nodes );
LIST_DESTROY( group_allocator_type, group_allocator_pair, current_group );
}
size -= static_cast<size_type>( beyond_last - ( last_endpoint_group + 1 ) );
}
void append( group_vector &source ) {
source.trim_trailing_groups();
trim_trailing_groups();
if( size + source.size > group_allocator_pair.capacity ) {
expand_capacity( size + source.size );
}
if LIST_CONSTEXPR( std::is_trivially_copyable<node_pointer_type>::value &&
std::is_trivially_destructible<node_pointer_type>::value ) {
// &* in order to deal with smart pointer situations ie. obtaining the raw pointer from the smart pointer
std::memcpy( static_cast<void *>( &*block_pointer + size ),
static_cast<void *>( &*source.block_pointer ), sizeof( group ) * source.size );
} else if LIST_CONSTEXPR( std::is_move_constructible<node_pointer_type>::value ) {
std::uninitialized_copy( std::make_move_iterator( source.block_pointer ),
std::make_move_iterator( source.block_pointer + source.size ), block_pointer + size );
} else {
group_pointer_type current_new_group = block_pointer + size;
const group_pointer_type beyond_end_source = source.block_pointer + source.size;
for( group_pointer_type current_group = source.block_pointer; current_group != beyond_end_source;
++current_group ) {
*( current_new_group++ ) = *current_group;
current_group->nodes = nullptr;
current_group->beyond_end = nullptr;
LIST_DESTROY( group_allocator_type, source.group_allocator_pair, current_group );
}
}
LIST_DEALLOCATE( group_allocator_type, source.group_allocator_pair, source.block_pointer,
source.group_allocator_pair.capacity );
size += source.size;
last_endpoint_group = block_pointer + size - 1;
element_allocator_pair.capacity += source.element_allocator_pair.capacity;
source.blank();
}
};
// Implement const/non-const iterator switching pattern:
template <bool flag, class IsTrue, class IsFalse> struct choose;
template <class IsTrue, class IsFalse> struct choose<true, IsTrue, IsFalse> {
using type = IsTrue;
};
template <class IsTrue, class IsFalse> struct choose<false, IsTrue, IsFalse> {
using type = IsFalse;
};
public:
template <bool is_const> class list_iterator
{
private:
node_pointer_type node_pointer;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename list::value_type;
using difference_type = typename list::difference_type;
using pointer = typename
choose<is_const, typename list::const_pointer, typename list::pointer>::type;
using reference = typename
choose<is_const, typename list::const_reference, typename list::reference>::type;
friend class list;
inline LIST_FORCE_INLINE bool operator==( const list_iterator rh ) const noexcept {
return node_pointer == rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator==( const list_iterator < !is_const > rh ) const
noexcept {
return node_pointer == rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator!=( const list_iterator rh ) const noexcept {
return node_pointer != rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator!=( const list_iterator < !is_const > rh ) const
noexcept {
return node_pointer != rh.node_pointer;
}
inline LIST_FORCE_INLINE reference operator*() const {
return node_pointer->element;
}
inline LIST_FORCE_INLINE pointer operator->() const {
return &( node_pointer->element );
}
inline LIST_FORCE_INLINE list_iterator &operator++() noexcept {
cata_assert( node_pointer != nullptr ); // covers uninitialized list_iterator
node_pointer = node_pointer->next;
return *this;
}
inline list_iterator operator++( int ) noexcept {
const list_iterator copy( *this );
++*this;
return copy;
}
inline LIST_FORCE_INLINE list_iterator &operator--() noexcept {
cata_assert( node_pointer != nullptr ); // covers uninitialized list_iterator
node_pointer = node_pointer->previous;
return *this;
}
inline list_iterator operator--( int ) noexcept {
const list_iterator copy( *this );
--*this;
return copy;
}
inline list_iterator &operator=( const list_iterator &rh ) noexcept {
node_pointer = rh.node_pointer;
return *this;
}
inline list_iterator &operator=( const list_iterator < !is_const > &rh ) noexcept {
node_pointer = rh.node_pointer;
return *this;
}
inline list_iterator &operator=( list_iterator &&rh ) noexcept {
node_pointer = std::move( rh.node_pointer );
return *this;
}
inline list_iterator &operator=( const list_iterator < !is_const > &&rh ) noexcept {
node_pointer = std::move( rh.node_pointer );
return *this;
}
list_iterator() noexcept: node_pointer( NULL ) {}
list_iterator( const list_iterator &source ) noexcept: node_pointer( source.node_pointer ) {}
// NOLINTNEXTLINE(google-explicit-constructor)
list_iterator( const list_iterator < !is_const > &source ) noexcept: node_pointer(
source.node_pointer ) {}
list_iterator( const list_iterator &&source ) noexcept: node_pointer( std::move(
source.node_pointer ) ) {}
// NOLINTNEXTLINE(google-explicit-constructor)
list_iterator( const list_iterator < !is_const > &&
source ) noexcept: node_pointer( std::move( source.node_pointer ) ) {}
private:
// NOLINTNEXTLINE(google-explicit-constructor)
list_iterator( const node_pointer_type node_p ) noexcept: node_pointer( node_p ) {}
};
template <bool is_const> class list_reverse_iterator
{
private:
node_pointer_type node_pointer;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename list::value_type;
using difference_type = typename list::difference_type;
using pointer = typename
choose<is_const, typename list::const_pointer, typename list::pointer>::type;
using reference = typename
choose<is_const, typename list::const_reference, typename list::reference>::type;
friend class list;
inline LIST_FORCE_INLINE bool operator==( const list_reverse_iterator rh ) const noexcept {
return node_pointer == rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator==( const list_reverse_iterator < !is_const > rh ) const
noexcept {
return node_pointer == rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator!=( const list_reverse_iterator rh ) const noexcept {
return node_pointer != rh.node_pointer;
}
inline LIST_FORCE_INLINE bool operator!=( const list_reverse_iterator < !is_const > rh ) const
noexcept {
return node_pointer != rh.node_pointer;
}
inline LIST_FORCE_INLINE reference operator*() const {
return node_pointer->element;
}
inline LIST_FORCE_INLINE pointer operator->() const {
return &( node_pointer->element );
}
inline LIST_FORCE_INLINE list_reverse_iterator &operator++() noexcept {
cata_assert( node_pointer != nullptr ); // covers uninitialized list_reverse_iterator
node_pointer = node_pointer->previous;
return *this;
}
inline list_reverse_iterator operator++( int ) noexcept {
const list_reverse_iterator copy( *this );
++*this;
return copy;
}
inline LIST_FORCE_INLINE list_reverse_iterator &operator--() noexcept {
cata_assert( node_pointer != nullptr );
node_pointer = node_pointer->next;
return *this;
}
inline list_reverse_iterator operator--( int ) noexcept {
const list_reverse_iterator copy( *this );
--*this;
return copy;
}
inline list_reverse_iterator &operator=( const list_reverse_iterator &rh ) noexcept {
node_pointer = rh.node_pointer;
return *this;
}
inline list_reverse_iterator &operator=( const list_reverse_iterator < !is_const > &rh )
noexcept {
node_pointer = rh.node_pointer;
return *this;
}
inline list_reverse_iterator &operator=( list_reverse_iterator &&rh ) noexcept {
node_pointer = std::move( rh.node_pointer );
return *this;
}
inline list_reverse_iterator &operator=( const list_reverse_iterator < !is_const > &&
rh ) noexcept {
node_pointer = std::move( rh.node_pointer );
return *this;
}
inline typename list::iterator base() const noexcept {
return typename list::iterator( node_pointer->next );
}
list_reverse_iterator() noexcept: node_pointer( NULL ) {}
list_reverse_iterator( const list_reverse_iterator &source ) noexcept: node_pointer(
source.node_pointer ) {}
list_reverse_iterator( const list_reverse_iterator &&source ) noexcept: node_pointer( std::move(
source.node_pointer ) ) {}
private:
explicit list_reverse_iterator( const node_pointer_type node_p ) noexcept: node_pointer( node_p ) {}
};
private:
// Used by range-insert and range-constructor to prevent fill-insert and fill-constructor function calls mistakenly resolving to the range insert/constructor
template <bool condition, class T = void>
struct plf_enable_if_c {
using type = T;
};
template <class T>
struct plf_enable_if_c<false, T> {
};
group_vector groups;
node_base end_node;
node_pointer_type
last_endpoint; // last_endpoint being NULL means no elements have been constructed, but there may still be groups available due to clear() or reserve()
iterator end_iterator; // end_iterator is always the last entry point in last group in list (or one past the end of group)
iterator begin_iterator;
// Packaging the group allocator with least-used member variables, for empty-base-class optimization
struct ebco_pair1 : node_pointer_allocator_type {
size_type total_number_of_elements;
explicit ebco_pair1( const size_type total_num_elements ) noexcept: total_number_of_elements(
total_num_elements ) {}
} node_pointer_allocator_pair;
struct ebco_pair2 : node_allocator_type {
size_type number_of_erased_nodes;
explicit ebco_pair2( const size_type num_erased_nodes ) noexcept: number_of_erased_nodes(
num_erased_nodes ) {}
} node_allocator_pair;
public:
// Default constructor:
list() noexcept:
element_allocator_type( element_allocator_type() ),
end_node( reinterpret_cast<node_pointer_type>( &end_node ),
reinterpret_cast<node_pointer_type>( &end_node ) ),
last_endpoint( nullptr ),
end_iterator( reinterpret_cast<node_pointer_type>( &end_node ) ),
begin_iterator( reinterpret_cast<node_pointer_type>( &end_node ) ),
node_pointer_allocator_pair( 0 ),