forked from microsoft/GSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_span
2293 lines (1952 loc) · 82.9 KB
/
multi_span
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.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_MULTI_SPAN_H
#define GSL_MULTI_SPAN_H
#include <gsl/gsl_assert> // for Expects
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast
#include <algorithm> // for transform, lexicographical_compare
#include <array> // for array
#include <cassert>
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <cstdint> // for PTRDIFF_MAX
#include <functional> // for divides, multiplies, minus, negate, plus
#include <initializer_list> // for initializer_list
#include <iterator> // for iterator, random_access_iterator_tag
#include <limits> // for numeric_limits
#include <new>
#include <numeric>
#include <stdexcept>
#include <string> // for basic_string
#include <type_traits> // for enable_if_t, remove_cv_t, is_same, is_co...
#include <utility>
#if defined(_MSC_VER) && !defined(__clang__)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(push)
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
#pragma warning(disable : 26495) // uninitalized member when constructor calls constructor
#pragma warning(disable : 26473) // in some instantiations we cast to the same type
#pragma warning(disable : 26490) // TODO: bug in parser - attributes and templates
#pragma warning(disable : 26465) // TODO: bug - suppression does not work on template functions
#if _MSC_VER < 1910
#pragma push_macro("constexpr")
#define constexpr /*constexpr*/
#endif // _MSC_VER < 1910
#endif // _MSC_VER
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
// While there is a conversion from signed to unsigned, it happens at
// compiletime, so the compiler wouldn't have to warn indiscriminently, but
// could check if the source value actually doesn't fit into the target type
// and only warn in those cases.
#if defined(__GNUC__) && __GNUC__ > 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
namespace gsl
{
/*
** begin definitions of index and bounds
*/
namespace details
{
template <typename SizeType>
struct SizeTypeTraits
{
static const SizeType max_value = std::numeric_limits<SizeType>::max();
};
template <typename... Ts>
class are_integral : public std::integral_constant<bool, true>
{
};
template <typename T, typename... Ts>
class are_integral<T, Ts...>
: public std::integral_constant<bool,
std::is_integral<T>::value && are_integral<Ts...>::value>
{
};
} // namespace details
template <std::size_t Rank>
class multi_span_index final
{
static_assert(Rank > 0, "Rank must be greater than 0!");
template <std::size_t OtherRank>
friend class multi_span_index;
public:
static const std::size_t rank = Rank;
using value_type = std::ptrdiff_t;
using size_type = value_type;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
constexpr multi_span_index() noexcept {}
constexpr multi_span_index(const value_type (&values)[Rank]) noexcept
{
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
std::copy(values, values + Rank, elems);
}
template <typename... Ts, typename = std::enable_if_t<(sizeof...(Ts) == Rank) &&
details::are_integral<Ts...>::value>>
constexpr multi_span_index(Ts... ds) noexcept : elems{narrow_cast<value_type>(ds)...}
{}
constexpr multi_span_index(const multi_span_index& other) noexcept = default;
constexpr multi_span_index& operator=(const multi_span_index& rhs) noexcept = default;
// Preconditions: component_idx < rank
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr reference operator[](std::size_t component_idx)
{
Expects(component_idx < Rank); // Component index must be less than rank
return elems[component_idx];
}
// Preconditions: component_idx < rank
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr const_reference operator[](std::size_t component_idx) const
{
Expects(component_idx < Rank); // Component index must be less than rank
return elems[component_idx];
}
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
constexpr bool operator==(const multi_span_index& rhs) const
{
return std::equal(elems, elems + rank, rhs.elems);
}
constexpr bool operator!=(const multi_span_index& rhs) const
{
return !(*this == rhs);
}
constexpr multi_span_index operator+() const noexcept { return *this; }
constexpr multi_span_index operator-() const
{
multi_span_index ret = *this;
std::transform(ret, ret + rank, ret, std::negate<value_type>{});
return ret;
}
constexpr multi_span_index operator+(const multi_span_index& rhs) const
{
multi_span_index ret = *this;
ret += rhs;
return ret;
}
constexpr multi_span_index operator-(const multi_span_index& rhs) const
{
multi_span_index ret = *this;
ret -= rhs;
return ret;
}
constexpr multi_span_index& operator+=(const multi_span_index& rhs)
{
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
std::transform(elems, elems + rank, rhs.elems, elems,
std::plus<value_type>{});
return *this;
}
constexpr multi_span_index& operator-=(const multi_span_index& rhs)
{
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
return *this;
}
constexpr multi_span_index operator*(value_type v) const
{
multi_span_index ret = *this;
ret *= v;
return ret;
}
constexpr multi_span_index operator/(value_type v) const
{
multi_span_index ret = *this;
ret /= v;
return ret;
}
friend constexpr multi_span_index operator*(value_type v, const multi_span_index& rhs)
{
return rhs * v;
}
constexpr multi_span_index& operator*=(value_type v)
{
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
std::transform(elems, elems + rank, elems,
[v](value_type x) { return std::multiplies<value_type>{}(x, v); });
return *this;
}
constexpr multi_span_index& operator/=(value_type v)
{
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute
std::transform(elems, elems + rank, elems,
[v](value_type x) { return std::divides<value_type>{}(x, v); });
return *this;
}
private:
value_type elems[Rank] = {};
};
#if !defined(_MSC_VER) || _MSC_VER >= 1910
struct static_bounds_dynamic_range_t
{
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr operator T() const noexcept
{
return narrow_cast<T>(-1);
}
};
constexpr bool operator==(static_bounds_dynamic_range_t, static_bounds_dynamic_range_t) noexcept
{
return true;
}
constexpr bool operator!=(static_bounds_dynamic_range_t, static_bounds_dynamic_range_t) noexcept
{
return false;
}
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator==(static_bounds_dynamic_range_t, T other) noexcept
{
return narrow_cast<T>(-1) == other;
}
template <typename T, typename = 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 = std::enable_if_t<std::is_integral<T>::value>>
constexpr bool operator!=(static_bounds_dynamic_range_t, T other) noexcept
{
return narrow_cast<T>(-1) != other;
}
template <typename T, typename = 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 std::ptrdiff_t dynamic_range = -1;
#endif
struct generalized_mapping_tag
{
};
struct contiguous_mapping_tag : generalized_mapping_tag
{
};
namespace details
{
template <std::ptrdiff_t Left, std::ptrdiff_t Right>
struct LessThan
{
static const bool value = Left < Right;
};
template <std::ptrdiff_t... Ranges>
struct BoundsRanges
{
using size_type = std::ptrdiff_t;
static const size_type Depth = 0;
static const size_type DynamicNum = 0;
static const size_type CurrentRange = 1;
static const size_type TotalSize = 1;
// TODO : following signature is for work around VS bug
template <typename OtherRange>
constexpr BoundsRanges(const OtherRange&, bool /* firstLevel */)
{}
constexpr BoundsRanges(const std::ptrdiff_t* const) {}
constexpr BoundsRanges() noexcept = default;
template <typename T, std::size_t Dim>
constexpr void serialize(T&) const
{}
template <typename T, std::size_t Dim>
constexpr size_type linearize(const T&) const
{
return 0;
}
template <typename T, std::size_t Dim>
constexpr size_type contains(const T&) const
{
return -1;
}
constexpr size_type elementNum(std::size_t) const noexcept { return 0; }
constexpr size_type totalSize() const noexcept { return TotalSize; }
constexpr bool operator==(const BoundsRanges&) const noexcept { return true; }
};
template <std::ptrdiff_t... RestRanges>
struct BoundsRanges<dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>
{
using Base = BoundsRanges<RestRanges...>;
using size_type = std::ptrdiff_t;
static const std::size_t Depth = Base::Depth + 1;
static const std::size_t DynamicNum = Base::DynamicNum + 1;
static const size_type CurrentRange = dynamic_range;
static const size_type TotalSize = dynamic_range;
private:
size_type m_bound;
public:
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute // this pointer type is cannot be assigned nullptr - issue in not_null
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr BoundsRanges(const std::ptrdiff_t* const arr)
: Base(arr + 1), m_bound(*arr * this->Base::totalSize())
{
Expects(0 <= *arr);
}
constexpr BoundsRanges() noexcept : m_bound(0) {}
template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
constexpr BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other,
bool /* firstLevel */ = true)
: Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
, m_bound(other.totalSize())
{}
template <typename T, std::size_t Dim = 0>
constexpr void serialize(T& arr) const
{
arr[Dim] = elementNum();
this->Base::template serialize<T, Dim + 1>(arr);
}
template <typename T, std::size_t Dim = 0>
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr size_type linearize(const T& arr) const
{
const size_type index = this->Base::totalSize() * arr[Dim];
Expects(index < m_bound);
return index + this->Base::template linearize<T, Dim + 1>(arr);
}
template <typename T, std::size_t Dim = 0>
constexpr size_type 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 cur < m_bound ? cur + last : -1;
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type totalSize() const noexcept
{
return m_bound;
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type elementNum() const noexcept
{
return totalSize() / this->Base::totalSize();
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type elementNum(std::size_t dim) const noexcept
{
if (dim > 0)
return this->Base::elementNum(dim - 1);
else
return elementNum();
}
constexpr bool operator==(const BoundsRanges& rhs) const noexcept
{
return m_bound == rhs.m_bound &&
static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
}
};
template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
struct BoundsRanges<CurRange, RestRanges...> : BoundsRanges<RestRanges...>
{
using Base = BoundsRanges<RestRanges...>;
using size_type = std::ptrdiff_t;
static const std::size_t Depth = Base::Depth + 1;
static const std::size_t DynamicNum = Base::DynamicNum;
static const size_type CurrentRange = CurRange;
static const size_type TotalSize =
Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
constexpr BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) {}
constexpr BoundsRanges() = default;
template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
constexpr BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other,
bool firstLevel = true)
: Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
{
GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: false positive
(void) firstLevel;
}
template <typename T, std::size_t Dim = 0>
constexpr void serialize(T& arr) const
{
arr[Dim] = elementNum();
this->Base::template serialize<T, Dim + 1>(arr);
}
template <typename T, std::size_t Dim = 0>
constexpr size_type linearize(const T& arr) const
{
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
Expects(arr[Dim] >= 0 && arr[Dim] < CurrentRange); // Index is out of range
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
const ptrdiff_t d = arr[Dim];
return this->Base::totalSize() * d +
this->Base::template linearize<T, Dim + 1>(arr);
}
template <typename T, std::size_t Dim = 0>
constexpr size_type contains(const T& arr) const
{
if (arr[Dim] >= CurrentRange) return -1;
const size_type last = this->Base::template contains<T, Dim + 1>(arr);
if (last == -1) return -1;
return this->Base::totalSize() * arr[Dim] + last;
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type totalSize() const noexcept
{
return CurrentRange * this->Base::totalSize();
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type elementNum() const noexcept
{
return CurrentRange;
}
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used
constexpr size_type elementNum(std::size_t dim) const noexcept
{
if (dim > 0)
return this->Base::elementNum(dim - 1);
else
return elementNum();
}
constexpr bool operator==(const BoundsRanges& rhs) const noexcept
{
return static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
}
};
template <typename SourceType, typename TargetType>
struct BoundsRangeConvertible
: public std::integral_constant<bool, (SourceType::TotalSize >= TargetType::TotalSize ||
TargetType::TotalSize == dynamic_range ||
SourceType::TotalSize == dynamic_range ||
TargetType::TotalSize == 0)>
{
};
template <typename TypeChain>
struct TypeListIndexer
{
const TypeChain& obj_;
TypeListIndexer(const TypeChain& obj) : obj_(obj) {}
template <std::size_t N>
const TypeChain& getObj(std::true_type)
{
return obj_;
}
template <std::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 <std::size_t N>
auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, N == 0>()))
{
return getObj<N - 1>(std::integral_constant<bool, N == 0>());
}
};
template <typename TypeChain>
TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain& obj)
{
return TypeListIndexer<TypeChain>(obj);
}
template <std::size_t Rank, bool Enabled = (Rank > 1),
typename Ret = std::enable_if_t<Enabled, multi_span_index<Rank - 1>>>
constexpr Ret shift_left(const multi_span_index<Rank>& other) noexcept
{
Ret ret{};
for (std::size_t i = 0; i < Rank - 1; ++i)
{
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
ret[i] = other[i + 1];
}
return ret;
}
} // namespace details
template <typename IndexType>
class bounds_iterator;
template <std::ptrdiff_t... Ranges>
class static_bounds
{
public:
static_bounds(const details::BoundsRanges<Ranges...>&) {}
};
template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
class static_bounds<FirstRange, RestRanges...>
{
using MyRanges = details::BoundsRanges<FirstRange, RestRanges...>;
MyRanges m_ranges;
constexpr static_bounds(const MyRanges& range) noexcept : m_ranges(range) {}
template <std::ptrdiff_t... OtherRanges>
friend class static_bounds;
public:
static const std::size_t rank = MyRanges::Depth;
static const std::size_t dynamic_rank = MyRanges::DynamicNum;
static const std::ptrdiff_t static_size = MyRanges::TotalSize;
using size_type = std::ptrdiff_t;
using index_type = multi_span_index<rank>;
using const_index_type = std::add_const_t<index_type>;
using iterator = bounds_iterator<const_index_type>;
using const_iterator = bounds_iterator<const_index_type>;
using difference_type = std::ptrdiff_t;
using sliced_type = static_bounds<RestRanges...>;
using mapping_type = contiguous_mapping_tag;
constexpr static_bounds() /*noexcept*/ = default;
template <typename SourceType, typename TargetType, std::size_t Rank>
struct BoundsRangeConvertible2;
template <std::size_t Rank, typename SourceType, typename TargetType,
typename Ret = BoundsRangeConvertible2<typename SourceType::Base,
typename TargetType::Base, Rank>>
static auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
template <std::size_t Rank, typename SourceType, typename TargetType>
static auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
template <typename SourceType, typename TargetType, std::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, std::ptrdiff_t Rank = TargetType::Depth>
struct BoundsRangeConvertible
: decltype(helpBoundsRangeConvertible<Rank - 1>(
SourceType(), TargetType(),
std::integral_constant<bool,
SourceType::Depth == TargetType::Depth &&
(!details::LessThan<SourceType::CurrentRange,
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 <std::ptrdiff_t... Ranges,
typename = std::enable_if_t<details::BoundsRangeConvertible<
details::BoundsRanges<Ranges...>,
details::BoundsRanges<FirstRange, RestRanges...>>::value>>
constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
{
Expects((MyRanges::DynamicNum == 0 && details::BoundsRanges<Ranges...>::DynamicNum == 0) ||
MyRanges::DynamicNum > 0 || other.m_ranges.totalSize() >= m_ranges.totalSize());
}
constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
{
// Size of the initializer list must match the rank of the array
Expects((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) ||
MyRanges::DynamicNum == il.size());
// Size of the range must be less than the max element of the size type
Expects(m_ranges.totalSize() <= PTRDIFF_MAX);
}
constexpr sliced_type slice() const noexcept
{
return sliced_type{static_cast<const details::BoundsRanges<RestRanges...>&>(m_ranges)};
}
constexpr size_type stride() const noexcept { return rank > 1 ? slice().size() : 1; }
constexpr size_type size() const noexcept { return m_ranges.totalSize(); }
constexpr size_type total_size() const noexcept { return 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[](std::size_t idx) const noexcept
{
return m_ranges.elementNum(idx);
}
template <std::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();
}
template <typename IntType>
constexpr size_type extent(IntType dim) const
{
static_assert(std::is_integral<IntType>::value,
"Dimension parameter must be supplied as an integral type.");
auto real_dim = narrow_cast<std::size_t>(dim);
Expects(real_dim < rank);
return m_ranges.elementNum(real_dim);
}
constexpr index_type index_bounds() const noexcept
{
size_type extents[rank] = {};
m_ranges.serialize(extents);
return {extents};
}
template <std::ptrdiff_t... Ranges>
constexpr bool operator==(const static_bounds<Ranges...>& rhs) const noexcept
{
return this->size() == rhs.size();
}
template <std::ptrdiff_t... Ranges>
constexpr bool operator!=(const static_bounds<Ranges...>& rhs) const noexcept
{
return !(*this == rhs);
}
constexpr const_iterator begin() const noexcept
{
return const_iterator(*this, index_type{});
}
constexpr const_iterator end() const noexcept
{
return const_iterator(*this, this->index_bounds());
}
};
template <std::size_t Rank>
class strided_bounds {
template <std::size_t OtherRank>
friend class strided_bounds;
public:
static const std::size_t rank = Rank;
using value_type = std::ptrdiff_t;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_const_t<reference>;
using size_type = value_type;
using difference_type = value_type;
using index_type = multi_span_index<rank>;
using const_index_type = std::add_const_t<index_type>;
using iterator = bounds_iterator<const_index_type>;
using const_iterator = bounds_iterator<const_index_type>;
static const value_type dynamic_rank = rank;
static const value_type 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&) noexcept = default;
constexpr strided_bounds& operator=(const strided_bounds&) noexcept = default;
constexpr strided_bounds(const value_type (&values)[rank], index_type strides)
: m_extents(values), m_strides(std::move(strides))
{}
constexpr strided_bounds(const index_type& extents, const index_type& strides) noexcept
: m_extents(extents), m_strides(strides)
{
}
constexpr index_type strides() const noexcept { return m_strides; }
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr size_type total_size() const noexcept
{
size_type ret = 0;
for (std::size_t i = 0; i < rank; ++i) { ret += (m_extents[i] - 1) * m_strides[i]; }
return ret + 1;
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr size_type size() const noexcept
{
size_type ret = 1;
for (std::size_t i = 0; i < rank; ++i) { ret *= m_extents[i]; }
return ret;
}
constexpr bool contains(const index_type& idx) const noexcept
{
for (std::size_t i = 0; i < rank; ++i)
{
if (idx[i] < 0 || idx[i] >= m_extents[i]) return false;
}
return true;
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr size_type linearize(const index_type& idx) const
{
size_type ret = 0;
for (std::size_t i = 0; i < rank; i++)
{
Expects(idx[i] < m_extents[i]); // index is out of bounds of the array
ret += idx[i] * m_strides[i];
}
return ret;
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
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 {details::shift_left(m_extents), details::shift_left(m_strides)};
}
template <std::size_t Dim = 0>
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr size_type extent() const noexcept
{
static_assert(Dim < Rank,
"dimension should be less than rank (dimension count starts from 0)");
return m_extents[Dim];
}
constexpr index_type index_bounds() const noexcept { return m_extents; }
constexpr const_iterator begin() const noexcept { return const_iterator{*this, index_type{}}; }
constexpr const_iterator end() const noexcept { return const_iterator{*this, index_bounds()}; }
private:
index_type m_extents;
index_type m_strides;
};
template <typename T>
struct is_bounds : std::integral_constant<bool, false>
{
};
template <std::ptrdiff_t... Ranges>
struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true>
{
};
template <std::size_t Rank>
struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true>
{
};
template <typename IndexType>
class bounds_iterator
{
public:
static const std::size_t rank = IndexType::rank;
using iterator_category = std::random_access_iterator_tag;
using value_type = IndexType;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
using index_type = value_type;
using index_size_type = typename IndexType::value_type;
template <typename Bounds>
explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
: boundary_(bnd.index_bounds()), curr_(std::move(curr))
{
static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
}
constexpr reference operator*() const noexcept { return curr_; }
constexpr pointer operator->() const noexcept { return &curr_; }
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
constexpr bounds_iterator& operator++() noexcept
{
for (std::size_t i = rank; i-- > 0;)
{
if (curr_[i] < boundary_[i] - 1)
{
curr_[i]++;
return *this;
}
curr_[i] = 0;
}
// If we're here we've wrapped over - set to past-the-end.
curr_ = boundary_;
return *this;
}
constexpr bounds_iterator operator++(int) noexcept
{
auto ret = *this;
++(*this);
return ret;
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr bounds_iterator& operator--()
{
if (!less(curr_, boundary_))
{
// if at the past-the-end, set to last element
for (std::size_t i = 0; i < rank; ++i) { curr_[i] = boundary_[i] - 1; }
return *this;
}
for (std::size_t i = rank; i-- > 0;)
{
if (curr_[i] >= 1)
{
curr_[i]--;
return *this;
}
curr_[i] = boundary_[i] - 1;
}
// If we're here the preconditions were violated
// "pre: there exists s such that r == ++s"
Expects(false);
return *this;
}
constexpr bounds_iterator operator--(int) noexcept
{
auto ret = *this;
--(*this);
return ret;
}
constexpr bounds_iterator operator+(difference_type n) const noexcept
{
bounds_iterator ret{*this};
return ret += n;
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr bounds_iterator& operator+=(difference_type n)
{
auto linear_idx = linearize(curr_) + n;
std::remove_const_t<value_type> stride = 0;
stride[rank - 1] = 1;
for (std::size_t i = rank - 1; i-- > 0;) { stride[i] = stride[i + 1] * boundary_[i + 1]; }
for (std::size_t i = 0; i < rank; ++i)
{
curr_[i] = linear_idx / stride[i];
linear_idx = linear_idx % stride[i];
}
// index is out of bounds of the array
Expects(!less(curr_, index_type{}) && !less(boundary_, curr_));
return *this;
}
constexpr bounds_iterator operator-(difference_type n) const noexcept
{
bounds_iterator ret{*this};
return ret -= n;
}
constexpr bounds_iterator& operator-=(difference_type n) noexcept { return *this += -n; }
constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
{
return linearize(curr_) - linearize(rhs.curr_);
}
constexpr value_type operator[](difference_type n) const noexcept { return *(*this + n); }
constexpr bool operator==(const bounds_iterator& rhs) const noexcept
{
return curr_ == rhs.curr_;
}
constexpr bool operator!=(const bounds_iterator& rhs) const noexcept { return !(*this == rhs); }
constexpr bool operator<(const bounds_iterator& rhs) const noexcept
{
return less(curr_, rhs.curr_);
}
constexpr bool operator<=(const bounds_iterator& rhs) const noexcept { return !(rhs < *this); }
constexpr bool operator>(const bounds_iterator& rhs) const noexcept { return rhs < *this; }
constexpr bool operator>=(const bounds_iterator& rhs) const noexcept { return !(rhs > *this); }
void swap(bounds_iterator& rhs) noexcept
{
std::swap(boundary_, rhs.boundary_);
std::swap(curr_, rhs.curr_);
}
private:
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
constexpr bool less(index_type& one, index_type& other) const noexcept