blob: ecc3d1ec3bd2a11bfed0487096470cdd59562b42 [file] [log] [blame]
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
19#include <new>
20#include <stdexcept>
21#include <cstddef>
22#include <cstdint>
23#include <limits>
24#include <type_traits>
25#include <utility>
26#include <array>
27#include <iterator>
28#include "fail_fast.h"
29
30#ifndef _MSC_VER
31#define _CONSTEXPR constexpr
32#else
33#define _CONSTEXPR
34#endif
35
36#pragma push_macro("_NOEXCEPT")
37
38#ifndef _NOEXCEPT
39
40#ifdef SAFER_CPP_TESTING
41#define _NOEXCEPT
42#else
43#define _NOEXCEPT noexcept
44#endif
45
46#else // _NOEXCEPT
47
48#ifdef SAFER_CPP_TESTING
49#undef _NOEXCEPT
50#define _NOEXCEPT
51#endif
52
53#endif // _NOEXCEPT
54
55namespace Guide {
56
57/*
58** begin definitions of index and bounds
59*/
60namespace details
61{
62 template <typename SizeType>
63 struct SizeTypeTraits
64 {
65 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);
66 };
67
68
69 template <typename ConcreteType, typename ValueType, unsigned int Rank>
70 class coordinate_facade
71 {
72 static_assert(std::is_integral<ValueType>::value
73 && sizeof(ValueType) <= sizeof(size_t), "ValueType must be unsigned integral type!");
74 static_assert(Rank > 0, "Rank must be greater than 0!");
75
76 template <typename OtherConcreteType, typename OtherValueType, unsigned int OtherRank>
77 friend class coordinate_facade;
78 public:
79 using reference = ValueType&;
80 using const_reference = const ValueType&;
81 using value_type = ValueType;
82 static const unsigned int rank = Rank;
83 _CONSTEXPR coordinate_facade() _NOEXCEPT
84 {
85 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
86 for (unsigned int i = 0; i < rank; ++i)
87 elems[i] = {};
88 }
89 _CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
90 {
91 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
92 static_assert(rank == 1, "This constructor can only be used with rank == 1.");
93 elems[0] = e0;
94 }
95 // Preconditions: il.size() == rank
96 _CONSTEXPR coordinate_facade(std::initializer_list<value_type> il)
97 {
98 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
99 fail_fast_assert(il.size() == rank);
100 for (unsigned int i = 0; i < rank; ++i)
101 {
102 elems[i] = begin(il)[i];
103 }
104 }
105
106 _CONSTEXPR coordinate_facade(const coordinate_facade & other) = default;
107
108 template <typename OtherConcreteType, typename OtherValueType>
109 _CONSTEXPR coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
110 {
111 for (unsigned int i = 0; i < rank; ++i)
112 {
113 fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
114 elems[i] = static_cast<value_type>(other.elems[i]);
115 }
116 }
117 protected:
118 coordinate_facade& operator=(const coordinate_facade& rhs) = default;
119 // Preconditions: component_idx < rank
120 _CONSTEXPR reference operator[](unsigned int component_idx)
121 {
122 fail_fast_assert(component_idx < rank);
123 return elems[component_idx];
124 }
125 // Preconditions: component_idx < rank
126 _CONSTEXPR const_reference operator[](unsigned int component_idx) const
127 {
128 fail_fast_assert(component_idx < rank);
129 return elems[component_idx];
130 }
131 _CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
132 {
133 for (unsigned int i = 0; i < rank; ++i)
134 {
135 if (elems[i] != rhs.elems[i])
136 return false;
137 }
138 return true;
139 }
140 _CONSTEXPR bool operator!=(const ConcreteType& rhs) const _NOEXCEPT
141 {
142 return !(to_concrete() == rhs);
143 }
144 _CONSTEXPR ConcreteType operator+() const _NOEXCEPT
145 {
146 return to_concrete();
147 }
148 _CONSTEXPR ConcreteType operator-() const
149 {
150 ConcreteType ret = to_concrete();
151 for (unsigned int i = 0; i < rank; ++i)
152 ret.elems[i] = -ret.elems[i];
153 return ret;
154 }
155 _CONSTEXPR ConcreteType operator+(const ConcreteType& rhs) const
156 {
157 ConcreteType ret = to_concrete();
158 ret += rhs;
159 return ret;
160 }
161 _CONSTEXPR ConcreteType operator-(const ConcreteType& rhs) const
162 {
163 ConcreteType ret = to_concrete();
164 ret -= rhs;
165 return ret;
166 }
167 _CONSTEXPR ConcreteType& operator+=(const ConcreteType& rhs)
168 {
169 for (unsigned int i = 0; i < rank; ++i)
170 elems[i] += rhs.elems[i];
171 return to_concrete();
172 }
173 _CONSTEXPR ConcreteType& operator-=(const ConcreteType& rhs)
174 {
175 for (unsigned int i = 0; i < rank; ++i)
176 elems[i] -= rhs.elems[i];
177 return to_concrete();
178 }
179 _CONSTEXPR ConcreteType& operator++()
180 {
181 static_assert(rank == 1, "This operator can only be used with rank == 1.");
182 ++elems[0];
183 return to_concrete();
184 }
185 _CONSTEXPR ConcreteType operator++(int)
186 {
187 static_assert(rank == 1, "This operator can only be used with rank == 1.");
188 ConcreteType ret = to_concrete();
189 ++(*this);
190 return ret;
191 }
192 _CONSTEXPR ConcreteType& operator--()
193 {
194 static_assert(rank == 1, "This operator can only be used with rank == 1.");
195 --elems[0];
196 return to_concrete();
197 }
198 _CONSTEXPR ConcreteType operator--(int)
199 {
200 static_assert(rank == 1, "This operator can only be used with rank == 1.");
201 ConcreteType ret = to_concrete();
202 --(*this);
203 return ret;
204 }
205 _CONSTEXPR ConcreteType operator*(value_type v) const
206 {
207 ConcreteType ret = to_concrete();
208 ret *= v;
209 return ret;
210 }
211 _CONSTEXPR ConcreteType operator/(value_type v) const
212 {
213 ConcreteType ret = to_concrete();
214 ret /= v;
215 return ret;
216 }
217 friend _CONSTEXPR ConcreteType operator*(value_type v, const ConcreteType& rhs)
218 {
219 return rhs * v;
220 }
221 _CONSTEXPR ConcreteType& operator*=(value_type v)
222 {
223 for (unsigned int i = 0; i < rank; ++i)
224 elems[i] *= v;
225 return to_concrete();
226 }
227 _CONSTEXPR ConcreteType& operator/=(value_type v)
228 {
229 for (unsigned int i = 0; i < rank; ++i)
230 elems[i] /= v;
231 return to_concrete();
232 }
233 value_type elems[rank];
234 private:
235 _CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
236 {
237 return static_cast<const ConcreteType&>(*this);
238 }
239 _CONSTEXPR ConcreteType& to_concrete() _NOEXCEPT
240 {
241 return static_cast<ConcreteType&>(*this);
242 }
243 };
244 template <typename T>
245 class arrow_proxy
246 {
247 public:
248 explicit arrow_proxy(T t)
249 : val(t)
250 {}
251 const T operator*() const _NOEXCEPT
252 {
253 return val;
254 }
255 const T* operator->() const _NOEXCEPT
256 {
257 return &val;
258 }
259 private:
260 T val;
261 };
262}
263
264template <unsigned int Rank, typename ValueType = size_t>
265class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
266{
267 using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
268 friend Base;
269
270public:
271 using Base::rank;
272 using reference = typename Base::reference;
273 using const_reference = typename Base::const_reference;
274 using size_type = typename Base::value_type;
275 using value_type = typename Base::value_type;
276 _CONSTEXPR index() _NOEXCEPT : Base(){}
277 template <bool Enabled = rank == 1, typename = std::enable_if_t<Enabled>>
278 _CONSTEXPR index(value_type e0) _NOEXCEPT : Base(e0){}
279 _CONSTEXPR index(std::initializer_list<value_type> il) : Base(il){}
280
281 _CONSTEXPR index(const index &) = default;
282
283 template <typename OtherValueType>
284 _CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
285 {
286 }
287
288 using Base::operator[];
289 using Base::operator==;
290 using Base::operator!=;
291 using Base::operator+;
292 using Base::operator-;
293 using Base::operator+=;
294 using Base::operator-=;
295 using Base::operator++;
296 using Base::operator--;
297 using Base::operator*;
298 using Base::operator/;
299 using Base::operator*=;
300 using Base::operator/=;
301};
302
303template <typename ValueType>
304class index<1, ValueType>
305{
306 template <unsigned int, typename OtherValueType>
307 friend class index;
308public:
309 static const unsigned int rank = 1;
310 using reference = ValueType&;
311 using const_reference = const ValueType&;
312 using size_type = ValueType;
313 using value_type = ValueType;
314
315 _CONSTEXPR index() _NOEXCEPT : value(0)
316 {
317 }
318 _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
319 {
320 }
321 // Preconditions: il.size() == rank
322 _CONSTEXPR index(std::initializer_list<value_type> il)
323 {
324 fail_fast_assert(il.size() == rank);
325 value = begin(il)[0];
326 }
327
328 _CONSTEXPR index(const index &) = default;
329
330 template <typename OtherValueType>
331 _CONSTEXPR index(const index<1, OtherValueType> & other)
332 {
333 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
334 value = static_cast<ValueType>(other.value);
335 }
336
337
338 // Preconditions: component_idx < rank
339 _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
340 {
341 fail_fast_assert(component_idx == 0);
342 (void)(component_idx);
343 return value;
344 }
345 // Preconditions: component_idx < rank
346 _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
347 {
348 fail_fast_assert(component_idx == 0);
349 (void)(component_idx);
350 return value;
351 }
352 _CONSTEXPR bool operator==(const index& rhs) const _NOEXCEPT
353 {
354 return value == rhs.value;
355 }
356 _CONSTEXPR bool operator!=(const index& rhs) const _NOEXCEPT
357 {
358 return !(*this == rhs);
359 }
360 _CONSTEXPR index operator+() const _NOEXCEPT
361 {
362 return *this;
363 }
364 _CONSTEXPR index operator-() const _NOEXCEPT
365 {
366 return index(-value);
367 }
368 _CONSTEXPR index operator+(const index& rhs) const _NOEXCEPT
369 {
370 return index(value + rhs.value);
371 }
372 _CONSTEXPR index operator-(const index& rhs) const _NOEXCEPT
373 {
374 return index(value - rhs.value);
375 }
376 _CONSTEXPR index& operator+=(const index& rhs) _NOEXCEPT
377 {
378 value += rhs.value;
379 return *this;
380 }
381 _CONSTEXPR index& operator-=(const index& rhs) _NOEXCEPT
382 {
383 value -= rhs.value;
384 return *this;
385 }
386 _CONSTEXPR index& operator++() _NOEXCEPT
387 {
388 ++value;
389 return *this;
390 }
391 _CONSTEXPR index operator++(int) _NOEXCEPT
392 {
393 index ret = *this;
394 ++(*this);
395 return ret;
396 }
397 _CONSTEXPR index& operator--() _NOEXCEPT
398 {
399 --value;
400 return *this;
401 }
402 _CONSTEXPR index operator--(int) _NOEXCEPT
403 {
404 index ret = *this;
405 --(*this);
406 return ret;
407 }
408 _CONSTEXPR index operator*(value_type v) const _NOEXCEPT
409 {
410 return index(value * v);
411 }
412 _CONSTEXPR index operator/(value_type v) const _NOEXCEPT
413 {
414 return index(value / v);
415 }
416 _CONSTEXPR index& operator*=(value_type v) _NOEXCEPT
417 {
418 value *= v;
419 return *this;
420 }
421 _CONSTEXPR index& operator/=(value_type v) _NOEXCEPT
422 {
423 value /= v;
424 return *this;
425 }
426 friend _CONSTEXPR index operator*(value_type v, const index& rhs) _NOEXCEPT
427 {
428 return index(rhs * v);
429 }
430private:
431 value_type value;
432};
433
434#ifndef _MSC_VER
435
436struct static_bounds_dynamic_range_t
437{
438 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
439 constexpr operator T() const noexcept
440 {
441 return static_cast<T>(-1);
442 }
443
444 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
445 constexpr bool operator ==(T other) const noexcept
446 {
447 return static_cast<T>(-1) == other;
448 }
449
450 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
451 constexpr bool operator !=(T other) const noexcept
452 {
453 return static_cast<T>(-1) != other;
454 }
455
456};
457
458template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
459constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
460{
461 return right == left;
462}
463
464template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
465constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
466{
467 return right != left;
468}
469
470constexpr static_bounds_dynamic_range_t dynamic_range{};
471#else
472const char dynamic_range = -1;
473#endif
474
475struct generalized_mapping_tag {};
476struct contiguous_mapping_tag : generalized_mapping_tag {};
477
478namespace details
479{
480 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
481 struct StaticSizeHelperImpl
482 {
483 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
484 static const SizeType value = Fact1 * Fact2;
485 };
486
487 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
488 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
489 {
490 static const SizeType value = ConstBound;
491 };
492
493 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
494 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
495 {
496 static const SizeType value = ConstBound;
497 };
498
499 template <typename SizeType, SizeType ConstBound>
500 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
501 {
502 static const SizeType value = static_cast<SizeType>(ConstBound);
503 };
504
505 template <typename SizeType, SizeType Fact1, SizeType Fact2>
506 struct StaticSizeHelper
507 {
508 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
509 };
510
511
512 template <size_t Left, size_t Right>
513 struct LessThan
514 {
515 static const bool value = Left < Right;
516 };
517
518 template <typename SizeType, size_t... Ranges>
519 struct BoundsRanges {
520 static const unsigned int Depth = 0;
521 static const unsigned int DynamicNum = 0;
522 static const SizeType CurrentRange = 1;
523 static const SizeType TotalSize = 1;
524
525 BoundsRanges (const BoundsRanges &) = default;
526
527 // TODO : following signature is for work around VS bug
528 template <typename OtherType>
529 BoundsRanges (const OtherType &, bool firstLevel) {}
530 BoundsRanges(const SizeType * const arr) { }
531 BoundsRanges() = default;
532
533
534 template <typename T, unsigned int Dim>
535 void serialize(T &) const {
536 }
537 template <typename T, unsigned int Dim>
538 SizeType linearize(const T &) const {
539 return 0;
540 }
541 template <typename T, unsigned int Dim>
542 ptrdiff_t contains(const T &) const {
543 return 0;
544 }
545
546 size_t totalSize() const _NOEXCEPT {
547 return TotalSize;
548 }
549
550 bool operator == (const BoundsRanges &) const _NOEXCEPT
551 {
552 return true;
553 }
554 };
555
556 template <typename SizeType, size_t... RestRanges>
557 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
558 using Base = BoundsRanges <SizeType, RestRanges... >;
559 static const unsigned int Depth = Base::Depth + 1;
560 static const unsigned int DynamicNum = Base::DynamicNum + 1;
561 static const SizeType CurrentRange = dynamic_range;
562 static const SizeType TotalSize = dynamic_range;
563 const SizeType m_bound;
564
565 BoundsRanges (const BoundsRanges &) = default;
566 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
567 {
568 fail_fast_assert(0 <= *arr);
569 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
570 }
571 BoundsRanges() : m_bound(0) {}
572
573 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
574 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) :
575 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
576 {
577 }
578
579 template <typename T, unsigned int Dim = 0>
580 void serialize(T & arr) const {
581 arr[Dim] = elementNum();
582 this->Base::template serialize<T, Dim + 1>(arr);
583 }
584 template <typename T, unsigned int Dim = 0>
585 SizeType linearize(const T & arr) const {
586 const size_t index = this->Base::totalSize() * arr[Dim];
587 fail_fast_assert(index < static_cast<size_t>(m_bound));
588 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
589 }
590
591 template <typename T, unsigned int Dim = 0>
592 ptrdiff_t contains(const T & arr) const {
593 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
594 if (last == -1)
595 return -1;
596 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
597 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
598 }
599
600 size_t totalSize() const _NOEXCEPT {
601 return m_bound;
602 }
603
604 SizeType elementNum() const _NOEXCEPT {
605 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
606 }
607
608 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
609 if (dim > 0)
610 return this->Base::elementNum(dim - 1);
611 else
612 return elementNum();
613 }
614
615 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
616 {
617 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
618 }
619 };
620
621 template <typename SizeType, size_t CurRange, size_t... RestRanges>
622 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
623 using Base = BoundsRanges <SizeType, RestRanges... >;
624 static const unsigned int Depth = Base::Depth + 1;
625 static const unsigned int DynamicNum = Base::DynamicNum;
626 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
627 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
628 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
629
630 BoundsRanges (const BoundsRanges &) = default;
631 BoundsRanges(const SizeType * const arr) : Base(arr) { }
632 BoundsRanges() = default;
633
634 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
635 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
636 {
637 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
638 }
639
640 template <typename T, unsigned int Dim = 0>
641 void serialize(T & arr) const {
642 arr[Dim] = elementNum();
643 this->Base::template serialize<T, Dim + 1>(arr);
644 }
645
646 template <typename T, unsigned int Dim = 0>
647 SizeType linearize(const T & arr) const {
648 fail_fast_assert(arr[Dim] < CurrentRange);
649 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
650 }
651
652 template <typename T, unsigned int Dim = 0>
653 ptrdiff_t contains(const T & arr) const {
654 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
655 return -1;
656 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
657 if (last == -1)
658 return -1;
659 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
660 }
661
662 size_t totalSize() const _NOEXCEPT{
663 return CurrentRange * this->Base::totalSize();
664 }
665
666 SizeType elementNum() const _NOEXCEPT{
667 return CurrentRange;
668 }
669
670 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
671 if (dim > 0)
672 return this->Base::elementNum(dim - 1);
673 else
674 return elementNum();
675 }
676
677 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
678 {
679 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
680 }
681 };
682
683 template <typename SourceType, typename TargetType, size_t Rank>
684 struct BoundsRangeConvertible2;
685
686 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
687 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
688 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
689
690 template <size_t Rank, typename SourceType, typename TargetType>
691 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
692
693 template <typename SourceType, typename TargetType, size_t Rank>
694 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
695 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
696 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
697 {};
698
699 template <typename SourceType, typename TargetType>
700 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
701
702 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
703 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
704 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
705 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
706 {};
707 template <typename SourceType, typename TargetType>
708 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
709
710 template <typename TypeChain>
711 struct TypeListIndexer
712 {
713 const TypeChain & obj;
714 TypeListIndexer(const TypeChain & obj) :obj(obj){}
715 template<unsigned int N>
716 const TypeChain & getObj(std::true_type)
717 {
718 return obj;
719 }
720 template<unsigned int N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
721 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
722 {
723 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
724 }
725 template <unsigned int N>
726 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
727 {
728 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
729 }
730 };
731
732 template <typename TypeChain>
733 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
734 {
735 return TypeListIndexer<TypeChain>(obj);
736 }
737}
738
739template <typename IndexType>
740class bounds_iterator;
741
742template <typename SizeType, size_t... Ranges>
743class static_bounds {
744public:
745 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &empty) {
746 }
747};
748
749template <typename SizeType, size_t FirstRange, size_t... RestRanges>
750class static_bounds<SizeType, FirstRange, RestRanges...>
751{
752 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
753 static_assert(std::is_integral<SizeType>::value
754 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
755
756 MyRanges m_ranges;
757 _CONSTEXPR static_bounds(const MyRanges & range) : m_ranges(range) { }
758
759 template <typename SizeType2, size_t... Ranges2>
760 friend class static_bounds;
761public:
762 static const unsigned int rank = MyRanges::Depth;
763 static const unsigned int dynamic_rank = MyRanges::DynamicNum;
764 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
765
766 using size_type = SizeType;
767 using index_type = index<rank, size_type>;
768 using iterator = bounds_iterator<index_type>;
769 using const_iterator = bounds_iterator<index_type>;
770 using difference_type = ptrdiff_t;
771 using sliced_type = static_bounds<SizeType, RestRanges...>;
772 using mapping_type = contiguous_mapping_tag;
773public:
774 _CONSTEXPR static_bounds(const static_bounds &) = default;
775
776 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
777 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
778 _CONSTEXPR static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
779 m_ranges(other.m_ranges)
780 {
781 }
782
783 _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
784 {
785 fail_fast_assert(MyRanges::DynamicNum == il.size() && m_ranges.totalSize() <= details::SizeTypeTraits<size_type>::max_value);
786 }
787
788 _CONSTEXPR static_bounds() = default;
789
790 _CONSTEXPR static_bounds & operator = (const static_bounds & otherBounds)
791 {
792 new(&m_ranges) MyRanges (otherBounds.m_ranges);
793 return *this;
794 }
795
796 _CONSTEXPR sliced_type slice() const _NOEXCEPT
797 {
798 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
799 }
800
801 _CONSTEXPR size_type stride() const _NOEXCEPT
802 {
803 return rank > 1 ? slice().size() : 1;
804 }
805
806 _CONSTEXPR size_type size() const _NOEXCEPT
807 {
808 return static_cast<size_type>(m_ranges.totalSize());
809 }
810
811 _CONSTEXPR size_type linearize(const index_type & idx) const
812 {
813 return m_ranges.linearize(idx);
814 }
815
816 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
817 {
818 return m_ranges.contains(idx) != -1;
819 }
820
821 _CONSTEXPR size_type operator[](unsigned int index) const _NOEXCEPT
822 {
823 return m_ranges.elementNum(index);
824 }
825
826 template <unsigned int Dim = 0>
827 _CONSTEXPR size_type extent() const _NOEXCEPT
828 {
829 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
830 }
831
832 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
833 {
834 index_type extents;
835 m_ranges.serialize(extents);
836 return extents;
837 }
838
839 template <typename OtherSizeTypes, size_t... Ranges>
840 _CONSTEXPR bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
841 {
842 return this->size() == rhs.size();
843 }
844
845 template <typename OtherSizeTypes, size_t... Ranges>
846 _CONSTEXPR bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
847 {
848 return !(*this == rhs);
849 }
850
851 _CONSTEXPR const_iterator begin() const _NOEXCEPT
852 {
853 return const_iterator(*this);
854 }
855
856 _CONSTEXPR const_iterator end() const _NOEXCEPT
857 {
858 index_type boundary;
859 m_ranges.serialize(boundary);
860 return const_iterator(*this, this->index_bounds());
861 }
862};
863
864template <unsigned int Rank, typename SizeType = size_t>
865class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
866{
867 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
868 friend Base;
869 _CONSTEXPR void makeRegularStriae() _NOEXCEPT
870 {
871 strides[rank - 1] = 1;
872 for (int i = rank - 2; i >= 0; i--)
873 strides[i] = strides[i + 1] * Base::elems[i + 1];
874 }
875public:
876 using Base::rank;
877 using reference = typename Base::reference;
878 using const_reference = typename Base::const_reference;
879 using size_type = typename Base::value_type;
880 using difference_type = typename Base::value_type;
881 using value_type = typename Base::value_type;
882 using index_type = index<rank, size_type>;
883 using iterator = bounds_iterator<index_type>;
884 using const_iterator = bounds_iterator<index_type>;
885 static const int dynamic_rank = rank;
886 static const size_t static_size = dynamic_range;
887 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
888 using mapping_type = generalized_mapping_tag;
889 _CONSTEXPR strided_bounds() _NOEXCEPT : Base(), strides() {}
890 _CONSTEXPR strided_bounds(const strided_bounds &) = default;
891
892 template <typename OtherSizeType>
893 _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other) : Base(other)
894 {
895 }
896
897 _CONSTEXPR strided_bounds(const index_type &extents, const index_type &stride)
898 : strides(stride)
899 {
900 for (unsigned int i = 0; i < rank; i++)
901 Base::elems[i] = extents[i];
902 }
903 _CONSTEXPR strided_bounds(std::initializer_list<value_type> il)
904 : Base(il)
905 {
906#ifndef NDEBUG
907 for (const auto& v : il)
908 {
909 fail_fast_assert(v >= 0);
910 }
911#endif
912 makeRegularStriae();
913 }
914 index_type strides;
915 _CONSTEXPR size_type size() const _NOEXCEPT
916 {
917 size_type ret = 0;
918 for (unsigned int i = 0; i < rank; ++i)
919 ret += (Base::elems[i] - 1) * strides[i];
920 return ret;
921 }
922 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
923 {
924 for (unsigned int i = 0; i < rank; ++i)
925 {
926 if (idx[i] < 0 || idx[i] >= Base::elems[i])
927 return false;
928 }
929 return true;
930 }
931 _CONSTEXPR size_type linearize(const index_type & idx) const
932 {
933 size_type ret = 0;
934 for (unsigned int i = 0; i < rank; i++)
935 {
936 fail_fast_assert(idx[i] < Base::elems[i]);
937 ret += idx[i] * strides[i];
938 }
939 return ret;
940 }
941 _CONSTEXPR sliced_type slice() const
942 {
943 sliced_type ret;
944 for (unsigned int i = 1; i < rank; ++i)
945 {
946 ret.elems[i - 1] = Base::elems[i];
947 ret.strides[i - 1] = strides[i];
948 }
949 return ret;
950 }
951 template <unsigned int Dim = 0>
952 _CONSTEXPR size_type extent() const _NOEXCEPT
953 {
954 return Base::elems[Dim];
955 }
956 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
957 {
958 index_type extents;
959 for (unsigned int i = 0; i < rank; ++i)
960 extents[i] = (*this)[i];
961 return extents;
962 }
963 const_iterator begin() const _NOEXCEPT
964 {
965 return const_iterator{ *this };
966 }
967 const_iterator end() const _NOEXCEPT
968 {
969 return const_iterator{ *this, index_bounds() };
970 }
971};
972
973template <typename T>
974struct is_bounds : std::integral_constant<bool, false> {};
975template <typename SizeType, size_t... Ranges>
976struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
977template <unsigned int Rank, typename SizeType>
978struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
979
980template <typename IndexType>
981class bounds_iterator
982 : public std::iterator<std::random_access_iterator_tag,
983 IndexType,
984 ptrdiff_t,
985 const details::arrow_proxy<IndexType>,
986 const IndexType>
987{
988private:
989 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
990public:
991 static const unsigned int rank = IndexType::rank;
992 using typename Base::reference;
993 using typename Base::pointer;
994 using typename Base::difference_type;
995 using typename Base::value_type;
996 using index_type = value_type;
997 using index_size_type = typename IndexType::size_type;
998 template <typename Bounds>
999 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT
1000 : boundary(bnd.index_bounds())
1001 , curr( std::move(curr) )
1002 {
1003 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1004 }
1005 reference operator*() const _NOEXCEPT
1006 {
1007 return curr;
1008 }
1009 pointer operator->() const _NOEXCEPT
1010 {
1011 return details::arrow_proxy<value_type>{ curr };
1012 }
1013 bounds_iterator& operator++() _NOEXCEPT
1014 {
1015 for (unsigned int i = rank; i-- > 0;)
1016 {
1017 if (++curr[i] < boundary[i])
1018 {
1019 return *this;
1020 }
1021 else
1022 {
1023 curr[i] = 0;
1024 }
1025 }
1026 // If we're here we've wrapped over - set to past-the-end.
1027 for (unsigned int i = 0; i < rank; ++i)
1028 {
1029 curr[i] = boundary[i];
1030 }
1031 return *this;
1032 }
1033 bounds_iterator operator++(int) _NOEXCEPT
1034 {
1035 auto ret = *this;
1036 ++(*this);
1037 return ret;
1038 }
1039 bounds_iterator& operator--() _NOEXCEPT
1040 {
1041 for (int i = rank; i-- > 0;)
1042 {
1043 if (curr[i]-- > 0)
1044 {
1045 return *this;
1046 }
1047 else
1048 {
1049 curr[i] = boundary[i] - 1;
1050 }
1051 }
1052 // If we're here the preconditions were violated
1053 // "pre: there exists s such that r == ++s"
1054 fail_fast_assert(false);
1055 return *this;
1056 }
1057 bounds_iterator operator--(int) _NOEXCEPT
1058 {
1059 auto ret = *this;
1060 --(*this);
1061 return ret;
1062 }
1063 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1064 {
1065 bounds_iterator ret{ *this };
1066 return ret += n;
1067 }
1068 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1069 {
1070 auto linear_idx = linearize(curr) + n;
1071 value_type stride;
1072 stride[rank - 1] = 1;
1073 for (unsigned int i = rank - 1; i-- > 0;)
1074 {
1075 stride[i] = stride[i + 1] * boundary[i + 1];
1076 }
1077 for (unsigned int i = 0; i < rank; ++i)
1078 {
1079 curr[i] = linear_idx / stride[i];
1080 linear_idx = linear_idx % stride[i];
1081 }
1082 return *this;
1083 }
1084 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1085 {
1086 bounds_iterator ret{ *this };
1087 return ret -= n;
1088 }
1089 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1090 {
1091 return *this += -n;
1092 }
1093 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1094 {
1095 return linearize(curr) - linearize(rhs.curr);
1096 }
1097 reference operator[](difference_type n) const _NOEXCEPT
1098 {
1099 return *(*this + n);
1100 }
1101 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1102 {
1103 return curr == rhs.curr;
1104 }
1105 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1106 {
1107 return !(*this == rhs);
1108 }
1109 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1110 {
1111 for (unsigned int i = 0; i < rank; ++i)
1112 {
1113 if (curr[i] < rhs.curr[i])
1114 return true;
1115 }
1116 return false;
1117 }
1118 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1119 {
1120 return !(rhs < *this);
1121 }
1122 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1123 {
1124 return rhs < *this;
1125 }
1126 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1127 {
1128 return !(rhs > *this);
1129 }
1130 void swap(bounds_iterator& rhs) _NOEXCEPT
1131 {
1132 std::swap(boundary, rhs.boundary);
1133 std::swap(curr, rhs.curr);
1134 }
1135private:
1136 index_size_type linearize(const value_type& idx) const _NOEXCEPT
1137 {
1138 // TODO: Smarter impl.
1139 // Check if past-the-end
1140 bool pte = true;
1141 for (unsigned int i = 0; i < rank; ++i)
1142 {
1143 if (idx[i] != boundary[i])
1144 {
1145 pte = false;
1146 break;
1147 }
1148 }
1149 index_size_type multiplier = 1;
1150 index_size_type res = 0;
1151 if (pte)
1152 {
1153 res = 1;
1154 for (unsigned int i = rank; i-- > 0;)
1155 {
1156 res += (idx[i] - 1) * multiplier;
1157 multiplier *= boundary[i];
1158 }
1159 }
1160 else
1161 {
1162 for (unsigned int i = rank; i-- > 0;)
1163 {
1164 res += idx[i] * multiplier;
1165 multiplier *= boundary[i];
1166 }
1167 }
1168 return res;
1169 }
1170 value_type boundary;
1171 value_type curr;
1172};
1173
1174template <typename SizeType>
1175class bounds_iterator<index<1, SizeType>>
1176 : public std::iterator<std::random_access_iterator_tag,
1177 index<1, SizeType>,
1178 ptrdiff_t,
1179 const details::arrow_proxy<index<1, SizeType>>,
1180 const index<1, SizeType>>
1181{
1182 using Base = std::iterator<std::random_access_iterator_tag, index<1, SizeType>, ptrdiff_t, const details::arrow_proxy<index<1, SizeType>>, const index<1, SizeType>>;
1183
1184public:
1185 using typename Base::reference;
1186 using typename Base::pointer;
1187 using typename Base::difference_type;
1188 using typename Base::value_type;
1189 using index_type = value_type;
1190 using index_size_type = typename index_type::size_type;
1191
1192 template <typename Bounds>
1193 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT
1194 : curr( std::move(curr) )
1195 {}
1196 reference operator*() const _NOEXCEPT
1197 {
1198 return curr;
1199 }
1200 pointer operator->() const _NOEXCEPT
1201 {
1202 return details::arrow_proxy<value_type>{ curr };
1203 }
1204 bounds_iterator& operator++() _NOEXCEPT
1205 {
1206 ++curr;
1207 return *this;
1208 }
1209 bounds_iterator operator++(int) _NOEXCEPT
1210 {
1211 auto ret = *this;
1212 ++(*this);
1213 return ret;
1214 }
1215 bounds_iterator& operator--() _NOEXCEPT
1216 {
1217 curr--;
1218 return *this;
1219 }
1220 bounds_iterator operator--(int) _NOEXCEPT
1221 {
1222 auto ret = *this;
1223 --(*this);
1224 return ret;
1225 }
1226 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1227 {
1228 bounds_iterator ret{ *this };
1229 return ret += n;
1230 }
1231 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1232 {
1233 curr += n;
1234 return *this;
1235 }
1236 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1237 {
1238 bounds_iterator ret{ *this };
1239 return ret -= n;
1240 }
1241 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1242 {
1243 return *this += -n;
1244 }
1245 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1246 {
1247 return curr[0] - rhs.curr[0];
1248 }
1249 reference operator[](difference_type n) const _NOEXCEPT
1250 {
1251 return curr + n;
1252 }
1253 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1254 {
1255 return curr == rhs.curr;
1256 }
1257 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1258 {
1259 return !(*this == rhs);
1260 }
1261 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1262 {
1263 return curr[0] < rhs.curr[0];
1264 }
1265 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1266 {
1267 return !(rhs < *this);
1268 }
1269 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1270 {
1271 return rhs < *this;
1272 }
1273 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1274 {
1275 return !(rhs > *this);
1276 }
1277 void swap(bounds_iterator& rhs) _NOEXCEPT
1278 {
1279 std::swap(curr, rhs.curr);
1280 }
1281private:
1282 value_type curr;
1283};
1284
1285template <typename IndexType>
1286bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) _NOEXCEPT
1287{
1288 return rhs + n;
1289}
1290
1291/*
1292** begin definitions of basic_array_view
1293*/
1294namespace details
1295{
1296 template <typename Bounds>
1297 _CONSTEXPR std::enable_if_t<std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT
1298 {
1299 return bnd.strides;
1300 }
1301
1302 // Make a stride vector from bounds, assuming continugous memory.
1303 template <typename Bounds>
1304 _CONSTEXPR std::enable_if_t<std::is_same<typename Bounds::mapping_type, contiguous_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT
1305 {
1306 auto extents = bnd.index_bounds();
1307 typename Bounds::index_type stride;
1308 stride[Bounds::rank - 1] = 1;
1309 for (int i = Bounds::rank - 2; i >= 0; --i)
1310 stride[i] = stride[i + 1] * extents[i + 1];
1311 return stride;
1312 }
1313
1314 template <typename BoundsSrc, typename BoundsDest>
1315 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1316 {
1317 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1318 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1319 static_assert(BoundsDest::static_size == dynamic_range || BoundsSrc::static_size == dynamic_range || BoundsDest::static_size == BoundsSrc::static_size, "The source bounds must have same size as dest bounds");
1320 fail_fast_assert(src.size() == dest.size());
1321 }
1322
1323
1324} // namespace details
1325
1326template <typename ArrayView>
1327class contiguous_array_view_iterator;
1328template <typename ArrayView>
1329class general_array_view_iterator;
1330enum class byte : std::uint8_t {};
1331
1332template <typename ValueType, typename BoundsType>
1333class basic_array_view
1334{
1335public:
1336 static const unsigned int rank = BoundsType::rank;
1337 using bounds_type = BoundsType;
1338 using size_type = typename bounds_type::size_type;
1339 using index_type = typename bounds_type::index_type;
1340 using value_type = ValueType;
1341 using pointer = ValueType*;
1342 using reference = ValueType&;
1343 using iterator = std::conditional_t<std::is_same<typename BoundsType::mapping_type, contiguous_mapping_tag>::value, contiguous_array_view_iterator<basic_array_view>, general_array_view_iterator<basic_array_view>>;
1344 using const_iterator = std::conditional_t<std::is_same<typename BoundsType::mapping_type, contiguous_mapping_tag>::value, contiguous_array_view_iterator<basic_array_view<const ValueType, BoundsType>>, general_array_view_iterator<basic_array_view<const ValueType, BoundsType>>>;
1345 using reverse_iterator = std::reverse_iterator<iterator>;
1346 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1347 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1348
1349private:
1350 pointer m_pdata;
1351 bounds_type m_bounds;
1352
1353public:
1354 _CONSTEXPR bounds_type bounds() const _NOEXCEPT
1355 {
1356 return m_bounds;
1357 }
1358 template <unsigned int Dim = 0>
1359 _CONSTEXPR size_type extent() const _NOEXCEPT
1360 {
1361 return m_bounds.template extent<Dim>();
1362 }
1363 _CONSTEXPR size_type size() const _NOEXCEPT
1364 {
1365 return m_bounds.size();
1366 }
1367 _CONSTEXPR reference operator[](const index_type& idx) const
1368 {
1369 return m_pdata[m_bounds.linearize(idx)];
1370 }
1371 _CONSTEXPR pointer data() const _NOEXCEPT
1372 {
1373 return m_pdata;
1374 }
1375 template <bool Enabled = rank != 1, typename Ret = std::enable_if_t<Enabled, sliced_type>>
1376 _CONSTEXPR Ret operator[](size_type idx) const
1377 {
1378 const size_type ridx = idx * m_bounds.stride();
1379 fail_fast_assert(ridx < m_bounds.size());
1380 return Ret {m_pdata + ridx, m_bounds.slice()};
1381 }
1382
1383 _CONSTEXPR operator bool () const _NOEXCEPT
1384 {
1385 return m_pdata != nullptr;
1386 }
1387
1388 _CONSTEXPR iterator begin() const
1389 {
1390 return iterator {this, true};
1391 }
1392 _CONSTEXPR iterator end() const
1393 {
1394 return iterator {this};
1395 }
1396 _CONSTEXPR const_iterator cbegin() const
1397 {
1398 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1399 }
1400 _CONSTEXPR const_iterator cend() const
1401 {
1402 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1403 }
1404
1405 _CONSTEXPR reverse_iterator rbegin() const
1406 {
1407 return reverse_iterator {end()};
1408 }
1409 _CONSTEXPR reverse_iterator rend() const
1410 {
1411 return reverse_iterator {begin()};
1412 }
1413 _CONSTEXPR const_reverse_iterator crbegin() const
1414 {
1415 return const_reverse_iterator {cend()};
1416 }
1417 _CONSTEXPR const_reverse_iterator crend() const
1418 {
1419 return const_reverse_iterator {cbegin()};
1420 }
1421
1422 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1423 _CONSTEXPR auto operator == (const basic_array_view<OtherValueType, OtherBoundsType> & other) const -> decltype(this->m_pdata == other.m_pdata && this->m_bounds == other.m_bounds) _NOEXCEPT
1424 {
1425 return m_pdata == other.m_pdata && m_bounds == other.m_bounds;
1426 }
1427
1428public:
1429 template <typename OtherValueType, typename OtherBounds,
1430 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1431 && std::is_convertible<OtherBounds, bounds_type>::value>>
1432 _CONSTEXPR basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) _NOEXCEPT
1433 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1434 {
1435 }
1436protected:
1437
1438 _CONSTEXPR basic_array_view(pointer data, bounds_type bound) _NOEXCEPT
1439 : m_pdata(data)
1440 , m_bounds(std::move(bound))
1441 {
1442 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1443 }
1444 template <typename T>
1445 _CONSTEXPR basic_array_view(T *data, std::enable_if_t<std::is_same<value_type, std::remove_all_extents_t<T>>::value, bounds_type> bound) _NOEXCEPT
1446 : m_pdata(reinterpret_cast<pointer>(data))
1447 , m_bounds(std::move(bound))
1448 {
1449 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1450 }
1451 template <typename DestBounds>
1452 _CONSTEXPR basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1453 {
1454 details::verifyBoundsReshape(m_bounds, bounds);
1455 return {m_pdata, bounds};
1456 }
1457private:
1458
1459 friend iterator;
1460 friend const_iterator;
1461 template <typename ValueType2, typename BoundsType2>
1462 friend class basic_array_view;
1463};
1464
1465template <size_t DimSize = dynamic_range>
1466struct dim
1467{
1468 static const size_t value = DimSize;
1469};
1470template <>
1471struct dim<dynamic_range>
1472{
1473 static const size_t value = dynamic_range;
1474 const size_t dvalue;
1475 dim(size_t size) : dvalue(size) {}
1476};
1477
1478template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1479class array_view;
1480template <typename ValueTypeOpt, unsigned int Rank>
1481class strided_array_view;
1482
1483namespace details
1484{
1485 template <typename T, typename = std::true_type>
1486 struct ArrayViewTypeTraits
1487 {
1488 using value_type = T;
1489 using size_type = size_t;
1490 };
1491
1492 template <typename Traits>
1493 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1494 {
1495 using value_type = typename Traits::array_view_traits::value_type;
1496 using size_type = typename Traits::array_view_traits::size_type;
1497 };
1498
1499 template <typename T, typename SizeType, size_t... Ranks>
1500 struct ArrayViewArrayTraits {
1501 using type = array_view<T, Ranks...>;
1502 using value_type = T;
1503 using bounds_type = static_bounds<SizeType, Ranks...>;
1504 using pointer = T*;
1505 using reference = T&;
1506 };
1507 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1508 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1509
1510 template <typename BoundsType>
1511 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1512 {
1513 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1514 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1515 }
1516 template <typename BoundsType>
1517 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1518 {
1519 fail_fast_assert(BoundsType::static_size == totalSize);
1520 return {};
1521 }
1522 template <typename BoundsType>
1523 BoundsType newBoundsHelper(size_t totalSize)
1524 {
1525 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1526 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1527 }
1528
1529 struct Sep{};
1530
1531 template <typename T, typename... Args>
1532 T static_as_array_view_helper(Sep, Args... args)
1533 {
1534 return T{static_cast<typename T::size_type>(args)...};
1535 }
1536 template <typename T, typename Arg, typename... Args>
1537 std::enable_if_t<!std::is_same<Arg, dim<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T> static_as_array_view_helper(Arg, Args... args)
1538 {
1539 return static_as_array_view_helper<T>(args...);
1540 }
1541 template <typename T, typename... Args>
1542 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1543 {
1544 return static_as_array_view_helper<T>(args..., val.dvalue);
1545 }
1546
1547 template <typename SizeType, typename ...Dimensions>
1548 struct static_as_array_view_static_bounds_helper
1549 {
1550 using type = static_bounds<SizeType, (Dimensions::value)...>;
1551 };
1552
1553 template <typename T>
1554 struct is_array_view_oracle : std::false_type
1555 {};
1556 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1557 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1558 {};
1559 template <typename ValueType, unsigned int Rank>
1560 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1561 {};
1562 template <typename T>
1563 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1564 {};
1565
1566}
1567
1568
1569template <typename ValueType, typename SizeType>
1570struct array_view_options
1571{
1572 struct array_view_traits
1573 {
1574 using value_type = ValueType;
1575 using size_type = SizeType;
1576 };
1577};
1578
1579template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1580class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1581 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1582{
1583 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1584 friend class array_view;
1585 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1586 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
1587
1588public:
1589 using typename Base::bounds_type;
1590 using typename Base::size_type;
1591 using typename Base::pointer;
1592 using typename Base::value_type;
1593 using typename Base::index_type;
1594 using Base::rank;
1595
1596public:
1597 // basic
1598 _CONSTEXPR array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1599 {
1600 }
1601
1602 _CONSTEXPR array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1603 {
1604 }
1605
1606 _CONSTEXPR array_view(nullptr_t, size_type size) : Base(nullptr, bounds_type{})
1607 {
1608 fail_fast_assert(size == 0);
1609 }
1610
1611 // default
1612 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
1613 _CONSTEXPR array_view() : Base(nullptr, bounds_type())
1614 {
1615 }
1616
1617 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1618 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1619 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1620 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
1621 _CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
1622 {
1623 }
1624
1625 // from n-dimensions static array
1626 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1627 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
1628 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
1629 _CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
1630 {
1631 }
1632
1633 // from n-dimensions static array with size
1634 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1635 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
1636 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1637 _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
1638 {
1639 fail_fast_assert(size <= N);
1640 }
1641
1642 // from std array
1643 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
1644 _CONSTEXPR array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1645 {
1646 }
1647
1648 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value && std::is_const<value_type>::value>>
1649 _CONSTEXPR array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1650 {
1651 }
1652
1653
1654 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1655 template <typename Ptr,
1656 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1657 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
1658 _CONSTEXPR array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1659 {
1660 }
1661
1662 // from containers. It must has .size() and .data() two function signatures
1663 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1664 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
1665 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
1666 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1667 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
1668 >
1669 _CONSTEXPR array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
1670 {
1671
1672 }
1673
1674 _CONSTEXPR array_view(const array_view &) = default;
1675
1676 // convertible
1677 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1678 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1679 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1680 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1681 >
1682 _CONSTEXPR array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
1683
1684 // reshape
1685 template <typename... Dimensions2>
1686 _CONSTEXPR array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
1687 {
1688 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
1689 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
1690 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1691 details::verifyBoundsReshape(this->bounds(), tobounds);
1692 return {this->data(), tobounds};
1693 }
1694
1695 // to bytes array
1696 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
1697 _CONSTEXPR auto as_bytes() const _NOEXCEPT ->
1698 array_view<array_view_options<const byte, size_type>, static_cast<size_t>(details::StaticSizeHelper<size_type, Base::bounds_type::static_size, sizeof(value_type)>::value)>
1699 {
1700 static_assert(Enabled, "The value_type of array_view must be standarded layout");
1701 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
1702 }
1703
1704 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
1705 _CONSTEXPR auto as_writeable_bytes() const _NOEXCEPT ->
1706 array_view<array_view_options<byte, size_type>, static_cast<size_t>(details::StaticSizeHelper<size_type, Base::bounds_type::static_size, sizeof(value_type)>::value)>
1707 {
1708 static_assert(Enabled, "The value_type of array_view must be standarded layout");
1709 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
1710 }
1711
1712
1713 // from bytes array
1714 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1715 _CONSTEXPR auto as_array_view() const _NOEXCEPT -> array_view<const U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : static_cast<size_type>(dynamic_range))>
1716 {
1717 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1718 "Target type must be standard layout and its size must match the byte array size");
1719 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
1720 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
1721 }
1722
1723 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1724 _CONSTEXPR auto as_array_view() const _NOEXCEPT -> array_view<U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : static_cast<size_type>(dynamic_range))>
1725 {
1726 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1727 "Target type must be standard layout and its size must match the byte array size");
1728 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
1729 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
1730 }
1731
1732 // section on linear space
1733 template<size_t Count>
1734 _CONSTEXPR array_view<ValueTypeOpt, Count> first() const _NOEXCEPT
1735 {
1736 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1737 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
1738 return { this->data(), Count };
1739 }
1740
1741 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
1742 {
1743 fail_fast_assert(count <= this->size());
1744 return { this->data(), count };
1745 }
1746
1747 template<size_t Count>
1748 _CONSTEXPR array_view<ValueTypeOpt, Count> last() const _NOEXCEPT
1749 {
1750 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1751 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
1752 return { this->data() + this->size() - Count, Count };
1753 }
1754
1755 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
1756 {
1757 fail_fast_assert(count <= this->size());
1758 return { this->data() + this->size() - count, count };
1759 }
1760
1761 template<size_t Offset, size_t Count>
1762 _CONSTEXPR array_view<ValueTypeOpt, Count> sub() const _NOEXCEPT
1763 {
1764 static_assert(bounds_type::static_size == dynamic_range || ((Offset == 0 || Offset < bounds_type::static_size) && Offset + Count <= bounds_type::static_size), "Index is out of bound");
1765 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset < this->size()) && Offset + Count <= this->size()));
1766 return { this->data() + Offset, Count };
1767 }
1768
1769 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count) const _NOEXCEPT
1770 {
1771 fail_fast_assert((offset == 0 || offset < this->size()) && offset + count <= this->size());
1772 return { this->data() + offset, count };
1773 }
1774
1775 // size
1776 _CONSTEXPR size_type length() const _NOEXCEPT
1777 {
1778 return this->size();
1779 }
1780 _CONSTEXPR size_type used_length() const _NOEXCEPT
1781 {
1782 return length();
1783 }
1784 _CONSTEXPR size_type bytes() const _NOEXCEPT
1785 {
1786 return sizeof(value_type) * this->size();
1787 }
1788 _CONSTEXPR size_type used_bytes() const _NOEXCEPT
1789 {
1790 return bytes();
1791 }
1792
1793 // section
1794 _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
1795 {
1796 return { &this->operator[](origin), strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())}};
1797 }
1798};
1799
1800template <typename T, size_t... Dimensions>
1801_CONSTEXPR auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
1802{
1803 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1804}
1805
1806template <typename T>
1807_CONSTEXPR auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
1808{
1809 return {arr, len};
1810}
1811
1812template <typename T, size_t N>
1813_CONSTEXPR auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
1814{
1815 return {arr};
1816}
1817
1818template <typename T, size_t N>
1819_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &arr)
1820{
1821 return {arr};
1822}
1823
1824template <typename T, size_t N>
1825_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
1826
1827template <typename T, size_t N>
1828_CONSTEXPR array_view<T, N> as_array_view(std::array<T, N> &arr)
1829{
1830 return {arr};
1831}
1832
1833template <typename T>
1834_CONSTEXPR array_view<T, dynamic_range> as_array_view(T *begin, T *end)
1835{
1836 return {begin, end};
1837}
1838
1839template <typename Cont>
1840_CONSTEXPR auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1841 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1842{
1843 return {arr.data(), arr.size()};
1844}
1845
1846template <typename Cont>
1847_CONSTEXPR auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1848 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1849
1850template <typename ValueTypeOpt, unsigned int Rank>
1851class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1852{
1853 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>;
1854public:
1855 using Base::rank;
1856 using typename Base::bounds_type;
1857 using typename Base::size_type;
1858 using typename Base::pointer;
1859 using typename Base::value_type;
1860 using typename Base::index_type;
1861
1862 strided_array_view (pointer ptr, bounds_type bounds): Base(ptr, std::move(bounds))
1863 {
1864 }
1865 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
1866 strided_array_view (array_view<ValueTypeOpt, Dimensions...> av, index_type strides): Base(av.data(), bounds_type{av.bounds().index_bounds(), strides})
1867 {
1868 }
1869 // section
1870 strided_array_view section(index_type origin, index_type extents) const
1871 {
1872 return { &this->operator[](origin), bounds_type {extents, details::make_stride(Base::bounds())}};
1873 }
1874};
1875
1876template <typename ArrayView>
1877class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1878{
1879 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1880public:
1881 using typename Base::reference;
1882 using typename Base::pointer;
1883 using typename Base::difference_type;
1884private:
1885 template <typename ValueType, typename Bounds>
1886 friend class basic_array_view;
1887 pointer m_pdata;
1888 const ArrayView * m_validator;
1889 void validateThis() const
1890 {
1891 fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size());
1892 }
1893 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
1894 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
1895public:
1896 reference operator*() const _NOEXCEPT
1897 {
1898 validateThis();
1899 return *m_pdata;
1900 }
1901 pointer operator->() const _NOEXCEPT
1902 {
1903 validateThis();
1904 return m_pdata;
1905 }
1906 contiguous_array_view_iterator& operator++() _NOEXCEPT
1907 {
1908 ++m_pdata;
1909 return *this;
1910 }
1911 contiguous_array_view_iterator operator++(int)_NOEXCEPT
1912 {
1913 auto ret = *this;
1914 ++(*this);
1915 return ret;
1916 }
1917 contiguous_array_view_iterator& operator--() _NOEXCEPT
1918 {
1919 --m_pdata;
1920 return *this;
1921 }
1922 contiguous_array_view_iterator operator--(int)_NOEXCEPT
1923 {
1924 auto ret = *this;
1925 --(*this);
1926 return ret;
1927 }
1928 contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT
1929 {
1930 contiguous_array_view_iterator ret{ *this };
1931 return ret += n;
1932 }
1933 contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
1934 {
1935 m_pdata += n;
1936 return *this;
1937 }
1938 contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT
1939 {
1940 contiguous_array_view_iterator ret{ *this };
1941 return ret -= n;
1942 }
1943 contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
1944 {
1945 return *this += -n;
1946 }
1947 difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1948 {
1949 fail_fast_assert(m_validator == rhs.m_validator);
1950 return m_pdata - rhs.m_pdata;
1951 }
1952 reference operator[](difference_type n) const _NOEXCEPT
1953 {
1954 return *(*this + n);
1955 }
1956 bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1957 {
1958 fail_fast_assert(m_validator == rhs.m_validator);
1959 return m_pdata == rhs.m_pdata;
1960 }
1961 bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1962 {
1963 return !(*this == rhs);
1964 }
1965 bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1966 {
1967 fail_fast_assert(m_validator == rhs.m_validator);
1968 return m_pdata < rhs.m_pdata;
1969 }
1970 bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1971 {
1972 return !(rhs < *this);
1973 }
1974 bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1975 {
1976 return rhs < *this;
1977 }
1978 bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
1979 {
1980 return !(rhs > *this);
1981 }
1982 void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT
1983 {
1984 std::swap(m_pdata, rhs.m_pdata);
1985 std::swap(m_validator, rhs.m_validator);
1986 }
1987};
1988
1989template <typename ArrayView>
1990contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
1991{
1992 return rhs + n;
1993}
1994
1995template <typename ArrayView>
1996class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1997{
1998 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1999public:
2000 using typename Base::reference;
2001 using typename Base::pointer;
2002 using typename Base::difference_type;
2003 using typename Base::value_type;
2004private:
2005 template <typename ValueType, typename Bounds>
2006 friend class basic_array_view;
2007 const ArrayView * m_container;
2008 typename ArrayView::iterator m_itr;
2009 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2010 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2011 {
2012 }
2013public:
2014 reference operator*() const _NOEXCEPT
2015 {
2016 return (*m_container)[*m_itr];
2017 }
2018 pointer operator->() const _NOEXCEPT
2019 {
2020 return &(*m_container)[*m_itr];
2021 }
2022 general_array_view_iterator& operator++() _NOEXCEPT
2023 {
2024 ++m_itr;
2025 return *this;
2026 }
2027 general_array_view_iterator operator++(int)_NOEXCEPT
2028 {
2029 auto ret = *this;
2030 ++(*this);
2031 return ret;
2032 }
2033 general_array_view_iterator& operator--() _NOEXCEPT
2034 {
2035 --m_itr;
2036 return *this;
2037 }
2038 general_array_view_iterator operator--(int)_NOEXCEPT
2039 {
2040 auto ret = *this;
2041 --(*this);
2042 return ret;
2043 }
2044 general_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2045 {
2046 general_array_view_iterator ret{ *this };
2047 return ret += n;
2048 }
2049 general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2050 {
2051 m_itr += n;
2052 return *this;
2053 }
2054 general_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2055 {
2056 general_array_view_iterator ret{ *this };
2057 return ret -= n;
2058 }
2059 general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2060 {
2061 return *this += -n;
2062 }
2063 difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT
2064 {
2065 fail_fast_assert(m_container == rhs.m_container);
2066 return m_itr - rhs.m_itr;
2067 }
2068 value_type operator[](difference_type n) const _NOEXCEPT
2069 {
2070 return (*m_container)[m_itr[n]];;
2071 }
2072 bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT
2073 {
2074 fail_fast_assert(m_container == rhs.m_container);
2075 return m_itr == rhs.m_itr;
2076 }
2077 bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT
2078 {
2079 return !(*this == rhs);
2080 }
2081 bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT
2082 {
2083 fail_fast_assert(m_container == rhs.m_container);
2084 return m_itr < rhs.m_itr;
2085 }
2086 bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT
2087 {
2088 return !(rhs < *this);
2089 }
2090 bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT
2091 {
2092 return rhs < *this;
2093 }
2094 bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT
2095 {
2096 return !(rhs > *this);
2097 }
2098 void swap(general_array_view_iterator& rhs) _NOEXCEPT
2099 {
2100 std::swap(m_itr, rhs.m_itr);
2101 std::swap(m_container, rhs.m_container);
2102 }
2103};
2104
2105template <typename ArrayView>
2106general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2107{
2108 return rhs + n;
2109}
2110
2111} // namespace Guide
2112
2113#pragma pop_macro("_NOEXCEPT")