blob: 2e061db75b73bdd2013f5b56b514a3706b7be2bc [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
Neil MacIntosh9a297122015-09-14 15:11:07 -070055#if _MSC_VER <= 1800
56#pragma warning(push)
57#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
58#endif // _MSC_VER <= 1800
59
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070060namespace Guide {
61
62/*
63** begin definitions of index and bounds
64*/
65namespace details
66{
67 template <typename SizeType>
68 struct SizeTypeTraits
69 {
70 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);
71 };
72
73
74 template <typename ConcreteType, typename ValueType, unsigned int Rank>
75 class coordinate_facade
76 {
77 static_assert(std::is_integral<ValueType>::value
78 && sizeof(ValueType) <= sizeof(size_t), "ValueType must be unsigned integral type!");
79 static_assert(Rank > 0, "Rank must be greater than 0!");
80
81 template <typename OtherConcreteType, typename OtherValueType, unsigned int OtherRank>
82 friend class coordinate_facade;
83 public:
84 using reference = ValueType&;
85 using const_reference = const ValueType&;
86 using value_type = ValueType;
87 static const unsigned int rank = Rank;
88 _CONSTEXPR coordinate_facade() _NOEXCEPT
89 {
90 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -070091 }
92 _CONSTEXPR coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT
93 {
94 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070095 for (unsigned int i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -070096 elems[i] = values[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070097 }
98 _CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
99 {
100 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
101 static_assert(rank == 1, "This constructor can only be used with rank == 1.");
102 elems[0] = e0;
103 }
104 // Preconditions: il.size() == rank
105 _CONSTEXPR coordinate_facade(std::initializer_list<value_type> il)
106 {
107 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700108 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 -0700109 for (unsigned int i = 0; i < rank; ++i)
110 {
111 elems[i] = begin(il)[i];
112 }
113 }
114
115 _CONSTEXPR coordinate_facade(const coordinate_facade & other) = default;
116
117 template <typename OtherConcreteType, typename OtherValueType>
118 _CONSTEXPR coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
119 {
120 for (unsigned int i = 0; i < rank; ++i)
121 {
122 fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
123 elems[i] = static_cast<value_type>(other.elems[i]);
124 }
125 }
126 protected:
127 coordinate_facade& operator=(const coordinate_facade& rhs) = default;
128 // Preconditions: component_idx < rank
129 _CONSTEXPR reference operator[](unsigned int component_idx)
130 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700131 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700132 return elems[component_idx];
133 }
134 // Preconditions: component_idx < rank
135 _CONSTEXPR const_reference operator[](unsigned int component_idx) const
136 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700137 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700138 return elems[component_idx];
139 }
140 _CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
141 {
142 for (unsigned int i = 0; i < rank; ++i)
143 {
144 if (elems[i] != rhs.elems[i])
145 return false;
146 }
147 return true;
148 }
149 _CONSTEXPR bool operator!=(const ConcreteType& rhs) const _NOEXCEPT
150 {
151 return !(to_concrete() == rhs);
152 }
153 _CONSTEXPR ConcreteType operator+() const _NOEXCEPT
154 {
155 return to_concrete();
156 }
157 _CONSTEXPR ConcreteType operator-() const
158 {
159 ConcreteType ret = to_concrete();
160 for (unsigned int i = 0; i < rank; ++i)
161 ret.elems[i] = -ret.elems[i];
162 return ret;
163 }
164 _CONSTEXPR ConcreteType operator+(const ConcreteType& rhs) const
165 {
166 ConcreteType ret = to_concrete();
167 ret += rhs;
168 return ret;
169 }
170 _CONSTEXPR ConcreteType operator-(const ConcreteType& rhs) const
171 {
172 ConcreteType ret = to_concrete();
173 ret -= rhs;
174 return ret;
175 }
176 _CONSTEXPR ConcreteType& operator+=(const ConcreteType& rhs)
177 {
178 for (unsigned int i = 0; i < rank; ++i)
179 elems[i] += rhs.elems[i];
180 return to_concrete();
181 }
182 _CONSTEXPR ConcreteType& operator-=(const ConcreteType& rhs)
183 {
184 for (unsigned int i = 0; i < rank; ++i)
185 elems[i] -= rhs.elems[i];
186 return to_concrete();
187 }
188 _CONSTEXPR ConcreteType& operator++()
189 {
190 static_assert(rank == 1, "This operator can only be used with rank == 1.");
191 ++elems[0];
192 return to_concrete();
193 }
194 _CONSTEXPR ConcreteType operator++(int)
195 {
196 static_assert(rank == 1, "This operator can only be used with rank == 1.");
197 ConcreteType ret = to_concrete();
198 ++(*this);
199 return ret;
200 }
201 _CONSTEXPR ConcreteType& operator--()
202 {
203 static_assert(rank == 1, "This operator can only be used with rank == 1.");
204 --elems[0];
205 return to_concrete();
206 }
207 _CONSTEXPR ConcreteType operator--(int)
208 {
209 static_assert(rank == 1, "This operator can only be used with rank == 1.");
210 ConcreteType ret = to_concrete();
211 --(*this);
212 return ret;
213 }
214 _CONSTEXPR ConcreteType operator*(value_type v) const
215 {
216 ConcreteType ret = to_concrete();
217 ret *= v;
218 return ret;
219 }
220 _CONSTEXPR ConcreteType operator/(value_type v) const
221 {
222 ConcreteType ret = to_concrete();
223 ret /= v;
224 return ret;
225 }
226 friend _CONSTEXPR ConcreteType operator*(value_type v, const ConcreteType& rhs)
227 {
228 return rhs * v;
229 }
230 _CONSTEXPR ConcreteType& operator*=(value_type v)
231 {
232 for (unsigned int i = 0; i < rank; ++i)
233 elems[i] *= v;
234 return to_concrete();
235 }
236 _CONSTEXPR ConcreteType& operator/=(value_type v)
237 {
238 for (unsigned int i = 0; i < rank; ++i)
239 elems[i] /= v;
240 return to_concrete();
241 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700242 value_type elems[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700243 private:
244 _CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
245 {
246 return static_cast<const ConcreteType&>(*this);
247 }
248 _CONSTEXPR ConcreteType& to_concrete() _NOEXCEPT
249 {
250 return static_cast<ConcreteType&>(*this);
251 }
252 };
253 template <typename T>
254 class arrow_proxy
255 {
256 public:
257 explicit arrow_proxy(T t)
258 : val(t)
259 {}
260 const T operator*() const _NOEXCEPT
261 {
262 return val;
263 }
264 const T* operator->() const _NOEXCEPT
265 {
266 return &val;
267 }
268 private:
269 T val;
270 };
271}
272
273template <unsigned int Rank, typename ValueType = size_t>
274class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
275{
276 using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
277 friend Base;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700278 template <unsigned int OtherRank, typename OtherValueType>
279 friend class index;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700280public:
281 using Base::rank;
282 using reference = typename Base::reference;
283 using const_reference = typename Base::const_reference;
284 using size_type = typename Base::value_type;
285 using value_type = typename Base::value_type;
286 _CONSTEXPR index() _NOEXCEPT : Base(){}
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700287 _CONSTEXPR index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {}
288 _CONSTEXPR index(std::initializer_list<value_type> il) : Base(il) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700289
290 _CONSTEXPR index(const index &) = default;
291
292 template <typename OtherValueType>
293 _CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
294 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700295 }
296 _CONSTEXPR static index shift_left(const index<rank+1, value_type>& other) _NOEXCEPT
297 {
Anna Gringauze1a864982015-09-14 18:55:06 -0700298 value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
299 return index(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700300 }
301
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 using Base::operator--;
311 using Base::operator*;
312 using Base::operator/;
313 using Base::operator*=;
314 using Base::operator/=;
315};
316
317template <typename ValueType>
318class index<1, ValueType>
319{
320 template <unsigned int, typename OtherValueType>
321 friend class index;
322public:
323 static const unsigned int rank = 1;
324 using reference = ValueType&;
325 using const_reference = const ValueType&;
326 using size_type = ValueType;
327 using value_type = ValueType;
328
329 _CONSTEXPR index() _NOEXCEPT : value(0)
330 {
331 }
332 _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
333 {
334 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700335 _CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
336 {
337 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700338 // Preconditions: il.size() == rank
339 _CONSTEXPR index(std::initializer_list<value_type> il)
340 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700341 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 -0700342 value = begin(il)[0];
343 }
344
345 _CONSTEXPR index(const index &) = default;
346
347 template <typename OtherValueType>
348 _CONSTEXPR index(const index<1, OtherValueType> & other)
349 {
350 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
351 value = static_cast<ValueType>(other.value);
352 }
353
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700354 _CONSTEXPR static index shift_left(const index<rank + 1, value_type>& other) _NOEXCEPT
355 {
356 return other.elems[1];
357 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700358 // Preconditions: component_idx < rank
359 _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
360 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700361 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700362 (void)(component_idx);
363 return value;
364 }
365 // Preconditions: component_idx < rank
366 _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
367 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700368 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700369 (void)(component_idx);
370 return value;
371 }
372 _CONSTEXPR bool operator==(const index& rhs) const _NOEXCEPT
373 {
374 return value == rhs.value;
375 }
376 _CONSTEXPR bool operator!=(const index& rhs) const _NOEXCEPT
377 {
378 return !(*this == rhs);
379 }
380 _CONSTEXPR index operator+() const _NOEXCEPT
381 {
382 return *this;
383 }
384 _CONSTEXPR index operator-() const _NOEXCEPT
385 {
386 return index(-value);
387 }
388 _CONSTEXPR index operator+(const index& rhs) const _NOEXCEPT
389 {
390 return index(value + rhs.value);
391 }
392 _CONSTEXPR index operator-(const index& rhs) const _NOEXCEPT
393 {
394 return index(value - rhs.value);
395 }
396 _CONSTEXPR index& operator+=(const index& rhs) _NOEXCEPT
397 {
398 value += rhs.value;
399 return *this;
400 }
401 _CONSTEXPR index& operator-=(const index& rhs) _NOEXCEPT
402 {
403 value -= rhs.value;
404 return *this;
405 }
406 _CONSTEXPR index& operator++() _NOEXCEPT
407 {
408 ++value;
409 return *this;
410 }
411 _CONSTEXPR index operator++(int) _NOEXCEPT
412 {
413 index ret = *this;
414 ++(*this);
415 return ret;
416 }
417 _CONSTEXPR index& operator--() _NOEXCEPT
418 {
419 --value;
420 return *this;
421 }
422 _CONSTEXPR index operator--(int) _NOEXCEPT
423 {
424 index ret = *this;
425 --(*this);
426 return ret;
427 }
428 _CONSTEXPR index operator*(value_type v) const _NOEXCEPT
429 {
430 return index(value * v);
431 }
432 _CONSTEXPR index operator/(value_type v) const _NOEXCEPT
433 {
434 return index(value / v);
435 }
436 _CONSTEXPR index& operator*=(value_type v) _NOEXCEPT
437 {
438 value *= v;
439 return *this;
440 }
441 _CONSTEXPR index& operator/=(value_type v) _NOEXCEPT
442 {
443 value /= v;
444 return *this;
445 }
446 friend _CONSTEXPR index operator*(value_type v, const index& rhs) _NOEXCEPT
447 {
448 return index(rhs * v);
449 }
450private:
451 value_type value;
452};
453
454#ifndef _MSC_VER
455
456struct static_bounds_dynamic_range_t
457{
458 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
459 constexpr operator T() const noexcept
460 {
461 return static_cast<T>(-1);
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 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
471 constexpr bool operator !=(T other) const noexcept
472 {
473 return static_cast<T>(-1) != other;
474 }
475
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
484template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
485constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
486{
487 return right != left;
488}
489
490constexpr static_bounds_dynamic_range_t dynamic_range{};
491#else
492const char dynamic_range = -1;
493#endif
494
495struct generalized_mapping_tag {};
496struct contiguous_mapping_tag : generalized_mapping_tag {};
497
498namespace details
499{
500 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
501 struct StaticSizeHelperImpl
502 {
503 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
504 static const SizeType value = Fact1 * Fact2;
505 };
506
507 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
508 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
509 {
510 static const SizeType value = ConstBound;
511 };
512
513 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
514 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
515 {
516 static const SizeType value = ConstBound;
517 };
518
519 template <typename SizeType, SizeType ConstBound>
520 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
521 {
522 static const SizeType value = static_cast<SizeType>(ConstBound);
523 };
524
525 template <typename SizeType, SizeType Fact1, SizeType Fact2>
526 struct StaticSizeHelper
527 {
528 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
529 };
530
531
532 template <size_t Left, size_t Right>
533 struct LessThan
534 {
535 static const bool value = Left < Right;
536 };
537
538 template <typename SizeType, size_t... Ranges>
539 struct BoundsRanges {
540 static const unsigned int Depth = 0;
541 static const unsigned int DynamicNum = 0;
542 static const SizeType CurrentRange = 1;
543 static const SizeType TotalSize = 1;
544
545 BoundsRanges (const BoundsRanges &) = default;
546
547 // TODO : following signature is for work around VS bug
548 template <typename OtherType>
549 BoundsRanges (const OtherType &, bool firstLevel) {}
550 BoundsRanges(const SizeType * const arr) { }
551 BoundsRanges() = default;
552
553
554 template <typename T, unsigned int Dim>
555 void serialize(T &) const {
556 }
557 template <typename T, unsigned int Dim>
558 SizeType linearize(const T &) const {
559 return 0;
560 }
561 template <typename T, unsigned int Dim>
562 ptrdiff_t contains(const T &) const {
563 return 0;
564 }
565
566 size_t totalSize() const _NOEXCEPT {
567 return TotalSize;
568 }
569
570 bool operator == (const BoundsRanges &) const _NOEXCEPT
571 {
572 return true;
573 }
574 };
575
576 template <typename SizeType, size_t... RestRanges>
577 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
578 using Base = BoundsRanges <SizeType, RestRanges... >;
579 static const unsigned int Depth = Base::Depth + 1;
580 static const unsigned int DynamicNum = Base::DynamicNum + 1;
581 static const SizeType CurrentRange = dynamic_range;
582 static const SizeType TotalSize = dynamic_range;
583 const SizeType m_bound;
584
585 BoundsRanges (const BoundsRanges &) = default;
586 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
587 {
588 fail_fast_assert(0 <= *arr);
589 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
590 }
591 BoundsRanges() : m_bound(0) {}
592
593 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
594 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) :
595 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
596 {
597 }
598
599 template <typename T, unsigned int Dim = 0>
600 void serialize(T & arr) const {
601 arr[Dim] = elementNum();
602 this->Base::template serialize<T, Dim + 1>(arr);
603 }
604 template <typename T, unsigned int Dim = 0>
605 SizeType linearize(const T & arr) const {
606 const size_t index = this->Base::totalSize() * arr[Dim];
607 fail_fast_assert(index < static_cast<size_t>(m_bound));
608 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
609 }
610
611 template <typename T, unsigned int Dim = 0>
612 ptrdiff_t contains(const T & arr) const {
613 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
614 if (last == -1)
615 return -1;
616 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
617 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
618 }
619
620 size_t totalSize() const _NOEXCEPT {
621 return m_bound;
622 }
623
624 SizeType elementNum() const _NOEXCEPT {
625 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
626 }
627
628 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
629 if (dim > 0)
630 return this->Base::elementNum(dim - 1);
631 else
632 return elementNum();
633 }
634
635 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
636 {
637 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
638 }
639 };
640
641 template <typename SizeType, size_t CurRange, size_t... RestRanges>
642 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
643 using Base = BoundsRanges <SizeType, RestRanges... >;
644 static const unsigned int Depth = Base::Depth + 1;
645 static const unsigned int DynamicNum = Base::DynamicNum;
646 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
647 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
648 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
649
650 BoundsRanges (const BoundsRanges &) = default;
651 BoundsRanges(const SizeType * const arr) : Base(arr) { }
652 BoundsRanges() = default;
653
654 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
655 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
656 {
657 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
658 }
659
660 template <typename T, unsigned int Dim = 0>
661 void serialize(T & arr) const {
662 arr[Dim] = elementNum();
663 this->Base::template serialize<T, Dim + 1>(arr);
664 }
665
666 template <typename T, unsigned int Dim = 0>
667 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700668 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700669 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
670 }
671
672 template <typename T, unsigned int Dim = 0>
673 ptrdiff_t contains(const T & arr) const {
674 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
675 return -1;
676 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
677 if (last == -1)
678 return -1;
679 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
680 }
681
682 size_t totalSize() const _NOEXCEPT{
683 return CurrentRange * this->Base::totalSize();
684 }
685
686 SizeType elementNum() const _NOEXCEPT{
687 return CurrentRange;
688 }
689
690 SizeType elementNum(unsigned int dim) const _NOEXCEPT{
691 if (dim > 0)
692 return this->Base::elementNum(dim - 1);
693 else
694 return elementNum();
695 }
696
697 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
698 {
699 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
700 }
701 };
702
703 template <typename SourceType, typename TargetType, size_t Rank>
704 struct BoundsRangeConvertible2;
705
706 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
707 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
708 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
709
710 template <size_t Rank, typename SourceType, typename TargetType>
711 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
712
713 template <typename SourceType, typename TargetType, size_t Rank>
714 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
715 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
716 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
717 {};
718
719 template <typename SourceType, typename TargetType>
720 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
721
722 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
723 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
724 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
725 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
726 {};
727 template <typename SourceType, typename TargetType>
728 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
729
730 template <typename TypeChain>
731 struct TypeListIndexer
732 {
733 const TypeChain & obj;
734 TypeListIndexer(const TypeChain & obj) :obj(obj){}
735 template<unsigned int N>
736 const TypeChain & getObj(std::true_type)
737 {
738 return obj;
739 }
740 template<unsigned int N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
741 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
742 {
743 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
744 }
745 template <unsigned int N>
746 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
747 {
748 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
749 }
750 };
751
752 template <typename TypeChain>
753 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
754 {
755 return TypeListIndexer<TypeChain>(obj);
756 }
757}
758
759template <typename IndexType>
760class bounds_iterator;
761
762template <typename SizeType, size_t... Ranges>
763class static_bounds {
764public:
765 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &empty) {
766 }
767};
768
769template <typename SizeType, size_t FirstRange, size_t... RestRanges>
770class static_bounds<SizeType, FirstRange, RestRanges...>
771{
772 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
773 static_assert(std::is_integral<SizeType>::value
774 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
775
776 MyRanges m_ranges;
777 _CONSTEXPR static_bounds(const MyRanges & range) : m_ranges(range) { }
778
779 template <typename SizeType2, size_t... Ranges2>
780 friend class static_bounds;
781public:
782 static const unsigned int rank = MyRanges::Depth;
783 static const unsigned int dynamic_rank = MyRanges::DynamicNum;
784 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
785
786 using size_type = SizeType;
787 using index_type = index<rank, size_type>;
788 using iterator = bounds_iterator<index_type>;
789 using const_iterator = bounds_iterator<index_type>;
790 using difference_type = ptrdiff_t;
791 using sliced_type = static_bounds<SizeType, RestRanges...>;
792 using mapping_type = contiguous_mapping_tag;
793public:
794 _CONSTEXPR static_bounds(const static_bounds &) = default;
795
796 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
797 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
798 _CONSTEXPR static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
799 m_ranges(other.m_ranges)
800 {
801 }
802
803 _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
804 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700805 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
806 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 -0700807 }
808
809 _CONSTEXPR static_bounds() = default;
810
811 _CONSTEXPR static_bounds & operator = (const static_bounds & otherBounds)
812 {
813 new(&m_ranges) MyRanges (otherBounds.m_ranges);
814 return *this;
815 }
816
817 _CONSTEXPR sliced_type slice() const _NOEXCEPT
818 {
819 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
820 }
821
822 _CONSTEXPR size_type stride() const _NOEXCEPT
823 {
824 return rank > 1 ? slice().size() : 1;
825 }
826
827 _CONSTEXPR size_type size() const _NOEXCEPT
828 {
829 return static_cast<size_type>(m_ranges.totalSize());
830 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700831
832 _CONSTEXPR size_type total_size() const _NOEXCEPT
833 {
834 return static_cast<size_type>(m_ranges.totalSize());
835 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700836
837 _CONSTEXPR size_type linearize(const index_type & idx) const
838 {
839 return m_ranges.linearize(idx);
840 }
841
842 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
843 {
844 return m_ranges.contains(idx) != -1;
845 }
846
847 _CONSTEXPR size_type operator[](unsigned int index) const _NOEXCEPT
848 {
849 return m_ranges.elementNum(index);
850 }
851
852 template <unsigned int Dim = 0>
853 _CONSTEXPR size_type extent() const _NOEXCEPT
854 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700855 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700856 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
857 }
858
859 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
860 {
861 index_type extents;
862 m_ranges.serialize(extents);
863 return extents;
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->size() == rhs.size();
870 }
871
872 template <typename OtherSizeTypes, size_t... Ranges>
873 _CONSTEXPR bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
874 {
875 return !(*this == rhs);
876 }
877
878 _CONSTEXPR const_iterator begin() const _NOEXCEPT
879 {
880 return const_iterator(*this);
881 }
882
883 _CONSTEXPR const_iterator end() const _NOEXCEPT
884 {
885 index_type boundary;
886 m_ranges.serialize(boundary);
887 return const_iterator(*this, this->index_bounds());
888 }
889};
890
891template <unsigned int Rank, typename SizeType = size_t>
892class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
893{
894 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
895 friend Base;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700896 template <unsigned int OtherRank, typename OtherSizeType>
897 friend class strided_bounds;
898
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700899public:
900 using Base::rank;
901 using reference = typename Base::reference;
902 using const_reference = typename Base::const_reference;
903 using size_type = typename Base::value_type;
904 using difference_type = typename Base::value_type;
905 using value_type = typename Base::value_type;
906 using index_type = index<rank, size_type>;
907 using iterator = bounds_iterator<index_type>;
908 using const_iterator = bounds_iterator<index_type>;
909 static const int dynamic_rank = rank;
910 static const size_t static_size = dynamic_range;
911 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
912 using mapping_type = generalized_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700913 _CONSTEXPR strided_bounds(const strided_bounds &) = default;
914
915 template <typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700916 _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
917 : Base(other), m_strides(other.strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700918 {
919 }
920
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700921 _CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
922 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700923 {
924 for (unsigned int i = 0; i < rank; i++)
925 Base::elems[i] = extents[i];
926 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700927 _CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
928 : Base(values), m_strides(std::move(strides))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700929 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700930 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700931 _CONSTEXPR index_type strides() const _NOEXCEPT
932 {
933 return m_strides;
934 }
935 _CONSTEXPR size_type total_size() const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700936 {
937 size_type ret = 0;
938 for (unsigned int i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700939 ret += (Base::elems[i] - 1) * m_strides[i];
940 return ret + 1;
941 }
942 _CONSTEXPR size_type size() const _NOEXCEPT
943 {
944 size_type ret = 1;
945 for (unsigned int i = 0; i < rank; ++i)
946 ret *= Base::elems[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700947 return ret;
948 }
949 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
950 {
951 for (unsigned int i = 0; i < rank; ++i)
952 {
953 if (idx[i] < 0 || idx[i] >= Base::elems[i])
954 return false;
955 }
956 return true;
957 }
958 _CONSTEXPR size_type linearize(const index_type & idx) const
959 {
960 size_type ret = 0;
961 for (unsigned int i = 0; i < rank; i++)
962 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700963 fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
964 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700965 }
966 return ret;
967 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700968 _CONSTEXPR size_type stride() const _NOEXCEPT
969 {
970 return m_strides[0];
971 }
972 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700973 _CONSTEXPR sliced_type slice() const
974 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700975 return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700976 }
977 template <unsigned int Dim = 0>
978 _CONSTEXPR size_type extent() const _NOEXCEPT
979 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700980 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 return Base::elems[Dim];
982 }
983 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
984 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700985 return index_type(Base::elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700986 }
987 const_iterator begin() const _NOEXCEPT
988 {
989 return const_iterator{ *this };
990 }
991 const_iterator end() const _NOEXCEPT
992 {
993 return const_iterator{ *this, index_bounds() };
994 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700995private:
996 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700997};
998
999template <typename T>
1000struct is_bounds : std::integral_constant<bool, false> {};
1001template <typename SizeType, size_t... Ranges>
1002struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
1003template <unsigned int Rank, typename SizeType>
1004struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
1005
1006template <typename IndexType>
1007class bounds_iterator
1008 : public std::iterator<std::random_access_iterator_tag,
1009 IndexType,
1010 ptrdiff_t,
1011 const details::arrow_proxy<IndexType>,
1012 const IndexType>
1013{
1014private:
1015 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
1016public:
1017 static const unsigned int rank = IndexType::rank;
1018 using typename Base::reference;
1019 using typename Base::pointer;
1020 using typename Base::difference_type;
1021 using typename Base::value_type;
1022 using index_type = value_type;
1023 using index_size_type = typename IndexType::size_type;
1024 template <typename Bounds>
1025 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT
1026 : boundary(bnd.index_bounds())
1027 , curr( std::move(curr) )
1028 {
1029 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1030 }
1031 reference operator*() const _NOEXCEPT
1032 {
1033 return curr;
1034 }
1035 pointer operator->() const _NOEXCEPT
1036 {
1037 return details::arrow_proxy<value_type>{ curr };
1038 }
1039 bounds_iterator& operator++() _NOEXCEPT
1040 {
1041 for (unsigned int i = rank; i-- > 0;)
1042 {
1043 if (++curr[i] < boundary[i])
1044 {
1045 return *this;
1046 }
1047 else
1048 {
1049 curr[i] = 0;
1050 }
1051 }
1052 // If we're here we've wrapped over - set to past-the-end.
1053 for (unsigned int i = 0; i < rank; ++i)
1054 {
1055 curr[i] = boundary[i];
1056 }
1057 return *this;
1058 }
1059 bounds_iterator operator++(int) _NOEXCEPT
1060 {
1061 auto ret = *this;
1062 ++(*this);
1063 return ret;
1064 }
1065 bounds_iterator& operator--() _NOEXCEPT
1066 {
1067 for (int i = rank; i-- > 0;)
1068 {
1069 if (curr[i]-- > 0)
1070 {
1071 return *this;
1072 }
1073 else
1074 {
1075 curr[i] = boundary[i] - 1;
1076 }
1077 }
1078 // If we're here the preconditions were violated
1079 // "pre: there exists s such that r == ++s"
1080 fail_fast_assert(false);
1081 return *this;
1082 }
1083 bounds_iterator operator--(int) _NOEXCEPT
1084 {
1085 auto ret = *this;
1086 --(*this);
1087 return ret;
1088 }
1089 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1090 {
1091 bounds_iterator ret{ *this };
1092 return ret += n;
1093 }
1094 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1095 {
1096 auto linear_idx = linearize(curr) + n;
1097 value_type stride;
1098 stride[rank - 1] = 1;
1099 for (unsigned int i = rank - 1; i-- > 0;)
1100 {
1101 stride[i] = stride[i + 1] * boundary[i + 1];
1102 }
1103 for (unsigned int i = 0; i < rank; ++i)
1104 {
1105 curr[i] = linear_idx / stride[i];
1106 linear_idx = linear_idx % stride[i];
1107 }
1108 return *this;
1109 }
1110 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1111 {
1112 bounds_iterator ret{ *this };
1113 return ret -= n;
1114 }
1115 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1116 {
1117 return *this += -n;
1118 }
1119 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1120 {
1121 return linearize(curr) - linearize(rhs.curr);
1122 }
1123 reference operator[](difference_type n) const _NOEXCEPT
1124 {
1125 return *(*this + n);
1126 }
1127 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1128 {
1129 return curr == rhs.curr;
1130 }
1131 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1132 {
1133 return !(*this == rhs);
1134 }
1135 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1136 {
1137 for (unsigned int i = 0; i < rank; ++i)
1138 {
1139 if (curr[i] < rhs.curr[i])
1140 return true;
1141 }
1142 return false;
1143 }
1144 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1145 {
1146 return !(rhs < *this);
1147 }
1148 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1149 {
1150 return rhs < *this;
1151 }
1152 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1153 {
1154 return !(rhs > *this);
1155 }
1156 void swap(bounds_iterator& rhs) _NOEXCEPT
1157 {
1158 std::swap(boundary, rhs.boundary);
1159 std::swap(curr, rhs.curr);
1160 }
1161private:
1162 index_size_type linearize(const value_type& idx) const _NOEXCEPT
1163 {
1164 // TODO: Smarter impl.
1165 // Check if past-the-end
1166 bool pte = true;
1167 for (unsigned int i = 0; i < rank; ++i)
1168 {
1169 if (idx[i] != boundary[i])
1170 {
1171 pte = false;
1172 break;
1173 }
1174 }
1175 index_size_type multiplier = 1;
1176 index_size_type res = 0;
1177 if (pte)
1178 {
1179 res = 1;
1180 for (unsigned int i = rank; i-- > 0;)
1181 {
1182 res += (idx[i] - 1) * multiplier;
1183 multiplier *= boundary[i];
1184 }
1185 }
1186 else
1187 {
1188 for (unsigned int i = rank; i-- > 0;)
1189 {
1190 res += idx[i] * multiplier;
1191 multiplier *= boundary[i];
1192 }
1193 }
1194 return res;
1195 }
1196 value_type boundary;
1197 value_type curr;
1198};
1199
1200template <typename SizeType>
1201class bounds_iterator<index<1, SizeType>>
1202 : public std::iterator<std::random_access_iterator_tag,
1203 index<1, SizeType>,
1204 ptrdiff_t,
1205 const details::arrow_proxy<index<1, SizeType>>,
1206 const index<1, SizeType>>
1207{
1208 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>>;
1209
1210public:
1211 using typename Base::reference;
1212 using typename Base::pointer;
1213 using typename Base::difference_type;
1214 using typename Base::value_type;
1215 using index_type = value_type;
1216 using index_size_type = typename index_type::size_type;
1217
1218 template <typename Bounds>
1219 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT
1220 : curr( std::move(curr) )
1221 {}
1222 reference operator*() const _NOEXCEPT
1223 {
1224 return curr;
1225 }
1226 pointer operator->() const _NOEXCEPT
1227 {
1228 return details::arrow_proxy<value_type>{ curr };
1229 }
1230 bounds_iterator& operator++() _NOEXCEPT
1231 {
1232 ++curr;
1233 return *this;
1234 }
1235 bounds_iterator operator++(int) _NOEXCEPT
1236 {
1237 auto ret = *this;
1238 ++(*this);
1239 return ret;
1240 }
1241 bounds_iterator& operator--() _NOEXCEPT
1242 {
1243 curr--;
1244 return *this;
1245 }
1246 bounds_iterator operator--(int) _NOEXCEPT
1247 {
1248 auto ret = *this;
1249 --(*this);
1250 return ret;
1251 }
1252 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1253 {
1254 bounds_iterator ret{ *this };
1255 return ret += n;
1256 }
1257 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1258 {
1259 curr += n;
1260 return *this;
1261 }
1262 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1263 {
1264 bounds_iterator ret{ *this };
1265 return ret -= n;
1266 }
1267 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1268 {
1269 return *this += -n;
1270 }
1271 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1272 {
1273 return curr[0] - rhs.curr[0];
1274 }
1275 reference operator[](difference_type n) const _NOEXCEPT
1276 {
1277 return curr + n;
1278 }
1279 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1280 {
1281 return curr == rhs.curr;
1282 }
1283 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1284 {
1285 return !(*this == rhs);
1286 }
1287 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1288 {
1289 return curr[0] < rhs.curr[0];
1290 }
1291 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1292 {
1293 return !(rhs < *this);
1294 }
1295 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1296 {
1297 return rhs < *this;
1298 }
1299 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1300 {
1301 return !(rhs > *this);
1302 }
1303 void swap(bounds_iterator& rhs) _NOEXCEPT
1304 {
1305 std::swap(curr, rhs.curr);
1306 }
1307private:
1308 value_type curr;
1309};
1310
1311template <typename IndexType>
1312bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) _NOEXCEPT
1313{
1314 return rhs + n;
1315}
1316
1317/*
1318** begin definitions of basic_array_view
1319*/
1320namespace details
1321{
1322 template <typename Bounds>
1323 _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
1324 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001325 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001326 }
1327
1328 // Make a stride vector from bounds, assuming continugous memory.
1329 template <typename Bounds>
1330 _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
1331 {
1332 auto extents = bnd.index_bounds();
1333 typename Bounds::index_type stride;
1334 stride[Bounds::rank - 1] = 1;
1335 for (int i = Bounds::rank - 2; i >= 0; --i)
1336 stride[i] = stride[i + 1] * extents[i + 1];
1337 return stride;
1338 }
1339
1340 template <typename BoundsSrc, typename BoundsDest>
1341 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1342 {
1343 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1344 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1345 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");
1346 fail_fast_assert(src.size() == dest.size());
1347 }
1348
1349
1350} // namespace details
1351
1352template <typename ArrayView>
1353class contiguous_array_view_iterator;
1354template <typename ArrayView>
1355class general_array_view_iterator;
1356enum class byte : std::uint8_t {};
1357
1358template <typename ValueType, typename BoundsType>
1359class basic_array_view
1360{
1361public:
1362 static const unsigned int rank = BoundsType::rank;
1363 using bounds_type = BoundsType;
1364 using size_type = typename bounds_type::size_type;
1365 using index_type = typename bounds_type::index_type;
1366 using value_type = ValueType;
1367 using pointer = ValueType*;
1368 using reference = ValueType&;
1369 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>>;
1370 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>>>;
1371 using reverse_iterator = std::reverse_iterator<iterator>;
1372 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1373 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1374
1375private:
1376 pointer m_pdata;
1377 bounds_type m_bounds;
1378
1379public:
1380 _CONSTEXPR bounds_type bounds() const _NOEXCEPT
1381 {
1382 return m_bounds;
1383 }
1384 template <unsigned int Dim = 0>
1385 _CONSTEXPR size_type extent() const _NOEXCEPT
1386 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001387 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001388 return m_bounds.template extent<Dim>();
1389 }
1390 _CONSTEXPR size_type size() const _NOEXCEPT
1391 {
1392 return m_bounds.size();
1393 }
1394 _CONSTEXPR reference operator[](const index_type& idx) const
1395 {
1396 return m_pdata[m_bounds.linearize(idx)];
1397 }
1398 _CONSTEXPR pointer data() const _NOEXCEPT
1399 {
1400 return m_pdata;
1401 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001402 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001403 _CONSTEXPR Ret operator[](size_type idx) const
1404 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001405 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001406 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001407
1408 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001409 return Ret {m_pdata + ridx, m_bounds.slice()};
1410 }
1411
1412 _CONSTEXPR operator bool () const _NOEXCEPT
1413 {
1414 return m_pdata != nullptr;
1415 }
1416
1417 _CONSTEXPR iterator begin() const
1418 {
1419 return iterator {this, true};
1420 }
1421 _CONSTEXPR iterator end() const
1422 {
1423 return iterator {this};
1424 }
1425 _CONSTEXPR const_iterator cbegin() const
1426 {
1427 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1428 }
1429 _CONSTEXPR const_iterator cend() const
1430 {
1431 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1432 }
1433
1434 _CONSTEXPR reverse_iterator rbegin() const
1435 {
1436 return reverse_iterator {end()};
1437 }
1438 _CONSTEXPR reverse_iterator rend() const
1439 {
1440 return reverse_iterator {begin()};
1441 }
1442 _CONSTEXPR const_reverse_iterator crbegin() const
1443 {
1444 return const_reverse_iterator {cend()};
1445 }
1446 _CONSTEXPR const_reverse_iterator crend() const
1447 {
1448 return const_reverse_iterator {cbegin()};
1449 }
1450
1451 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 -07001452 _CONSTEXPR bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001453 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001454 return m_bounds.size() == other.m_bounds.size() &&
1455 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001456 }
1457
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 !(*this == other);
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 std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
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 (other < *this);
1480 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001481
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001482 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>>
1483 _CONSTEXPR bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1484 {
1485 return !(*this < other);
1486 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001487
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001488public:
1489 template <typename OtherValueType, typename OtherBounds,
1490 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1491 && std::is_convertible<OtherBounds, bounds_type>::value>>
1492 _CONSTEXPR basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) _NOEXCEPT
1493 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1494 {
1495 }
1496protected:
1497
1498 _CONSTEXPR basic_array_view(pointer data, bounds_type bound) _NOEXCEPT
1499 : m_pdata(data)
1500 , m_bounds(std::move(bound))
1501 {
1502 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1503 }
1504 template <typename T>
1505 _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
1506 : m_pdata(reinterpret_cast<pointer>(data))
1507 , m_bounds(std::move(bound))
1508 {
1509 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1510 }
1511 template <typename DestBounds>
1512 _CONSTEXPR basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1513 {
1514 details::verifyBoundsReshape(m_bounds, bounds);
1515 return {m_pdata, bounds};
1516 }
1517private:
1518
1519 friend iterator;
1520 friend const_iterator;
1521 template <typename ValueType2, typename BoundsType2>
1522 friend class basic_array_view;
1523};
1524
1525template <size_t DimSize = dynamic_range>
1526struct dim
1527{
1528 static const size_t value = DimSize;
1529};
1530template <>
1531struct dim<dynamic_range>
1532{
1533 static const size_t value = dynamic_range;
1534 const size_t dvalue;
1535 dim(size_t size) : dvalue(size) {}
1536};
1537
1538template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1539class array_view;
1540template <typename ValueTypeOpt, unsigned int Rank>
1541class strided_array_view;
1542
1543namespace details
1544{
1545 template <typename T, typename = std::true_type>
1546 struct ArrayViewTypeTraits
1547 {
1548 using value_type = T;
1549 using size_type = size_t;
1550 };
1551
1552 template <typename Traits>
1553 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1554 {
1555 using value_type = typename Traits::array_view_traits::value_type;
1556 using size_type = typename Traits::array_view_traits::size_type;
1557 };
1558
1559 template <typename T, typename SizeType, size_t... Ranks>
1560 struct ArrayViewArrayTraits {
1561 using type = array_view<T, Ranks...>;
1562 using value_type = T;
1563 using bounds_type = static_bounds<SizeType, Ranks...>;
1564 using pointer = T*;
1565 using reference = T&;
1566 };
1567 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1568 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1569
1570 template <typename BoundsType>
1571 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1572 {
1573 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1574 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1575 }
1576 template <typename BoundsType>
1577 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1578 {
1579 fail_fast_assert(BoundsType::static_size == totalSize);
1580 return {};
1581 }
1582 template <typename BoundsType>
1583 BoundsType newBoundsHelper(size_t totalSize)
1584 {
1585 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1586 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1587 }
1588
1589 struct Sep{};
1590
1591 template <typename T, typename... Args>
1592 T static_as_array_view_helper(Sep, Args... args)
1593 {
1594 return T{static_cast<typename T::size_type>(args)...};
1595 }
1596 template <typename T, typename Arg, typename... Args>
1597 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)
1598 {
1599 return static_as_array_view_helper<T>(args...);
1600 }
1601 template <typename T, typename... Args>
1602 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1603 {
1604 return static_as_array_view_helper<T>(args..., val.dvalue);
1605 }
1606
1607 template <typename SizeType, typename ...Dimensions>
1608 struct static_as_array_view_static_bounds_helper
1609 {
1610 using type = static_bounds<SizeType, (Dimensions::value)...>;
1611 };
1612
1613 template <typename T>
1614 struct is_array_view_oracle : std::false_type
1615 {};
1616 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1617 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1618 {};
1619 template <typename ValueType, unsigned int Rank>
1620 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1621 {};
1622 template <typename T>
1623 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1624 {};
1625
1626}
1627
1628
1629template <typename ValueType, typename SizeType>
1630struct array_view_options
1631{
1632 struct array_view_traits
1633 {
1634 using value_type = ValueType;
1635 using size_type = SizeType;
1636 };
1637};
1638
1639template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1640class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1641 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1642{
1643 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1644 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001645 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001646 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001647
1648public:
1649 using typename Base::bounds_type;
1650 using typename Base::size_type;
1651 using typename Base::pointer;
1652 using typename Base::value_type;
1653 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001654 using typename Base::iterator;
1655 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001656 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001657 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001658
1659public:
1660 // basic
1661 _CONSTEXPR array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1662 {
1663 }
1664
1665 _CONSTEXPR array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1666 {
1667 }
1668
Neil MacIntosh9b40a0a2015-08-27 19:49:27 -07001669 _CONSTEXPR array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001670 {
1671 fail_fast_assert(size == 0);
1672 }
1673
1674 // default
1675 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
1676 _CONSTEXPR array_view() : Base(nullptr, bounds_type())
1677 {
1678 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001679
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001680 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1681 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001682 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1683 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
1684 _CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001685 {
1686 }
1687
1688 // from n-dimensions static array
1689 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1690 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 -07001691 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Anna Gringauzee5b79d22015-09-14 16:38:25 -07001692 _CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001693 {
1694 }
1695
1696 // from n-dimensions static array with size
1697 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1698 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 -07001699 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1700 _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001701 {
1702 fail_fast_assert(size <= N);
1703 }
1704
1705 // from std array
1706 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
1707 _CONSTEXPR array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1708 {
1709 }
1710
1711 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>>
1712 _CONSTEXPR array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1713 {
1714 }
1715
1716
1717 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1718 template <typename Ptr,
1719 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1720 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
1721 _CONSTEXPR array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1722 {
1723 }
1724
1725 // from containers. It must has .size() and .data() two function signatures
1726 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1727 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001728 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001729 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1730 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001731 >
Anna Gringauze18cd9802015-09-14 16:34:26 -07001732 _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 -07001733 {
1734
1735 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001736
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001737 _CONSTEXPR array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001738
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001739 // convertible
1740 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1741 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1742 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1743 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1744 >
1745 _CONSTEXPR array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
1746
1747 // reshape
1748 template <typename... Dimensions2>
1749 _CONSTEXPR array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
1750 {
1751 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001752 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001753 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1754 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001755 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 }
1757
1758 // to bytes array
1759 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001760 _CONSTEXPR auto as_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001761 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)>
1762 {
1763 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001764 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001765 }
1766
1767 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001768 _CONSTEXPR auto as_writeable_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001769 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)>
1770 {
1771 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001772 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001773 }
1774
Anna Gringauze18cd9802015-09-14 16:34:26 -07001775
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001776 // from bytes array
1777 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1778 _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))>
1779 {
1780 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1781 "Target type must be standard layout and its size must match the byte array size");
1782 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001783 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001784 }
1785
1786 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1787 _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))>
1788 {
1789 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1790 "Target type must be standard layout and its size must match the byte array size");
1791 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001792 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001793 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001794
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001795 // section on linear space
1796 template<size_t Count>
1797 _CONSTEXPR array_view<ValueTypeOpt, Count> first() const _NOEXCEPT
1798 {
1799 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1800 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 -07001801 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802 }
1803
1804 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
1805 {
1806 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001807 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001808 }
1809
1810 template<size_t Count>
1811 _CONSTEXPR array_view<ValueTypeOpt, Count> last() const _NOEXCEPT
1812 {
1813 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1814 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001815 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001816 }
1817
1818 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
1819 {
1820 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001821 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001822 }
1823
1824 template<size_t Offset, size_t Count>
1825 _CONSTEXPR array_view<ValueTypeOpt, Count> sub() const _NOEXCEPT
1826 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001827 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");
1828 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 -07001829 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001830 }
1831
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001832 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001833 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001834 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1835 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001836 }
1837
1838 // size
1839 _CONSTEXPR size_type length() const _NOEXCEPT
1840 {
1841 return this->size();
1842 }
1843 _CONSTEXPR size_type used_length() const _NOEXCEPT
1844 {
1845 return length();
1846 }
1847 _CONSTEXPR size_type bytes() const _NOEXCEPT
1848 {
1849 return sizeof(value_type) * this->size();
1850 }
1851 _CONSTEXPR size_type used_bytes() const _NOEXCEPT
1852 {
1853 return bytes();
1854 }
1855
1856 // section
1857 _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
1858 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001859 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001860 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1861 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001862
Anna Gringauze1a864982015-09-14 18:55:06 -07001863 _CONSTEXPR reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001864 {
1865 return Base::operator[](idx);
1866 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001867
Anna Gringauze1a864982015-09-14 18:55:06 -07001868 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001869 _CONSTEXPR array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
1870 {
1871 auto ret = Base::operator[](idx);
1872 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001873 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001874
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001875 using Base::operator==;
1876 using Base::operator!=;
1877 using Base::operator<;
1878 using Base::operator<=;
1879 using Base::operator>;
1880 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001881};
1882
1883template <typename T, size_t... Dimensions>
1884_CONSTEXPR auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
1885{
1886 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1887}
1888
1889template <typename T>
1890_CONSTEXPR auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
1891{
1892 return {arr, len};
1893}
1894
1895template <typename T, size_t N>
1896_CONSTEXPR auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
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> &arr)
1903{
1904 return {arr};
1905}
1906
1907template <typename T, size_t N>
1908_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
1909
1910template <typename T, size_t N>
1911_CONSTEXPR array_view<T, N> as_array_view(std::array<T, N> &arr)
1912{
1913 return {arr};
1914}
1915
1916template <typename T>
1917_CONSTEXPR array_view<T, dynamic_range> as_array_view(T *begin, T *end)
1918{
1919 return {begin, end};
1920}
1921
1922template <typename Cont>
1923_CONSTEXPR auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1924 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1925{
1926 return {arr.data(), arr.size()};
1927}
1928
1929template <typename Cont>
1930_CONSTEXPR auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1931 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1932
1933template <typename ValueTypeOpt, unsigned int Rank>
1934class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1935{
1936 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 -07001937
1938 template<typename OtherValueOpt, unsigned int OtherRank>
1939 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001940public:
1941 using Base::rank;
1942 using typename Base::bounds_type;
1943 using typename Base::size_type;
1944 using typename Base::pointer;
1945 using typename Base::value_type;
1946 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001947 using typename Base::iterator;
1948 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001949 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001950
1951 // from static array of size N
1952 template<size_type N>
1953 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1954 {
1955 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1956 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001957
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001958 // from raw data
1959 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001960 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001961 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001962 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001963
1964 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001965 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001966 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1967 {
1968 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1969 }
1970
1971 // convertible
1972 template <typename OtherValueTypeOpt,
1973 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1974 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1975 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1976 >
1977 _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 -07001978 {
1979 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001980
1981 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001982 template <typename OtherValueType>
1983 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 -07001984 {
1985 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1986 auto d = sizeof(OtherValueType) / sizeof(value_type);
1987
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001988 size_type size = this->bounds().total_size() / d;
1989 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 -07001990 }
1991
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001992 strided_array_view section(index_type origin, index_type extents) const
1993 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001994 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001995 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1996 }
1997
1998 _CONSTEXPR reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001999 {
2000 return Base::operator[](idx);
2001 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002002
2003 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2004 _CONSTEXPR strided_array_view<value_type, rank-1> operator[](size_type idx) const
2005 {
2006 auto ret = Base::operator[](idx);
2007 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
2008 }
2009
2010private:
2011 static index_type resize_extent(const index_type& extent, size_t d)
2012 {
2013 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");
2014
2015 index_type ret = extent;
2016 ret[rank - 1] /= d;
2017
2018 return ret;
2019 }
2020
2021 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
2022 static index_type resize_stride(const index_type& strides, size_t d, void *p = 0)
2023 {
2024 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2025
2026 return strides;
2027 }
2028
2029 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2030 static index_type resize_stride(const index_type& strides, size_t d)
2031 {
2032 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2033 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");
2034
2035 for (int i = rank - 2; i >= 0; --i)
2036 {
2037 fail_fast_assert((strides[i] >= strides[i + 1]) && (strides[i] % strides[i + 1] == 0), "Only strided arrays with regular strides can be resized");
2038 }
2039
2040 index_type ret = strides / d;
2041 ret[rank - 1] = 1;
2042
2043 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002044 }
2045};
2046
2047template <typename ArrayView>
2048class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2049{
2050 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2051public:
2052 using typename Base::reference;
2053 using typename Base::pointer;
2054 using typename Base::difference_type;
2055private:
2056 template <typename ValueType, typename Bounds>
2057 friend class basic_array_view;
2058 pointer m_pdata;
2059 const ArrayView * m_validator;
2060 void validateThis() const
2061 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07002062 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 -07002063 }
2064 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2065 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2066public:
2067 reference operator*() const _NOEXCEPT
2068 {
2069 validateThis();
2070 return *m_pdata;
2071 }
2072 pointer operator->() const _NOEXCEPT
2073 {
2074 validateThis();
2075 return m_pdata;
2076 }
2077 contiguous_array_view_iterator& operator++() _NOEXCEPT
2078 {
2079 ++m_pdata;
2080 return *this;
2081 }
2082 contiguous_array_view_iterator operator++(int)_NOEXCEPT
2083 {
2084 auto ret = *this;
2085 ++(*this);
2086 return ret;
2087 }
2088 contiguous_array_view_iterator& operator--() _NOEXCEPT
2089 {
2090 --m_pdata;
2091 return *this;
2092 }
2093 contiguous_array_view_iterator operator--(int)_NOEXCEPT
2094 {
2095 auto ret = *this;
2096 --(*this);
2097 return ret;
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 m_pdata += n;
2107 return *this;
2108 }
2109 contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2110 {
2111 contiguous_array_view_iterator ret{ *this };
2112 return ret -= n;
2113 }
2114 contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2115 {
2116 return *this += -n;
2117 }
2118 difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2119 {
2120 fail_fast_assert(m_validator == rhs.m_validator);
2121 return m_pdata - rhs.m_pdata;
2122 }
2123 reference operator[](difference_type n) const _NOEXCEPT
2124 {
2125 return *(*this + n);
2126 }
2127 bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2128 {
2129 fail_fast_assert(m_validator == rhs.m_validator);
2130 return m_pdata == rhs.m_pdata;
2131 }
2132 bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2133 {
2134 return !(*this == rhs);
2135 }
2136 bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2137 {
2138 fail_fast_assert(m_validator == rhs.m_validator);
2139 return m_pdata < rhs.m_pdata;
2140 }
2141 bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2142 {
2143 return !(rhs < *this);
2144 }
2145 bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2146 {
2147 return rhs < *this;
2148 }
2149 bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2150 {
2151 return !(rhs > *this);
2152 }
2153 void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT
2154 {
2155 std::swap(m_pdata, rhs.m_pdata);
2156 std::swap(m_validator, rhs.m_validator);
2157 }
2158};
2159
2160template <typename ArrayView>
2161contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2162{
2163 return rhs + n;
2164}
2165
2166template <typename ArrayView>
2167class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2168{
2169 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2170public:
2171 using typename Base::reference;
2172 using typename Base::pointer;
2173 using typename Base::difference_type;
2174 using typename Base::value_type;
2175private:
2176 template <typename ValueType, typename Bounds>
2177 friend class basic_array_view;
2178 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002179 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002180 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2181 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2182 {
2183 }
2184public:
2185 reference operator*() const _NOEXCEPT
2186 {
2187 return (*m_container)[*m_itr];
2188 }
2189 pointer operator->() const _NOEXCEPT
2190 {
2191 return &(*m_container)[*m_itr];
2192 }
2193 general_array_view_iterator& operator++() _NOEXCEPT
2194 {
2195 ++m_itr;
2196 return *this;
2197 }
2198 general_array_view_iterator operator++(int)_NOEXCEPT
2199 {
2200 auto ret = *this;
2201 ++(*this);
2202 return ret;
2203 }
2204 general_array_view_iterator& operator--() _NOEXCEPT
2205 {
2206 --m_itr;
2207 return *this;
2208 }
2209 general_array_view_iterator operator--(int)_NOEXCEPT
2210 {
2211 auto ret = *this;
2212 --(*this);
2213 return ret;
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 m_itr += n;
2223 return *this;
2224 }
2225 general_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2226 {
2227 general_array_view_iterator ret{ *this };
2228 return ret -= n;
2229 }
2230 general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2231 {
2232 return *this += -n;
2233 }
2234 difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT
2235 {
2236 fail_fast_assert(m_container == rhs.m_container);
2237 return m_itr - rhs.m_itr;
2238 }
2239 value_type operator[](difference_type n) const _NOEXCEPT
2240 {
2241 return (*m_container)[m_itr[n]];;
2242 }
2243 bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT
2244 {
2245 fail_fast_assert(m_container == rhs.m_container);
2246 return m_itr == rhs.m_itr;
2247 }
2248 bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT
2249 {
2250 return !(*this == rhs);
2251 }
2252 bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT
2253 {
2254 fail_fast_assert(m_container == rhs.m_container);
2255 return m_itr < rhs.m_itr;
2256 }
2257 bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT
2258 {
2259 return !(rhs < *this);
2260 }
2261 bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT
2262 {
2263 return rhs < *this;
2264 }
2265 bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT
2266 {
2267 return !(rhs > *this);
2268 }
2269 void swap(general_array_view_iterator& rhs) _NOEXCEPT
2270 {
2271 std::swap(m_itr, rhs.m_itr);
2272 std::swap(m_container, rhs.m_container);
2273 }
2274};
2275
2276template <typename ArrayView>
2277general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2278{
2279 return rhs + n;
2280}
2281
2282} // namespace Guide
2283
Neil MacIntosh9a297122015-09-14 15:11:07 -07002284#if _MSC_VER <= 1800
2285#pragma warning(pop)
2286#endif // _MSC_VER <= 1800
2287
2288
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002289#pragma pop_macro("_NOEXCEPT")