blob: 097d20a84ac4c7a1e18487fc96554bc3fe2eb130 [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.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -070086 }
87 _CONSTEXPR coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT
88 {
89 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070090 for (unsigned int i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -070091 elems[i] = values[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070092 }
93 _CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
94 {
95 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
96 static_assert(rank == 1, "This constructor can only be used with rank == 1.");
97 elems[0] = e0;
98 }
99 // Preconditions: il.size() == rank
100 _CONSTEXPR coordinate_facade(std::initializer_list<value_type> il)
101 {
102 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700103 fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700104 for (unsigned int i = 0; i < rank; ++i)
105 {
106 elems[i] = begin(il)[i];
107 }
108 }
109
110 _CONSTEXPR coordinate_facade(const coordinate_facade & other) = default;
111
112 template <typename OtherConcreteType, typename OtherValueType>
113 _CONSTEXPR coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
114 {
115 for (unsigned int i = 0; i < rank; ++i)
116 {
117 fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
118 elems[i] = static_cast<value_type>(other.elems[i]);
119 }
120 }
121 protected:
122 coordinate_facade& operator=(const coordinate_facade& rhs) = default;
123 // Preconditions: component_idx < rank
124 _CONSTEXPR reference operator[](unsigned int component_idx)
125 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700126 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700127 return elems[component_idx];
128 }
129 // Preconditions: component_idx < rank
130 _CONSTEXPR const_reference operator[](unsigned int component_idx) const
131 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700132 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700133 return elems[component_idx];
134 }
135 _CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
136 {
137 for (unsigned int i = 0; i < rank; ++i)
138 {
139 if (elems[i] != rhs.elems[i])
140 return false;
141 }
142 return true;
143 }
144 _CONSTEXPR bool operator!=(const ConcreteType& rhs) const _NOEXCEPT
145 {
146 return !(to_concrete() == rhs);
147 }
148 _CONSTEXPR ConcreteType operator+() const _NOEXCEPT
149 {
150 return to_concrete();
151 }
152 _CONSTEXPR ConcreteType operator-() const
153 {
154 ConcreteType ret = to_concrete();
155 for (unsigned int i = 0; i < rank; ++i)
156 ret.elems[i] = -ret.elems[i];
157 return ret;
158 }
159 _CONSTEXPR ConcreteType operator+(const ConcreteType& rhs) const
160 {
161 ConcreteType ret = to_concrete();
162 ret += rhs;
163 return ret;
164 }
165 _CONSTEXPR ConcreteType operator-(const ConcreteType& rhs) const
166 {
167 ConcreteType ret = to_concrete();
168 ret -= rhs;
169 return ret;
170 }
171 _CONSTEXPR ConcreteType& operator+=(const ConcreteType& rhs)
172 {
173 for (unsigned int i = 0; i < rank; ++i)
174 elems[i] += rhs.elems[i];
175 return to_concrete();
176 }
177 _CONSTEXPR ConcreteType& operator-=(const ConcreteType& rhs)
178 {
179 for (unsigned int i = 0; i < rank; ++i)
180 elems[i] -= rhs.elems[i];
181 return to_concrete();
182 }
183 _CONSTEXPR ConcreteType& operator++()
184 {
185 static_assert(rank == 1, "This operator can only be used with rank == 1.");
186 ++elems[0];
187 return to_concrete();
188 }
189 _CONSTEXPR ConcreteType operator++(int)
190 {
191 static_assert(rank == 1, "This operator can only be used with rank == 1.");
192 ConcreteType ret = to_concrete();
193 ++(*this);
194 return ret;
195 }
196 _CONSTEXPR ConcreteType& operator--()
197 {
198 static_assert(rank == 1, "This operator can only be used with rank == 1.");
199 --elems[0];
200 return to_concrete();
201 }
202 _CONSTEXPR ConcreteType operator--(int)
203 {
204 static_assert(rank == 1, "This operator can only be used with rank == 1.");
205 ConcreteType ret = to_concrete();
206 --(*this);
207 return ret;
208 }
209 _CONSTEXPR ConcreteType operator*(value_type v) const
210 {
211 ConcreteType ret = to_concrete();
212 ret *= v;
213 return ret;
214 }
215 _CONSTEXPR ConcreteType operator/(value_type v) const
216 {
217 ConcreteType ret = to_concrete();
218 ret /= v;
219 return ret;
220 }
221 friend _CONSTEXPR ConcreteType operator*(value_type v, const ConcreteType& rhs)
222 {
223 return rhs * v;
224 }
225 _CONSTEXPR ConcreteType& operator*=(value_type v)
226 {
227 for (unsigned int i = 0; i < rank; ++i)
228 elems[i] *= v;
229 return to_concrete();
230 }
231 _CONSTEXPR ConcreteType& operator/=(value_type v)
232 {
233 for (unsigned int i = 0; i < rank; ++i)
234 elems[i] /= v;
235 return to_concrete();
236 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700237 value_type elems[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700238 private:
239 _CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
240 {
241 return static_cast<const ConcreteType&>(*this);
242 }
243 _CONSTEXPR ConcreteType& to_concrete() _NOEXCEPT
244 {
245 return static_cast<ConcreteType&>(*this);
246 }
247 };
248 template <typename T>
249 class arrow_proxy
250 {
251 public:
252 explicit arrow_proxy(T t)
253 : val(t)
254 {}
255 const T operator*() const _NOEXCEPT
256 {
257 return val;
258 }
259 const T* operator->() const _NOEXCEPT
260 {
261 return &val;
262 }
263 private:
264 T val;
265 };
266}
267
268template <unsigned int Rank, typename ValueType = size_t>
269class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
270{
271 using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
272 friend Base;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700273 template <unsigned int OtherRank, typename OtherValueType>
274 friend class index;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700275public:
276 using Base::rank;
277 using reference = typename Base::reference;
278 using const_reference = typename Base::const_reference;
279 using size_type = typename Base::value_type;
280 using value_type = typename Base::value_type;
281 _CONSTEXPR index() _NOEXCEPT : Base(){}
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700282 _CONSTEXPR index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {}
283 _CONSTEXPR index(std::initializer_list<value_type> il) : Base(il) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700284
285 _CONSTEXPR index(const index &) = default;
286
287 template <typename OtherValueType>
288 _CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
289 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700290 }
291 _CONSTEXPR static index shift_left(const index<rank+1, value_type>& other) _NOEXCEPT
292 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +0000293 value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
294 return index(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700295 }
296
297 using Base::operator[];
298 using Base::operator==;
299 using Base::operator!=;
300 using Base::operator+;
301 using Base::operator-;
302 using Base::operator+=;
303 using Base::operator-=;
304 using Base::operator++;
305 using Base::operator--;
306 using Base::operator*;
307 using Base::operator/;
308 using Base::operator*=;
309 using Base::operator/=;
310};
311
312template <typename ValueType>
313class index<1, ValueType>
314{
315 template <unsigned int, typename OtherValueType>
316 friend class index;
317public:
318 static const unsigned int rank = 1;
319 using reference = ValueType&;
320 using const_reference = const ValueType&;
321 using size_type = ValueType;
322 using value_type = ValueType;
323
324 _CONSTEXPR index() _NOEXCEPT : value(0)
325 {
326 }
327 _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
328 {
329 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700330 _CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
331 {
332 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700333 // Preconditions: il.size() == rank
334 _CONSTEXPR index(std::initializer_list<value_type> il)
335 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700336 fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700337 value = begin(il)[0];
338 }
339
340 _CONSTEXPR index(const index &) = default;
341
342 template <typename OtherValueType>
343 _CONSTEXPR index(const index<1, OtherValueType> & other)
344 {
345 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
346 value = static_cast<ValueType>(other.value);
347 }
348
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700349 _CONSTEXPR static index shift_left(const index<rank + 1, value_type>& other) _NOEXCEPT
350 {
351 return other.elems[1];
352 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700353 // Preconditions: component_idx < rank
354 _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
355 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700356 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700357 (void)(component_idx);
358 return value;
359 }
360 // Preconditions: component_idx < rank
361 _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
362 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700363 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700364 (void)(component_idx);
365 return value;
366 }
367 _CONSTEXPR bool operator==(const index& rhs) const _NOEXCEPT
368 {
369 return value == rhs.value;
370 }
371 _CONSTEXPR bool operator!=(const index& rhs) const _NOEXCEPT
372 {
373 return !(*this == rhs);
374 }
375 _CONSTEXPR index operator+() const _NOEXCEPT
376 {
377 return *this;
378 }
379 _CONSTEXPR index operator-() const _NOEXCEPT
380 {
381 return index(-value);
382 }
383 _CONSTEXPR index operator+(const index& rhs) const _NOEXCEPT
384 {
385 return index(value + rhs.value);
386 }
387 _CONSTEXPR index operator-(const index& rhs) const _NOEXCEPT
388 {
389 return index(value - rhs.value);
390 }
391 _CONSTEXPR index& operator+=(const index& rhs) _NOEXCEPT
392 {
393 value += rhs.value;
394 return *this;
395 }
396 _CONSTEXPR index& operator-=(const index& rhs) _NOEXCEPT
397 {
398 value -= rhs.value;
399 return *this;
400 }
401 _CONSTEXPR index& operator++() _NOEXCEPT
402 {
403 ++value;
404 return *this;
405 }
406 _CONSTEXPR index operator++(int) _NOEXCEPT
407 {
408 index ret = *this;
409 ++(*this);
410 return ret;
411 }
412 _CONSTEXPR index& operator--() _NOEXCEPT
413 {
414 --value;
415 return *this;
416 }
417 _CONSTEXPR index operator--(int) _NOEXCEPT
418 {
419 index ret = *this;
420 --(*this);
421 return ret;
422 }
423 _CONSTEXPR index operator*(value_type v) const _NOEXCEPT
424 {
425 return index(value * v);
426 }
427 _CONSTEXPR index operator/(value_type v) const _NOEXCEPT
428 {
429 return index(value / v);
430 }
431 _CONSTEXPR index& operator*=(value_type v) _NOEXCEPT
432 {
433 value *= v;
434 return *this;
435 }
436 _CONSTEXPR index& operator/=(value_type v) _NOEXCEPT
437 {
438 value /= v;
439 return *this;
440 }
441 friend _CONSTEXPR index operator*(value_type v, const index& rhs) _NOEXCEPT
442 {
443 return index(rhs * v);
444 }
445private:
446 value_type value;
447};
448
449#ifndef _MSC_VER
450
451struct static_bounds_dynamic_range_t
452{
453 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
454 constexpr operator T() const noexcept
455 {
456 return static_cast<T>(-1);
457 }
458
459 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
460 constexpr bool operator ==(T other) const noexcept
461 {
462 return static_cast<T>(-1) == other;
463 }
464
465 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
466 constexpr bool operator !=(T other) const noexcept
467 {
468 return static_cast<T>(-1) != other;
469 }
470
471};
472
473template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
474constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
475{
476 return right == left;
477}
478
479template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
480constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
481{
482 return right != left;
483}
484
485constexpr static_bounds_dynamic_range_t dynamic_range{};
486#else
487const char dynamic_range = -1;
488#endif
489
490struct generalized_mapping_tag {};
491struct contiguous_mapping_tag : generalized_mapping_tag {};
492
493namespace details
494{
495 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
496 struct StaticSizeHelperImpl
497 {
498 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
499 static const SizeType value = Fact1 * Fact2;
500 };
501
502 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
503 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
504 {
505 static const SizeType value = ConstBound;
506 };
507
508 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
509 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
510 {
511 static const SizeType value = ConstBound;
512 };
513
514 template <typename SizeType, SizeType ConstBound>
515 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
516 {
517 static const SizeType value = static_cast<SizeType>(ConstBound);
518 };
519
520 template <typename SizeType, SizeType Fact1, SizeType Fact2>
521 struct StaticSizeHelper
522 {
523 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
524 };
525
526
527 template <size_t Left, size_t Right>
528 struct LessThan
529 {
530 static const bool value = Left < Right;
531 };
532
533 template <typename SizeType, size_t... Ranges>
534 struct BoundsRanges {
535 static const unsigned int Depth = 0;
536 static const unsigned int DynamicNum = 0;
537 static const SizeType CurrentRange = 1;
538 static const SizeType TotalSize = 1;
539
540 BoundsRanges (const BoundsRanges &) = default;
541
542 // TODO : following signature is for work around VS bug
543 template <typename OtherType>
544 BoundsRanges (const OtherType &, bool firstLevel) {}
545 BoundsRanges(const SizeType * const arr) { }
546 BoundsRanges() = default;
547
548
549 template <typename T, unsigned int Dim>
550 void serialize(T &) const {
551 }
552 template <typename T, unsigned int Dim>
553 SizeType linearize(const T &) const {
554 return 0;
555 }
556 template <typename T, unsigned int Dim>
557 ptrdiff_t contains(const T &) const {
558 return 0;
559 }
560
561 size_t totalSize() const _NOEXCEPT {
562 return TotalSize;
563 }
564
565 bool operator == (const BoundsRanges &) const _NOEXCEPT
566 {
567 return true;
568 }
569 };
570
571 template <typename SizeType, size_t... RestRanges>
572 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
573 using Base = BoundsRanges <SizeType, RestRanges... >;
574 static const unsigned int Depth = Base::Depth + 1;
575 static const unsigned int DynamicNum = Base::DynamicNum + 1;
576 static const SizeType CurrentRange = dynamic_range;
577 static const SizeType TotalSize = dynamic_range;
578 const SizeType m_bound;
579
580 BoundsRanges (const BoundsRanges &) = default;
581 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
582 {
583 fail_fast_assert(0 <= *arr);
584 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
585 }
586 BoundsRanges() : m_bound(0) {}
587
588 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
589 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) :
590 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
591 {
592 }
593
594 template <typename T, unsigned int Dim = 0>
595 void serialize(T & arr) const {
596 arr[Dim] = elementNum();
597 this->Base::template serialize<T, Dim + 1>(arr);
598 }
599 template <typename T, unsigned int Dim = 0>
600 SizeType linearize(const T & arr) const {
601 const size_t index = this->Base::totalSize() * arr[Dim];
602 fail_fast_assert(index < static_cast<size_t>(m_bound));
603 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
604 }
605
606 template <typename T, unsigned int Dim = 0>
607 ptrdiff_t contains(const T & arr) const {
608 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
609 if (last == -1)
610 return -1;
611 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
612 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
613 }
614
615 size_t totalSize() const _NOEXCEPT {
616 return m_bound;
617 }
618
619 SizeType elementNum() const _NOEXCEPT {
620 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
621 }
622
623 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
624 if (dim > 0)
625 return this->Base::elementNum(dim - 1);
626 else
627 return elementNum();
628 }
629
630 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
631 {
632 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
633 }
634 };
635
636 template <typename SizeType, size_t CurRange, size_t... RestRanges>
637 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
638 using Base = BoundsRanges <SizeType, RestRanges... >;
639 static const unsigned int Depth = Base::Depth + 1;
640 static const unsigned int DynamicNum = Base::DynamicNum;
641 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
642 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
643 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
644
645 BoundsRanges (const BoundsRanges &) = default;
646 BoundsRanges(const SizeType * const arr) : Base(arr) { }
647 BoundsRanges() = default;
648
649 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
650 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
651 {
652 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
653 }
654
655 template <typename T, unsigned int Dim = 0>
656 void serialize(T & arr) const {
657 arr[Dim] = elementNum();
658 this->Base::template serialize<T, Dim + 1>(arr);
659 }
660
661 template <typename T, unsigned int Dim = 0>
662 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700663 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700664 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
665 }
666
667 template <typename T, unsigned int Dim = 0>
668 ptrdiff_t contains(const T & arr) const {
669 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
670 return -1;
671 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
672 if (last == -1)
673 return -1;
674 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
675 }
676
677 size_t totalSize() const _NOEXCEPT{
678 return CurrentRange * this->Base::totalSize();
679 }
680
681 SizeType elementNum() const _NOEXCEPT{
682 return CurrentRange;
683 }
684
685 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
686 if (dim > 0)
687 return this->Base::elementNum(dim - 1);
688 else
689 return elementNum();
690 }
691
692 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
693 {
694 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
695 }
696 };
697
698 template <typename SourceType, typename TargetType, size_t Rank>
699 struct BoundsRangeConvertible2;
700
701 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
702 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
703 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
704
705 template <size_t Rank, typename SourceType, typename TargetType>
706 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
707
708 template <typename SourceType, typename TargetType, size_t Rank>
709 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
710 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
711 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
712 {};
713
714 template <typename SourceType, typename TargetType>
715 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
716
717 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
718 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
719 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
720 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
721 {};
722 template <typename SourceType, typename TargetType>
723 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
724
725 template <typename TypeChain>
726 struct TypeListIndexer
727 {
728 const TypeChain & obj;
729 TypeListIndexer(const TypeChain & obj) :obj(obj){}
730 template<unsigned int N>
731 const TypeChain & getObj(std::true_type)
732 {
733 return obj;
734 }
735 template<unsigned int N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
736 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
737 {
738 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
739 }
740 template <unsigned int N>
741 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
742 {
743 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
744 }
745 };
746
747 template <typename TypeChain>
748 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
749 {
750 return TypeListIndexer<TypeChain>(obj);
751 }
752}
753
754template <typename IndexType>
755class bounds_iterator;
756
757template <typename SizeType, size_t... Ranges>
758class static_bounds {
759public:
760 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &empty) {
761 }
762};
763
764template <typename SizeType, size_t FirstRange, size_t... RestRanges>
765class static_bounds<SizeType, FirstRange, RestRanges...>
766{
767 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
768 static_assert(std::is_integral<SizeType>::value
769 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
770
771 MyRanges m_ranges;
772 _CONSTEXPR static_bounds(const MyRanges & range) : m_ranges(range) { }
773
774 template <typename SizeType2, size_t... Ranges2>
775 friend class static_bounds;
776public:
777 static const unsigned int rank = MyRanges::Depth;
778 static const unsigned int dynamic_rank = MyRanges::DynamicNum;
779 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
780
781 using size_type = SizeType;
782 using index_type = index<rank, size_type>;
783 using iterator = bounds_iterator<index_type>;
784 using const_iterator = bounds_iterator<index_type>;
785 using difference_type = ptrdiff_t;
786 using sliced_type = static_bounds<SizeType, RestRanges...>;
787 using mapping_type = contiguous_mapping_tag;
788public:
789 _CONSTEXPR static_bounds(const static_bounds &) = default;
790
791 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
792 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
793 _CONSTEXPR static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
794 m_ranges(other.m_ranges)
795 {
796 }
797
798 _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
799 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700800 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
801 fail_fast_assert(m_ranges.totalSize() <= details::SizeTypeTraits<size_type>::max_value, "Size of the range is larger than the max element of the size type");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700802 }
803
804 _CONSTEXPR static_bounds() = default;
805
806 _CONSTEXPR static_bounds & operator = (const static_bounds & otherBounds)
807 {
808 new(&m_ranges) MyRanges (otherBounds.m_ranges);
809 return *this;
810 }
811
812 _CONSTEXPR sliced_type slice() const _NOEXCEPT
813 {
814 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
815 }
816
817 _CONSTEXPR size_type stride() const _NOEXCEPT
818 {
819 return rank > 1 ? slice().size() : 1;
820 }
821
822 _CONSTEXPR size_type size() const _NOEXCEPT
823 {
824 return static_cast<size_type>(m_ranges.totalSize());
825 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700826
827 _CONSTEXPR size_type total_size() const _NOEXCEPT
828 {
829 return static_cast<size_type>(m_ranges.totalSize());
830 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700831
832 _CONSTEXPR size_type linearize(const index_type & idx) const
833 {
834 return m_ranges.linearize(idx);
835 }
836
837 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
838 {
839 return m_ranges.contains(idx) != -1;
840 }
841
842 _CONSTEXPR size_type operator[](unsigned int index) const _NOEXCEPT
843 {
844 return m_ranges.elementNum(index);
845 }
846
847 template <unsigned int Dim = 0>
848 _CONSTEXPR size_type extent() const _NOEXCEPT
849 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700850 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700851 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
852 }
853
854 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
855 {
856 index_type extents;
857 m_ranges.serialize(extents);
858 return extents;
859 }
860
861 template <typename OtherSizeTypes, size_t... Ranges>
862 _CONSTEXPR bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
863 {
864 return this->size() == rhs.size();
865 }
866
867 template <typename OtherSizeTypes, size_t... Ranges>
868 _CONSTEXPR bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
869 {
870 return !(*this == rhs);
871 }
872
873 _CONSTEXPR const_iterator begin() const _NOEXCEPT
874 {
875 return const_iterator(*this);
876 }
877
878 _CONSTEXPR const_iterator end() const _NOEXCEPT
879 {
880 index_type boundary;
881 m_ranges.serialize(boundary);
882 return const_iterator(*this, this->index_bounds());
883 }
884};
885
886template <unsigned int Rank, typename SizeType = size_t>
887class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
888{
889 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
890 friend Base;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700891 template <unsigned int OtherRank, typename OtherSizeType>
892 friend class strided_bounds;
893
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700894public:
895 using Base::rank;
896 using reference = typename Base::reference;
897 using const_reference = typename Base::const_reference;
898 using size_type = typename Base::value_type;
899 using difference_type = typename Base::value_type;
900 using value_type = typename Base::value_type;
901 using index_type = index<rank, size_type>;
902 using iterator = bounds_iterator<index_type>;
903 using const_iterator = bounds_iterator<index_type>;
904 static const int dynamic_rank = rank;
905 static const size_t static_size = dynamic_range;
906 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
907 using mapping_type = generalized_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700908 _CONSTEXPR strided_bounds(const strided_bounds &) = default;
909
910 template <typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700911 _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
912 : Base(other), m_strides(other.strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700913 {
914 }
915
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700916 _CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
917 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700918 {
919 for (unsigned int i = 0; i < rank; i++)
920 Base::elems[i] = extents[i];
921 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700922 _CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
923 : Base(values), m_strides(std::move(strides))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700924 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700925 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700926 _CONSTEXPR index_type strides() const _NOEXCEPT
927 {
928 return m_strides;
929 }
930 _CONSTEXPR size_type total_size() const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700931 {
932 size_type ret = 0;
933 for (unsigned int i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700934 ret += (Base::elems[i] - 1) * m_strides[i];
935 return ret + 1;
936 }
937 _CONSTEXPR size_type size() const _NOEXCEPT
938 {
939 size_type ret = 1;
940 for (unsigned int i = 0; i < rank; ++i)
941 ret *= Base::elems[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700942 return ret;
943 }
944 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
945 {
946 for (unsigned int i = 0; i < rank; ++i)
947 {
948 if (idx[i] < 0 || idx[i] >= Base::elems[i])
949 return false;
950 }
951 return true;
952 }
953 _CONSTEXPR size_type linearize(const index_type & idx) const
954 {
955 size_type ret = 0;
956 for (unsigned int i = 0; i < rank; i++)
957 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700958 fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
959 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700960 }
961 return ret;
962 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700963 _CONSTEXPR size_type stride() const _NOEXCEPT
964 {
965 return m_strides[0];
966 }
967 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700968 _CONSTEXPR sliced_type slice() const
969 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700970 return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700971 }
972 template <unsigned int Dim = 0>
973 _CONSTEXPR size_type extent() const _NOEXCEPT
974 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700975 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700976 return Base::elems[Dim];
977 }
978 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
979 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700980 return index_type(Base::elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 }
982 const_iterator begin() const _NOEXCEPT
983 {
984 return const_iterator{ *this };
985 }
986 const_iterator end() const _NOEXCEPT
987 {
988 return const_iterator{ *this, index_bounds() };
989 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700990private:
991 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700992};
993
994template <typename T>
995struct is_bounds : std::integral_constant<bool, false> {};
996template <typename SizeType, size_t... Ranges>
997struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
998template <unsigned int Rank, typename SizeType>
999struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
1000
1001template <typename IndexType>
1002class bounds_iterator
1003 : public std::iterator<std::random_access_iterator_tag,
1004 IndexType,
1005 ptrdiff_t,
1006 const details::arrow_proxy<IndexType>,
1007 const IndexType>
1008{
1009private:
1010 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
1011public:
1012 static const unsigned int rank = IndexType::rank;
1013 using typename Base::reference;
1014 using typename Base::pointer;
1015 using typename Base::difference_type;
1016 using typename Base::value_type;
1017 using index_type = value_type;
1018 using index_size_type = typename IndexType::size_type;
1019 template <typename Bounds>
1020 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT
1021 : boundary(bnd.index_bounds())
1022 , curr( std::move(curr) )
1023 {
1024 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1025 }
1026 reference operator*() const _NOEXCEPT
1027 {
1028 return curr;
1029 }
1030 pointer operator->() const _NOEXCEPT
1031 {
1032 return details::arrow_proxy<value_type>{ curr };
1033 }
1034 bounds_iterator& operator++() _NOEXCEPT
1035 {
1036 for (unsigned int i = rank; i-- > 0;)
1037 {
1038 if (++curr[i] < boundary[i])
1039 {
1040 return *this;
1041 }
1042 else
1043 {
1044 curr[i] = 0;
1045 }
1046 }
1047 // If we're here we've wrapped over - set to past-the-end.
1048 for (unsigned int i = 0; i < rank; ++i)
1049 {
1050 curr[i] = boundary[i];
1051 }
1052 return *this;
1053 }
1054 bounds_iterator operator++(int) _NOEXCEPT
1055 {
1056 auto ret = *this;
1057 ++(*this);
1058 return ret;
1059 }
1060 bounds_iterator& operator--() _NOEXCEPT
1061 {
1062 for (int i = rank; i-- > 0;)
1063 {
1064 if (curr[i]-- > 0)
1065 {
1066 return *this;
1067 }
1068 else
1069 {
1070 curr[i] = boundary[i] - 1;
1071 }
1072 }
1073 // If we're here the preconditions were violated
1074 // "pre: there exists s such that r == ++s"
1075 fail_fast_assert(false);
1076 return *this;
1077 }
1078 bounds_iterator operator--(int) _NOEXCEPT
1079 {
1080 auto ret = *this;
1081 --(*this);
1082 return ret;
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 auto linear_idx = linearize(curr) + n;
1092 value_type stride;
1093 stride[rank - 1] = 1;
1094 for (unsigned int i = rank - 1; i-- > 0;)
1095 {
1096 stride[i] = stride[i + 1] * boundary[i + 1];
1097 }
1098 for (unsigned int i = 0; i < rank; ++i)
1099 {
1100 curr[i] = linear_idx / stride[i];
1101 linear_idx = linear_idx % stride[i];
1102 }
1103 return *this;
1104 }
1105 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1106 {
1107 bounds_iterator ret{ *this };
1108 return ret -= n;
1109 }
1110 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1111 {
1112 return *this += -n;
1113 }
1114 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1115 {
1116 return linearize(curr) - linearize(rhs.curr);
1117 }
1118 reference operator[](difference_type n) const _NOEXCEPT
1119 {
1120 return *(*this + n);
1121 }
1122 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1123 {
1124 return curr == rhs.curr;
1125 }
1126 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1127 {
1128 return !(*this == rhs);
1129 }
1130 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1131 {
1132 for (unsigned int i = 0; i < rank; ++i)
1133 {
1134 if (curr[i] < rhs.curr[i])
1135 return true;
1136 }
1137 return false;
1138 }
1139 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1140 {
1141 return !(rhs < *this);
1142 }
1143 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1144 {
1145 return rhs < *this;
1146 }
1147 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1148 {
1149 return !(rhs > *this);
1150 }
1151 void swap(bounds_iterator& rhs) _NOEXCEPT
1152 {
1153 std::swap(boundary, rhs.boundary);
1154 std::swap(curr, rhs.curr);
1155 }
1156private:
1157 index_size_type linearize(const value_type& idx) const _NOEXCEPT
1158 {
1159 // TODO: Smarter impl.
1160 // Check if past-the-end
1161 bool pte = true;
1162 for (unsigned int i = 0; i < rank; ++i)
1163 {
1164 if (idx[i] != boundary[i])
1165 {
1166 pte = false;
1167 break;
1168 }
1169 }
1170 index_size_type multiplier = 1;
1171 index_size_type res = 0;
1172 if (pte)
1173 {
1174 res = 1;
1175 for (unsigned int i = rank; i-- > 0;)
1176 {
1177 res += (idx[i] - 1) * multiplier;
1178 multiplier *= boundary[i];
1179 }
1180 }
1181 else
1182 {
1183 for (unsigned int i = rank; i-- > 0;)
1184 {
1185 res += idx[i] * multiplier;
1186 multiplier *= boundary[i];
1187 }
1188 }
1189 return res;
1190 }
1191 value_type boundary;
1192 value_type curr;
1193};
1194
1195template <typename SizeType>
1196class bounds_iterator<index<1, SizeType>>
1197 : public std::iterator<std::random_access_iterator_tag,
1198 index<1, SizeType>,
1199 ptrdiff_t,
1200 const details::arrow_proxy<index<1, SizeType>>,
1201 const index<1, SizeType>>
1202{
1203 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>>;
1204
1205public:
1206 using typename Base::reference;
1207 using typename Base::pointer;
1208 using typename Base::difference_type;
1209 using typename Base::value_type;
1210 using index_type = value_type;
1211 using index_size_type = typename index_type::size_type;
1212
1213 template <typename Bounds>
1214 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT
1215 : curr( std::move(curr) )
1216 {}
1217 reference operator*() const _NOEXCEPT
1218 {
1219 return curr;
1220 }
1221 pointer operator->() const _NOEXCEPT
1222 {
1223 return details::arrow_proxy<value_type>{ curr };
1224 }
1225 bounds_iterator& operator++() _NOEXCEPT
1226 {
1227 ++curr;
1228 return *this;
1229 }
1230 bounds_iterator operator++(int) _NOEXCEPT
1231 {
1232 auto ret = *this;
1233 ++(*this);
1234 return ret;
1235 }
1236 bounds_iterator& operator--() _NOEXCEPT
1237 {
1238 curr--;
1239 return *this;
1240 }
1241 bounds_iterator operator--(int) _NOEXCEPT
1242 {
1243 auto ret = *this;
1244 --(*this);
1245 return ret;
1246 }
1247 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1248 {
1249 bounds_iterator ret{ *this };
1250 return ret += n;
1251 }
1252 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1253 {
1254 curr += n;
1255 return *this;
1256 }
1257 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1258 {
1259 bounds_iterator ret{ *this };
1260 return ret -= n;
1261 }
1262 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1263 {
1264 return *this += -n;
1265 }
1266 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1267 {
1268 return curr[0] - rhs.curr[0];
1269 }
1270 reference operator[](difference_type n) const _NOEXCEPT
1271 {
1272 return curr + n;
1273 }
1274 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1275 {
1276 return curr == rhs.curr;
1277 }
1278 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1279 {
1280 return !(*this == rhs);
1281 }
1282 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1283 {
1284 return curr[0] < rhs.curr[0];
1285 }
1286 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1287 {
1288 return !(rhs < *this);
1289 }
1290 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1291 {
1292 return rhs < *this;
1293 }
1294 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1295 {
1296 return !(rhs > *this);
1297 }
1298 void swap(bounds_iterator& rhs) _NOEXCEPT
1299 {
1300 std::swap(curr, rhs.curr);
1301 }
1302private:
1303 value_type curr;
1304};
1305
1306template <typename IndexType>
1307bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) _NOEXCEPT
1308{
1309 return rhs + n;
1310}
1311
1312/*
1313** begin definitions of basic_array_view
1314*/
1315namespace details
1316{
1317 template <typename Bounds>
1318 _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
1319 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001320 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001321 }
1322
1323 // Make a stride vector from bounds, assuming continugous memory.
1324 template <typename Bounds>
1325 _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
1326 {
1327 auto extents = bnd.index_bounds();
1328 typename Bounds::index_type stride;
1329 stride[Bounds::rank - 1] = 1;
1330 for (int i = Bounds::rank - 2; i >= 0; --i)
1331 stride[i] = stride[i + 1] * extents[i + 1];
1332 return stride;
1333 }
1334
1335 template <typename BoundsSrc, typename BoundsDest>
1336 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1337 {
1338 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1339 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1340 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");
1341 fail_fast_assert(src.size() == dest.size());
1342 }
1343
1344
1345} // namespace details
1346
1347template <typename ArrayView>
1348class contiguous_array_view_iterator;
1349template <typename ArrayView>
1350class general_array_view_iterator;
1351enum class byte : std::uint8_t {};
1352
1353template <typename ValueType, typename BoundsType>
1354class basic_array_view
1355{
1356public:
1357 static const unsigned int rank = BoundsType::rank;
1358 using bounds_type = BoundsType;
1359 using size_type = typename bounds_type::size_type;
1360 using index_type = typename bounds_type::index_type;
1361 using value_type = ValueType;
1362 using pointer = ValueType*;
1363 using reference = ValueType&;
1364 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>>;
1365 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>>>;
1366 using reverse_iterator = std::reverse_iterator<iterator>;
1367 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1368 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1369
1370private:
1371 pointer m_pdata;
1372 bounds_type m_bounds;
1373
1374public:
1375 _CONSTEXPR bounds_type bounds() const _NOEXCEPT
1376 {
1377 return m_bounds;
1378 }
1379 template <unsigned int Dim = 0>
1380 _CONSTEXPR size_type extent() const _NOEXCEPT
1381 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001382 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001383 return m_bounds.template extent<Dim>();
1384 }
1385 _CONSTEXPR size_type size() const _NOEXCEPT
1386 {
1387 return m_bounds.size();
1388 }
1389 _CONSTEXPR reference operator[](const index_type& idx) const
1390 {
1391 return m_pdata[m_bounds.linearize(idx)];
1392 }
1393 _CONSTEXPR pointer data() const _NOEXCEPT
1394 {
1395 return m_pdata;
1396 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001397 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001398 _CONSTEXPR Ret operator[](size_type idx) const
1399 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001400 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001401 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001402
1403 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001404 return Ret {m_pdata + ridx, m_bounds.slice()};
1405 }
1406
1407 _CONSTEXPR operator bool () const _NOEXCEPT
1408 {
1409 return m_pdata != nullptr;
1410 }
1411
1412 _CONSTEXPR iterator begin() const
1413 {
1414 return iterator {this, true};
1415 }
1416 _CONSTEXPR iterator end() const
1417 {
1418 return iterator {this};
1419 }
1420 _CONSTEXPR const_iterator cbegin() const
1421 {
1422 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1423 }
1424 _CONSTEXPR const_iterator cend() const
1425 {
1426 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1427 }
1428
1429 _CONSTEXPR reverse_iterator rbegin() const
1430 {
1431 return reverse_iterator {end()};
1432 }
1433 _CONSTEXPR reverse_iterator rend() const
1434 {
1435 return reverse_iterator {begin()};
1436 }
1437 _CONSTEXPR const_reverse_iterator crbegin() const
1438 {
1439 return const_reverse_iterator {cend()};
1440 }
1441 _CONSTEXPR const_reverse_iterator crend() const
1442 {
1443 return const_reverse_iterator {cbegin()};
1444 }
1445
1446 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>>
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001447 _CONSTEXPR bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001448 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001449 return m_bounds.size() == other.m_bounds.size() &&
1450 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001451 }
1452
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001453 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>>
1454 _CONSTEXPR bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1455 {
1456 return !(*this == other);
1457 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001458
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001459 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>>
1460 _CONSTEXPR bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1461 {
1462 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1463 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001464
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001465 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>>
1466 _CONSTEXPR bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1467 {
1468 return !(other < *this);
1469 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001470
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001471 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>>
1472 _CONSTEXPR bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1473 {
1474 return (other < *this);
1475 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001476
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001477 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>>
1478 _CONSTEXPR bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1479 {
1480 return !(*this < other);
1481 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001482
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001483public:
1484 template <typename OtherValueType, typename OtherBounds,
1485 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1486 && std::is_convertible<OtherBounds, bounds_type>::value>>
1487 _CONSTEXPR basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) _NOEXCEPT
1488 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1489 {
1490 }
1491protected:
1492
1493 _CONSTEXPR basic_array_view(pointer data, bounds_type bound) _NOEXCEPT
1494 : m_pdata(data)
1495 , m_bounds(std::move(bound))
1496 {
1497 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1498 }
1499 template <typename T>
1500 _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
1501 : m_pdata(reinterpret_cast<pointer>(data))
1502 , m_bounds(std::move(bound))
1503 {
1504 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1505 }
1506 template <typename DestBounds>
1507 _CONSTEXPR basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1508 {
1509 details::verifyBoundsReshape(m_bounds, bounds);
1510 return {m_pdata, bounds};
1511 }
1512private:
1513
1514 friend iterator;
1515 friend const_iterator;
1516 template <typename ValueType2, typename BoundsType2>
1517 friend class basic_array_view;
1518};
1519
1520template <size_t DimSize = dynamic_range>
1521struct dim
1522{
1523 static const size_t value = DimSize;
1524};
1525template <>
1526struct dim<dynamic_range>
1527{
1528 static const size_t value = dynamic_range;
1529 const size_t dvalue;
1530 dim(size_t size) : dvalue(size) {}
1531};
1532
1533template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1534class array_view;
1535template <typename ValueTypeOpt, unsigned int Rank>
1536class strided_array_view;
1537
1538namespace details
1539{
1540 template <typename T, typename = std::true_type>
1541 struct ArrayViewTypeTraits
1542 {
1543 using value_type = T;
1544 using size_type = size_t;
1545 };
1546
1547 template <typename Traits>
1548 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1549 {
1550 using value_type = typename Traits::array_view_traits::value_type;
1551 using size_type = typename Traits::array_view_traits::size_type;
1552 };
1553
1554 template <typename T, typename SizeType, size_t... Ranks>
1555 struct ArrayViewArrayTraits {
1556 using type = array_view<T, Ranks...>;
1557 using value_type = T;
1558 using bounds_type = static_bounds<SizeType, Ranks...>;
1559 using pointer = T*;
1560 using reference = T&;
1561 };
1562 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1563 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1564
1565 template <typename BoundsType>
1566 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1567 {
1568 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1569 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1570 }
1571 template <typename BoundsType>
1572 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1573 {
1574 fail_fast_assert(BoundsType::static_size == totalSize);
1575 return {};
1576 }
1577 template <typename BoundsType>
1578 BoundsType newBoundsHelper(size_t totalSize)
1579 {
1580 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1581 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1582 }
1583
1584 struct Sep{};
1585
1586 template <typename T, typename... Args>
1587 T static_as_array_view_helper(Sep, Args... args)
1588 {
1589 return T{static_cast<typename T::size_type>(args)...};
1590 }
1591 template <typename T, typename Arg, typename... Args>
1592 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)
1593 {
1594 return static_as_array_view_helper<T>(args...);
1595 }
1596 template <typename T, typename... Args>
1597 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1598 {
1599 return static_as_array_view_helper<T>(args..., val.dvalue);
1600 }
1601
1602 template <typename SizeType, typename ...Dimensions>
1603 struct static_as_array_view_static_bounds_helper
1604 {
1605 using type = static_bounds<SizeType, (Dimensions::value)...>;
1606 };
1607
1608 template <typename T>
1609 struct is_array_view_oracle : std::false_type
1610 {};
1611 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1612 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1613 {};
1614 template <typename ValueType, unsigned int Rank>
1615 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1616 {};
1617 template <typename T>
1618 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1619 {};
1620
1621}
1622
1623
1624template <typename ValueType, typename SizeType>
1625struct array_view_options
1626{
1627 struct array_view_traits
1628 {
1629 using value_type = ValueType;
1630 using size_type = SizeType;
1631 };
1632};
1633
1634template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1635class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1636 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1637{
1638 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1639 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001640 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1641 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions... >>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001642
1643public:
1644 using typename Base::bounds_type;
1645 using typename Base::size_type;
1646 using typename Base::pointer;
1647 using typename Base::value_type;
1648 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001649 using typename Base::iterator;
1650 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001651 using typename Base::reference;
1652 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001653
1654public:
1655 // basic
1656 _CONSTEXPR array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1657 {
1658 }
1659
1660 _CONSTEXPR array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1661 {
1662 }
1663
Neil MacIntosh9b40a0a2015-08-27 19:49:27 -07001664 _CONSTEXPR array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001665 {
1666 fail_fast_assert(size == 0);
1667 }
1668
1669 // default
1670 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
1671 _CONSTEXPR array_view() : Base(nullptr, bounds_type())
1672 {
1673 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001674
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001675 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1676 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001677 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
1678 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1679 _CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001680 {
1681 }
1682
1683 // from n-dimensions static array
1684 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1685 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001686 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1687 _CONSTEXPR array_view(T(&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001688 {
1689 }
1690
1691 // from n-dimensions static array with size
1692 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1693 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001694 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1695 _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001696 {
1697 fail_fast_assert(size <= N);
1698 }
1699
1700 // from std array
1701 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
1702 _CONSTEXPR array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1703 {
1704 }
1705
1706 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>>
1707 _CONSTEXPR array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1708 {
1709 }
1710
1711
1712 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1713 template <typename Ptr,
1714 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1715 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
1716 _CONSTEXPR array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1717 {
1718 }
1719
1720 // from containers. It must has .size() and .data() two function signatures
1721 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1722 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001723 && std::is_convertible<DataType(*)[], typename Base::value_type(*)[]>::value
1724 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1725 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001726 >
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001727 _CONSTEXPR array_view(Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001728 {
1729
1730 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001731
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001732 _CONSTEXPR array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001733
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001734 // convertible
1735 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1736 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1737 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1738 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1739 >
1740 _CONSTEXPR array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
1741
1742 // reshape
1743 template <typename... Dimensions2>
1744 _CONSTEXPR array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
1745 {
1746 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001747 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001748 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1749 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001750 return{ this->data(), tobounds };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001751 }
1752
1753 // to bytes array
1754 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001755 _CONSTEXPR auto as_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 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)>
1757 {
1758 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001759 return{ reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001760 }
1761
1762 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001763 _CONSTEXPR auto as_writeable_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001764 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)>
1765 {
1766 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001767 return{ reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001768 }
1769
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001770 // from bytes array
1771 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1772 _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))>
1773 {
1774 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1775 "Target type must be standard layout and its size must match the byte array size");
1776 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001777 return{ reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001778 }
1779
1780 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1781 _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))>
1782 {
1783 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1784 "Target type must be standard layout and its size must match the byte array size");
1785 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001786 return{ reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001787 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001788
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001789 // section on linear space
1790 template<size_t Count>
1791 _CONSTEXPR array_view<ValueTypeOpt, Count> first() const _NOEXCEPT
1792 {
1793 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1794 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001795 return{ this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001796 }
1797
1798 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
1799 {
1800 fail_fast_assert(count <= this->size());
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001801 return{ this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802 }
1803
1804 template<size_t Count>
1805 _CONSTEXPR array_view<ValueTypeOpt, Count> last() const _NOEXCEPT
1806 {
1807 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1808 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001809 return{ this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001810 }
1811
1812 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
1813 {
1814 fail_fast_assert(count <= this->size());
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001815 return{ this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001816 }
1817
1818 template<size_t Offset, size_t Count>
1819 _CONSTEXPR array_view<ValueTypeOpt, Count> sub() const _NOEXCEPT
1820 {
1821 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");
1822 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset < this->size()) && Offset + Count <= this->size()));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001823 return{ this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001824 }
1825
1826 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count) const _NOEXCEPT
1827 {
1828 fail_fast_assert((offset == 0 || offset < this->size()) && offset + count <= this->size());
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001829 return{ this->data() + offset, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001830 }
1831
1832 // size
1833 _CONSTEXPR size_type length() const _NOEXCEPT
1834 {
1835 return this->size();
1836 }
1837 _CONSTEXPR size_type used_length() const _NOEXCEPT
1838 {
1839 return length();
1840 }
1841 _CONSTEXPR size_type bytes() const _NOEXCEPT
1842 {
1843 return sizeof(value_type) * this->size();
1844 }
1845 _CONSTEXPR size_type used_bytes() const _NOEXCEPT
1846 {
1847 return bytes();
1848 }
1849
1850 // section
1851 _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
1852 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001853 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001854 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1855 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001856
1857 _CONSTEXPR reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001858 {
1859 return Base::operator[](idx);
1860 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001861
1862 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001863 _CONSTEXPR array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
1864 {
1865 auto ret = Base::operator[](idx);
1866 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001867 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001868
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001869 using Base::operator==;
1870 using Base::operator!=;
1871 using Base::operator<;
1872 using Base::operator<=;
1873 using Base::operator>;
1874 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001875};
1876
1877template <typename T, size_t... Dimensions>
1878_CONSTEXPR auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
1879{
1880 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1881}
1882
1883template <typename T>
1884_CONSTEXPR auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
1885{
1886 return {arr, len};
1887}
1888
1889template <typename T, size_t N>
1890_CONSTEXPR auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
1891{
1892 return {arr};
1893}
1894
1895template <typename T, size_t N>
1896_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &arr)
1897{
1898 return {arr};
1899}
1900
1901template <typename T, size_t N>
1902_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
1903
1904template <typename T, size_t N>
1905_CONSTEXPR array_view<T, N> as_array_view(std::array<T, N> &arr)
1906{
1907 return {arr};
1908}
1909
1910template <typename T>
1911_CONSTEXPR array_view<T, dynamic_range> as_array_view(T *begin, T *end)
1912{
1913 return {begin, end};
1914}
1915
1916template <typename Cont>
1917_CONSTEXPR auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1918 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1919{
1920 return {arr.data(), arr.size()};
1921}
1922
1923template <typename Cont>
1924_CONSTEXPR auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1925 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1926
1927template <typename ValueTypeOpt, unsigned int Rank>
1928class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1929{
1930 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001931
1932 template<typename OtherValueOpt, unsigned int OtherRank>
1933 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001934public:
1935 using Base::rank;
1936 using typename Base::bounds_type;
1937 using typename Base::size_type;
1938 using typename Base::pointer;
1939 using typename Base::value_type;
1940 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001941 using typename Base::iterator;
1942 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001943 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001944
1945 // from static array of size N
1946 template<size_type N>
1947 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1948 {
1949 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1950 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001951
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001952 // from raw data
1953 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001954 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001955 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001956 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001957
1958 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001959 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001960 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1961 {
1962 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1963 }
1964
1965 // convertible
1966 template <typename OtherValueTypeOpt,
1967 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1968 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1969 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1970 >
1971 _CONSTEXPR strided_array_view(const strided_array_view<OtherValueTypeOpt, Rank> &av): Base(static_cast<const typename strided_array_view<OtherValueTypeOpt, Rank>::Base &>(av)) // static_cast is required
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001972 {
1973 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001974
1975 // convert from bytes
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001976 template <typename OtherValueType>
1977 strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001978 {
1979 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1980 auto d = sizeof(OtherValueType) / sizeof(value_type);
1981
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001982 size_type size = this->bounds().total_size() / d;
1983 return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001984 }
1985
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001986 strided_array_view section(index_type origin, index_type extents) const
1987 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001988 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001989 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1990 }
1991
1992 _CONSTEXPR reference operator[](const index_type& idx) const
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001993 {
1994 return Base::operator[](idx);
1995 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001996
1997 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1998 _CONSTEXPR strided_array_view<value_type, rank-1> operator[](size_type idx) const
1999 {
2000 auto ret = Base::operator[](idx);
2001 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
2002 }
2003
2004private:
2005 static index_type resize_extent(const index_type& extent, size_t d)
2006 {
2007 fail_fast_assert(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements");
2008
2009 index_type ret = extent;
2010 ret[rank - 1] /= d;
2011
2012 return ret;
2013 }
2014
2015 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
2016 static index_type resize_stride(const index_type& strides, size_t d, void *p = 0)
2017 {
2018 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2019
2020 return strides;
2021 }
2022
2023 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2024 static index_type resize_stride(const index_type& strides, size_t d)
2025 {
2026 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2027 fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements");
2028
2029 for (int i = rank - 2; i >= 0; --i)
2030 {
2031 fail_fast_assert((strides[i] >= strides[i + 1]) && (strides[i] % strides[i + 1] == 0), "Only strided arrays with regular strides can be resized");
2032 }
2033
2034 index_type ret = strides / d;
2035 ret[rank - 1] = 1;
2036
2037 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002038 }
2039};
2040
2041template <typename ArrayView>
2042class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2043{
2044 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2045public:
2046 using typename Base::reference;
2047 using typename Base::pointer;
2048 using typename Base::difference_type;
2049private:
2050 template <typename ValueType, typename Bounds>
2051 friend class basic_array_view;
2052 pointer m_pdata;
2053 const ArrayView * m_validator;
2054 void validateThis() const
2055 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00002056 fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size());
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002057 }
2058 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2059 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2060public:
2061 reference operator*() const _NOEXCEPT
2062 {
2063 validateThis();
2064 return *m_pdata;
2065 }
2066 pointer operator->() const _NOEXCEPT
2067 {
2068 validateThis();
2069 return m_pdata;
2070 }
2071 contiguous_array_view_iterator& operator++() _NOEXCEPT
2072 {
2073 ++m_pdata;
2074 return *this;
2075 }
2076 contiguous_array_view_iterator operator++(int)_NOEXCEPT
2077 {
2078 auto ret = *this;
2079 ++(*this);
2080 return ret;
2081 }
2082 contiguous_array_view_iterator& operator--() _NOEXCEPT
2083 {
2084 --m_pdata;
2085 return *this;
2086 }
2087 contiguous_array_view_iterator operator--(int)_NOEXCEPT
2088 {
2089 auto ret = *this;
2090 --(*this);
2091 return ret;
2092 }
2093 contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2094 {
2095 contiguous_array_view_iterator ret{ *this };
2096 return ret += n;
2097 }
2098 contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2099 {
2100 m_pdata += n;
2101 return *this;
2102 }
2103 contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2104 {
2105 contiguous_array_view_iterator ret{ *this };
2106 return ret -= n;
2107 }
2108 contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2109 {
2110 return *this += -n;
2111 }
2112 difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2113 {
2114 fail_fast_assert(m_validator == rhs.m_validator);
2115 return m_pdata - rhs.m_pdata;
2116 }
2117 reference operator[](difference_type n) const _NOEXCEPT
2118 {
2119 return *(*this + n);
2120 }
2121 bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2122 {
2123 fail_fast_assert(m_validator == rhs.m_validator);
2124 return m_pdata == rhs.m_pdata;
2125 }
2126 bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2127 {
2128 return !(*this == rhs);
2129 }
2130 bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2131 {
2132 fail_fast_assert(m_validator == rhs.m_validator);
2133 return m_pdata < rhs.m_pdata;
2134 }
2135 bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2136 {
2137 return !(rhs < *this);
2138 }
2139 bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2140 {
2141 return rhs < *this;
2142 }
2143 bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2144 {
2145 return !(rhs > *this);
2146 }
2147 void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT
2148 {
2149 std::swap(m_pdata, rhs.m_pdata);
2150 std::swap(m_validator, rhs.m_validator);
2151 }
2152};
2153
2154template <typename ArrayView>
2155contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2156{
2157 return rhs + n;
2158}
2159
2160template <typename ArrayView>
2161class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2162{
2163 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2164public:
2165 using typename Base::reference;
2166 using typename Base::pointer;
2167 using typename Base::difference_type;
2168 using typename Base::value_type;
2169private:
2170 template <typename ValueType, typename Bounds>
2171 friend class basic_array_view;
2172 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002173 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002174 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2175 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2176 {
2177 }
2178public:
2179 reference operator*() const _NOEXCEPT
2180 {
2181 return (*m_container)[*m_itr];
2182 }
2183 pointer operator->() const _NOEXCEPT
2184 {
2185 return &(*m_container)[*m_itr];
2186 }
2187 general_array_view_iterator& operator++() _NOEXCEPT
2188 {
2189 ++m_itr;
2190 return *this;
2191 }
2192 general_array_view_iterator operator++(int)_NOEXCEPT
2193 {
2194 auto ret = *this;
2195 ++(*this);
2196 return ret;
2197 }
2198 general_array_view_iterator& operator--() _NOEXCEPT
2199 {
2200 --m_itr;
2201 return *this;
2202 }
2203 general_array_view_iterator operator--(int)_NOEXCEPT
2204 {
2205 auto ret = *this;
2206 --(*this);
2207 return ret;
2208 }
2209 general_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2210 {
2211 general_array_view_iterator ret{ *this };
2212 return ret += n;
2213 }
2214 general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2215 {
2216 m_itr += n;
2217 return *this;
2218 }
2219 general_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2220 {
2221 general_array_view_iterator ret{ *this };
2222 return ret -= n;
2223 }
2224 general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2225 {
2226 return *this += -n;
2227 }
2228 difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT
2229 {
2230 fail_fast_assert(m_container == rhs.m_container);
2231 return m_itr - rhs.m_itr;
2232 }
2233 value_type operator[](difference_type n) const _NOEXCEPT
2234 {
2235 return (*m_container)[m_itr[n]];;
2236 }
2237 bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT
2238 {
2239 fail_fast_assert(m_container == rhs.m_container);
2240 return m_itr == rhs.m_itr;
2241 }
2242 bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT
2243 {
2244 return !(*this == rhs);
2245 }
2246 bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT
2247 {
2248 fail_fast_assert(m_container == rhs.m_container);
2249 return m_itr < rhs.m_itr;
2250 }
2251 bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT
2252 {
2253 return !(rhs < *this);
2254 }
2255 bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT
2256 {
2257 return rhs < *this;
2258 }
2259 bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT
2260 {
2261 return !(rhs > *this);
2262 }
2263 void swap(general_array_view_iterator& rhs) _NOEXCEPT
2264 {
2265 std::swap(m_itr, rhs.m_itr);
2266 std::swap(m_container, rhs.m_container);
2267 }
2268};
2269
2270template <typename ArrayView>
2271general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2272{
2273 return rhs + n;
2274}
2275
2276} // namespace Guide
2277
2278#pragma pop_macro("_NOEXCEPT")