blob: 419397dd09426e5f603dfb521ef9d0ef5cbce3ee [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 {
293 return (value_type(&)[rank])other.elems[1];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700294 }
295
296 using Base::operator[];
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};
310
311template <typename ValueType>
312class index<1, ValueType>
313{
314 template <unsigned int, typename OtherValueType>
315 friend class index;
316public:
317 static const unsigned int rank = 1;
318 using reference = ValueType&;
319 using const_reference = const ValueType&;
320 using size_type = ValueType;
321 using value_type = ValueType;
322
323 _CONSTEXPR index() _NOEXCEPT : value(0)
324 {
325 }
326 _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
327 {
328 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700329 _CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
330 {
331 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700332 // Preconditions: il.size() == rank
333 _CONSTEXPR index(std::initializer_list<value_type> il)
334 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700335 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 -0700336 value = begin(il)[0];
337 }
338
339 _CONSTEXPR index(const index &) = default;
340
341 template <typename OtherValueType>
342 _CONSTEXPR index(const index<1, OtherValueType> & other)
343 {
344 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
345 value = static_cast<ValueType>(other.value);
346 }
347
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700348 _CONSTEXPR static index shift_left(const index<rank + 1, value_type>& other) _NOEXCEPT
349 {
350 return other.elems[1];
351 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700352 // Preconditions: component_idx < rank
353 _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
354 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700355 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700356 (void)(component_idx);
357 return value;
358 }
359 // Preconditions: component_idx < rank
360 _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
361 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700362 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700363 (void)(component_idx);
364 return value;
365 }
366 _CONSTEXPR bool operator==(const index& rhs) const _NOEXCEPT
367 {
368 return value == rhs.value;
369 }
370 _CONSTEXPR bool operator!=(const index& rhs) const _NOEXCEPT
371 {
372 return !(*this == rhs);
373 }
374 _CONSTEXPR index operator+() const _NOEXCEPT
375 {
376 return *this;
377 }
378 _CONSTEXPR index operator-() const _NOEXCEPT
379 {
380 return index(-value);
381 }
382 _CONSTEXPR index operator+(const index& rhs) const _NOEXCEPT
383 {
384 return index(value + rhs.value);
385 }
386 _CONSTEXPR index operator-(const index& rhs) const _NOEXCEPT
387 {
388 return index(value - rhs.value);
389 }
390 _CONSTEXPR index& operator+=(const index& rhs) _NOEXCEPT
391 {
392 value += rhs.value;
393 return *this;
394 }
395 _CONSTEXPR index& operator-=(const index& rhs) _NOEXCEPT
396 {
397 value -= rhs.value;
398 return *this;
399 }
400 _CONSTEXPR index& operator++() _NOEXCEPT
401 {
402 ++value;
403 return *this;
404 }
405 _CONSTEXPR index operator++(int) _NOEXCEPT
406 {
407 index ret = *this;
408 ++(*this);
409 return ret;
410 }
411 _CONSTEXPR index& operator--() _NOEXCEPT
412 {
413 --value;
414 return *this;
415 }
416 _CONSTEXPR index operator--(int) _NOEXCEPT
417 {
418 index ret = *this;
419 --(*this);
420 return ret;
421 }
422 _CONSTEXPR index operator*(value_type v) const _NOEXCEPT
423 {
424 return index(value * v);
425 }
426 _CONSTEXPR index operator/(value_type v) const _NOEXCEPT
427 {
428 return index(value / v);
429 }
430 _CONSTEXPR index& operator*=(value_type v) _NOEXCEPT
431 {
432 value *= v;
433 return *this;
434 }
435 _CONSTEXPR index& operator/=(value_type v) _NOEXCEPT
436 {
437 value /= v;
438 return *this;
439 }
440 friend _CONSTEXPR index operator*(value_type v, const index& rhs) _NOEXCEPT
441 {
442 return index(rhs * v);
443 }
444private:
445 value_type value;
446};
447
448#ifndef _MSC_VER
449
450struct static_bounds_dynamic_range_t
451{
452 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
453 constexpr operator T() const noexcept
454 {
455 return static_cast<T>(-1);
456 }
457
458 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
459 constexpr bool operator ==(T other) const noexcept
460 {
461 return static_cast<T>(-1) == other;
462 }
463
464 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
465 constexpr bool operator !=(T other) const noexcept
466 {
467 return static_cast<T>(-1) != other;
468 }
469
470};
471
472template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
473constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
474{
475 return right == left;
476}
477
478template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
479constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
480{
481 return right != left;
482}
483
484constexpr static_bounds_dynamic_range_t dynamic_range{};
485#else
486const char dynamic_range = -1;
487#endif
488
489struct generalized_mapping_tag {};
490struct contiguous_mapping_tag : generalized_mapping_tag {};
491
492namespace details
493{
494 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
495 struct StaticSizeHelperImpl
496 {
497 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
498 static const SizeType value = Fact1 * Fact2;
499 };
500
501 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
502 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
503 {
504 static const SizeType value = ConstBound;
505 };
506
507 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
508 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
509 {
510 static const SizeType value = ConstBound;
511 };
512
513 template <typename SizeType, SizeType ConstBound>
514 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
515 {
516 static const SizeType value = static_cast<SizeType>(ConstBound);
517 };
518
519 template <typename SizeType, SizeType Fact1, SizeType Fact2>
520 struct StaticSizeHelper
521 {
522 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
523 };
524
525
526 template <size_t Left, size_t Right>
527 struct LessThan
528 {
529 static const bool value = Left < Right;
530 };
531
532 template <typename SizeType, size_t... Ranges>
533 struct BoundsRanges {
534 static const unsigned int Depth = 0;
535 static const unsigned int DynamicNum = 0;
536 static const SizeType CurrentRange = 1;
537 static const SizeType TotalSize = 1;
538
539 BoundsRanges (const BoundsRanges &) = default;
540
541 // TODO : following signature is for work around VS bug
542 template <typename OtherType>
543 BoundsRanges (const OtherType &, bool firstLevel) {}
544 BoundsRanges(const SizeType * const arr) { }
545 BoundsRanges() = default;
546
547
548 template <typename T, unsigned int Dim>
549 void serialize(T &) const {
550 }
551 template <typename T, unsigned int Dim>
552 SizeType linearize(const T &) const {
553 return 0;
554 }
555 template <typename T, unsigned int Dim>
556 ptrdiff_t contains(const T &) const {
557 return 0;
558 }
559
560 size_t totalSize() const _NOEXCEPT {
561 return TotalSize;
562 }
563
564 bool operator == (const BoundsRanges &) const _NOEXCEPT
565 {
566 return true;
567 }
568 };
569
570 template <typename SizeType, size_t... RestRanges>
571 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
572 using Base = BoundsRanges <SizeType, RestRanges... >;
573 static const unsigned int Depth = Base::Depth + 1;
574 static const unsigned int DynamicNum = Base::DynamicNum + 1;
575 static const SizeType CurrentRange = dynamic_range;
576 static const SizeType TotalSize = dynamic_range;
577 const SizeType m_bound;
578
579 BoundsRanges (const BoundsRanges &) = default;
580 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
581 {
582 fail_fast_assert(0 <= *arr);
583 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
584 }
585 BoundsRanges() : m_bound(0) {}
586
587 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
588 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) :
589 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
590 {
591 }
592
593 template <typename T, unsigned int Dim = 0>
594 void serialize(T & arr) const {
595 arr[Dim] = elementNum();
596 this->Base::template serialize<T, Dim + 1>(arr);
597 }
598 template <typename T, unsigned int Dim = 0>
599 SizeType linearize(const T & arr) const {
600 const size_t index = this->Base::totalSize() * arr[Dim];
601 fail_fast_assert(index < static_cast<size_t>(m_bound));
602 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
603 }
604
605 template <typename T, unsigned int Dim = 0>
606 ptrdiff_t contains(const T & arr) const {
607 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
608 if (last == -1)
609 return -1;
610 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
611 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
612 }
613
614 size_t totalSize() const _NOEXCEPT {
615 return m_bound;
616 }
617
618 SizeType elementNum() const _NOEXCEPT {
619 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
620 }
621
622 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
623 if (dim > 0)
624 return this->Base::elementNum(dim - 1);
625 else
626 return elementNum();
627 }
628
629 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
630 {
631 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
632 }
633 };
634
635 template <typename SizeType, size_t CurRange, size_t... RestRanges>
636 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
637 using Base = BoundsRanges <SizeType, RestRanges... >;
638 static const unsigned int Depth = Base::Depth + 1;
639 static const unsigned int DynamicNum = Base::DynamicNum;
640 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
641 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
642 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
643
644 BoundsRanges (const BoundsRanges &) = default;
645 BoundsRanges(const SizeType * const arr) : Base(arr) { }
646 BoundsRanges() = default;
647
648 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
649 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
650 {
651 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
652 }
653
654 template <typename T, unsigned int Dim = 0>
655 void serialize(T & arr) const {
656 arr[Dim] = elementNum();
657 this->Base::template serialize<T, Dim + 1>(arr);
658 }
659
660 template <typename T, unsigned int Dim = 0>
661 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700662 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700663 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
664 }
665
666 template <typename T, unsigned int Dim = 0>
667 ptrdiff_t contains(const T & arr) const {
668 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
669 return -1;
670 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
671 if (last == -1)
672 return -1;
673 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
674 }
675
676 size_t totalSize() const _NOEXCEPT{
677 return CurrentRange * this->Base::totalSize();
678 }
679
680 SizeType elementNum() const _NOEXCEPT{
681 return CurrentRange;
682 }
683
684 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
685 if (dim > 0)
686 return this->Base::elementNum(dim - 1);
687 else
688 return elementNum();
689 }
690
691 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
692 {
693 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
694 }
695 };
696
697 template <typename SourceType, typename TargetType, size_t Rank>
698 struct BoundsRangeConvertible2;
699
700 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
701 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
702 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
703
704 template <size_t Rank, typename SourceType, typename TargetType>
705 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
706
707 template <typename SourceType, typename TargetType, size_t Rank>
708 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
709 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
710 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
711 {};
712
713 template <typename SourceType, typename TargetType>
714 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
715
716 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
717 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
718 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
719 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
720 {};
721 template <typename SourceType, typename TargetType>
722 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
723
724 template <typename TypeChain>
725 struct TypeListIndexer
726 {
727 const TypeChain & obj;
728 TypeListIndexer(const TypeChain & obj) :obj(obj){}
729 template<unsigned int N>
730 const TypeChain & getObj(std::true_type)
731 {
732 return obj;
733 }
734 template<unsigned int N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
735 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
736 {
737 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
738 }
739 template <unsigned int N>
740 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
741 {
742 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
743 }
744 };
745
746 template <typename TypeChain>
747 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
748 {
749 return TypeListIndexer<TypeChain>(obj);
750 }
751}
752
753template <typename IndexType>
754class bounds_iterator;
755
756template <typename SizeType, size_t... Ranges>
757class static_bounds {
758public:
759 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &empty) {
760 }
761};
762
763template <typename SizeType, size_t FirstRange, size_t... RestRanges>
764class static_bounds<SizeType, FirstRange, RestRanges...>
765{
766 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
767 static_assert(std::is_integral<SizeType>::value
768 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
769
770 MyRanges m_ranges;
771 _CONSTEXPR static_bounds(const MyRanges & range) : m_ranges(range) { }
772
773 template <typename SizeType2, size_t... Ranges2>
774 friend class static_bounds;
775public:
776 static const unsigned int rank = MyRanges::Depth;
777 static const unsigned int dynamic_rank = MyRanges::DynamicNum;
778 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
779
780 using size_type = SizeType;
781 using index_type = index<rank, size_type>;
782 using iterator = bounds_iterator<index_type>;
783 using const_iterator = bounds_iterator<index_type>;
784 using difference_type = ptrdiff_t;
785 using sliced_type = static_bounds<SizeType, RestRanges...>;
786 using mapping_type = contiguous_mapping_tag;
787public:
788 _CONSTEXPR static_bounds(const static_bounds &) = default;
789
790 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
791 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
792 _CONSTEXPR static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
793 m_ranges(other.m_ranges)
794 {
795 }
796
797 _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
798 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700799 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
800 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 -0700801 }
802
803 _CONSTEXPR static_bounds() = default;
804
805 _CONSTEXPR static_bounds & operator = (const static_bounds & otherBounds)
806 {
807 new(&m_ranges) MyRanges (otherBounds.m_ranges);
808 return *this;
809 }
810
811 _CONSTEXPR sliced_type slice() const _NOEXCEPT
812 {
813 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
814 }
815
816 _CONSTEXPR size_type stride() const _NOEXCEPT
817 {
818 return rank > 1 ? slice().size() : 1;
819 }
820
821 _CONSTEXPR size_type size() const _NOEXCEPT
822 {
823 return static_cast<size_type>(m_ranges.totalSize());
824 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700825
826 _CONSTEXPR size_type total_size() const _NOEXCEPT
827 {
828 return static_cast<size_type>(m_ranges.totalSize());
829 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700830
831 _CONSTEXPR size_type linearize(const index_type & idx) const
832 {
833 return m_ranges.linearize(idx);
834 }
835
836 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
837 {
838 return m_ranges.contains(idx) != -1;
839 }
840
841 _CONSTEXPR size_type operator[](unsigned int index) const _NOEXCEPT
842 {
843 return m_ranges.elementNum(index);
844 }
845
846 template <unsigned int Dim = 0>
847 _CONSTEXPR size_type extent() const _NOEXCEPT
848 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700849 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700850 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
851 }
852
853 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
854 {
855 index_type extents;
856 m_ranges.serialize(extents);
857 return extents;
858 }
859
860 template <typename OtherSizeTypes, size_t... Ranges>
861 _CONSTEXPR bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
862 {
863 return this->size() == rhs.size();
864 }
865
866 template <typename OtherSizeTypes, size_t... Ranges>
867 _CONSTEXPR bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
868 {
869 return !(*this == rhs);
870 }
871
872 _CONSTEXPR const_iterator begin() const _NOEXCEPT
873 {
874 return const_iterator(*this);
875 }
876
877 _CONSTEXPR const_iterator end() const _NOEXCEPT
878 {
879 index_type boundary;
880 m_ranges.serialize(boundary);
881 return const_iterator(*this, this->index_bounds());
882 }
883};
884
885template <unsigned int Rank, typename SizeType = size_t>
886class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
887{
888 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
889 friend Base;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700890 template <unsigned int OtherRank, typename OtherSizeType>
891 friend class strided_bounds;
892
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700893public:
894 using Base::rank;
895 using reference = typename Base::reference;
896 using const_reference = typename Base::const_reference;
897 using size_type = typename Base::value_type;
898 using difference_type = typename Base::value_type;
899 using value_type = typename Base::value_type;
900 using index_type = index<rank, size_type>;
901 using iterator = bounds_iterator<index_type>;
902 using const_iterator = bounds_iterator<index_type>;
903 static const int dynamic_rank = rank;
904 static const size_t static_size = dynamic_range;
905 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
906 using mapping_type = generalized_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700907 _CONSTEXPR strided_bounds(const strided_bounds &) = default;
908
909 template <typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700910 _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
911 : Base(other), m_strides(other.strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700912 {
913 }
914
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700915 _CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
916 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700917 {
918 for (unsigned int i = 0; i < rank; i++)
919 Base::elems[i] = extents[i];
920 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700921 _CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
922 : Base(values), m_strides(std::move(strides))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700923 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700924 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700925 _CONSTEXPR index_type strides() const _NOEXCEPT
926 {
927 return m_strides;
928 }
929 _CONSTEXPR size_type total_size() const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700930 {
931 size_type ret = 0;
932 for (unsigned int i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700933 ret += (Base::elems[i] - 1) * m_strides[i];
934 return ret + 1;
935 }
936 _CONSTEXPR size_type size() const _NOEXCEPT
937 {
938 size_type ret = 1;
939 for (unsigned int i = 0; i < rank; ++i)
940 ret *= Base::elems[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700941 return ret;
942 }
943 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
944 {
945 for (unsigned int i = 0; i < rank; ++i)
946 {
947 if (idx[i] < 0 || idx[i] >= Base::elems[i])
948 return false;
949 }
950 return true;
951 }
952 _CONSTEXPR size_type linearize(const index_type & idx) const
953 {
954 size_type ret = 0;
955 for (unsigned int i = 0; i < rank; i++)
956 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700957 fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
958 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700959 }
960 return ret;
961 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700962 _CONSTEXPR size_type stride() const _NOEXCEPT
963 {
964 return m_strides[0];
965 }
966 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700967 _CONSTEXPR sliced_type slice() const
968 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700969 return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700970 }
971 template <unsigned int Dim = 0>
972 _CONSTEXPR size_type extent() const _NOEXCEPT
973 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700974 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700975 return Base::elems[Dim];
976 }
977 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
978 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700979 return index_type(Base::elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700980 }
981 const_iterator begin() const _NOEXCEPT
982 {
983 return const_iterator{ *this };
984 }
985 const_iterator end() const _NOEXCEPT
986 {
987 return const_iterator{ *this, index_bounds() };
988 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700989private:
990 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700991};
992
993template <typename T>
994struct is_bounds : std::integral_constant<bool, false> {};
995template <typename SizeType, size_t... Ranges>
996struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
997template <unsigned int Rank, typename SizeType>
998struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
999
1000template <typename IndexType>
1001class bounds_iterator
1002 : public std::iterator<std::random_access_iterator_tag,
1003 IndexType,
1004 ptrdiff_t,
1005 const details::arrow_proxy<IndexType>,
1006 const IndexType>
1007{
1008private:
1009 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
1010public:
1011 static const unsigned int rank = IndexType::rank;
1012 using typename Base::reference;
1013 using typename Base::pointer;
1014 using typename Base::difference_type;
1015 using typename Base::value_type;
1016 using index_type = value_type;
1017 using index_size_type = typename IndexType::size_type;
1018 template <typename Bounds>
1019 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT
1020 : boundary(bnd.index_bounds())
1021 , curr( std::move(curr) )
1022 {
1023 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1024 }
1025 reference operator*() const _NOEXCEPT
1026 {
1027 return curr;
1028 }
1029 pointer operator->() const _NOEXCEPT
1030 {
1031 return details::arrow_proxy<value_type>{ curr };
1032 }
1033 bounds_iterator& operator++() _NOEXCEPT
1034 {
1035 for (unsigned int i = rank; i-- > 0;)
1036 {
1037 if (++curr[i] < boundary[i])
1038 {
1039 return *this;
1040 }
1041 else
1042 {
1043 curr[i] = 0;
1044 }
1045 }
1046 // If we're here we've wrapped over - set to past-the-end.
1047 for (unsigned int i = 0; i < rank; ++i)
1048 {
1049 curr[i] = boundary[i];
1050 }
1051 return *this;
1052 }
1053 bounds_iterator operator++(int) _NOEXCEPT
1054 {
1055 auto ret = *this;
1056 ++(*this);
1057 return ret;
1058 }
1059 bounds_iterator& operator--() _NOEXCEPT
1060 {
1061 for (int i = rank; i-- > 0;)
1062 {
1063 if (curr[i]-- > 0)
1064 {
1065 return *this;
1066 }
1067 else
1068 {
1069 curr[i] = boundary[i] - 1;
1070 }
1071 }
1072 // If we're here the preconditions were violated
1073 // "pre: there exists s such that r == ++s"
1074 fail_fast_assert(false);
1075 return *this;
1076 }
1077 bounds_iterator operator--(int) _NOEXCEPT
1078 {
1079 auto ret = *this;
1080 --(*this);
1081 return ret;
1082 }
1083 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1084 {
1085 bounds_iterator ret{ *this };
1086 return ret += n;
1087 }
1088 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1089 {
1090 auto linear_idx = linearize(curr) + n;
1091 value_type stride;
1092 stride[rank - 1] = 1;
1093 for (unsigned int i = rank - 1; i-- > 0;)
1094 {
1095 stride[i] = stride[i + 1] * boundary[i + 1];
1096 }
1097 for (unsigned int i = 0; i < rank; ++i)
1098 {
1099 curr[i] = linear_idx / stride[i];
1100 linear_idx = linear_idx % stride[i];
1101 }
1102 return *this;
1103 }
1104 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1105 {
1106 bounds_iterator ret{ *this };
1107 return ret -= n;
1108 }
1109 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1110 {
1111 return *this += -n;
1112 }
1113 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1114 {
1115 return linearize(curr) - linearize(rhs.curr);
1116 }
1117 reference operator[](difference_type n) const _NOEXCEPT
1118 {
1119 return *(*this + n);
1120 }
1121 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1122 {
1123 return curr == rhs.curr;
1124 }
1125 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1126 {
1127 return !(*this == rhs);
1128 }
1129 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1130 {
1131 for (unsigned int i = 0; i < rank; ++i)
1132 {
1133 if (curr[i] < rhs.curr[i])
1134 return true;
1135 }
1136 return false;
1137 }
1138 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1139 {
1140 return !(rhs < *this);
1141 }
1142 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1143 {
1144 return rhs < *this;
1145 }
1146 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1147 {
1148 return !(rhs > *this);
1149 }
1150 void swap(bounds_iterator& rhs) _NOEXCEPT
1151 {
1152 std::swap(boundary, rhs.boundary);
1153 std::swap(curr, rhs.curr);
1154 }
1155private:
1156 index_size_type linearize(const value_type& idx) const _NOEXCEPT
1157 {
1158 // TODO: Smarter impl.
1159 // Check if past-the-end
1160 bool pte = true;
1161 for (unsigned int i = 0; i < rank; ++i)
1162 {
1163 if (idx[i] != boundary[i])
1164 {
1165 pte = false;
1166 break;
1167 }
1168 }
1169 index_size_type multiplier = 1;
1170 index_size_type res = 0;
1171 if (pte)
1172 {
1173 res = 1;
1174 for (unsigned int i = rank; i-- > 0;)
1175 {
1176 res += (idx[i] - 1) * multiplier;
1177 multiplier *= boundary[i];
1178 }
1179 }
1180 else
1181 {
1182 for (unsigned int i = rank; i-- > 0;)
1183 {
1184 res += idx[i] * multiplier;
1185 multiplier *= boundary[i];
1186 }
1187 }
1188 return res;
1189 }
1190 value_type boundary;
1191 value_type curr;
1192};
1193
1194template <typename SizeType>
1195class bounds_iterator<index<1, SizeType>>
1196 : public std::iterator<std::random_access_iterator_tag,
1197 index<1, SizeType>,
1198 ptrdiff_t,
1199 const details::arrow_proxy<index<1, SizeType>>,
1200 const index<1, SizeType>>
1201{
1202 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>>;
1203
1204public:
1205 using typename Base::reference;
1206 using typename Base::pointer;
1207 using typename Base::difference_type;
1208 using typename Base::value_type;
1209 using index_type = value_type;
1210 using index_size_type = typename index_type::size_type;
1211
1212 template <typename Bounds>
1213 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT
1214 : curr( std::move(curr) )
1215 {}
1216 reference operator*() const _NOEXCEPT
1217 {
1218 return curr;
1219 }
1220 pointer operator->() const _NOEXCEPT
1221 {
1222 return details::arrow_proxy<value_type>{ curr };
1223 }
1224 bounds_iterator& operator++() _NOEXCEPT
1225 {
1226 ++curr;
1227 return *this;
1228 }
1229 bounds_iterator operator++(int) _NOEXCEPT
1230 {
1231 auto ret = *this;
1232 ++(*this);
1233 return ret;
1234 }
1235 bounds_iterator& operator--() _NOEXCEPT
1236 {
1237 curr--;
1238 return *this;
1239 }
1240 bounds_iterator operator--(int) _NOEXCEPT
1241 {
1242 auto ret = *this;
1243 --(*this);
1244 return ret;
1245 }
1246 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1247 {
1248 bounds_iterator ret{ *this };
1249 return ret += n;
1250 }
1251 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1252 {
1253 curr += n;
1254 return *this;
1255 }
1256 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1257 {
1258 bounds_iterator ret{ *this };
1259 return ret -= n;
1260 }
1261 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1262 {
1263 return *this += -n;
1264 }
1265 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1266 {
1267 return curr[0] - rhs.curr[0];
1268 }
1269 reference operator[](difference_type n) const _NOEXCEPT
1270 {
1271 return curr + n;
1272 }
1273 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1274 {
1275 return curr == rhs.curr;
1276 }
1277 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1278 {
1279 return !(*this == rhs);
1280 }
1281 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1282 {
1283 return curr[0] < rhs.curr[0];
1284 }
1285 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1286 {
1287 return !(rhs < *this);
1288 }
1289 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1290 {
1291 return rhs < *this;
1292 }
1293 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1294 {
1295 return !(rhs > *this);
1296 }
1297 void swap(bounds_iterator& rhs) _NOEXCEPT
1298 {
1299 std::swap(curr, rhs.curr);
1300 }
1301private:
1302 value_type curr;
1303};
1304
1305template <typename IndexType>
1306bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) _NOEXCEPT
1307{
1308 return rhs + n;
1309}
1310
1311/*
1312** begin definitions of basic_array_view
1313*/
1314namespace details
1315{
1316 template <typename Bounds>
1317 _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
1318 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001319 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001320 }
1321
1322 // Make a stride vector from bounds, assuming continugous memory.
1323 template <typename Bounds>
1324 _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
1325 {
1326 auto extents = bnd.index_bounds();
1327 typename Bounds::index_type stride;
1328 stride[Bounds::rank - 1] = 1;
1329 for (int i = Bounds::rank - 2; i >= 0; --i)
1330 stride[i] = stride[i + 1] * extents[i + 1];
1331 return stride;
1332 }
1333
1334 template <typename BoundsSrc, typename BoundsDest>
1335 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1336 {
1337 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1338 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1339 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");
1340 fail_fast_assert(src.size() == dest.size());
1341 }
1342
1343
1344} // namespace details
1345
1346template <typename ArrayView>
1347class contiguous_array_view_iterator;
1348template <typename ArrayView>
1349class general_array_view_iterator;
1350enum class byte : std::uint8_t {};
1351
1352template <typename ValueType, typename BoundsType>
1353class basic_array_view
1354{
1355public:
1356 static const unsigned int rank = BoundsType::rank;
1357 using bounds_type = BoundsType;
1358 using size_type = typename bounds_type::size_type;
1359 using index_type = typename bounds_type::index_type;
1360 using value_type = ValueType;
1361 using pointer = ValueType*;
1362 using reference = ValueType&;
1363 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>>;
1364 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>>>;
1365 using reverse_iterator = std::reverse_iterator<iterator>;
1366 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1367 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1368
1369private:
1370 pointer m_pdata;
1371 bounds_type m_bounds;
1372
1373public:
1374 _CONSTEXPR bounds_type bounds() const _NOEXCEPT
1375 {
1376 return m_bounds;
1377 }
1378 template <unsigned int Dim = 0>
1379 _CONSTEXPR size_type extent() const _NOEXCEPT
1380 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001381 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001382 return m_bounds.template extent<Dim>();
1383 }
1384 _CONSTEXPR size_type size() const _NOEXCEPT
1385 {
1386 return m_bounds.size();
1387 }
1388 _CONSTEXPR reference operator[](const index_type& idx) const
1389 {
1390 return m_pdata[m_bounds.linearize(idx)];
1391 }
1392 _CONSTEXPR pointer data() const _NOEXCEPT
1393 {
1394 return m_pdata;
1395 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001396 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001397 _CONSTEXPR Ret operator[](size_type idx) const
1398 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001399 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001400 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001401
1402 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001403 return Ret {m_pdata + ridx, m_bounds.slice()};
1404 }
1405
1406 _CONSTEXPR operator bool () const _NOEXCEPT
1407 {
1408 return m_pdata != nullptr;
1409 }
1410
1411 _CONSTEXPR iterator begin() const
1412 {
1413 return iterator {this, true};
1414 }
1415 _CONSTEXPR iterator end() const
1416 {
1417 return iterator {this};
1418 }
1419 _CONSTEXPR const_iterator cbegin() const
1420 {
1421 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1422 }
1423 _CONSTEXPR const_iterator cend() const
1424 {
1425 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1426 }
1427
1428 _CONSTEXPR reverse_iterator rbegin() const
1429 {
1430 return reverse_iterator {end()};
1431 }
1432 _CONSTEXPR reverse_iterator rend() const
1433 {
1434 return reverse_iterator {begin()};
1435 }
1436 _CONSTEXPR const_reverse_iterator crbegin() const
1437 {
1438 return const_reverse_iterator {cend()};
1439 }
1440 _CONSTEXPR const_reverse_iterator crend() const
1441 {
1442 return const_reverse_iterator {cbegin()};
1443 }
1444
1445 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 -07001446 _CONSTEXPR bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001447 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001448 return m_bounds.size() == other.m_bounds.size() &&
1449 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001450 }
1451
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001452 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>>
1453 _CONSTEXPR bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1454 {
1455 return !(*this == other);
1456 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001457
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001458 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>>
1459 _CONSTEXPR bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1460 {
1461 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1462 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001463
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001464 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>>
1465 _CONSTEXPR bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1466 {
1467 return !(other < *this);
1468 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001469
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001470 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>>
1471 _CONSTEXPR bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1472 {
1473 return (other < *this);
1474 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001475
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001476 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>>
1477 _CONSTEXPR bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1478 {
1479 return !(*this < other);
1480 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001481
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001482public:
1483 template <typename OtherValueType, typename OtherBounds,
1484 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1485 && std::is_convertible<OtherBounds, bounds_type>::value>>
1486 _CONSTEXPR basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) _NOEXCEPT
1487 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1488 {
1489 }
1490protected:
1491
1492 _CONSTEXPR basic_array_view(pointer data, bounds_type bound) _NOEXCEPT
1493 : m_pdata(data)
1494 , m_bounds(std::move(bound))
1495 {
1496 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1497 }
1498 template <typename T>
1499 _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
1500 : m_pdata(reinterpret_cast<pointer>(data))
1501 , m_bounds(std::move(bound))
1502 {
1503 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1504 }
1505 template <typename DestBounds>
1506 _CONSTEXPR basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1507 {
1508 details::verifyBoundsReshape(m_bounds, bounds);
1509 return {m_pdata, bounds};
1510 }
1511private:
1512
1513 friend iterator;
1514 friend const_iterator;
1515 template <typename ValueType2, typename BoundsType2>
1516 friend class basic_array_view;
1517};
1518
1519template <size_t DimSize = dynamic_range>
1520struct dim
1521{
1522 static const size_t value = DimSize;
1523};
1524template <>
1525struct dim<dynamic_range>
1526{
1527 static const size_t value = dynamic_range;
1528 const size_t dvalue;
1529 dim(size_t size) : dvalue(size) {}
1530};
1531
1532template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1533class array_view;
1534template <typename ValueTypeOpt, unsigned int Rank>
1535class strided_array_view;
1536
1537namespace details
1538{
1539 template <typename T, typename = std::true_type>
1540 struct ArrayViewTypeTraits
1541 {
1542 using value_type = T;
1543 using size_type = size_t;
1544 };
1545
1546 template <typename Traits>
1547 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1548 {
1549 using value_type = typename Traits::array_view_traits::value_type;
1550 using size_type = typename Traits::array_view_traits::size_type;
1551 };
1552
1553 template <typename T, typename SizeType, size_t... Ranks>
1554 struct ArrayViewArrayTraits {
1555 using type = array_view<T, Ranks...>;
1556 using value_type = T;
1557 using bounds_type = static_bounds<SizeType, Ranks...>;
1558 using pointer = T*;
1559 using reference = T&;
1560 };
1561 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1562 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1563
1564 template <typename BoundsType>
1565 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1566 {
1567 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1568 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1569 }
1570 template <typename BoundsType>
1571 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1572 {
1573 fail_fast_assert(BoundsType::static_size == totalSize);
1574 return {};
1575 }
1576 template <typename BoundsType>
1577 BoundsType newBoundsHelper(size_t totalSize)
1578 {
1579 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1580 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1581 }
1582
1583 struct Sep{};
1584
1585 template <typename T, typename... Args>
1586 T static_as_array_view_helper(Sep, Args... args)
1587 {
1588 return T{static_cast<typename T::size_type>(args)...};
1589 }
1590 template <typename T, typename Arg, typename... Args>
1591 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)
1592 {
1593 return static_as_array_view_helper<T>(args...);
1594 }
1595 template <typename T, typename... Args>
1596 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1597 {
1598 return static_as_array_view_helper<T>(args..., val.dvalue);
1599 }
1600
1601 template <typename SizeType, typename ...Dimensions>
1602 struct static_as_array_view_static_bounds_helper
1603 {
1604 using type = static_bounds<SizeType, (Dimensions::value)...>;
1605 };
1606
1607 template <typename T>
1608 struct is_array_view_oracle : std::false_type
1609 {};
1610 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1611 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1612 {};
1613 template <typename ValueType, unsigned int Rank>
1614 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1615 {};
1616 template <typename T>
1617 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1618 {};
1619
1620}
1621
1622
1623template <typename ValueType, typename SizeType>
1624struct array_view_options
1625{
1626 struct array_view_traits
1627 {
1628 using value_type = ValueType;
1629 using size_type = SizeType;
1630 };
1631};
1632
1633template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1634class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1635 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1636{
1637 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1638 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001639 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001640 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001641
1642public:
1643 using typename Base::bounds_type;
1644 using typename Base::size_type;
1645 using typename Base::pointer;
1646 using typename Base::value_type;
1647 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001648 using typename Base::iterator;
1649 using typename Base::const_iterator;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001650 using Base::rank;
1651
1652public:
1653 // basic
1654 _CONSTEXPR array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1655 {
1656 }
1657
1658 _CONSTEXPR array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1659 {
1660 }
1661
Neil MacIntosh9b40a0a2015-08-27 19:49:27 -07001662 _CONSTEXPR array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001663 {
1664 fail_fast_assert(size == 0);
1665 }
1666
1667 // default
1668 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
1669 _CONSTEXPR array_view() : Base(nullptr, bounds_type())
1670 {
1671 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001672
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001673 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1674 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001675 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1676 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
1677 _CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001678 {
1679 }
1680
1681 // from n-dimensions static array
1682 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1683 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001684 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Anna Gringauzee5b79d22015-09-14 16:38:25 -07001685 _CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001686 {
1687 }
1688
1689 // from n-dimensions static array with size
1690 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1691 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 -07001692 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1693 _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001694 {
1695 fail_fast_assert(size <= N);
1696 }
1697
1698 // from std array
1699 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
1700 _CONSTEXPR array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1701 {
1702 }
1703
1704 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>>
1705 _CONSTEXPR array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1706 {
1707 }
1708
1709
1710 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1711 template <typename Ptr,
1712 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1713 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
1714 _CONSTEXPR array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1715 {
1716 }
1717
1718 // from containers. It must has .size() and .data() two function signatures
1719 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1720 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001721 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001722 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1723 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001724 >
Anna Gringauze18cd9802015-09-14 16:34:26 -07001725 _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 -07001726 {
1727
1728 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001729
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001730 _CONSTEXPR array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001731
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001732 // convertible
1733 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1734 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1735 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1736 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1737 >
1738 _CONSTEXPR array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
1739
1740 // reshape
1741 template <typename... Dimensions2>
1742 _CONSTEXPR array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
1743 {
1744 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001745 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001746 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1747 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001748 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001749 }
1750
1751 // to bytes array
1752 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001753 _CONSTEXPR auto as_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001754 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)>
1755 {
1756 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001757 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001758 }
1759
1760 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001761 _CONSTEXPR auto as_writeable_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001762 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)>
1763 {
1764 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001765 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001766 }
1767
Anna Gringauze18cd9802015-09-14 16:34:26 -07001768
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001769 // from bytes array
1770 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1771 _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))>
1772 {
1773 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1774 "Target type must be standard layout and its size must match the byte array size");
1775 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001776 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001777 }
1778
1779 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1780 _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))>
1781 {
1782 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1783 "Target type must be standard layout and its size must match the byte array size");
1784 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001785 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001786 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001787
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001788 // section on linear space
1789 template<size_t Count>
1790 _CONSTEXPR array_view<ValueTypeOpt, Count> first() const _NOEXCEPT
1791 {
1792 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1793 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
Anna Gringauze18cd9802015-09-14 16:34:26 -07001794 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001795 }
1796
1797 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
1798 {
1799 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001800 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001801 }
1802
1803 template<size_t Count>
1804 _CONSTEXPR array_view<ValueTypeOpt, Count> last() const _NOEXCEPT
1805 {
1806 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1807 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001808 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001809 }
1810
1811 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
1812 {
1813 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001814 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001815 }
1816
1817 template<size_t Offset, size_t Count>
1818 _CONSTEXPR array_view<ValueTypeOpt, Count> sub() const _NOEXCEPT
1819 {
1820 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");
1821 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset < this->size()) && Offset + Count <= this->size()));
Anna Gringauze18cd9802015-09-14 16:34:26 -07001822 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001823 }
1824
1825 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count) const _NOEXCEPT
1826 {
1827 fail_fast_assert((offset == 0 || offset < this->size()) && offset + count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001828 return { this->data() + offset, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001829 }
1830
1831 // size
1832 _CONSTEXPR size_type length() const _NOEXCEPT
1833 {
1834 return this->size();
1835 }
1836 _CONSTEXPR size_type used_length() const _NOEXCEPT
1837 {
1838 return length();
1839 }
1840 _CONSTEXPR size_type bytes() const _NOEXCEPT
1841 {
1842 return sizeof(value_type) * this->size();
1843 }
1844 _CONSTEXPR size_type used_bytes() const _NOEXCEPT
1845 {
1846 return bytes();
1847 }
1848
1849 // section
1850 _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
1851 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001852 size_type size = bounds().total_size() - bounds().linearize(origin);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001853 return { &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001854 }
1855 _CONSTEXPR reference operator[](const index_type& idx) const
1856 {
1857 return Base::operator[](idx);
1858 }
1859 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1860 _CONSTEXPR array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
1861 {
1862 auto ret = Base::operator[](idx);
1863 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001864 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001865
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001866 using Base::operator==;
1867 using Base::operator!=;
1868 using Base::operator<;
1869 using Base::operator<=;
1870 using Base::operator>;
1871 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001872};
1873
1874template <typename T, size_t... Dimensions>
1875_CONSTEXPR auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
1876{
1877 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1878}
1879
1880template <typename T>
1881_CONSTEXPR auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
1882{
1883 return {arr, len};
1884}
1885
1886template <typename T, size_t N>
1887_CONSTEXPR auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
1888{
1889 return {arr};
1890}
1891
1892template <typename T, size_t N>
1893_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &arr)
1894{
1895 return {arr};
1896}
1897
1898template <typename T, size_t N>
1899_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
1900
1901template <typename T, size_t N>
1902_CONSTEXPR array_view<T, N> as_array_view(std::array<T, N> &arr)
1903{
1904 return {arr};
1905}
1906
1907template <typename T>
1908_CONSTEXPR array_view<T, dynamic_range> as_array_view(T *begin, T *end)
1909{
1910 return {begin, end};
1911}
1912
1913template <typename Cont>
1914_CONSTEXPR auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1915 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1916{
1917 return {arr.data(), arr.size()};
1918}
1919
1920template <typename Cont>
1921_CONSTEXPR auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1922 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1923
1924template <typename ValueTypeOpt, unsigned int Rank>
1925class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1926{
1927 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 -07001928
1929 template<typename OtherValueOpt, unsigned int OtherRank>
1930 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001931public:
1932 using Base::rank;
1933 using typename Base::bounds_type;
1934 using typename Base::size_type;
1935 using typename Base::pointer;
1936 using typename Base::value_type;
1937 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001938 using typename Base::iterator;
1939 using typename Base::const_iterator;
1940
1941 // from static array of size N
1942 template<size_type N>
1943 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1944 {
1945 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1946 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001947
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001948 // from raw data
1949 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001950 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001951 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001952 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001953
1954 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001955 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001956 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1957 {
1958 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1959 }
1960
1961 // convertible
1962 template <typename OtherValueTypeOpt,
1963 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1964 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1965 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1966 >
1967 _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 -07001968 {
1969 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001970
1971 // convert from bytes
1972 template <typename OtherValueType, typename Dummy = std::enable_if_t<std::is_same<value_type, const byte>::value>>
1973 strided_array_view<OtherValueType, rank> as_strided_array_view() const
1974 {
1975 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1976 auto d = sizeof(OtherValueType) / sizeof(value_type);
1977
1978 size_type size = bounds().total_size() / d;
1979 return{ (OtherValueType*)data(), size, bounds_type{ resize_extent(bounds().index_bounds(), d), resize_stride(bounds().strides(), d)} };
1980 }
1981
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001982 strided_array_view section(index_type origin, index_type extents) const
1983 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001984 size_type size = bounds().total_size() - bounds().linearize(origin);
1985 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1986 }
1987
1988 _CONSTEXPR reference operator[](const index_type& idx) const
1989 {
1990 return Base::operator[](idx);
1991 }
1992
1993 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1994 _CONSTEXPR strided_array_view<value_type, rank-1> operator[](size_type idx) const
1995 {
1996 auto ret = Base::operator[](idx);
1997 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1998 }
1999
2000private:
2001 static index_type resize_extent(const index_type& extent, size_t d)
2002 {
2003 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");
2004
2005 index_type ret = extent;
2006 ret[rank - 1] /= d;
2007
2008 return ret;
2009 }
2010
2011 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
2012 static index_type resize_stride(const index_type& strides, size_t d, void *p = 0)
2013 {
2014 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2015
2016 return strides;
2017 }
2018
2019 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2020 static index_type resize_stride(const index_type& strides, size_t d)
2021 {
2022 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2023 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");
2024
2025 for (int i = rank - 2; i >= 0; --i)
2026 {
2027 fail_fast_assert((strides[i] >= strides[i + 1]) && (strides[i] % strides[i + 1] == 0), "Only strided arrays with regular strides can be resized");
2028 }
2029
2030 index_type ret = strides / d;
2031 ret[rank - 1] = 1;
2032
2033 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002034 }
2035};
2036
2037template <typename ArrayView>
2038class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2039{
2040 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2041public:
2042 using typename Base::reference;
2043 using typename Base::pointer;
2044 using typename Base::difference_type;
2045private:
2046 template <typename ValueType, typename Bounds>
2047 friend class basic_array_view;
2048 pointer m_pdata;
2049 const ArrayView * m_validator;
2050 void validateThis() const
2051 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002052 fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), "iterator is out of range of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002053 }
2054 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2055 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2056public:
2057 reference operator*() const _NOEXCEPT
2058 {
2059 validateThis();
2060 return *m_pdata;
2061 }
2062 pointer operator->() const _NOEXCEPT
2063 {
2064 validateThis();
2065 return m_pdata;
2066 }
2067 contiguous_array_view_iterator& operator++() _NOEXCEPT
2068 {
2069 ++m_pdata;
2070 return *this;
2071 }
2072 contiguous_array_view_iterator operator++(int)_NOEXCEPT
2073 {
2074 auto ret = *this;
2075 ++(*this);
2076 return ret;
2077 }
2078 contiguous_array_view_iterator& operator--() _NOEXCEPT
2079 {
2080 --m_pdata;
2081 return *this;
2082 }
2083 contiguous_array_view_iterator operator--(int)_NOEXCEPT
2084 {
2085 auto ret = *this;
2086 --(*this);
2087 return ret;
2088 }
2089 contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2090 {
2091 contiguous_array_view_iterator ret{ *this };
2092 return ret += n;
2093 }
2094 contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2095 {
2096 m_pdata += n;
2097 return *this;
2098 }
2099 contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2100 {
2101 contiguous_array_view_iterator ret{ *this };
2102 return ret -= n;
2103 }
2104 contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2105 {
2106 return *this += -n;
2107 }
2108 difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2109 {
2110 fail_fast_assert(m_validator == rhs.m_validator);
2111 return m_pdata - rhs.m_pdata;
2112 }
2113 reference operator[](difference_type n) const _NOEXCEPT
2114 {
2115 return *(*this + n);
2116 }
2117 bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2118 {
2119 fail_fast_assert(m_validator == rhs.m_validator);
2120 return m_pdata == rhs.m_pdata;
2121 }
2122 bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2123 {
2124 return !(*this == rhs);
2125 }
2126 bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2127 {
2128 fail_fast_assert(m_validator == rhs.m_validator);
2129 return m_pdata < rhs.m_pdata;
2130 }
2131 bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2132 {
2133 return !(rhs < *this);
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 void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT
2144 {
2145 std::swap(m_pdata, rhs.m_pdata);
2146 std::swap(m_validator, rhs.m_validator);
2147 }
2148};
2149
2150template <typename ArrayView>
2151contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2152{
2153 return rhs + n;
2154}
2155
2156template <typename ArrayView>
2157class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2158{
2159 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2160public:
2161 using typename Base::reference;
2162 using typename Base::pointer;
2163 using typename Base::difference_type;
2164 using typename Base::value_type;
2165private:
2166 template <typename ValueType, typename Bounds>
2167 friend class basic_array_view;
2168 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002169 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002170 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2171 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2172 {
2173 }
2174public:
2175 reference operator*() const _NOEXCEPT
2176 {
2177 return (*m_container)[*m_itr];
2178 }
2179 pointer operator->() const _NOEXCEPT
2180 {
2181 return &(*m_container)[*m_itr];
2182 }
2183 general_array_view_iterator& operator++() _NOEXCEPT
2184 {
2185 ++m_itr;
2186 return *this;
2187 }
2188 general_array_view_iterator operator++(int)_NOEXCEPT
2189 {
2190 auto ret = *this;
2191 ++(*this);
2192 return ret;
2193 }
2194 general_array_view_iterator& operator--() _NOEXCEPT
2195 {
2196 --m_itr;
2197 return *this;
2198 }
2199 general_array_view_iterator operator--(int)_NOEXCEPT
2200 {
2201 auto ret = *this;
2202 --(*this);
2203 return ret;
2204 }
2205 general_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2206 {
2207 general_array_view_iterator ret{ *this };
2208 return ret += n;
2209 }
2210 general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2211 {
2212 m_itr += n;
2213 return *this;
2214 }
2215 general_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2216 {
2217 general_array_view_iterator ret{ *this };
2218 return ret -= n;
2219 }
2220 general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2221 {
2222 return *this += -n;
2223 }
2224 difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT
2225 {
2226 fail_fast_assert(m_container == rhs.m_container);
2227 return m_itr - rhs.m_itr;
2228 }
2229 value_type operator[](difference_type n) const _NOEXCEPT
2230 {
2231 return (*m_container)[m_itr[n]];;
2232 }
2233 bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT
2234 {
2235 fail_fast_assert(m_container == rhs.m_container);
2236 return m_itr == rhs.m_itr;
2237 }
2238 bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT
2239 {
2240 return !(*this == rhs);
2241 }
2242 bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT
2243 {
2244 fail_fast_assert(m_container == rhs.m_container);
2245 return m_itr < rhs.m_itr;
2246 }
2247 bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT
2248 {
2249 return !(rhs < *this);
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 void swap(general_array_view_iterator& rhs) _NOEXCEPT
2260 {
2261 std::swap(m_itr, rhs.m_itr);
2262 std::swap(m_container, rhs.m_container);
2263 }
2264};
2265
2266template <typename ArrayView>
2267general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2268{
2269 return rhs + n;
2270}
2271
2272} // namespace Guide
2273
2274#pragma pop_macro("_NOEXCEPT")