-
Notifications
You must be signed in to change notification settings - Fork 2
/
tiered_vector.hpp
3623 lines (3283 loc) · 130 KB
/
tiered_vector.hpp
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
/**
* MIT License
*
* Copyright (c) 2022 Victor Moncada <vtr.moncada@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SEQ_TIERED_VECTOR_HPP
#define SEQ_TIERED_VECTOR_HPP
#include "devector.hpp"
#include "pdqsort.hpp"
#include "utils.hpp"
#include "tiny_string.hpp"
#include <algorithm>
#include <string>
#include <cmath>
#ifndef SEQ_MIN_BUCKET_SIZE
/// @brief Minimum bucket size for given type
#define SEQ_MIN_BUCKET_SIZE(T) \
(sizeof(T) <= 4 ? 64 : (sizeof(T) <= 8 ? 32 : (sizeof(T) <= 16 ? 16 : (sizeof(T) <= 64 ? 4 : 2))))
#endif
/// @brief Maximum bucket size (default to 1<<16)
#define SEQ_MAX_BUCKET_SIZE (1U<<16U)
namespace seq
{
namespace detail
{
using cbuffer_pos = int; //index type within circular buffer, must be signed
// Deque const iterator
template<class BucketMgr >
struct deque_const_iterator
{
using bucket_manager = BucketMgr;
using bucket_type = typename bucket_manager::StoreBucketType;
using value_type = typename BucketMgr::value_type;
using iterator_category = std::random_access_iterator_tag;
using size_type = typename BucketMgr::size_type;
using difference_type = typename BucketMgr::difference_type;
using pointer = typename BucketMgr::const_pointer;
using reference = const value_type&;
bucket_manager* mgr;
bucket_type* bucket;
difference_type pos ;
cbuffer_pos ptr ;
cbuffer_pos begin_ptr ;
cbuffer_pos first_stop ;
SEQ_ALWAYS_INLINE deque_const_iterator() noexcept {}
SEQ_ALWAYS_INLINE deque_const_iterator(const bucket_manager* d) noexcept
: mgr(const_cast<bucket_manager*>(d)),
bucket(d ? const_cast<bucket_type*>(d->d_buckets.data() + d->d_buckets.size()) : nullptr),
pos(d ? static_cast<difference_type>(d->size()) : 0),
ptr(0),
begin_ptr(0),
first_stop(0)
{} //end()
SEQ_ALWAYS_INLINE deque_const_iterator(const bucket_manager* d, size_type /*unused*/) noexcept
: mgr(const_cast<bucket_manager*>(d)),
bucket(d ? const_cast<bucket_type*>(d->d_buckets.data()) : nullptr),
pos(0),
ptr(d ? (d->d_buckets.empty() ? 0 : d->d_buckets.front()->begin_index()) : 0)
{
begin_ptr = ptr;
if(d) first_stop = d->d_buckets.empty() ? 0 : (*bucket)->first_stop();
} //begin
SEQ_ALWAYS_INLINE deque_const_iterator(const bucket_manager* d, size_type p, size_type /*unused*/) noexcept
: mgr(const_cast<bucket_manager*>(d))
{
setPos(p);
} //any pos
SEQ_ALWAYS_INLINE auto absolutePos() const noexcept -> size_type {
return static_cast<size_t>(pos);
}
SEQ_ALWAYS_INLINE void setPos(size_t new_pos) noexcept
{
SEQ_ASSERT_DEBUG( new_pos <= static_cast<size_t>(mgr->d_size), "invalid iterator position");
size_type front_size = static_cast<size_t>(mgr->d_buckets.front()->size);
size_type bindex = (new_pos + (static_cast<size_t>(mgr->d_bucket_size) - front_size)) >> static_cast<size_t>(mgr->d_bucket_size_bits);
size_type index_in_bucket = (new_pos - (new_pos < (front_size) ? 0 : front_size)) & static_cast<size_t>(mgr->d_bucket_size1);
bucket = mgr->d_buckets.data() + bindex;
if (SEQ_UNLIKELY(bindex == mgr->d_buckets.size())) {
// end iterator
ptr = begin_ptr = first_stop = 0;
}
else {
ptr = static_cast<cbuffer_pos>(&(*bucket)->at(static_cast<cbuffer_pos>(index_in_bucket)) - (*bucket)->buffer());
begin_ptr = (*bucket)->begin_index();
first_stop = (ptr < begin_ptr) ? (*bucket)->second_stop() : (*bucket)->first_stop();
}
pos = static_cast<difference_type>(new_pos);
}
SEQ_ALWAYS_INLINE void offset(difference_type diff) noexcept
{
setPos(static_cast<size_t>(pos + diff));
}
SEQ_ALWAYS_INLINE void go_next() noexcept
{
const cbuffer_pos sstop = (*bucket)->second_stop();
if (first_stop == sstop)
{
bucket++;
if (pos != static_cast<difference_type>(mgr->size())) {
begin_ptr = ptr = (*bucket)->begin_index();
first_stop = (*bucket)->first_stop();
}
else
ptr = begin_ptr = first_stop = 0;
}
else {
first_stop = sstop;
ptr = 0;
}
}
SEQ_ALWAYS_INLINE void go_prev() noexcept
{
if (ptr == begin_ptr - 1)
{
bucket--;
ptr = static_cast<cbuffer_pos>((*bucket)->last_ptr() - (*bucket)->buffer());
begin_ptr = (*bucket)->begin_index();
first_stop = (*bucket)->second_stop();
}
else if (ptr == -1) {
first_stop = (*bucket)->first_stop();
ptr = (*bucket)->max_size1;
}
}
SEQ_ALWAYS_INLINE auto operator++() noexcept -> deque_const_iterator&
{
SEQ_ASSERT_DEBUG(pos < static_cast<difference_type>(mgr->d_size), "increment iterator already at the end");
++ptr;
++pos;
if (SEQ_UNLIKELY(ptr == first_stop )) {
go_next();
}
return *this;
}
SEQ_ALWAYS_INLINE auto operator++(int) noexcept -> deque_const_iterator {
deque_const_iterator _Tmp = *this;
++(*this);
return _Tmp;
}
SEQ_ALWAYS_INLINE auto operator--() noexcept -> deque_const_iterator& {
SEQ_ASSERT_DEBUG(pos > 0, "increment iterator already at the end");
--ptr;
--pos;
if(SEQ_UNLIKELY(ptr < begin_ptr )) {
go_prev();
}
return *this;
}
SEQ_ALWAYS_INLINE auto operator--(int) noexcept -> deque_const_iterator {
deque_const_iterator _Tmp = *this;
--(*this);
return _Tmp;
}
SEQ_ALWAYS_INLINE auto operator*() const noexcept -> reference {
SEQ_ASSERT_DEBUG(pos >= 0 && pos < static_cast<difference_type>(mgr->d_size), "dereference invalid iterator");
return (*bucket)->buffer()[ptr];
}
SEQ_ALWAYS_INLINE auto operator->() const noexcept -> pointer {
return std::pointer_traits<pointer>::pointer_to(**this);
}
SEQ_ALWAYS_INLINE auto operator+=(difference_type diff) noexcept -> deque_const_iterator& {
offset(diff);
return *this;
}
SEQ_ALWAYS_INLINE auto operator-=(difference_type diff) noexcept -> deque_const_iterator& {
offset(-diff);
return *this;
}
};
// Deque iterator class
template<class BucketMgr >
struct deque_iterator : public deque_const_iterator< BucketMgr>
{
using base_type = deque_const_iterator< BucketMgr>;
using bucket_manager = BucketMgr;
using bucket_type = typename bucket_manager::BucketType;
using value_type = typename BucketMgr::value_type;
using size_type = typename BucketMgr::size_type;
using iterator_category = std::random_access_iterator_tag;
using difference_type = typename BucketMgr::difference_type;
using pointer = typename BucketMgr::pointer;
using const_pointer = typename BucketMgr::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
deque_iterator() noexcept {}
deque_iterator(const bucket_manager* d) noexcept :base_type(d) {}
deque_iterator(const bucket_manager* d, size_type b) noexcept :base_type(d, b) {}
deque_iterator(const deque_const_iterator<BucketMgr>& other) noexcept : base_type(other) {}
deque_iterator(const bucket_manager* d, size_type p, size_type unused) noexcept : base_type(d,p,unused){} //any pos
SEQ_ALWAYS_INLINE auto operator*() noexcept -> reference {
return const_cast<reference>(base_type::operator*());
}
SEQ_ALWAYS_INLINE auto operator->() noexcept -> pointer {
return std::pointer_traits<pointer>::pointer_to(**this);
}
SEQ_ALWAYS_INLINE auto operator*() const noexcept -> const_reference {
return base_type::operator*();
}
SEQ_ALWAYS_INLINE auto operator->() const noexcept -> const_pointer {
return std::pointer_traits<const_pointer>::pointer_to(**this);
}
SEQ_ALWAYS_INLINE auto operator++() noexcept -> deque_iterator& {
base_type::operator++();
return *this;
}
SEQ_ALWAYS_INLINE auto operator++(int) noexcept -> deque_iterator {
deque_iterator _Tmp = *this;
base_type::operator++();
return _Tmp;
}
SEQ_ALWAYS_INLINE auto operator--() noexcept -> deque_iterator& {
base_type::operator--();
return *this;
}
SEQ_ALWAYS_INLINE auto operator--(int) noexcept -> deque_iterator {
deque_iterator _Tmp = *this;
base_type::operator--();
return _Tmp;
}
SEQ_ALWAYS_INLINE auto operator+=(difference_type diff)noexcept -> deque_iterator& {
base_type::operator+=(diff);
return *this;
}
SEQ_ALWAYS_INLINE auto operator-=(difference_type diff)noexcept -> deque_iterator& {
base_type::operator-=(diff);
return *this;
}
};
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator+(const deque_const_iterator< BucketMgr>& it, typename deque_const_iterator< BucketMgr>::difference_type diff)noexcept -> deque_const_iterator< BucketMgr> {
deque_const_iterator< BucketMgr> res = it;
res += diff;
return res;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator+(const deque_iterator< BucketMgr>& it, typename deque_iterator< BucketMgr>::difference_type diff)noexcept -> deque_iterator< BucketMgr> {
deque_iterator< BucketMgr> res = it;
res += diff;
return res;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator-(const deque_const_iterator< BucketMgr>& it, typename deque_const_iterator< BucketMgr>::difference_type diff)noexcept -> deque_const_iterator< BucketMgr> {
deque_const_iterator< BucketMgr> res = it;
res -= diff;
return res;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator-(const deque_iterator< BucketMgr>& it, typename deque_iterator< BucketMgr>::difference_type diff)noexcept -> deque_iterator< BucketMgr> {
deque_iterator< BucketMgr> res = it;
res -= diff;
return res;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator-(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> typename deque_const_iterator< BucketMgr>::difference_type {
return it1.pos - it2.pos;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator==(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> bool {
return it1.pos == it2.pos;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator!=(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> bool {
return it1.pos != it2.pos;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator<(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> bool {
return (it1.pos) < (it2.pos);
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator>(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2) noexcept -> bool {
return (it1.pos) > (it2.pos);
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator<=(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> bool {
return it1.pos <= it2.pos;
}
template < class BucketMgr>
SEQ_ALWAYS_INLINE auto operator>=(const deque_const_iterator< BucketMgr>& it1, const deque_const_iterator< BucketMgr>& it2)noexcept -> bool {
return it1.pos >= it2.pos;
}
template< class BucketMgr>
struct tvector_ra_iterator
{
using iterator_category = std::random_access_iterator_tag;
using value_type = typename BucketMgr::value_type;
using difference_type = typename BucketMgr::difference_type;
using pointer = typename BucketMgr::pointer;
using reference = value_type&;
using pos_type = difference_type;
using chunk_type = typename BucketMgr::StoreBucketType;
BucketMgr* data;
chunk_type* node;
int pos;
tvector_ra_iterator() noexcept {}
tvector_ra_iterator(const BucketMgr* d) noexcept //begin
:data(const_cast<BucketMgr*>(d)), node(const_cast<chunk_type*>(d->d_buckets.data())), pos(0){}
tvector_ra_iterator(const BucketMgr* d, size_t) noexcept //end
:data(const_cast<BucketMgr*>(d)){
if (data->d_buckets.back().bucket->size == data->d_bucket_size) {
node = data->d_buckets.data() + data->d_buckets.size();
pos = 0;
}
else {
node = data->d_buckets.data() + data->d_buckets.size() - 1;
pos = data->d_buckets.back().bucket->size;
}
}
auto computeAbsolutePos() const noexcept -> difference_type {
return ((node - data->d_buckets.data()) << data->d_bucket_size_bits) + pos;
}
void add(difference_type diff) noexcept{
difference_type abs = computeAbsolutePos();
abs += diff;
this->node = data->d_buckets.data() + (abs >> data->d_bucket_size_bits);
this->pos = static_cast<int>(abs & data->d_bucket_size1);
}
auto operator*() noexcept -> reference {
return node->bucket->buffer()[pos];
}
auto operator->() noexcept -> pointer {
return std::pointer_traits<pointer>::pointer_to(**this);
}
auto operator++() noexcept -> tvector_ra_iterator& {
++pos;
if (SEQ_UNLIKELY(pos >= data->d_bucket_size)) {
++node;
pos = 0;
}
return *this;
}
auto operator++(int) noexcept -> tvector_ra_iterator {
tvector_ra_iterator _Tmp = *this;
++(*this);
return _Tmp;
}
auto operator--() noexcept -> tvector_ra_iterator& {
--pos;
if (SEQ_UNLIKELY(pos < 0)) {
--node;
pos = data->d_bucket_size1;
}
return*this;
}
auto operator--(int) noexcept -> tvector_ra_iterator {
tvector_ra_iterator _Tmp = *this;
--(*this);
return _Tmp;
}
auto operator+=(difference_type diff) noexcept -> tvector_ra_iterator& {
add( diff);
return *this;
}
auto operator-=(difference_type diff) noexcept -> tvector_ra_iterator& {
add(-diff);
return *this;
}
bool operator==(const tvector_ra_iterator& other) const noexcept { return node == other.node && pos == other.pos; }
bool operator!=(const tvector_ra_iterator& other) const noexcept { return node != other.node || pos != other.pos; }
bool operator<(const tvector_ra_iterator& other) const noexcept { return node < other.node || (node == other.node && pos < other.pos); }
bool operator>(const tvector_ra_iterator& other) const noexcept { return node > other.node || (node == other.node && pos > other.pos); }
bool operator<=(const tvector_ra_iterator& other) const noexcept { return !(*this > other); }
bool operator>=(const tvector_ra_iterator& other) const noexcept { return !(other > *this); }
auto operator+(difference_type diff) const noexcept -> tvector_ra_iterator {
tvector_ra_iterator tmp = *this;
tmp += diff;
return tmp;
}
auto operator-(difference_type diff) const noexcept -> tvector_ra_iterator {
tvector_ra_iterator tmp = *this;
tmp -= diff;
return tmp;
}
auto operator-(const tvector_ra_iterator& other) const noexcept -> difference_type {
return ((node - other.node) << data->d_bucket_size_bits) + (pos - other.pos);
}
};
} //end detail
namespace detail
{
// Base class for circular buffer
template<class T, class Allocator>
struct alignas(alignof(T) > alignof(cbuffer_pos) ? alignof(T) : alignof(cbuffer_pos)) BaseCircularBuffer
{
cbuffer_pos size; //buffer size
cbuffer_pos max_size1; //buffer max size -1
cbuffer_pos max_size_; //buffer max size
cbuffer_pos begin; //begin index of data
BaseCircularBuffer(cbuffer_pos max_size) noexcept
:size(0), max_size1(max_size - 1), max_size_(max_size), begin(0) {}
};
// Circular buffer class used internally by tiered_vector
// Actuall data are located in the same memory block and starts at start_data_T
template<class T, class Allocator>
struct CircularBuffer : BaseCircularBuffer<T, Allocator>
{
static constexpr size_t size_BCB = sizeof(BaseCircularBuffer<T, Allocator>);
// Start position for actual data in bytes
static constexpr size_t start_data_T = (size_BCB > sizeof(T)) ?
(size_BCB / sizeof(T) + ((size_BCB % sizeof(T)) != 0u ? 1 : 0)) :
1;
static constexpr size_t start_data = start_data_T * sizeof(T);
static constexpr bool relocatable = is_relocatable<T>::value && (sizeof(T) >= sizeof(size_t));
using BaseCircularBuffer<T, Allocator>::size;
using BaseCircularBuffer<T, Allocator>::max_size1;
using BaseCircularBuffer<T, Allocator>::max_size_;
using BaseCircularBuffer<T, Allocator>::begin;
// Initialize from a maximum size
CircularBuffer(cbuffer_pos max_size) noexcept
:BaseCircularBuffer<T, Allocator>(max_size) {
//initialize empty buffer
}
// Initialize from a maximum size and a default value.
// This will set the buffer size to its maximum.
CircularBuffer(cbuffer_pos max_size, const T& val, Allocator& /*unused*/)
:BaseCircularBuffer<T, Allocator>(max_size) {
size = max_size;
//initialize full buffer
cbuffer_pos i = 0;
try {
for (; i < max_size; ++i)
construct_ptr(buffer() + i, val);
}
catch (...) {
for (cbuffer_pos j = 0; j < i; ++j)
destroy_ptr(buffer() + j);
size = 0;
throw;
}
}
// Destroy all values within buffer
void destroy(Allocator& /*unused*/) noexcept {
if (!std::is_trivially_destructible<T>::value) {
for (cbuffer_pos i = 0; i < size; ++i)
destroy_ptr(&(*this)[i]);
}
}
//Disable copy
CircularBuffer(const CircularBuffer& other) = delete;
auto operator=(const CircularBuffer& other) -> CircularBuffer& = delete;
//Raw buffer access
SEQ_ALWAYS_INLINE auto buffer() noexcept -> T* { return reinterpret_cast<T*>(reinterpret_cast<char*>(this) + start_data); }
SEQ_ALWAYS_INLINE auto buffer() const noexcept -> const T* { return reinterpret_cast<const T*>(reinterpret_cast<const char*>(this) + start_data); }
//Pointer on the first data
SEQ_ALWAYS_INLINE auto begin_ptr() noexcept -> T* { return buffer() + ((begin)&max_size1); }
SEQ_ALWAYS_INLINE auto begin_ptr() const noexcept -> const T* { return buffer() + ((begin)&max_size1); }
//Pointer on the last data
SEQ_ALWAYS_INLINE auto last_ptr() noexcept -> T* { return (buffer() + ((begin + size - 1) & max_size1)); }
SEQ_ALWAYS_INLINE auto last_ptr() const noexcept -> const T* { return (buffer() + ((begin + size - 1) & max_size1)); }
//Index of the first data
SEQ_ALWAYS_INLINE auto begin_index() const noexcept -> cbuffer_pos { return (begin)&max_size1; }
// Index of the first stop (either at size or max size)
SEQ_ALWAYS_INLINE auto first_stop() const noexcept -> cbuffer_pos {
cbuffer_pos p = begin_index();
return (p + size) > max_size() ? (max_size()) : (p + size);
}
// Index of the second stop (either at size or max size)
SEQ_ALWAYS_INLINE auto second_stop() const noexcept -> cbuffer_pos {
cbuffer_pos p = begin_index();
return (p + size) > max_size() ? (((p + size) & max_size1)) : (p + size);
}
//Buffer maximum size
SEQ_ALWAYS_INLINE auto max_size() const noexcept -> cbuffer_pos {
return max_size_;
}
//Check if full
auto isFull() const noexcept -> bool { return size == max_size(); }
//Element access.
SEQ_ALWAYS_INLINE auto operator[](cbuffer_pos index) noexcept -> T& {
//SEQ_ASSERT_DEBUG(index >= 0, "Invalid index");
SEQ_ASSERT_DEBUG(!(index >= max_size() && begin == 0), "Invalid index");
return buffer()[(begin == 0) ? index : ((begin + index) & (max_size1))];
}
SEQ_ALWAYS_INLINE auto operator[](cbuffer_pos index) const noexcept -> const T& {
//SEQ_ASSERT_DEBUG(index >= 0, "Invalid index");
SEQ_ASSERT_DEBUG(!(index >= max_size() && begin == 0), "Invalid index");
return buffer()[(begin == 0) ? index : ((begin + index) & (max_size1))];
}
SEQ_ALWAYS_INLINE auto at(cbuffer_pos index) noexcept -> T& {
SEQ_ASSERT_DEBUG(index >= 0, "Invalid index");
return buffer()[((begin + index) & (max_size1))];
}
SEQ_ALWAYS_INLINE auto at(cbuffer_pos index) const noexcept -> const T& {
SEQ_ASSERT_DEBUG(index >= 0, "Invalid index");
return buffer()[((begin + index) & (max_size1))];
}
// Front/back access
SEQ_ALWAYS_INLINE auto front() noexcept -> T& { return buffer()[begin]; }
SEQ_ALWAYS_INLINE auto front() const noexcept -> const T& { return buffer()[begin]; }
SEQ_ALWAYS_INLINE auto back() noexcept -> T& { return (*this)[size - 1]; }
SEQ_ALWAYS_INLINE auto back() const noexcept -> const T& { return (*this)[size - 1]; }
// Initialize a front buffer
SEQ_ALWAYS_INLINE void init_front() noexcept {
//init members for front buffer
begin = size = 0;
}
// Resize buffer
void resize(cbuffer_pos s, Allocator& /*unused*/) {
if (s < size) {
if (!std::is_trivially_destructible<T>::value) {
for (cbuffer_pos i = s; i < size; ++i)
destroy_ptr(&(*this)[i]);
}
}
else if (s > size) {
cbuffer_pos i = size;
// Make sure to destroy constructed values in case of exception
try {
for (; i < s; ++i)
construct_ptr(&(*this)[i]);
}
catch (...) {
for (cbuffer_pos j = size; j < i; ++j)
destroy_ptr(&(*this)[j]);
throw;
}
}
size = s;
}
void resize(cbuffer_pos s, const T& value, Allocator& /*unused*/) {
if (s < size) {
if (!std::is_trivially_destructible<T>::value) {
for (cbuffer_pos i = s; i < size; ++i)
destroy_ptr(&at(i));
}
}
else if (s > size) {
cbuffer_pos i = size;
// Make sure to destroy constructed values in case of exception
try {
for (; i < s; ++i)
construct_ptr(&at(i), value);
}
catch (...) {
for (cbuffer_pos j = size; j < i; ++j)
destroy_ptr(&at(j));
throw;
}
}
size = s;
}
// Extend front buffer
void grow_front(cbuffer_pos s) noexcept {
begin += (s - size);
size = s;
if (begin < 0) begin += max_size();
else begin &= max_size1;
}
template <class... Args>
auto emplace_back(Allocator& /*unused*/, Args&&... args) -> T* {
//only works for non full array
T* ptr = begin ? &at(size) : (buffer() + size);
// Might throw, which is fine
construct_ptr(ptr, std::forward<Args>(args)...);
++size;
return ptr;
}
SEQ_ALWAYS_INLINE auto push_back(Allocator& allocator, const T& value) -> T* {
//only works for non full array
return emplace_back(allocator, value);
}
SEQ_ALWAYS_INLINE auto push_back(Allocator& allocator, T&& value) -> T* {
//only works for non full array
return emplace_back(allocator, std::move(value));
}
template <class... Args>
auto emplace_front(Allocator& /*unused*/, Args&&... args) -> T* {
//only works for non full array
if (--begin < 0) begin = max_size1;
try {
construct_ptr(buffer() + begin, std::forward<Args>(args)...);
}
catch (...) {
begin = (begin + 1) & max_size1;
throw;
}
++size;
return buffer() + begin;
}
SEQ_ALWAYS_INLINE auto push_front(Allocator& allocator, const T& value) -> T* {
//only works for non full array
return emplace_front(allocator, value);
}
SEQ_ALWAYS_INLINE auto push_front(Allocator& allocator, T&& value) -> T* {
//only works for non full array
return emplace_front(allocator, std::move(value));
}
// Pushing front while poping back value for relocatable types
SEQ_ALWAYS_INLINE void push_front_pop_back_use_relocatable(T& inout) noexcept {
typename std::aligned_storage<sizeof(T), alignof(T)>::type tmp;
memcpy(static_cast<void*>(&tmp), static_cast<void*>( &back()), sizeof(T));
if (--begin < 0) begin = max_size1;
memcpy(static_cast<void*>(buffer() + begin), static_cast<void*>( &inout), sizeof(T));
memcpy(static_cast<void*>(&inout), static_cast<void*>(&tmp), sizeof(T));
}
// Pushing front while poping back value for relocatable types, except arithmetic ones
SEQ_ALWAYS_INLINE void push_front_pop_back_relocatable(T& inout)noexcept(std::is_nothrow_move_assignable<T>::value) {
if (std::is_arithmetic<T>::value)
inout = std::move(push_front_pop_back(std::move(inout)));
else
push_front_pop_back_use_relocatable(inout);
}
// Pushing front while poping back value
// Might throw, but leave the buffer in a valid state
SEQ_ALWAYS_INLINE auto push_front_pop_back(T&& value) noexcept(std::is_nothrow_move_assignable<T>::value) -> T {
// Only works for filled array
T res = std::move(back());
if (--begin < 0) begin = max_size1;
buffer()[begin] = std::move(value);
return (res);
}
// Pushing front while poping back for relocatable types
SEQ_ALWAYS_INLINE void push_back_pop_front_use_relocatable(T& inout) noexcept {
typename std::aligned_storage<sizeof(T), alignof(T)>::type tmp;
memcpy(static_cast<void*>(&tmp), static_cast<void*>( &front()), sizeof(T));
begin = (begin + 1) & (max_size1);
memcpy(static_cast<void*>(&(*this)[size - 1]), static_cast<void*>( &inout), sizeof(T));
memcpy(static_cast<void*>(&inout), static_cast<void*>(&tmp), sizeof(T));
}
// Pushing front while poping back for relocatable types, except arithmetic ones
SEQ_ALWAYS_INLINE void push_back_pop_front_relocatable(T& inout) noexcept(std::is_nothrow_move_assignable<T>::value) {
if (std::is_arithmetic<T>::value)
inout = std::move(push_back_pop_front(std::move(inout)));
else
push_back_pop_front_use_relocatable(inout);
}
// Pushing front while poping back
// Might throw, but leave the buffer in a valid state since it is already full
SEQ_ALWAYS_INLINE auto push_back_pop_front(T&& value) noexcept(std::is_nothrow_move_assignable<T>::value) -> T {
// Only works for filled array
T res = std::move(front());
begin = (begin + 1) & (max_size1);
(*this)[size - 1] = std::move(value);
return (res);
}
// Pop back/front
void pop_back(Allocator& /*unused*/) noexcept {
destroy_ptr(&back());
--size;
}
void pop_front(Allocator& /*unused*/) noexcept {
destroy_ptr(buffer() + begin);
begin = (begin + 1) & (max_size1);
--size;
}
// Pop front n values
void pop_front_n(Allocator& allocator, cbuffer_pos n) noexcept {
for (cbuffer_pos i = 0; i != n; ++i)
pop_front(allocator);
}
// Push front n copies of value
template<class U>
void push_front_n(Allocator& allocator, cbuffer_pos n, const U& value) {
for (cbuffer_pos i = 0; i < n; ++i)
push_front(allocator, value);
}
// Push front n values
void push_front_n(Allocator& /*unused*/, cbuffer_pos n) {
try {
for (cbuffer_pos i = 0; i < n; ++i)
{
if (--begin < 0) begin = max_size1;
construct_ptr(buffer() + begin);
++size;
}
}
catch (...) {
begin = (begin + 1) & max_size1;
throw;
}
}
// Move buffer content toward the right by 1 element
// Might throw
void move_right_1(int pos) noexcept(std::is_nothrow_move_assignable<T>::value || relocatable)
{
//starting from pos, move elements toward the end
T* ptr1 = &at(size - 1);
T* stop = &at(pos);
if (stop > ptr1)
stop = buffer();
if (!relocatable) {
while (ptr1 > stop) {
ptr1[0] = std::move(ptr1[-1]);
--ptr1;
}
}
else {
memmove(static_cast<void*>(stop + 1), static_cast<void*>(stop), static_cast<size_t>(ptr1 - stop) * sizeof(T)); ptr1 = stop;
}
if (ptr1 != &at(pos)) {
if (!relocatable)
*ptr1 = std::move(*(buffer() + max_size1));
else
memcpy(static_cast<void*>(ptr1), static_cast<void*>(buffer() + max_size1), sizeof(T));
ptr1 = (buffer() + max_size1);
stop = &at(pos);
if (!relocatable) {
while (ptr1 > stop) {
ptr1[0] = std::move(ptr1[-1]);
--ptr1;
}
}
else {
memmove(static_cast<void*>(stop + 1), static_cast<void*>(stop), static_cast<size_t>(ptr1 - stop) * sizeof(T));
}
}
}
// Move buffer content toward the left by 1 element
// Might throw
void move_left_1(int pos) noexcept(std::is_nothrow_move_assignable<T>::value || relocatable)
{
//starting from pos, move elements toward the beginning
T* ptr1 = &at(0);
//T* ptr2 = ptr1 + 1;
T* stop = buffer() + ((begin + pos - 1) & max_size1);//&at(pos);
if (stop < ptr1)
stop = buffer() + max_size1;
if (!relocatable) {
while (ptr1 < stop) {
*ptr1 = std::move(ptr1[1]);
++ptr1;
}
}
else {
memmove(static_cast<void*>(ptr1), static_cast<void*>(ptr1 + 1), static_cast<size_t>(stop - ptr1) * sizeof(T)); ptr1 = stop;
}
if (ptr1 != buffer() + ((begin + pos - 1) & max_size1)) {
if (!relocatable)
*ptr1 = std::move(*(buffer()));
else
memcpy(static_cast<void*>(ptr1), static_cast<void*>(buffer()), sizeof(T));
ptr1 = buffer();
stop = &at(pos - 1);
if (!relocatable) {
while (ptr1 < stop) {
*ptr1 = std::move(ptr1[1]);
++ptr1;
}
}
else {
memmove(static_cast<void*>(ptr1), static_cast<void*>(ptr1 + 1), static_cast<size_t>(stop - ptr1) * sizeof(T));
}
}
}
// Move buffer content toward the left by 1 element
// Might throw
void move_right(Allocator& /*unused*/, cbuffer_pos pos) noexcept((std::is_nothrow_move_assignable<T>::value&& std::is_nothrow_default_constructible<T>::value) || relocatable)
{
if (!relocatable)
construct_ptr(&(*this)[size]);
//move elems after pos
++size;
// Might throw, fine since no more values are constructed/destructed
move_right_1(pos);
// Non optimized way:
//for (cbuffer_pos i = size - 1; i > pos; --i)
// (*this)[i] = std::move((*this)[i - 1]);
}
void move_left(Allocator& /*unused*/, cbuffer_pos pos) noexcept((std::is_nothrow_move_assignable<T>::value&& std::is_nothrow_default_constructible<T>::value) || relocatable)
{
if (!relocatable)
construct_ptr(&(*this)[begin ? -1 : max_size1]);
//move elems before pos
if (--begin < 0) begin = max_size1;
++size;
// Might throw, fine since no more values are constructed/destructed
move_left_1(pos + 1);
// Non optimized way:
//for (cbuffer_pos i = 0; i < pos -1; ++i)
// (*this)[i] = std::move((*this)[i + 1]);
}
// Insert value at given position. Only works if the buffer is not full.
template <class... Args>
auto emplace(Allocator& allocator, cbuffer_pos pos, Args&&... args) -> T*
{
SEQ_ASSERT_DEBUG(size != max_size_, "cannot insert in a full circular buffer");
if (pos > size / 2) {
move_right(allocator, pos);
}
else {
move_left(allocator, pos);
}
T* res = &(*this)[pos];
if (relocatable) {
try {
construct_ptr(res, std::forward<Args>(args)...);
}
catch (...) {
//No choice but to destroy all values after pos and reduce the size in order to leave the buffer in a valid state
for (int i = pos + 1; i < size; ++i)
destroy_ptr(&(*this)[i]);
size = pos;
throw;
}
}
else {
// For non relocatable types, use move assignment to provide basic exception guarantee
*res = T(std::forward<Args>(args)...);
}
return res;
}
// Insert value at given position. Only works if the buffer is not full.
auto insert(Allocator& allocator, cbuffer_pos pos, const T& value) -> T* {
return emplace(allocator, pos, value);
}
auto insert(Allocator& allocator, cbuffer_pos pos, T&& value) -> T* {
return emplace(allocator, pos, std::move(value));
}
// Insert at given position while poping back
template <class... Args>
auto insert_pop_back(Allocator& /*unused*/, cbuffer_pos pos, Args&&... args) -> T
{
SEQ_ASSERT_DEBUG(pos != max_size(), "invalid insertion position");
//move elems after pos
T* p = &(*this)[size - 1];
// Might throw, fine
T res = std::move(*p);
// For relocatable types, we must destroy the back value
if (relocatable && !std::is_trivially_destructible<T>::value)
destroy_ptr(p);
if (pos > size / 2) {
move_right_1(pos);
}
else {
if (--begin < 0) begin = max_size1;
move_left_1(pos + 1);
}
if (relocatable) {
try {
construct_ptr(&(*this)[pos], std::forward<Args>(args)...);
}
catch (...) {
//No choice but to destroy all values after pos and reduce the size in order to leave the buffer in a valid state
for (int i = pos + 1; i < size; ++i)
destroy_ptr(&(*this)[i]);
size = pos;
throw;
}
}
else {
// For non relocatable types, use move assignment to provide basic exception guarantee
(*this)[pos] = T(std::forward<Args>(args)...);
}
return res;
}
// Insert at given position while poping front
template <class... Args>
auto insert_pop_front(Allocator& /*unused*/, cbuffer_pos pos, Args&&... args) -> T
{
SEQ_ASSERT_DEBUG(pos != 0, "invalid insertion position");
//move elems after pos
T* p = &(*this)[0];
// Might throw, fine
T res = std::move(*p);
if (relocatable && !std::is_trivially_destructible<T>::value)
destroy_ptr(p);
if (pos < size / 2) {
move_left_1(pos);
}
else {
begin = (begin + 1) & (max_size1);
move_right_1(pos - 1);
}
p = &(*this)[pos - 1];
if (relocatable) {
try {
construct_ptr(p, std::forward<Args>(args)...);
}
catch (...) {
//No choice but to destroy all values after pos and reduce the size in order to leave the buffer in a valid state
for (int i = pos; i < size; ++i)
destroy_ptr(&(*this)[i]);
size = pos - 1;
throw;
}
}
else {
// For non relocatable types, use move assignment to provide basic exception guarantee
*p = T(std::forward<Args>(args)...);
}
return res;
}
void move_erase_right_1(int pos) noexcept(std::is_nothrow_move_assignable<T>::value || relocatable)
{
//starting from pos, move elements toward the end
T* ptr1 = &at(pos);
T* stop = &at(size);
if (stop < ptr1)
stop = buffer() + max_size1;
if (!relocatable) {
while (ptr1 < stop) {
*ptr1 = std::move(ptr1[1]); ++ptr1;
}
}
else { memmove(static_cast<void*>(ptr1), static_cast<void*>(ptr1 + 1), static_cast<size_t>(stop - ptr1) * sizeof(T)); ptr1 = stop; }
if (ptr1 != &at(size)) {