-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.hpp
868 lines (723 loc) · 23.6 KB
/
array.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
#ifndef CA_ARRAY_HPP
# define CA_ARRAY_HPP
# pragma once
#include <cstdint> // PTRDIFF_MAX
#include <algorithm> // std::move()
#include <compare> // std::three_way_comparable
#include <execution> // std::execution
#include <initializer_list> // std::initializer_list
#include <ranges>
#include "arrayiterator.hpp"
namespace ca
{
struct from_range_t { explicit from_range_t() = default; };
inline constexpr from_range_t from_range{};
struct multi_t { explicit multi_t() = default; };
inline constexpr multi_t multi{};
template <typename T, std::size_t CAP, enum Method M = MEMBER,
auto E = std::execution::unseq>
requires(
!std::is_reference_v<T> &&
!std::is_const_v<T> &&
std::is_default_constructible_v<T> &&
(CAP > 0) && (CAP < PTRDIFF_MAX) &&
(std::is_copy_assignable_v<T> || std::is_move_assignable_v<T>)
) // N = CAP + 1 <= PTRDIFF_MAX
class array
{
friend class arrayiterator<T, array>;
friend class arrayiterator<T const, array>;
public:
using value_type = T;
using difference_type = std::ptrdiff_t;
using size_type = std::size_t;
using reference = value_type&;
using const_reference = value_type const&;
using iterator = arrayiterator<T, array>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_iterator = arrayiterator<T const, array>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
enum {ca_array_tag};
//private:
enum : size_type { N = CAP + 1 };
T* f_, *l_; // pointer to first and last elements of element array
std::conditional_t<MEMBER == M, T[N], T*> a_; // element array
constexpr auto next_(auto const p) const noexcept
{
return const_cast<decltype(p)>(p == &a_[N - 1] ? a_ : p + 1);
}
constexpr auto prev_(auto const p) const noexcept
{
return const_cast<decltype(p)>(p == a_ ? &a_[N - 1] : p - 1);
}
constexpr auto next_(auto const p, difference_type const n) const noexcept
{ // 0 <= n < N, N is the period
auto const u(N - n);
return const_cast<decltype(p)>(size_type(p - a_) < u ? p + n : p - u);
}
constexpr auto prev_(auto const p, difference_type const n) const noexcept
{ // 0 <= n < N
return const_cast<decltype(p)>(p - a_ < n ? p + (N - n) : p - n);
}
constexpr auto adv_(auto const p, difference_type const n) const noexcept
{ // -N < n < N
if (size_type const o(p - a_); difference_type(o) < -n)
{
return const_cast<decltype(p)>(p + (N + n));
}
else
{
auto const u(N - n);
return const_cast<decltype(p)>(o < u ? p + n : p - u);
}
}
static constexpr size_type distance_(auto const a, decltype(a) b) noexcept
{
auto const n(b - a); // N = CAP + 1 <= PTRDIFF_MAX
return n < decltype(n){} ? N + n : n;
}
public:
constexpr array()
noexcept(std::is_nothrow_default_constructible_v<T[N]>)
requires(MEMBER == M)
{
if (std::is_constant_evaluated())
{
[&]<auto ...I>(std::index_sequence<I...>)
noexcept(std::is_nothrow_move_assignable_v<T>)
{
((a_[I] = T()), ...);
}(std::make_index_sequence<N>());
}
f_ = l_ = a_;
}
constexpr array() noexcept(noexcept(new T[N])) requires(NEW == M)
{
f_ = l_ = a_ = new T[N];
}
constexpr explicit array(T* const a) noexcept requires(USER == M)
{
f_ = l_ = a_ = a;
}
constexpr array(array const& o)
noexcept(noexcept(array(o.begin(), o.end())))
requires((USER != M) && std::is_copy_assignable_v<value_type>):
array(o.begin(), o.end())
{
}
constexpr array(array const& o) requires(USER == M) = delete;
constexpr array(array&& o)
noexcept(noexcept(o.clear(),
std::move(E, o.begin(), o.end(), std::back_inserter(*this))))
requires(MEMBER == M):
array()
{
std::move(E, o.begin(), o.end(), std::back_inserter(*this));
o.clear();
}
constexpr array(array&& o) noexcept requires(NEW == M):
array()
{ // swap & o.reset()
detail::assign(f_, l_, a_, o.f_, o.l_, o.a_)
(o.f_, o.l_, o.a_, a_, a_, a_);
}
constexpr array(array&& o) noexcept requires(USER == M)
{
f_ = o.f_; l_ = o.l_; a_ = o.a_; // o.a_ stays unchanged
o.clear();
}
constexpr array(multi_t, auto&& ...a)
noexcept(noexcept(push_back(std::forward<decltype(a)>(a)...)))
requires(USER != M):
array()
{
push_back(std::forward<decltype(a)>(a)...);
}
constexpr array(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(std::copy(E, i, j, std::back_inserter(*this))))
requires(USER != M):
array()
{
std::copy(E, i, j, std::back_inserter(*this));
}
constexpr array(std::initializer_list<value_type> l)
noexcept(noexcept(array(l.begin(), l.end())))
requires(USER != M):
array(l.begin(), l.end())
{
}
constexpr explicit array(size_type const c)
noexcept(noexcept(array(), resize(c)))
requires(USER != M):
array()
{
resize(c);
}
constexpr explicit array(size_type const c, auto const& v, int = 0)
noexcept(noexcept(array(c), std::fill(E, f_, l_, v)))
requires((USER != M) && std::is_assignable_v<value_type&, decltype(v)>):
array(c)
{
std::fill(E, f_, l_, v);
}
constexpr explicit array(size_type const c, value_type const v)
noexcept(noexcept(array(c, v, 0)))
requires(USER != M):
array(c, v, 0)
{
}
constexpr array(from_range_t, std::ranges::input_range auto&& rg)
noexcept(noexcept(array(std::ranges::begin(rg), std::ranges::end(rg))))
requires(USER != M):
array(std::ranges::begin(rg), std::ranges::end(rg))
{
}
constexpr ~array() requires(NEW != M) = default;
constexpr ~array() noexcept(noexcept(delete [] a_)) requires(NEW == M)
{
delete [] a_;
}
//
constexpr array& operator=(array const& o)
noexcept(noexcept(
std::copy(E, o.begin(), o.end(), std::back_inserter(*this))))
requires(std::is_copy_assignable_v<value_type>)
{ // self-assign neglected
clear();
std::copy(E, o.begin(), o.end(), std::back_inserter(*this));
return *this;
}
constexpr array& operator=(array&& o)
noexcept(noexcept(o.clear(),
std::move(E, o.begin(), o.end(), std::back_inserter(*this))))
requires(MEMBER == M)
{
clear();
std::move(E, o.begin(), o.end(), std::back_inserter(*this));
o.clear();
return *this;
}
constexpr array& operator=(array&& o) noexcept requires(NEW == M)
{ // swap & o.reset()
detail::assign(f_, l_, a_, o.f_, o.l_, o.a_)
(o.f_, o.l_, o.a_, a_, a_, a_);
return *this;
}
constexpr array& operator=(array&& o) noexcept requires(USER == M)
{ // o.a_ stays unchanged
f_ = o.f_; l_ = o.l_; a_ = o.a_; o.clear(); return *this;
}
constexpr array& operator=(std::initializer_list<value_type> l)
noexcept(noexcept(assign(l)))
{
assign(l); return *this;
}
constexpr array& operator=(std::ranges::input_range auto&& rg)
noexcept(noexcept(assign_range(std::forward<decltype(rg)>(rg))))
{
assign_range(std::forward<decltype(rg)>(rg)); return *this;
}
//
constexpr T* data() noexcept { return a_; }
constexpr T const* data() const noexcept { return a_; }
constexpr T* first() noexcept { return f_; }
constexpr T const* first() const noexcept { return f_; }
constexpr T* last() noexcept { return l_; }
constexpr T const* last() const noexcept { return l_; }
// iterators
constexpr iterator begin() noexcept { return {this, f_}; }
constexpr iterator end() noexcept { return {this, l_}; }
constexpr const_iterator begin() const noexcept { return {this, f_}; }
constexpr const_iterator end() const noexcept { return {this, l_}; }
constexpr auto cbegin() const noexcept { return begin(); }
constexpr auto cend() const noexcept { return end(); }
// reverse iterators
constexpr reverse_iterator rbegin() noexcept
{
return reverse_iterator{iterator(this, l_)};
}
constexpr reverse_iterator rend() noexcept
{
return reverse_iterator{iterator(this, f_)};
}
constexpr const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator{const_iterator(this, l_)};
}
constexpr const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator{const_iterator{this, f_}};
}
constexpr auto crbegin() const noexcept { return rbegin(); }
constexpr auto crend() const noexcept { return rend(); }
// N = CAP + 1 <= PTRDIFF_MAX
static constexpr size_type capacity() noexcept { return CAP; }
static constexpr size_type max_size() noexcept { return PTRDIFF_MAX; }
//
constexpr bool empty() const noexcept { return f_ == l_; }
constexpr bool full() const noexcept { return next_(l_) == f_; }
constexpr size_type size() const noexcept { return distance_(f_, l_); }
//
constexpr auto& operator[](size_type const i) noexcept
{
return *next_(f_, i);
}
constexpr auto const& operator[](size_type const i) const noexcept
{
return *next_(f_, i);
}
constexpr auto& at(size_type const i) noexcept { return (*this)[i]; }
constexpr auto& at(size_type const i) const noexcept { return (*this)[i]; }
constexpr auto& back() noexcept { return *prev_(l_); }
constexpr auto const& back() const noexcept { return *prev_(l_); }
constexpr auto& front() noexcept { return *f_; }
constexpr auto const& front() const noexcept { return *f_; }
//
template <int = 0>
constexpr void assign(size_type count, auto const& v)
noexcept(noexcept(clear(), push_back(v)))
requires(std::is_constructible_v<value_type, decltype(v)>)
{
clear(); while (count--) push_back(v);
}
constexpr void assign(size_type const count, value_type const v)
noexcept(noexcept(assign<0>(count, v)))
{
assign<0>(count, v);
}
constexpr void assign(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(clear(),
std::copy(E, i, j, std::back_inserter(*this))))
{
clear();
std::copy(E, i, j, std::back_inserter(*this));
}
constexpr void assign(std::initializer_list<value_type> l)
noexcept(noexcept(assign(l.begin(), l.end())))
{
assign(l.begin(), l.end());
}
constexpr void assign_range(std::ranges::input_range auto&& rg)
noexcept(noexcept(assign(std::ranges::begin(rg), std::ranges::end(rg))))
{
assign(std::ranges::begin(rg), std::ranges::end(rg));
}
//
constexpr void clear() noexcept { l_ = f_; }
constexpr void reset() noexcept { l_ = f_ = a_; }
constexpr void resize(size_type const c) noexcept { l_ = next_(f_, c); }
template <int = 0>
constexpr void resize(size_type const c, auto const& a)
noexcept(noexcept(push_back(a)))
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
for (auto sz(size()); c > sz; ++sz, push_back(a));
resize(c);
}
constexpr void resize(size_type const c, value_type const a)
noexcept(noexcept(resize<0>(c, a)))
{
resize<0>(c, a);
}
// emplacing is a bad idea in this container, avoid if possible
constexpr void emplace_back(auto&& a)
noexcept(noexcept(push_back(std::forward<decltype(a)>(a))))
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
push_back(std::forward<decltype(a)>(a));
}
constexpr void emplace_back(auto&& ...a)
noexcept(noexcept(push_back(std::declval<T>())))
requires(std::is_constructible_v<T, decltype(a)...>)
{
push_back(T(std::forward<decltype(a)>(a)...));
}
constexpr void emplace_back(value_type a)
noexcept(noexcept(push_back(std::move(a))))
{
push_back(std::move(a));
}
constexpr void emplace_front(auto&& a)
noexcept(noexcept(push_front(std::forward<decltype(a)>(a))))
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
push_front(std::forward<decltype(a)>(a));
}
constexpr void emplace_front(auto&& ...a)
noexcept(noexcept(push_front(std::declval<T>())))
requires(std::is_constructible_v<T, decltype(a)...>)
{
push_front(T(std::forward<decltype(a)>(a)...));
}
constexpr void emplace_front(value_type a)
noexcept(noexcept(push_front(std::move(a))))
{
push_front(std::move(a));
}
constexpr auto emplace(const_iterator const i, auto&& a)
noexcept(noexcept(insert(i, std::forward<decltype(a)>(a))))
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
return insert(i, std::forward<decltype(a)>(a));
}
constexpr auto emplace(const_iterator const i, auto&& ...a)
noexcept(noexcept(insert(i, std::declval<T>())))
requires(std::is_constructible_v<T, decltype(a)...>)
{
return insert(i, T(std::forward<decltype(a)>(a)...));
}
constexpr auto emplace(const_iterator const i, value_type a)
noexcept(noexcept(insert(i, std::move(a))))
{
return insert(i, std::move(a));
}
//
constexpr iterator erase(const_iterator const i)
noexcept(noexcept(std::move(E, i, i, i)))
{
iterator const ii{this, i.n_}, jj{this, next_(i.n_)};
return distance_(f_, ii.n_) <= distance_(jj.n_, l_) ?
//f_ = std::move_backward(begin(), ii, jj).n_, jj:
f_ = std::move(E, reverse_iterator(ii), rend(),
reverse_iterator(jj)).base().n_, jj:
(l_ = std::move(E, jj, end(), ii).n_, ii);
}
constexpr iterator erase(const_iterator const i, const_iterator const j)
noexcept(noexcept(std::move(E, i, i, i)))
{
if (iterator const ii{this, i.n_}; i == j) [[unlikely]]
{
return ii;
}
else [[likely]]
{
decltype(ii) jj{this, j.n_};
return distance_(f_, ii.n_) <= distance_(jj.n_, l_) ?
f_ = std::move(E, reverse_iterator(ii), rend(),
reverse_iterator(jj)).base().n_, jj:
(l_ = std::move(E, jj, end(), ii).n_, ii);
}
}
//
template <int = 0>
constexpr iterator insert(const_iterator const i, auto&& a)
noexcept(noexcept(std::move(E, i, i, i)))
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
if (full()) [[unlikely]] pop_front();
//
iterator j{this};
if (distance_(f_, i.n_) <= distance_(i.n_, l_))
{ // [f, i) is moved backwards
auto const f(f_); f_ = prev_(f);
j.n_ = std::move(E, {this, f}, i, begin()).n_;
}
else
{ // [j, l) is moved forwards
j.n_ = i.n_; auto const l(l_); l_ = next_(l);
//std::move_backward(j, {this, l}, end());
std::move(E,
const_reverse_iterator(const_iterator{this, l}),
const_reverse_iterator(i), rbegin());
}
//
*j = std::forward<decltype(a)>(a); return j;
}
constexpr auto insert(const_iterator const i, value_type v)
noexcept(noexcept(insert<0>(i, std::move(v))))
{
return insert<0>(i, std::move(v));
}
template <int = 0>
constexpr iterator insert(multi_t, const_iterator i, auto&& ...a)
noexcept(noexcept((insert(i, std::forward<decltype(a)>(a)), ...)))
requires(requires{(insert(i, std::forward<decltype(a)>(a)), ...);})
{
(++(i = insert(i, std::forward<decltype(a)>(a))), ...);
return {this, prev(i.n_, sizeof...(a))};
}
constexpr auto insert(multi_t, const_iterator const i, value_type a)
noexcept(noexcept(insert<0>(multi_t{}, i, std::move(a))))
{
return insert<0>(multi, i, std::move(a));
}
template <int = 0>
constexpr iterator insert(const_iterator i, size_type const count,
auto const& v)
noexcept(noexcept(insert(i, v)))
requires(std::is_assignable_v<value_type&, decltype(v)>)
{
for (auto n(count); n; --n, ++(i = insert(i, v)));
return {this, prev(i.n_, count)};
}
constexpr auto insert(const_iterator const i, size_type const count,
value_type const v)
noexcept(noexcept(insert<0>(i, count, v)))
{
return insert<0>(i, count, v);
}
constexpr iterator insert(const_iterator i,
std::input_iterator auto const j, decltype(j) k)
noexcept(noexcept(insert(i, *j)))
{
size_type n{};
std::for_each(
j,
k,
[&](auto&& v) noexcept(noexcept(
insert(i, std::forward<decltype(v)>(v))))
{
++n, ++(i = insert(i, std::forward<decltype(v)>(v)));
}
);
return {this, prev_(i.n_, n)};
}
constexpr auto insert(const_iterator const i,
std::initializer_list<value_type> l)
noexcept(noexcept(insert(i, l.begin(), l.end())))
{
return insert(i, l.begin(), l.end());
}
//
constexpr auto insert_range(const_iterator const pos,
std::ranges::input_range auto&& rg)
noexcept(noexcept(insert(pos, std::ranges::begin(rg),
std::ranges::end(rg))))
{
return insert(pos, std::ranges::begin(rg), std::ranges::end(rg));
}
constexpr void append_range(std::ranges::input_range auto&& rg)
noexcept(noexcept(std::copy(E, std::ranges::begin(rg),
std::ranges::end(rg), std::back_inserter(*this))))
{
std::copy(E, std::ranges::begin(rg), std::ranges::end(rg),
std::back_inserter(*this));
}
constexpr void prepend_range(std::ranges::input_range auto&& rg)
noexcept(noexcept(std::copy(E, std::ranges::rbegin(rg),
std::ranges::rend(rg), std::front_inserter(*this))))
{
std::copy(E, std::ranges::rbegin(rg),
std::ranges::rend(rg), std::front_inserter(*this));
}
//
constexpr void pop_back() noexcept { l_ = prev_(l_); }
constexpr void pop_back(size_type const n) noexcept { l_ = prev_(l_, n); }
constexpr void pop_front() noexcept { f_ = next_(f_); }
constexpr void pop_front(size_type const n) noexcept { f_ = next_(f_, n); }
//
template <int = 0>
constexpr void push_back(auto&& a)
noexcept(std::is_nothrow_assignable_v<value_type&, decltype(a)>)
requires(std::is_assignable_v<value_type&, decltype(a)>)
{
*l_ = std::forward<decltype(a)>(a);
if ((l_ = next_(l_)) == f_) [[unlikely]] pop_front();
}
constexpr void push_back(auto&& ...a)
noexcept(noexcept((push_back<0>(std::forward<decltype(a)>(a)), ...)))
requires(requires{(push_back<0>(std::forward<decltype(a)>(a)), ...);})
{
(push_back<0>(std::forward<decltype(a)>(a)), ...);
}
constexpr void push_back(value_type a)
noexcept(noexcept(push_back<0>(std::move(a))))
{
push_back<0>(std::move(a));
}
template <int = 0>
constexpr void push_front(auto&& a)
noexcept(std::is_nothrow_assignable_v<value_type&, decltype(a)>)
requires(std::is_assignable_v<value_type&, decltype(a)>)
{ // pop_front() + push_front() = overwrite_front()
*(full() ? f_ : f_ = prev_(f_)) = std::forward<decltype(a)>(a);
}
constexpr void push_front(auto&& ...a)
noexcept(noexcept((push_front<0>(std::forward<decltype(a)>(a)), ...)))
requires(requires{(push_front<0>(std::forward<decltype(a)>(a)), ...);})
{
(push_front<0>(std::forward<decltype(a)>(a)), ...);
}
constexpr void push_front(value_type a)
noexcept(noexcept(push_front<0>(std::move(a))))
{
push_front<0>(std::move(a));
}
//
constexpr void swap(array& o)
noexcept(noexcept(std::swap(*this, o)))
requires(MEMBER == M)
{
std::swap(*this, o);
}
constexpr void swap(array& o) noexcept
requires((NEW == M) || (USER == M))
{ // swap state
detail::assign(f_, l_, a_, o.f_, o.l_, o.a_)
(o.f_, o.l_, o.a_, f_, l_, a_);
}
//
constexpr auto append(T const* const p, size_type cnt) noexcept
{ // appends to container from a memory region
cnt = std::min(cnt, capacity() - size());
auto const nc(std::min(size_type(
f_ <= l_ ? &a_[N] - l_ : f_ - l_ - 1), cnt));
std::copy_n(E, p, nc, l_);
std::copy_n(E, p, cnt - nc, a_);
l_ = next_(l_, cnt);
return cnt;
}
constexpr std::array<std::array<T*, 2>, 2> split() noexcept
{
using pair_t = decltype(split())::value_type;
if (auto const c(f_ <=> l_); c < 0) // f_ > l_ >= a_, f_ > a_
{
return {pair_t{f_, l_}};
}
else if (c > 0)
{
return {pair_t{f_, (T*)(&a_[N])}, pair_t{(T*)(a_), l_}};
}
else [[unlikely]]
{
return {};
}
}
constexpr std::array<std::array<T const*, 2>, 2> split() const noexcept
{
using pair_t = decltype(split())::value_type;
if (auto const c(f_ <=> l_); c < 0) // f_ > l_ >= a_, f_ > a_
{
return {pair_t{f_, l_}};
}
else if (c > 0)
{
return {pair_t{f_, (T const*)(&a_[N])}, pair_t{(T const*)(a_), l_}};
}
else [[unlikely]]
{
return {};
}
}
auto csplit() const noexcept { return split(); }
};
//////////////////////////////////////////////////////////////////////////////
template <typename T, auto S, auto M, auto E>
constexpr auto erase_if(array<T, S, M, E>& c, auto&& pred)
noexcept(noexcept(c.erase(c.cbegin()), pred(*c.cbegin())))
{
typename std::remove_reference_t<decltype(c)>::size_type r{};
for (auto&& [i, j]: c.csplit())
{
if (!i) break;
for (; (i != j) && (i != c.last());
pred(*i) ? ++r, i = std::addressof(*c.erase({&c, i})) : ++i);
if (c.first() <= c.last()) break;
}
return r;
}
template <int = 0, typename T, auto S, auto M, auto E>
constexpr auto erase(array<T, S, M, E>& c, auto const& ...k)
noexcept(noexcept(c.erase({}), ((*c.cbegin() == k), ...)))
requires(requires{((*c.cbegin() == k), ...);})
{
return erase_if(
c,
[&k...](auto& a) noexcept(noexcept(((a == k), ...)))
{
return ((a == k) || ...);
}
);
}
template <typename T, auto S, auto M, auto E>
constexpr auto erase(array<T, S, M, E>& c, T const k)
noexcept(noexcept(erase<0>(c, k)))
{
return erase<0>(c, k);
}
constexpr auto find_if(auto&& c, auto pred)
noexcept(noexcept(pred(*c.cbegin()))) ->
decltype(c.end())
requires(requires{std::remove_cvref_t<decltype(c)>::ca_array_tag;})
{
for (auto [i, j]: c.split())
{
if (!i) break;
for (; i < --j; ++i)
if (pred(std::as_const(*i))) return {&c, i};
else if (pred(std::as_const(*j))) return {&c, j};
if ((i == j) && pred(std::as_const(*i))) return {&c, i}; // !!!
}
return c.end();
}
template <int = 0>
constexpr auto find(auto&& c, auto const& ...k)
noexcept(noexcept(((*c.cbegin() == k), ...)))
requires(requires{((*c.cbegin() == k), ...);})
{
return find_if(
std::forward<decltype(c)>(c),
[&k...](auto const& a) noexcept(noexcept(((a == k), ...)))
{
return ((a == k) || ...);
}
);
}
template <typename T, auto S, auto M, auto E>
constexpr auto find(array<T, S, M, E>& c, T const k)
noexcept(noexcept(find<0>(c, k)))
{
return find<0>(c, k);
}
template <typename T, auto S, auto M, auto E>
constexpr auto find(array<T, S, M, E> const& c, T const k)
noexcept(noexcept(find<0>(c, k)))
{
return find<0>(c, k);
}
//////////////////////////////////////////////////////////////////////////////
template <typename T1, auto S1, auto M1, auto E1,
typename T2, auto S2, auto M2, auto E2>
constexpr bool operator==(array<T1, S1, M1, E1> const& l,
array<T2, S2, M2, E2> const& r)
noexcept(noexcept(std::equal(l.begin(), l.end(), r.begin(), r.end())))
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <typename T1, auto S1, auto M1, auto E1,
typename T2, auto S2, auto M2, auto E2>
constexpr auto operator<=>(array<T1, S1, M1, E1> const& l,
array<T2, S2, M2, E2> const& r)
noexcept(noexcept(std::lexicographical_compare_three_way(
l.begin(), l.end(), r.begin(), r.end())))
{
return std::lexicographical_compare_three_way(
l.begin(), l.end(), r.begin(), r.end());
}
template <auto EX = std::execution::unseq, typename T, auto S, auto M, auto E>
constexpr void copy(array<T, S, M, E> const& a, T* p) noexcept
{ // copies from container to a memory region
for (auto const [i, j]: a.split()) // !!!
{
if (!i) break;
std::copy(EX, i, j, p);
p += j - i;
}
}
template <auto EX = std::execution::unseq, typename T, auto S, auto M, auto E>
constexpr void copy(array<T, S, M, E> const& a, T* p,
typename array<T, S, M, E>::size_type sz) noexcept
{ // copies from container to a memory region
for (auto const [i, j]: a.split()) // !!!
{
if (!i) break;
auto const nc(std::min(decltype(sz)(j - i), sz));
sz -= nc;
std::copy_n(EX, i, nc, p);
p += nc;
}
}
template <typename T, auto S, auto M>
constexpr void swap(array<T, S, M>& l, decltype(l) r) noexcept { l.swap(r); }
}
#endif // CA_ARRAY_HPP