-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray.hh
831 lines (697 loc) · 24 KB
/
array.hh
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
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Aggregate array (always initialized)
// Value-initialized by default (an `uninitialized_array` type can be added in the future if
// needed).
// With minimal internal dependencies.
#pragma once
#include "snn-core/array_view.fwd.hh"
#include "snn-core/optional.fwd.hh"
#include <algorithm> // is_sorted, reverse, sort
#include <functional> // less<void>
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
namespace snn
{
namespace detail
{
template <typename Int, usize Count>
constexpr bool can_overflow_count() noexcept
{
return constant::limit<Int>::min < Int{} || constant::limit<Int>::max >= Count;
}
}
// ## Classes
// ### array
template <typename T, usize Count>
requires(std::is_same_v<std::remove_cvref_t<T>, T>)
struct array final
{
// #### Types
using value_type = T;
using iterator = T*;
using const_iterator = const T*;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using trivially_relocatable_type =
std::conditional_t<is_trivially_relocatable_v<T>, array, void>;
// #### "Private" buffer
// Should never be accessed directly.
T priv_buf_[Count > 0 ? Count : 1]{};
// #### Explicit conversion operators
constexpr explicit operator bool() const noexcept
{
return !is_empty();
}
// #### Iterators
[[nodiscard]] constexpr iterator begin() noexcept
{
return priv_buf_;
}
[[nodiscard]] constexpr const_iterator begin() const noexcept
{
return priv_buf_;
}
[[nodiscard]] constexpr iterator end() noexcept
{
return priv_buf_ + Count;
}
[[nodiscard]] constexpr const_iterator end() const noexcept
{
return priv_buf_ + Count;
}
// #### Count/Size
[[nodiscard]] static constexpr snn::byte_size byte_size() noexcept
{
return snn::byte_size{sizeof(T) * Count};
}
[[nodiscard]] static constexpr usize count() noexcept
{
return Count;
}
[[nodiscard]] static constexpr bool is_empty() noexcept
{
return Count == 0;
}
[[nodiscard]] static constexpr usize size() noexcept
requires(sizeof(T) == 1) // Use `count()` or `byte_size()` instead.
{
return Count;
}
// #### Capacity
[[nodiscard]] static constexpr usize capacity() noexcept
{
return Count;
}
// #### Data
[[nodiscard]] constexpr not_null<const_pointer> data() const noexcept
{
return not_null<const_pointer>{priv_buf_};
}
[[nodiscard]] constexpr not_null<pointer> writable() noexcept
{
return not_null<pointer>{priv_buf_};
}
// #### Single element access
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> at(const usize pos)
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (pos < Count)
{
return priv_buf_[pos];
}
return optional<R>::default_error_code();
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> at(const usize pos) const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if (pos < Count)
{
return priv_buf_[pos];
}
return optional<R>::default_error_code();
}
[[nodiscard]] constexpr reference at(const strict_integral auto pos,
promise::within_bounds_t) noexcept
{
if constexpr (detail::can_overflow_count<decltype(pos), Count>())
{
snn_assert(pos >= 0 && force_unsigned(pos) < Count);
}
return priv_buf_[pos];
}
[[nodiscard]] constexpr const_reference at(const strict_integral auto pos,
promise::within_bounds_t) const noexcept
{
if constexpr (detail::can_overflow_count<decltype(pos), Count>())
{
snn_assert(pos >= 0 && force_unsigned(pos) < Count);
}
return priv_buf_[pos];
}
[[nodiscard]] constexpr reference at(const strict_integral auto pos,
bounds::mask_t) noexcept
requires(power_of_two<Count>)
{
return priv_buf_[force_unsigned(pos) & (Count - 1u)];
}
[[nodiscard]] constexpr const_reference at(const strict_integral auto pos,
bounds::mask_t) const noexcept
requires(power_of_two<Count>)
{
return priv_buf_[force_unsigned(pos) & (Count - 1u)];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> back() noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if constexpr (Count > 0)
{
return priv_buf_[Count - 1];
}
return optional<R>::default_error_code();
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> back() const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if constexpr (Count > 0)
{
return priv_buf_[Count - 1];
}
return optional<R>::default_error_code();
}
[[nodiscard]] constexpr reference back(promise::not_empty_t) noexcept
requires(Count > 0)
{
return priv_buf_[Count - 1];
}
[[nodiscard]] constexpr const_reference back(promise::not_empty_t) const noexcept
requires(Count > 0)
{
return priv_buf_[Count - 1];
}
template <value_type_or<reference> R = reference>
[[nodiscard]] constexpr optional<R> front()
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if constexpr (Count > 0)
{
return priv_buf_[0];
}
return optional<R>::default_error_code();
}
template <value_type_or<const_reference> R = const_reference>
[[nodiscard]] constexpr optional<R> front() const
noexcept(std::is_nothrow_copy_constructible_v<R>)
{
if constexpr (Count > 0)
{
return priv_buf_[0];
}
return optional<R>::default_error_code();
}
[[nodiscard]] constexpr reference front(promise::not_empty_t) noexcept
requires(Count > 0)
{
return priv_buf_[0];
}
[[nodiscard]] constexpr const_reference front(promise::not_empty_t) const noexcept
requires(Count > 0)
{
return priv_buf_[0];
}
template <usize Index>
requires(Index < Count)
[[nodiscard]] constexpr reference get() noexcept
{
return priv_buf_[Index];
}
template <usize Index>
requires(Index < Count)
[[nodiscard]] constexpr const_reference get() const noexcept
{
return priv_buf_[Index];
}
// #### View - Dynamic count
[[nodiscard]] constexpr auto view() noexcept
{
return array_view<T>{not_null{priv_buf_}, Count};
}
[[nodiscard]] constexpr auto view() const noexcept
{
return array_view<const T>{not_null{priv_buf_}, Count};
}
[[nodiscard]] constexpr auto view(const usize pos,
const usize count = constant::npos) noexcept
{
return view().view(pos, count);
}
[[nodiscard]] constexpr auto view(const usize pos,
const usize count = constant::npos) const noexcept
{
return view().view(pos, count);
}
// #### View - Static count
template <usize Pos = 0, usize C = constant::npos>
[[nodiscard]] constexpr auto view() noexcept
{
return array_view<T, Count>{priv_buf_, promise::has_capacity}.template view<Pos, C>();
}
template <usize Pos = 0, usize C = constant::npos>
[[nodiscard]] constexpr auto view() const noexcept
{
return array_view<const T, Count>{priv_buf_, promise::has_capacity}
.template view<Pos, C>();
}
// #### View exactly
template <usize Pos, usize C>
requires(Pos <= Count && (Count - Pos) >= C)
[[nodiscard]] constexpr auto view_exactly() noexcept
{
return array_view<T, C>{priv_buf_ + Pos, promise::has_capacity};
}
template <usize Pos, usize C>
requires(Pos <= Count && (Count - Pos) >= C)
[[nodiscard]] constexpr auto view_exactly() const noexcept
{
return array_view<const T, C>{priv_buf_ + Pos, promise::has_capacity};
}
template <usize Pos, usize C>
[[nodiscard]] constexpr auto view_exactly(promise::within_bounds_t) noexcept
{
static_assert(Pos <= Count && (Count - Pos) >= C);
return array_view<T, C>{priv_buf_ + Pos, promise::has_capacity};
}
template <usize Pos, usize C>
[[nodiscard]] constexpr auto view_exactly(promise::within_bounds_t) const noexcept
{
static_assert(Pos <= Count && (Count - Pos) >= C);
return array_view<const T, C>{priv_buf_ + Pos, promise::has_capacity};
}
// #### View offset - Dynamic count
[[nodiscard]] constexpr auto view_offset(
const isize pos, const isize count = constant::limit<isize>::max) noexcept
{
return view().view_offset(pos, count);
}
[[nodiscard]] constexpr auto view_offset(
const isize pos, const isize count = constant::limit<isize>::max) const noexcept
{
return view().view_offset(pos, count);
}
// #### View offset - Static count
template <isize Pos, isize C = constant::limit<isize>::max>
[[nodiscard]] constexpr auto view_offset() noexcept
{
return array_view<T, Count>{priv_buf_, promise::has_capacity}
.template view_offset<Pos, C>();
}
template <isize Pos, isize C = constant::limit<isize>::max>
[[nodiscard]] constexpr auto view_offset() const noexcept
{
return array_view<const T, Count>{priv_buf_, promise::has_capacity}
.template view_offset<Pos, C>();
}
// #### Range
[[nodiscard]] constexpr auto range() noexcept
{
return view().range();
}
[[nodiscard]] constexpr auto range() const noexcept
{
return view().range();
}
// #### Operations
template <typename V>
constexpr void fill(const V& value)
{
for (usize i = 0; i < Count; ++i)
{
priv_buf_[i] = value;
}
}
template <typename V, usize C>
constexpr usize fill_front(const array<V, C>& values)
{
constexpr usize fill_count = Count < C ? Count : C;
for (usize i = 0; i < fill_count; ++i)
{
priv_buf_[i] = values.priv_buf_[i];
}
return fill_count;
}
template <typename V, usize C>
constexpr usize fill_front(const array_view<V, C> values)
{
const usize fill_count = Count < values.count() ? Count : values.count();
for (usize i = 0; i < fill_count; ++i)
{
priv_buf_[i] = values.begin()[i];
}
return fill_count;
}
// #### Search
template <typename V>
[[nodiscard]] constexpr bool contains(const V& needle) const
{
for (usize i = 0; i < Count; ++i)
{
if (priv_buf_[i] == needle)
{
return true;
}
}
return false;
}
template <typename V>
[[nodiscard]] constexpr usize count(const V& needle) const
{
usize needle_count = 0;
for (usize i = 0; i < Count; ++i)
{
if (priv_buf_[i] == needle)
{
++needle_count;
}
}
return needle_count;
}
template <typename OneArgPred>
[[nodiscard]] constexpr usize count_if(OneArgPred p) const
{
usize pred_count = 0;
for (usize i = 0; i < Count; ++i)
{
if (p(std::as_const(priv_buf_[i])))
{
++pred_count;
}
}
return pred_count;
}
template <typename V>
constexpr auto find(V&& value, const usize start_pos = 0) const
{
return view<>().find(std::forward<V>(value), start_pos);
}
template <typename OneArgPred>
constexpr auto find_if(OneArgPred&& p, const usize start_pos = 0) const
{
return view<>().find_if(std::forward<OneArgPred>(p), start_pos);
}
template <typename V>
constexpr auto find_in_reverse(V&& value, const usize start_pos = constant::npos) const
{
return view<>().find_in_reverse(std::forward<V>(value), start_pos);
}
template <typename OneArgPred>
constexpr auto find_in_reverse_if(OneArgPred&& p,
const usize start_pos = constant::npos) const
{
return view<>().find_in_reverse_if(std::forward<OneArgPred>(p), start_pos);
}
// #### Swap
constexpr void swap(array& other) noexcept(Count == 0 || std::is_nothrow_swappable_v<T>)
{
if (this != &other)
{
for (usize i = 0; i < Count; ++i)
{
using std::swap;
swap(priv_buf_[i], other.priv_buf_[i]);
}
}
}
// #### Comparison
template <typename V>
[[nodiscard]] constexpr bool has_front(const V& value) const
{
return Count > 0 && priv_buf_[0] == value;
}
template <typename V>
[[nodiscard]] constexpr bool has_back(const V& value) const
{
return Count > 0 && priv_buf_[Count - 1] == value;
}
// #### Algorithm helpers
template <typename OneArgFn>
constexpr void each(OneArgFn f)
{
for (usize i = 0; i < Count; ++i)
{
f(priv_buf_[i]);
}
}
template <typename OneArgFn>
constexpr void each(OneArgFn f) const
{
for (usize i = 0; i < Count; ++i)
{
f(std::as_const(priv_buf_[i]));
}
}
template <typename OneArgFn>
constexpr void each_in_reverse(OneArgFn f)
{
usize i = Count;
while (i > 0)
{
--i;
f(priv_buf_[i]);
}
}
template <typename OneArgFn>
constexpr void each_in_reverse(OneArgFn f) const
{
usize i = Count;
while (i > 0)
{
--i;
f(std::as_const(priv_buf_[i]));
}
}
template <typename TwoArgPred = std::less<void>>
[[nodiscard]] constexpr bool is_sorted(TwoArgPred is_less = TwoArgPred{}) const
{
return std::is_sorted(begin(), end(), std::move(is_less));
}
template <typename U, typename V>
constexpr usize replace(const U& needle, const V& replacement)
{
usize replace_count = 0;
for (usize i = 0; i < Count; ++i)
{
if (priv_buf_[i] == needle)
{
priv_buf_[i] = replacement;
++replace_count;
}
}
return replace_count;
}
template <typename OneArgPred, typename V>
constexpr usize replace_if(OneArgPred p, const V& replacement)
{
usize replace_count = 0;
for (usize i = 0; i < Count; ++i)
{
if (p(std::as_const(priv_buf_[i])))
{
priv_buf_[i] = replacement;
++replace_count;
}
}
return replace_count;
}
constexpr void reverse()
{
std::reverse(begin(), end());
}
template <typename TwoArgPred = std::less<void>>
constexpr void sort(TwoArgPred is_less = TwoArgPred{})
{
std::sort(begin(), end(), std::move(is_less));
}
template <typename OneArgOp>
constexpr void transform(OneArgOp op)
{
for (usize i = 0; i < Count; ++i)
{
priv_buf_[i] = op(std::as_const(priv_buf_[i]));
}
}
template <typename U, typename TwoArgOp>
constexpr void transform(const array<U, Count>& other, TwoArgOp op)
{
for (usize i = 0; i < Count; ++i)
{
priv_buf_[i] = op(std::as_const(priv_buf_[i]), other.begin()[i]);
}
}
template <typename U, typename TwoArgOp>
constexpr void transform(const array_view<U, Count> other, TwoArgOp op)
{
for (usize i = 0; i < Count; ++i)
{
priv_buf_[i] = op(std::as_const(priv_buf_[i]), other.begin()[i]);
}
}
// #### Validation helpers
template <typename OneArgPred>
[[nodiscard]] constexpr bool all(OneArgPred p) const
{
for (usize i = 0; i < Count; ++i)
{
if (!p(std::as_const(priv_buf_[i])))
{
return false;
}
}
return true;
}
template <typename OneArgPred>
[[nodiscard]] constexpr bool any(OneArgPred p) const
{
for (usize i = 0; i < Count; ++i)
{
if (p(std::as_const(priv_buf_[i])))
{
return true;
}
}
return false;
}
template <typename OneArgPred>
[[nodiscard]] constexpr bool none(OneArgPred p) const
{
for (usize i = 0; i < Count; ++i)
{
if (p(std::as_const(priv_buf_[i])))
{
return false;
}
}
return true;
}
};
// ## Deduction guides
// ### array
template <typename T, typename... Tn>
array(T, Tn...) -> array<T, 1 + sizeof...(Tn)>;
template <usize N, typename... Tn>
array(const char (&)[N], Tn...) -> array<cstrview, 1 + sizeof...(Tn)>;
// ## Functions
// ### Comparison
template <typename T, usize Count>
constexpr bool operator==(const array<T, Count>& left, const array<T, Count>& right)
{
if (!std::is_constant_evaluated() && is_strict_integral_v<T>)
{
return __builtin_memcmp(left.priv_buf_, right.priv_buf_, left.byte_size().get()) == 0;
}
else
{
for (usize i = 0; i < Count; ++i)
{
if (left.priv_buf_[i] != right.priv_buf_[i])
{
return false;
}
}
return true;
}
}
template <typename T, usize Count>
constexpr auto operator<=>(const array<T, Count>& left, const array<T, Count>& right) //
-> decltype(left.priv_buf_[0] <=> right.priv_buf_[0])
{
// char is compared as unsigned char, just like array_view.
if constexpr (std::is_same_v<T, char>)
{
const auto cmp = __builtin_memcmp(left.priv_buf_, right.priv_buf_, Count) <=> 0;
if (cmp != 0)
{
return cmp;
}
}
else
{
for (usize i = 0; i < Count; ++i)
{
const auto cmp = left.priv_buf_[i] <=> right.priv_buf_[i];
if (cmp != 0)
{
return cmp;
}
}
}
return std::strong_ordering::equal;
}
// ### get
template <usize Index, typename T, usize Count>
requires(Index < Count)
[[nodiscard]] constexpr T& get(array<T, Count>& a) noexcept
{
return a.priv_buf_[Index];
}
template <usize Index, typename T, usize Count>
requires(Index < Count)
[[nodiscard]] constexpr const T& get(const array<T, Count>& a) noexcept
{
return a.priv_buf_[Index];
}
template <usize Index, typename T, usize Count>
requires(Index < Count)
[[nodiscard]] constexpr T&& get(array<T, Count>&& a) noexcept
{
return std::move(a.priv_buf_[Index]);
}
template <usize Index, typename T, usize Count>
requires(Index < Count)
[[nodiscard]] constexpr const T&& get(const array<T, Count>&& a) noexcept
{
return std::move(a.priv_buf_[Index]);
}
// ### swap
template <typename T, usize Count>
constexpr void swap(array<T, Count>& a, array<T, Count>& b)
noexcept(Count == 0 || std::is_nothrow_swappable_v<T>)
{
a.swap(b);
}
// ### to_array
namespace detail
{
template <usize Count, typename T, usize N, usize... Index>
constexpr array<std::remove_cv_t<T>, Count> to_array_copy(T (&a)[N],
std::index_sequence<Index...>)
{
return {{a[Index]...}};
}
template <usize Count, typename T, usize N, usize... Index>
constexpr array<std::remove_cv_t<T>, Count> to_array_move(T (&&a)[N],
std::index_sequence<Index...>)
{
return {{std::move(a[Index])...}};
}
}
template <typename T, usize Count>
requires(!std::is_array_v<T> && !std::is_same_v<T, const char>)
[[nodiscard]] constexpr array<std::remove_cv_t<T>, Count> to_array(T (&a)[Count])
noexcept(std::is_nothrow_constructible_v<T, T&>)
{
static_assert(std::is_constructible_v<T, T&>);
return detail::to_array_copy<Count>(a, std::make_index_sequence<Count>{});
}
template <typename T, usize Count>
requires(!std::is_array_v<T> && !std::is_same_v<T, const char>)
[[nodiscard]] constexpr array<std::remove_cv_t<T>, Count> to_array(T (&&a)[Count])
noexcept(std::is_nothrow_move_constructible_v<T>)
{
static_assert(std::is_move_constructible_v<T>);
return detail::to_array_move<Count>(std::move(a), std::make_index_sequence<Count>{});
}
template <same_as<const char> ConstChar, usize N>
[[nodiscard]] constexpr array<char, N - 1> to_array(ConstChar (&s)[N]) noexcept
{
return detail::to_array_copy<N - 1>(s, std::make_index_sequence<N - 1>{});
}
}
SNN_DIAGNOSTIC_POP
// ## Specializations
// ### std::tuple_size
template <typename T, std::size_t Count>
struct std::tuple_size<snn::array<T, Count>> : public std::integral_constant<std::size_t, Count>
{
};
// ### std::tuple_element
template <std::size_t Index, typename T, std::size_t Count>
requires(Index < Count)
struct std::tuple_element<Index, snn::array<T, Count>>
{
using type = T;
};