forked from microsoft/GSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_view.h
2300 lines (2076 loc) · 73 KB
/
array_view.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) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef GSL_ARRAY_VIEW_H
#define GSL_ARRAY_VIEW_H
#include <new>
#include <stdexcept>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <array>
#include <iterator>
#include <algorithm>
#include "fail_fast.h"
#ifdef _MSC_VER
// No MSVC does constexpr fully yet
#pragma push_macro("constexpr")
#define constexpr /* nothing */
// VS 2013 workarounds
#if _MSC_VER <= 1800
// noexcept is not understood
#ifndef GSL_THROWS_FOR_TESTING
#define noexcept /* nothing */
#endif
// turn off some misguided warnings
#pragma warning(push)
#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
#endif // _MSC_VER <= 1800
#endif // _MSC_VER
// In order to test the library, we need it to throw exceptions that we can catch
#ifdef GSL_THROWS_FOR_TESTING
#define noexcept /* nothing */
#endif // GSL_THROWS_FOR_TESTING
namespace gsl {
/*
** begin definitions of index and bounds
*/
namespace details
{
template <typename SizeType>
struct SizeTypeTraits
{
static const size_t max_value = std::is_signed<SizeType>::value ? static_cast<typename std::make_unsigned<SizeType>::type>(-1) / 2 : static_cast<SizeType>(-1);
};
template <typename ConcreteType, typename ValueType, size_t Rank>
class coordinate_facade
{
static_assert(std::is_integral<ValueType>::value
&& sizeof(ValueType) <= sizeof(size_t), "ValueType must be an integral type!");
static_assert(Rank > 0, "Rank must be greater than 0!");
template <typename OtherConcreteType, typename OtherValueType, size_t OtherRank>
friend class coordinate_facade;
public:
using reference = ValueType&;
using const_reference = const ValueType&;
using value_type = ValueType;
static const size_t rank = Rank;
constexpr coordinate_facade() noexcept
{
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
}
constexpr coordinate_facade(const value_type(&values)[rank]) noexcept
{
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
for (size_t i = 0; i < rank; ++i)
elems[i] = values[i];
}
constexpr coordinate_facade(value_type e0) noexcept
{
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
static_assert(rank == 1, "This constructor can only be used with rank == 1.");
elems[0] = e0;
}
// Preconditions: il.size() == rank
constexpr coordinate_facade(std::initializer_list<value_type> il)
{
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array");
for (size_t i = 0; i < rank; ++i)
{
elems[i] = begin(il)[i];
}
}
constexpr coordinate_facade(const coordinate_facade & other) = default;
template <typename OtherConcreteType, typename OtherValueType>
constexpr coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
{
for (size_t i = 0; i < rank; ++i)
{
fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
elems[i] = static_cast<value_type>(other.elems[i]);
}
}
protected:
coordinate_facade& operator=(const coordinate_facade& rhs) = default;
// Preconditions: component_idx < rank
constexpr reference operator[](size_t component_idx)
{
fail_fast_assert(component_idx < rank, "Component index must be less than rank");
return elems[component_idx];
}
// Preconditions: component_idx < rank
constexpr const_reference operator[](size_t component_idx) const
{
fail_fast_assert(component_idx < rank, "Component index must be less than rank");
return elems[component_idx];
}
constexpr bool operator==(const ConcreteType& rhs) const noexcept
{
return std::equal(elems, elems + rank, rhs.elems);
}
constexpr bool operator!=(const ConcreteType& rhs) const noexcept
{
return !(to_concrete() == rhs);
}
constexpr ConcreteType operator+() const noexcept
{
return to_concrete();
}
constexpr ConcreteType operator-() const
{
ConcreteType ret = to_concrete();
std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
return ret;
}
constexpr ConcreteType operator+(const ConcreteType& rhs) const
{
ConcreteType ret = to_concrete();
ret += rhs;
return ret;
}
constexpr ConcreteType operator-(const ConcreteType& rhs) const
{
ConcreteType ret = to_concrete();
ret -= rhs;
return ret;
}
constexpr ConcreteType& operator+=(const ConcreteType& rhs)
{
for (size_t i = 0; i < rank; ++i)
elems[i] += rhs.elems[i];
return to_concrete();
}
constexpr ConcreteType& operator-=(const ConcreteType& rhs)
{
for (size_t i = 0; i < rank; ++i)
elems[i] -= rhs.elems[i];
return to_concrete();
}
constexpr ConcreteType& operator++()
{
static_assert(rank == 1, "This operator can only be used with rank == 1.");
++elems[0];
return to_concrete();
}
constexpr ConcreteType operator++(int)
{
static_assert(rank == 1, "This operator can only be used with rank == 1.");
ConcreteType ret = to_concrete();
++(*this);
return ret;
}
constexpr ConcreteType& operator--()
{
static_assert(rank == 1, "This operator can only be used with rank == 1.");
--elems[0];
return to_concrete();
}
constexpr ConcreteType operator--(int)
{
static_assert(rank == 1, "This operator can only be used with rank == 1.");
ConcreteType ret = to_concrete();
--(*this);
return ret;
}
constexpr ConcreteType operator*(value_type v) const
{
ConcreteType ret = to_concrete();
ret *= v;
return ret;
}
constexpr ConcreteType operator/(value_type v) const
{
ConcreteType ret = to_concrete();
ret /= v;
return ret;
}
friend constexpr ConcreteType operator*(value_type v, const ConcreteType& rhs)
{
return rhs * v;
}
constexpr ConcreteType& operator*=(value_type v)
{
for (size_t i = 0; i < rank; ++i)
elems[i] *= v;
return to_concrete();
}
constexpr ConcreteType& operator/=(value_type v)
{
for (size_t i = 0; i < rank; ++i)
elems[i] /= v;
return to_concrete();
}
value_type elems[rank] = {};
private:
constexpr const ConcreteType& to_concrete() const noexcept
{
return static_cast<const ConcreteType&>(*this);
}
constexpr ConcreteType& to_concrete() noexcept
{
return static_cast<ConcreteType&>(*this);
}
};
template <typename T>
class arrow_proxy
{
public:
explicit arrow_proxy(T t)
: val(t)
{}
const T operator*() const noexcept
{
return val;
}
const T* operator->() const noexcept
{
return &val;
}
private:
T val;
};
}
template <size_t Rank, typename ValueType = size_t>
class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
{
using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
friend Base;
template <size_t OtherRank, typename OtherValueType>
friend class index;
public:
using Base::rank;
using reference = typename Base::reference;
using const_reference = typename Base::const_reference;
using size_type = typename Base::value_type;
using value_type = typename Base::value_type;
constexpr index() noexcept : Base(){}
constexpr index(const value_type (&values)[rank]) noexcept : Base(values) {}
constexpr index(std::initializer_list<value_type> il) : Base(il) {}
constexpr index(const index &) = default;
template <typename OtherValueType>
constexpr index(const index<Rank, OtherValueType> &other) : Base(other)
{
}
constexpr static index shift_left(const index<rank+1, value_type>& other) noexcept
{
value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
return index(arr);
}
using Base::operator[];
using Base::operator==;
using Base::operator!=;
using Base::operator+;
using Base::operator-;
using Base::operator+=;
using Base::operator-=;
using Base::operator++;
using Base::operator--;
using Base::operator*;
using Base::operator/;
using Base::operator*=;
using Base::operator/=;
};
template <typename ValueType>
class index<1, ValueType>
{
template <size_t, typename OtherValueType>
friend class index;
public:
static const size_t rank = 1;
using reference = ValueType&;
using const_reference = const ValueType&;
using size_type = ValueType;
using value_type = ValueType;
constexpr index() noexcept : value(0)
{
}
constexpr index(value_type e0) noexcept : value(e0)
{
}
constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
{
}
// Preconditions: il.size() == rank
constexpr index(std::initializer_list<value_type> il)
{
fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
value = begin(il)[0];
}
constexpr index(const index &) = default;
template <typename OtherValueType>
constexpr index(const index<1, OtherValueType> & other)
{
fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
value = static_cast<ValueType>(other.value);
}
constexpr static index shift_left(const index<rank + 1, value_type>& other) noexcept
{
return other.elems[1];
}
// Preconditions: component_idx < rank
constexpr reference operator[](size_type component_idx) noexcept
{
fail_fast_assert(component_idx == 0, "Component index must be less than rank");
(void)(component_idx);
return value;
}
// Preconditions: component_idx < rank
constexpr const_reference operator[](size_type component_idx) const noexcept
{
fail_fast_assert(component_idx == 0, "Component index must be less than rank");
(void)(component_idx);
return value;
}
constexpr bool operator==(const index& rhs) const noexcept
{
return value == rhs.value;
}
constexpr bool operator!=(const index& rhs) const noexcept
{
return !(*this == rhs);
}
constexpr index operator+() const noexcept
{
return *this;
}
constexpr index operator-() const noexcept
{
return index(-value);
}
constexpr index operator+(const index& rhs) const noexcept
{
return index(value + rhs.value);
}
constexpr index operator-(const index& rhs) const noexcept
{
return index(value - rhs.value);
}
constexpr index& operator+=(const index& rhs) noexcept
{
value += rhs.value;
return *this;
}
constexpr index& operator-=(const index& rhs) noexcept
{
value -= rhs.value;
return *this;
}
constexpr index& operator++() noexcept
{
++value;
return *this;
}
constexpr index operator++(int) noexcept
{
index ret = *this;
++(*this);
return ret;
}
constexpr index& operator--() noexcept
{
--value;
return *this;
}
constexpr index operator--(int) noexcept
{
index ret = *this;
--(*this);
return ret;
}
constexpr index operator*(value_type v) const noexcept
{
return index(value * v);
}
constexpr index operator/(value_type v) const noexcept
{
return index(value / v);
}
constexpr index& operator*=(value_type v) noexcept
{
value *= v;
return *this;
}
constexpr index& operator/=(value_type v) noexcept
{
value /= v;
return *this;
}
friend constexpr index operator*(value_type v, const index& rhs) noexcept
{
return index(rhs * v);
}
private:
value_type value;
};
#ifndef _MSC_VER
struct static_bounds_dynamic_range_t
{
template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
constexpr operator T() const noexcept
{
return static_cast<T>(-1);
}
template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator ==(T other) const noexcept
{
return static_cast<T>(-1) == other;
}
template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator !=(T other) const noexcept
{
return static_cast<T>(-1) != other;
}
};
template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
{
return right == left;
}
template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
{
return right != left;
}
constexpr static_bounds_dynamic_range_t dynamic_range{};
#else
const char dynamic_range = -1;
#endif
struct generalized_mapping_tag {};
struct contiguous_mapping_tag : generalized_mapping_tag {};
namespace details
{
template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
struct StaticSizeHelperImpl
{
static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
static const SizeType value = Fact1 * Fact2;
};
template <typename SizeType, SizeType Fact1, SizeType ConstBound>
struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
{
static const SizeType value = ConstBound;
};
template <typename SizeType, SizeType Fact2, SizeType ConstBound>
struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
{
static const SizeType value = ConstBound;
};
template <typename SizeType, SizeType ConstBound>
struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
{
static const SizeType value = static_cast<SizeType>(ConstBound);
};
template <typename SizeType, SizeType Fact1, SizeType Fact2>
struct StaticSizeHelper
{
static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
};
template <size_t Left, size_t Right>
struct LessThan
{
static const bool value = Left < Right;
};
template <typename SizeType, size_t... Ranges>
struct BoundsRanges {
static const size_t Depth = 0;
static const size_t DynamicNum = 0;
static const SizeType CurrentRange = 1;
static const SizeType TotalSize = 1;
BoundsRanges (const BoundsRanges &) = default;
// TODO : following signature is for work around VS bug
template <typename OtherType>
BoundsRanges (const OtherType &, bool /* firstLevel */) {}
BoundsRanges(const SizeType * const) { }
BoundsRanges() = default;
template <typename T, size_t Dim>
void serialize(T &) const {
}
template <typename T, size_t Dim>
SizeType linearize(const T &) const {
return 0;
}
template <typename T, size_t Dim>
ptrdiff_t contains(const T &) const {
return 0;
}
size_t totalSize() const noexcept {
return TotalSize;
}
bool operator == (const BoundsRanges &) const noexcept
{
return true;
}
};
template <typename SizeType, size_t... RestRanges>
struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
using Base = BoundsRanges <SizeType, RestRanges... >;
static const size_t Depth = Base::Depth + 1;
static const size_t DynamicNum = Base::DynamicNum + 1;
static const SizeType CurrentRange = dynamic_range;
static const SizeType TotalSize = dynamic_range;
const SizeType m_bound;
BoundsRanges (const BoundsRanges &) = default;
BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
{
fail_fast_assert(0 <= *arr);
fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
}
BoundsRanges() : m_bound(0) {}
template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool /* firstLevel */ = true) :
Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
{
}
template <typename T, size_t Dim = 0>
void serialize(T & arr) const {
arr[Dim] = elementNum();
this->Base::template serialize<T, Dim + 1>(arr);
}
template <typename T, size_t Dim = 0>
SizeType linearize(const T & arr) const {
const size_t index = this->Base::totalSize() * arr[Dim];
fail_fast_assert(index < static_cast<size_t>(m_bound));
return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
}
template <typename T, size_t Dim = 0>
ptrdiff_t contains(const T & arr) const {
const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
if (last == -1)
return -1;
const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
}
size_t totalSize() const noexcept {
return m_bound;
}
SizeType elementNum() const noexcept {
return static_cast<SizeType>(totalSize() / this->Base::totalSize());
}
SizeType elementNum(size_t dim) const noexcept{
if (dim > 0)
return this->Base::elementNum(dim - 1);
else
return elementNum();
}
bool operator == (const BoundsRanges & rhs) const noexcept
{
return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
}
};
template <typename SizeType, size_t CurRange, size_t... RestRanges>
struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
using Base = BoundsRanges <SizeType, RestRanges... >;
static const size_t Depth = Base::Depth + 1;
static const size_t DynamicNum = Base::DynamicNum;
static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
BoundsRanges (const BoundsRanges &) = default;
BoundsRanges(const SizeType * const arr) : Base(arr) { }
BoundsRanges() = default;
template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
{
fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
}
template <typename T, size_t Dim = 0>
void serialize(T & arr) const {
arr[Dim] = elementNum();
this->Base::template serialize<T, Dim + 1>(arr);
}
template <typename T, size_t Dim = 0>
SizeType linearize(const T & arr) const {
fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
}
template <typename T, size_t Dim = 0>
ptrdiff_t contains(const T & arr) const {
if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
return -1;
const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
if (last == -1)
return -1;
return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
}
size_t totalSize() const noexcept{
return CurrentRange * this->Base::totalSize();
}
SizeType elementNum() const noexcept{
return CurrentRange;
}
SizeType elementNum(size_t dim) const noexcept{
if (dim > 0)
return this->Base::elementNum(dim - 1);
else
return elementNum();
}
bool operator == (const BoundsRanges & rhs) const noexcept
{
return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
}
};
template <typename SourceType, typename TargetType, size_t Rank>
struct BoundsRangeConvertible2;
// TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
template <size_t Rank, typename SourceType, typename TargetType>
auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
template <typename SourceType, typename TargetType, size_t Rank>
struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
std::integral_constant<bool, SourceType::Depth == TargetType::Depth
&& (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
{};
template <typename SourceType, typename TargetType>
struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
std::integral_constant<bool, SourceType::Depth == TargetType::Depth
&& (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
{};
template <typename SourceType, typename TargetType>
struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
template <typename TypeChain>
struct TypeListIndexer
{
const TypeChain & obj;
TypeListIndexer(const TypeChain & obj) :obj(obj){}
template<size_t N>
const TypeChain & getObj(std::true_type)
{
return obj;
}
template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
{
return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
}
template <size_t N>
auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
{
return getObj<N - 1>(std::integral_constant<bool, N == 0>());
}
};
template <typename TypeChain>
TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
{
return TypeListIndexer<TypeChain>(obj);
}
}
template <typename IndexType>
class bounds_iterator;
template <typename SizeType, size_t... Ranges>
class static_bounds {
public:
static_bounds(const details::BoundsRanges<SizeType, Ranges...> &) {
}
};
template <typename SizeType, size_t FirstRange, size_t... RestRanges>
class static_bounds<SizeType, FirstRange, RestRanges...>
{
using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
static_assert(std::is_integral<SizeType>::value
&& details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
MyRanges m_ranges;
constexpr static_bounds(const MyRanges & range) : m_ranges(range) { }
template <typename SizeType2, size_t... Ranges2>
friend class static_bounds;
public:
static const size_t rank = MyRanges::Depth;
static const size_t dynamic_rank = MyRanges::DynamicNum;
static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
using size_type = SizeType;
using index_type = index<rank, size_type>;
using iterator = bounds_iterator<index_type>;
using const_iterator = bounds_iterator<index_type>;
using difference_type = ptrdiff_t;
using sliced_type = static_bounds<SizeType, RestRanges...>;
using mapping_type = contiguous_mapping_tag;
public:
constexpr static_bounds(const static_bounds &) = default;
template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
constexpr static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
m_ranges(other.m_ranges)
{
}
constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
{
fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
fail_fast_assert(m_ranges.totalSize() <= details::SizeTypeTraits<size_type>::max_value, "Size of the range is larger than the max element of the size type");
}
constexpr static_bounds() = default;
constexpr static_bounds & operator = (const static_bounds & otherBounds)
{
new(&m_ranges) MyRanges (otherBounds.m_ranges);
return *this;
}
constexpr sliced_type slice() const noexcept
{
return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
}
constexpr size_type stride() const noexcept
{
return rank > 1 ? slice().size() : 1;
}
constexpr size_type size() const noexcept
{
return static_cast<size_type>(m_ranges.totalSize());
}
constexpr size_type total_size() const noexcept
{
return static_cast<size_type>(m_ranges.totalSize());
}
constexpr size_type linearize(const index_type & idx) const
{
return m_ranges.linearize(idx);
}
constexpr bool contains(const index_type& idx) const noexcept
{
return m_ranges.contains(idx) != -1;
}
constexpr size_type operator[](size_t index) const noexcept
{
return m_ranges.elementNum(index);
}
template <size_t Dim = 0>
constexpr size_type extent() const noexcept
{
static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
}
constexpr index_type index_bounds() const noexcept
{
index_type extents;
m_ranges.serialize(extents);
return extents;
}
template <typename OtherSizeTypes, size_t... Ranges>
constexpr bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
{
return this->size() == rhs.size();
}
template <typename OtherSizeTypes, size_t... Ranges>
constexpr bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
{
return !(*this == rhs);
}
constexpr const_iterator begin() const noexcept
{
return const_iterator(*this);
}
constexpr const_iterator end() const noexcept
{
index_type boundary;
m_ranges.serialize(boundary);
return const_iterator(*this, this->index_bounds());
}
};
template <size_t Rank, typename SizeType = size_t>
class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
{
using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
friend Base;
template <size_t OtherRank, typename OtherSizeType>
friend class strided_bounds;
public:
using Base::rank;
using reference = typename Base::reference;
using const_reference = typename Base::const_reference;
using size_type = typename Base::value_type;
using difference_type = typename Base::value_type;
using value_type = typename Base::value_type;
using index_type = index<rank, size_type>;
using iterator = bounds_iterator<index_type>;
using const_iterator = bounds_iterator<index_type>;
static const int dynamic_rank = rank;
static const size_t static_size = dynamic_range;
using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
using mapping_type = generalized_mapping_tag;
constexpr strided_bounds(const strided_bounds &) = default;
template <typename OtherSizeType>
constexpr strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
: Base(other), m_strides(other.strides)
{
}
constexpr strided_bounds(const index_type &extents, const index_type &strides)
: m_strides(strides)
{
for (size_t i = 0; i < rank; i++)
Base::elems[i] = extents[i];
}
constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
: Base(values), m_strides(std::move(strides))
{
}
constexpr index_type strides() const noexcept
{
return m_strides;
}
constexpr size_type total_size() const noexcept
{
size_type ret = 0;
for (size_t i = 0; i < rank; ++i)
ret += (Base::elems[i] - 1) * m_strides[i];
return ret + 1;
}
constexpr size_type size() const noexcept
{
size_type ret = 1;
for (size_t i = 0; i < rank; ++i)
ret *= Base::elems[i];
return ret;
}
constexpr bool contains(const index_type& idx) const noexcept
{
for (size_t i = 0; i < rank; ++i)
{
if (idx[i] < 0 || idx[i] >= Base::elems[i])
return false;
}
return true;
}
constexpr size_type linearize(const index_type & idx) const
{
size_type ret = 0;
for (size_t i = 0; i < rank; i++)
{
fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
ret += idx[i] * m_strides[i];
}
return ret;
}
constexpr size_type stride() const noexcept
{
return m_strides[0];
}
template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
constexpr sliced_type slice() const
{
return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
}
template <size_t Dim = 0>
constexpr size_type extent() const noexcept
{
static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
return Base::elems[Dim];
}
constexpr index_type index_bounds() const noexcept
{
return index_type(Base::elems);
}
const_iterator begin() const noexcept
{
return const_iterator{ *this };
}
const_iterator end() const noexcept
{
return const_iterator{ *this, index_bounds() };
}
private:
index_type m_strides;
};
template <typename T>
struct is_bounds : std::integral_constant<bool, false> {};
template <typename SizeType, size_t... Ranges>
struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
template <size_t Rank, typename SizeType>