-
Notifications
You must be signed in to change notification settings - Fork 8
/
plf_hive.h
4729 lines (3638 loc) · 169 KB
/
plf_hive.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) 2024, 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.
#ifndef PLF_HIVE_H
#define PLF_HIVE_H
#define __cpp_lib_hive
#define PLF_EXCEPTIONS_SUPPORT
#if ((defined(__clang__) || defined(__GNUC__)) && !defined(__EXCEPTIONS)) || (defined(_MSC_VER) && !defined(_CPPUNWIND))
#undef PLF_EXCEPTIONS_SUPPORT
#include <exception> // std::terminate
#endif
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
// Suppress incorrect (unfixed MSVC bug at warning level 4) warnings re: constant expressions in constexpr-if statements
#pragma warning ( push )
#pragma warning ( disable : 4127 )
#endif
#include <algorithm> // std::fill_n, std::sort, std::swap
#include <cassert> // assert
#include <cstring> // memset, memcpy, size_t
#include <limits> // std::numeric_limits
#include <memory> // std::allocator, std::to_address
#include <iterator> // std::bidirectional_iterator_tag, iterator_traits, make_move_iterator, std::distance for range insert
#include <stdexcept> // std::length_error, std::out_of_range
#include <functional> // std::less
#include <cstddef> // offsetof, used in blank()
#include <type_traits> // std::is_trivially_destructible, enable_if_t, type_identity_t, etc
#include <utility> // std::move
#include <initializer_list>
#include <concepts>
#include <compare> // std::strong_ordering
#include <ranges>
namespace plf
{
// For getting std:: overloads to match hive iterators specifically:
template <class T>
concept hive_iterator_concept = requires { typename T::hive_iterator_tag; };
#ifndef PLF_FROM_RANGE // To ensure interoperability with other plf lib containers
#define PLF_FROM_RANGE
// Until such point as standard libraries ubiquitously include std::from_range_t, including this so the rangesv3 constructor overloads will work unambiguously:
namespace ranges
{
struct from_range_t {};
inline constexpr from_range_t from_range;
}
#endif
}
// Overloads for advance etc must be defined here as they are called by range-assign/insert functions - otherwise compiler will call std:: bidirectional overloads:
namespace std
{
template <plf::hive_iterator_concept it_type, typename distance_type>
void advance(it_type &it, const distance_type distance)
{
it.advance(static_cast<typename iterator_traits<it_type>::difference_type>(distance));
}
template <plf::hive_iterator_concept it_type>
[[nodiscard]] it_type next(it_type it, const typename iterator_traits<it_type>::difference_type distance = 1)
{
it.advance(distance);
return it;
}
template <plf::hive_iterator_concept it_type>
[[nodiscard]] it_type prev(it_type it, const typename iterator_traits<it_type>::difference_type distance = 1)
{
it.advance(-distance);
return it;
}
template <plf::hive_iterator_concept it_type>
typename iterator_traits<it_type>::difference_type distance(const it_type first, const it_type last)
{
return first.distance(last);
}
}
namespace plf
{
struct hive_limits // for use in block_capacity setting/getting functions and constructors
{
size_t min, max;
constexpr hive_limits(const size_t minimum, const size_t maximum) noexcept : min(minimum), max(maximum) {}
};
template <class element_type, class allocator_type = std::allocator<element_type> >
class hive : private allocator_type // Empty base class optimisation - inheriting allocator functions
{
typedef std::conditional_t<(sizeof(element_type) > 10 || alignof(element_type) > 10), uint_least16_t, uint_least8_t> skipfield_type;
public:
// Standard container typedefs:
typedef element_type value_type;
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;
// Iterator forward declarations:
template <bool is_const> class hive_iterator;
typedef hive_iterator<false> iterator;
typedef hive_iterator<true> const_iterator;
friend iterator;
friend const_iterator;
template <bool is_const_r> class hive_reverse_iterator;
typedef hive_reverse_iterator<false> reverse_iterator;
typedef hive_reverse_iterator<true> const_reverse_iterator;
friend reverse_iterator;
friend const_reverse_iterator;
private:
// The element as allocated in memory needs to be at-least 2*skipfield_type width in order to support free list indexes in erased element memory space, so:
// make the size of this struct the larger of alignof(T), sizeof(T) or 2*skipfield_type (the latter is only relevant for type char/uchar), and
// make the alignment alignof(T).
// This type is used mainly for correct pointer arithmetic while iterating over elements in memory.
struct alignas(alignof(element_type)) aligned_element_struct
{
// 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.
char data[
(sizeof(element_type) < (sizeof(skipfield_type) * 2)) ?
((sizeof(skipfield_type) * 2) < alignof(element_type) ? alignof(element_type) : (sizeof(skipfield_type) * 2)) :
((sizeof(element_type) < alignof(element_type)) ? alignof(element_type) : sizeof(element_type))
];
};
// We combine the allocation of elements and skipfield into one allocation to save performance. This memory must be allocated as an aligned type with the same alignment as T in order for the elements to align with memory boundaries correctly (which won't happen if we allocate as char or uint_8). But the larger the sizeof in the type we use for allocation, the greater the chance of creating a lot of unused memory in the skipfield portion of the allocated block. So we create a type that is sizeof(alignof(T)), as in most cases alignof(T) < sizeof(T). If alignof(t) >= sizeof(t) this makes no difference.
struct alignas(alignof(element_type)) aligned_allocation_struct
{
char data[alignof(element_type)];
};
// Calculate the capacity of a group's elements+skipfield memory block when expressed in multiples of the value_type's alignment (rounding up).
static size_type get_aligned_block_capacity(const skipfield_type elements_per_group) noexcept
{
return ((elements_per_group * (sizeof(aligned_element_struct) + sizeof(skipfield_type))) + sizeof(skipfield_type) + sizeof(aligned_allocation_struct) - 1) / sizeof(aligned_allocation_struct);
}
// To enable conversion when allocator supplies non-raw pointers:
template <class destination_pointer_type, class source_pointer_type>
static constexpr destination_pointer_type pointer_cast(const source_pointer_type source_pointer) noexcept
{
if constexpr (std::is_trivial<destination_pointer_type>::value)
{
return reinterpret_cast<destination_pointer_type>(std::to_address(source_pointer));
}
else
{
return destination_pointer_type(std::to_address(source_pointer));
}
}
// forward declarations for typedefs below
struct group;
struct item_index_tuple; // for use in sort()
typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<aligned_element_struct> aligned_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_allocator_type>::pointer aligned_pointer_type; // pointer to the (potentially 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;
// group == element memory block + skipfield + block metadata
struct group
{
skipfield_pointer_type skipfield; // Skipfield storage. The element and skipfield arrays are allocated contiguously, in a single allocation, 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. This is present before elements in the group struct as it is referenced constantly by the ++ operator, hence having it first results in a minor performance increase.
group_pointer_type next_group; // Next group in the linked list of all groups. nullptr if no following group. 2nd in struct because it is so frequently used during iteration.
const aligned_pointer_type elements; // Element storage.
group_pointer_type previous_group; // Previous group in the linked list of all groups. nullptr 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/emplace/assign).
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 sizeof(skipfield_type), and calculating it once is faster in benchmarking.
skipfield_type size; // The 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. Also used in combination with capacity to check if group is full, which is used in the next/previous/advance/distance overloads, and range-erase.
group_pointer_type erasures_list_next_group, erasures_list_previous_group; // The next and previous groups in the list of groups with erasures ie. with active erased-element free lists. nullptr if no next or previous group.
size_type group_number; // Used for comparison (> < >= <= <=>) iterator operators (used by distance function and user).
group(aligned_struct_allocator_type &aligned_struct_allocator, const skipfield_type elements_per_group, const group_pointer_type previous):
next_group(nullptr),
elements(pointer_cast<aligned_pointer_type>(std::allocator_traits<aligned_struct_allocator_type>::allocate(aligned_struct_allocator, get_aligned_block_capacity(elements_per_group), (previous == nullptr) ? 0 : previous->elements))),
previous_group(previous),
free_list_head(std::numeric_limits<skipfield_type>::max()),
capacity(elements_per_group),
size(1),
erasures_list_next_group(nullptr),
erasures_list_previous_group(nullptr),
group_number((previous == nullptr) ? 0 : previous->group_number + 1u)
{
skipfield = pointer_cast<skipfield_pointer_type>(elements + elements_per_group);
std::memset(static_cast<void *>(std::to_address(skipfield)), 0, sizeof(skipfield_type) * (static_cast<size_type>(elements_per_group) + 1u));
}
void reset(const skipfield_type increment, const group_pointer_type next, const group_pointer_type previous, const size_type group_num) noexcept
{
next_group = next;
free_list_head = std::numeric_limits<skipfield_type>::max();
previous_group = previous;
size = increment;
erasures_list_next_group = nullptr;
erasures_list_previous_group = nullptr;
group_number = group_num;
std::memset(static_cast<void *>(std::to_address(skipfield)), 0, sizeof(skipfield_type) * static_cast<size_type>(capacity)); // capacity + 1 is not necessary here as the final skipfield node is never written to after initialization
}
};
// Hive member variables:
iterator end_iterator, begin_iterator;
group_pointer_type erasure_groups_head, // Head of doubly-linked list of groups which have erased-element memory locations available for re-use
unused_groups_head; // Head of singly-linked list of reserved groups retained by erase()/clear() or created by reserve()
size_type total_size, total_capacity;
skipfield_type min_block_capacity, max_block_capacity;
group_allocator_type group_allocator;
aligned_struct_allocator_type aligned_struct_allocator;
skipfield_allocator_type skipfield_allocator;
tuple_allocator_type tuple_allocator;
static constexpr size_t max_size_static() noexcept
{
return static_cast<size_t>(std::allocator_traits<allocator_type>::max_size(allocator_type()));
}
// Adaptive minimum based around aligned size, sizeof(group) and sizeof(hive):
static constexpr skipfield_type block_capacity_default_min() noexcept
{
constexpr skipfield_type adaptive_size = static_cast<skipfield_type>(((sizeof(hive) + sizeof(group)) * 2) / sizeof(aligned_element_struct));
constexpr skipfield_type max_block_capacity = block_capacity_default_max(); // Necessary to check against in situations with > 64bit pointer sizes and small sizeof(T)
return std::max(static_cast<skipfield_type>(8), std::min(adaptive_size, max_block_capacity));
}
// Adaptive maximum based on numeric_limits and best outcome from multiple benchmark's (on balance) in terms of memory usage and performance:
static constexpr skipfield_type block_capacity_default_max() noexcept
{
return static_cast<skipfield_type>(std::min(std::min(static_cast<size_t>(std::numeric_limits<skipfield_type>::max()), static_cast<size_t>(8192u)), max_size_static()));
}
void check_capacities_conformance(const hive_limits capacities) const
{
constexpr hive_limits hard_capacities = block_capacity_hard_limits();
if (capacities.min < hard_capacities.min || capacities.min > capacities.max || capacities.max > hard_capacities.max)
{
#ifdef PLF_EXCEPTIONS_SUPPORT
throw std::out_of_range("Supplied memory block capacity limits are either invalid or outside of block_capacity_hard_limits()");
#else
std::terminate();
#endif
}
}
void blank() noexcept
{
if constexpr (std::is_standard_layout<hive>::value && std::allocator_traits<allocator_type>::is_always_equal::value && std::is_trivial<group_pointer_type>::value && std::is_trivial<aligned_pointer_type>::value && std::is_trivial<skipfield_pointer_type>::value)
{ // If all pointer types are trivial, we can just nuke the member variables from orbit with memset (nullptr is always 0):
std::memset(static_cast<void *>(this), 0, offsetof(hive, min_block_capacity));
}
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;
erasure_groups_head = nullptr;
unused_groups_head = nullptr;
total_size = 0;
total_capacity = 0;
}
}
public:
static constexpr hive_limits block_capacity_default_limits() noexcept
{
return hive_limits(static_cast<size_t>(block_capacity_default_min()), static_cast<size_t>(block_capacity_default_max()));
}
// Default constructors:
constexpr explicit hive(const allocator_type &alloc) noexcept:
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(block_capacity_default_min()),
max_block_capacity(block_capacity_default_max()),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{}
constexpr hive() noexcept(noexcept(allocator_type())) :
hive(allocator_type())
{}
constexpr hive(const hive_limits block_limits, const allocator_type &alloc):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(static_cast<skipfield_type>(block_limits.min)),
max_block_capacity(static_cast<skipfield_type>(block_limits.max)),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
check_capacities_conformance(block_limits);
}
constexpr explicit hive(const hive_limits block_limits):
hive(block_limits, allocator_type())
{}
// Copy constructors:
hive(const hive &source, const std::type_identity_t<allocator_type> &alloc):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(std::max(source.min_block_capacity, static_cast<skipfield_type>(std::min(source.total_size, static_cast<size_type>(source.max_block_capacity))))), // min group size is set to value closest to total number of elements in source hive, in order to not create unnecessary small groups in the range-insert below, then reverts to the original min group size afterwards. This effectively saves a call to reserve.
max_block_capacity(source.max_block_capacity),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{ // can skip checking for skipfield conformance here as source will have already checked theirs. Same applies for other copy and move constructors below
range_assign(source.begin_iterator, source.total_size);
min_block_capacity = source.min_block_capacity; // reset to correct value for future operations
}
hive(const hive &source):
hive(source, std::allocator_traits<allocator_type>::select_on_container_copy_construction(source))
{}
// Move constructors:
hive(hive &&source, const std::type_identity_t<allocator_type> &alloc):
allocator_type(alloc),
end_iterator(source.end_iterator),
begin_iterator(source.begin_iterator),
erasure_groups_head(source.erasure_groups_head),
unused_groups_head(source.unused_groups_head),
total_size(source.total_size),
total_capacity(source.total_capacity),
min_block_capacity(source.min_block_capacity),
max_block_capacity(source.max_block_capacity),
group_allocator(alloc),
aligned_struct_allocator(alloc),
skipfield_allocator(alloc),
tuple_allocator(alloc)
{
assert(&source != this);
if constexpr (!std::allocator_traits<allocator_type>::is_always_equal::value)
{
if (alloc != static_cast<allocator_type &>(source))
{
blank();
static_cast<allocator_type &>(*this) = static_cast<allocator_type &>(source);
range_assign(std::make_move_iterator(source.begin_iterator), source.total_size);
source.destroy_all_data();
}
}
source.blank();
}
hive(hive &&source) noexcept:
allocator_type(std::move(static_cast<allocator_type &>(source))),
end_iterator(std::move(source.end_iterator)),
begin_iterator(std::move(source.begin_iterator)),
erasure_groups_head(std::move(source.erasure_groups_head)),
unused_groups_head(std::move(source.unused_groups_head)),
total_size(source.total_size),
total_capacity(source.total_capacity),
min_block_capacity(source.min_block_capacity),
max_block_capacity(source.max_block_capacity),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
assert(&source != this);
source.blank();
}
// Fill constructors:
hive(const size_type fill_number, const element_type &element, const hive_limits block_limits, const allocator_type &alloc = allocator_type()):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(static_cast<skipfield_type>(block_limits.min)),
max_block_capacity(static_cast<skipfield_type>(block_limits.max)),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
check_capacities_conformance(block_limits);
assign(fill_number, element);
}
hive(const size_type fill_number, const element_type &element, const allocator_type &alloc = allocator_type()) :
hive(fill_number, element, block_capacity_default_limits(), alloc)
{}
// Default-value fill constructors:
hive(const size_type fill_number, const hive_limits block_limits, const allocator_type &alloc = allocator_type()):
hive(fill_number, element_type(), block_limits, alloc)
{}
hive(const size_type fill_number, const allocator_type &alloc = allocator_type()):
hive(fill_number, element_type(), block_capacity_default_limits(), alloc)
{}
// Range constructors:
template<typename iterator_type>
hive(const typename std::enable_if_t<!std::numeric_limits<iterator_type>::is_integer, iterator_type> &first, const iterator_type &last, const hive_limits block_limits, const allocator_type &alloc = allocator_type()):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(static_cast<skipfield_type>(block_limits.min)),
max_block_capacity(static_cast<skipfield_type>(block_limits.max)),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
check_capacities_conformance(block_limits);
assign<iterator_type>(first, last);
}
template<typename iterator_type>
hive(const typename std::enable_if_t<!std::numeric_limits<iterator_type>::is_integer, iterator_type> &first, const iterator_type &last, const allocator_type &alloc = allocator_type()):
hive(first, last, block_capacity_default_limits(), alloc)
{}
// Initializer-list constructors:
hive(const std::initializer_list<element_type> &element_list, const hive_limits block_limits, const allocator_type &alloc = allocator_type()):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(static_cast<skipfield_type>(block_limits.min)),
max_block_capacity(static_cast<skipfield_type>(block_limits.max)),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
check_capacities_conformance(block_limits);
range_assign(element_list.begin(), static_cast<size_type>(element_list.size()));
}
hive(const std::initializer_list<element_type> &element_list, const allocator_type &alloc = allocator_type()):
hive(element_list, block_capacity_default_limits(), alloc)
{}
// Ranges v3 constructors:
template<class range_type>
requires std::ranges::range<range_type>
hive(plf::ranges::from_range_t, range_type &&rg, const hive_limits block_limits, const allocator_type &alloc = allocator_type()):
allocator_type(alloc),
erasure_groups_head(nullptr),
unused_groups_head(nullptr),
total_size(0),
total_capacity(0),
min_block_capacity(static_cast<skipfield_type>(block_limits.min)),
max_block_capacity(static_cast<skipfield_type>(block_limits.max)),
group_allocator(*this),
aligned_struct_allocator(*this),
skipfield_allocator(*this),
tuple_allocator(*this)
{
check_capacities_conformance(block_limits);
range_assign(std::ranges::begin(rg), static_cast<size_type>(std::ranges::distance(rg)));
}
template<class range_type>
requires std::ranges::range<range_type>
hive(plf::ranges::from_range_t, range_type &&rg, const allocator_type &alloc = allocator_type()):
hive(plf::ranges::from_range, std::move(rg), block_capacity_default_limits(), alloc)
{}
// Everything else:
iterator begin() noexcept
{
return begin_iterator;
}
const_iterator begin() const noexcept
{
return begin_iterator;
}
iterator end() noexcept
{
return end_iterator;
}
const_iterator end() const noexcept
{
return end_iterator;
}
const_iterator cbegin() const noexcept
{
return begin_iterator;
}
const_iterator cend() const noexcept
{
return end_iterator;
}
reverse_iterator rbegin() noexcept
{
return (end_iterator.group_pointer != nullptr) ? ++reverse_iterator(end_iterator.group_pointer, end_iterator.element_pointer, end_iterator.skipfield_pointer) : reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1);
}
const_reverse_iterator rbegin() const noexcept
{
return crbegin();
}
reverse_iterator rend() noexcept
{
return reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1);
}
const_reverse_iterator rend() const noexcept
{
return crend();
}
const_reverse_iterator crbegin() const noexcept
{
return (end_iterator.group_pointer != nullptr) ? ++const_reverse_iterator(end_iterator.group_pointer, end_iterator.element_pointer, end_iterator.skipfield_pointer) : const_reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1);
}
const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(begin_iterator.group_pointer, begin_iterator.element_pointer - 1, begin_iterator.skipfield_pointer - 1);
}
~hive() noexcept
{
destroy_all_data();
}
private:
group_pointer_type allocate_new_group(const skipfield_type elements_per_group, const group_pointer_type previous = nullptr)
{
const group_pointer_type new_group = std::allocator_traits<group_allocator_type>::allocate(group_allocator, 1, previous);
#ifdef PLF_EXCEPTIONS_SUPPORT
try
{
std::allocator_traits<group_allocator_type>::construct(group_allocator, new_group, aligned_struct_allocator, elements_per_group, previous);
}
catch (...)
{
std::allocator_traits<group_allocator_type>::deallocate(group_allocator, new_group, 1);
throw;
}
#else
std::allocator_traits<group_allocator_type>::construct(group_allocator, new_group, aligned_struct_allocator, elements_per_group, previous);
#endif
return new_group;
}
void deallocate_group(const group_pointer_type the_group) noexcept
{
std::allocator_traits<aligned_struct_allocator_type>::deallocate(aligned_struct_allocator, pointer_cast<aligned_struct_pointer_type>(the_group->elements), get_aligned_block_capacity(the_group->capacity));
std::allocator_traits<group_allocator_type>::deallocate(group_allocator, the_group, 1);
}
constexpr void destroy_element(const aligned_pointer_type element) noexcept
{
if constexpr (!std::is_trivially_destructible<element_type>::value) // to avoid codegen for trivial types
{
std::allocator_traits<allocator_type>::destroy(*this, pointer_cast<pointer>(element));
}
}
void destroy_group(const aligned_pointer_type end_pointer) noexcept
{
if constexpr (!std::is_trivially_destructible<element_type>::value)
{
do
{
destroy_element(begin_iterator.element_pointer);
begin_iterator.element_pointer += static_cast<size_type>(*++begin_iterator.skipfield_pointer) + 1u;
begin_iterator.skipfield_pointer += *begin_iterator.skipfield_pointer;
} while(begin_iterator.element_pointer != end_pointer);
}
deallocate_group(begin_iterator.group_pointer);
}
void destroy_all_data() noexcept
{
if (begin_iterator.group_pointer != nullptr)
{
end_iterator.group_pointer->next_group = unused_groups_head; // Link used and unused_group lists together
if constexpr (!std::is_trivially_destructible<element_type>::value)
{
if (total_size != 0)
{
while (begin_iterator.group_pointer != end_iterator.group_pointer) // Erase elements without bothering to update skipfield - much faster:
{
const group_pointer_type next_group = begin_iterator.group_pointer->next_group;
destroy_group(pointer_cast<aligned_pointer_type>(begin_iterator.group_pointer->skipfield));
begin_iterator.group_pointer = next_group;
begin_iterator.element_pointer = next_group->elements + *(next_group->skipfield);
begin_iterator.skipfield_pointer = next_group->skipfield + *(next_group->skipfield);
}
destroy_group(end_iterator.element_pointer);
begin_iterator.group_pointer = unused_groups_head;
}
}
while (begin_iterator.group_pointer != nullptr)
{
const group_pointer_type next_group = begin_iterator.group_pointer->next_group;
deallocate_group(begin_iterator.group_pointer);
begin_iterator.group_pointer = next_group;
}
}
}
void initialize(const skipfield_type first_group_size)
{
end_iterator.group_pointer = begin_iterator.group_pointer = allocate_new_group(first_group_size);
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;
}
void edit_free_list(const skipfield_pointer_type location, const skipfield_type value) noexcept
{
std::allocator_traits<skipfield_allocator_type>::destroy(skipfield_allocator, location);
std::allocator_traits<skipfield_allocator_type>::construct(skipfield_allocator, location, value);
}
void edit_free_list_prev(const aligned_pointer_type location, const skipfield_type value) noexcept // Write to the 'previous erased element' index in the erased element memory location
{
edit_free_list(pointer_cast<skipfield_pointer_type>(location), value);
}
void edit_free_list_next(const aligned_pointer_type location, const skipfield_type value) noexcept // Ditto 'next'
{
edit_free_list(pointer_cast<skipfield_pointer_type>(location) + 1, value);
}
void edit_free_list_head(const aligned_pointer_type location, const skipfield_type value) noexcept
{
const skipfield_pointer_type converted_location = pointer_cast<skipfield_pointer_type>(location);
edit_free_list(converted_location, value);
edit_free_list(converted_location + 1, std::numeric_limits<skipfield_type>::max());
}
void update_skipblock(const iterator &new_location, const skipfield_type prev_free_list_index) noexcept
{
const skipfield_type new_value = static_cast<skipfield_type>(*(new_location.skipfield_pointer) - 1);
if (new_value != 0) // ie. skipfield was not 1, ie. a single-node skipblock, with no additional nodes to update
{
// set (new) start and (original) end of skipblock to new value:
*(new_location.skipfield_pointer + new_value) = *(new_location.skipfield_pointer + 1) = new_value;
// transfer free list node to new start node:
++(erasure_groups_head->free_list_head);
if (prev_free_list_index != std::numeric_limits<skipfield_type>::max()) // ie. not the tail free list node
{
edit_free_list_next(new_location.group_pointer->elements + prev_free_list_index, erasure_groups_head->free_list_head);
}
edit_free_list_head(new_location.element_pointer + 1, prev_free_list_index);
}
else // single-node skipblock, remove skipblock
{
erasure_groups_head->free_list_head = prev_free_list_index;
if (prev_free_list_index != std::numeric_limits<skipfield_type>::max()) // ie. not the last free list node
{
edit_free_list_next(new_location.group_pointer->elements + prev_free_list_index, std::numeric_limits<skipfield_type>::max());
}
else // remove this group from the list of groups with erasures
{
erasure_groups_head = erasure_groups_head->erasures_list_next_group; // No need to update previous group for new head, as this is never accessed if group == head
}
}
*(new_location.skipfield_pointer) = 0;
++(new_location.group_pointer->size);
if (new_location.group_pointer == begin_iterator.group_pointer && new_location.element_pointer < begin_iterator.element_pointer)
{ /* ie. begin_iterator was moved forwards as the result of an erasure at some point, this erased element is before the current begin, hence, set current begin iterator to this element */
begin_iterator = new_location;
}
++total_size;
}
void reset() noexcept
{
destroy_all_data();
blank();
}
void update_subsequent_group_numbers(size_type current_group_number, group_pointer_type update_group) noexcept
{
do
{
update_group->group_number = current_group_number++;
update_group = update_group->next_group;
} while (update_group != nullptr);
}
void reset_group_numbers() noexcept
{
update_subsequent_group_numbers(0, begin_iterator.group_pointer);
}
void reset_group_numbers_if_necessary() noexcept
{
if (end_iterator.group_pointer->group_number == std::numeric_limits<size_type>::max()) [[unlikely]] reset_group_numbers();
}
group_pointer_type reuse_unused_group() noexcept
{
const group_pointer_type reused_group = unused_groups_head;
unused_groups_head = reused_group->next_group;
reset_group_numbers_if_necessary();
reused_group->reset(1, nullptr, end_iterator.group_pointer, end_iterator.group_pointer->group_number + 1u);
return reused_group;
}
template<typename... arguments>
constexpr void construct_element(const aligned_pointer_type location, arguments &&... parameters)
{
std::allocator_traits<allocator_type>::construct(*this, pointer_cast<pointer>(location), std::forward<arguments>(parameters) ...);
}
public:
iterator insert(const element_type &element) // Note: defining insert & and insert && as calls to emplace results in larger codegen in release mode (under GCC at least), and prevents more accurate is_nothrow tests
{
if (end_iterator.element_pointer != nullptr) // ie. empty hive, no blocks allocated yet
{
if (erasure_groups_head == nullptr) // ie. there are no erased elements
{
if (end_iterator.element_pointer != pointer_cast<aligned_pointer_type>(end_iterator.group_pointer->skipfield)) // ie. end_iterator is not at end of block
{
construct_element(end_iterator.element_pointer, element);
const iterator return_iterator = end_iterator;
++end_iterator.element_pointer;
++end_iterator.skipfield_pointer;
++(end_iterator.group_pointer->size);
++total_size;
return return_iterator;
}
group_pointer_type next_group;
if (unused_groups_head == nullptr)
{
const skipfield_type new_group_size = static_cast<skipfield_type>(std::min(total_size, static_cast<size_type>(max_block_capacity)));
reset_group_numbers_if_necessary();
next_group = allocate_new_group(new_group_size, end_iterator.group_pointer);
#ifdef PLF_EXCEPTIONS_SUPPORT
if constexpr (!std::is_nothrow_copy_constructible<element_type>::value)
{
try
{
construct_element(next_group->elements, element);
}
catch (...)
{
deallocate_group(next_group);
throw;
}
}
else
#endif
{
construct_element(next_group->elements, element);
}
total_capacity += new_group_size;
}
else
{
construct_element(unused_groups_head->elements, element);
next_group = reuse_unused_group();