blob: 68a3cf06f68c4d5c2718ef8017393abe85f311c4 [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
Treb Connell51da1362015-09-24 18:08:34 -070019#ifndef GSL_ARRAY_VIEW_H
20#define GSL_ARRAY_VIEW_H
21
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070022#include <new>
23#include <stdexcept>
24#include <cstddef>
25#include <cstdint>
26#include <limits>
27#include <type_traits>
28#include <utility>
29#include <array>
30#include <iterator>
31#include "fail_fast.h"
32
33#ifndef _MSC_VER
34#define _CONSTEXPR constexpr
35#else
36#define _CONSTEXPR
37#endif
38
39#pragma push_macro("_NOEXCEPT")
40
41#ifndef _NOEXCEPT
42
43#ifdef SAFER_CPP_TESTING
44#define _NOEXCEPT
45#else
46#define _NOEXCEPT noexcept
47#endif
48
49#else // _NOEXCEPT
50
51#ifdef SAFER_CPP_TESTING
52#undef _NOEXCEPT
53#define _NOEXCEPT
54#endif
55
56#endif // _NOEXCEPT
57
Gabriel Dos Reis65655da2015-09-21 03:09:33 -070058#if defined(_MSC_VER) && _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -070059#pragma warning(push)
60#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
61#endif // _MSC_VER <= 1800
62
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070063namespace Guide {
64
65/*
66** begin definitions of index and bounds
67*/
68namespace details
69{
70 template <typename SizeType>
71 struct SizeTypeTraits
72 {
73 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);
74 };
75
76
Kern Handae1570262015-09-25 00:42:38 -070077 template <typename ConcreteType, typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070078 class coordinate_facade
79 {
80 static_assert(std::is_integral<ValueType>::value
81 && sizeof(ValueType) <= sizeof(size_t), "ValueType must be unsigned integral type!");
82 static_assert(Rank > 0, "Rank must be greater than 0!");
83
Kern Handae1570262015-09-25 00:42:38 -070084 template <typename OtherConcreteType, typename OtherValueType, size_t OtherRank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070085 friend class coordinate_facade;
86 public:
87 using reference = ValueType&;
88 using const_reference = const ValueType&;
89 using value_type = ValueType;
Kern Handae1570262015-09-25 00:42:38 -070090 static const size_t rank = Rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070091 _CONSTEXPR coordinate_facade() _NOEXCEPT
92 {
93 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -070094 }
95 _CONSTEXPR coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT
96 {
97 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Kern Handae1570262015-09-25 00:42:38 -070098 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -070099 elems[i] = values[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700100 }
101 _CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
102 {
103 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
104 static_assert(rank == 1, "This constructor can only be used with rank == 1.");
105 elems[0] = e0;
106 }
107 // Preconditions: il.size() == rank
108 _CONSTEXPR coordinate_facade(std::initializer_list<value_type> il)
109 {
110 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700111 fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array");
Kern Handae1570262015-09-25 00:42:38 -0700112 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700113 {
114 elems[i] = begin(il)[i];
115 }
116 }
117
118 _CONSTEXPR coordinate_facade(const coordinate_facade & other) = default;
119
120 template <typename OtherConcreteType, typename OtherValueType>
121 _CONSTEXPR coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
122 {
Kern Handae1570262015-09-25 00:42:38 -0700123 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700124 {
125 fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
126 elems[i] = static_cast<value_type>(other.elems[i]);
127 }
128 }
129 protected:
130 coordinate_facade& operator=(const coordinate_facade& rhs) = default;
131 // Preconditions: component_idx < rank
Kern Handae1570262015-09-25 00:42:38 -0700132 _CONSTEXPR reference operator[](size_t component_idx)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700133 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700134 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700135 return elems[component_idx];
136 }
137 // Preconditions: component_idx < rank
Kern Handae1570262015-09-25 00:42:38 -0700138 _CONSTEXPR const_reference operator[](size_t component_idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700139 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700140 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700141 return elems[component_idx];
142 }
143 _CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
144 {
Kern Handae1570262015-09-25 00:42:38 -0700145 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700146 {
147 if (elems[i] != rhs.elems[i])
148 return false;
149 }
150 return true;
151 }
152 _CONSTEXPR bool operator!=(const ConcreteType& rhs) const _NOEXCEPT
153 {
154 return !(to_concrete() == rhs);
155 }
156 _CONSTEXPR ConcreteType operator+() const _NOEXCEPT
157 {
158 return to_concrete();
159 }
160 _CONSTEXPR ConcreteType operator-() const
161 {
162 ConcreteType ret = to_concrete();
Kern Handae1570262015-09-25 00:42:38 -0700163 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700164 ret.elems[i] = -ret.elems[i];
165 return ret;
166 }
167 _CONSTEXPR ConcreteType operator+(const ConcreteType& rhs) const
168 {
169 ConcreteType ret = to_concrete();
170 ret += rhs;
171 return ret;
172 }
173 _CONSTEXPR ConcreteType operator-(const ConcreteType& rhs) const
174 {
175 ConcreteType ret = to_concrete();
176 ret -= rhs;
177 return ret;
178 }
179 _CONSTEXPR ConcreteType& operator+=(const ConcreteType& rhs)
180 {
Kern Handae1570262015-09-25 00:42:38 -0700181 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700182 elems[i] += rhs.elems[i];
183 return to_concrete();
184 }
185 _CONSTEXPR ConcreteType& operator-=(const ConcreteType& rhs)
186 {
Kern Handae1570262015-09-25 00:42:38 -0700187 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700188 elems[i] -= rhs.elems[i];
189 return to_concrete();
190 }
191 _CONSTEXPR ConcreteType& operator++()
192 {
193 static_assert(rank == 1, "This operator can only be used with rank == 1.");
194 ++elems[0];
195 return to_concrete();
196 }
197 _CONSTEXPR ConcreteType operator++(int)
198 {
199 static_assert(rank == 1, "This operator can only be used with rank == 1.");
200 ConcreteType ret = to_concrete();
201 ++(*this);
202 return ret;
203 }
204 _CONSTEXPR ConcreteType& operator--()
205 {
206 static_assert(rank == 1, "This operator can only be used with rank == 1.");
207 --elems[0];
208 return to_concrete();
209 }
210 _CONSTEXPR ConcreteType operator--(int)
211 {
212 static_assert(rank == 1, "This operator can only be used with rank == 1.");
213 ConcreteType ret = to_concrete();
214 --(*this);
215 return ret;
216 }
217 _CONSTEXPR ConcreteType operator*(value_type v) const
218 {
219 ConcreteType ret = to_concrete();
220 ret *= v;
221 return ret;
222 }
223 _CONSTEXPR ConcreteType operator/(value_type v) const
224 {
225 ConcreteType ret = to_concrete();
226 ret /= v;
227 return ret;
228 }
229 friend _CONSTEXPR ConcreteType operator*(value_type v, const ConcreteType& rhs)
230 {
231 return rhs * v;
232 }
233 _CONSTEXPR ConcreteType& operator*=(value_type v)
234 {
Kern Handae1570262015-09-25 00:42:38 -0700235 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700236 elems[i] *= v;
237 return to_concrete();
238 }
239 _CONSTEXPR ConcreteType& operator/=(value_type v)
240 {
Kern Handae1570262015-09-25 00:42:38 -0700241 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700242 elems[i] /= v;
243 return to_concrete();
244 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700245 value_type elems[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700246 private:
247 _CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
248 {
249 return static_cast<const ConcreteType&>(*this);
250 }
251 _CONSTEXPR ConcreteType& to_concrete() _NOEXCEPT
252 {
253 return static_cast<ConcreteType&>(*this);
254 }
255 };
256 template <typename T>
257 class arrow_proxy
258 {
259 public:
260 explicit arrow_proxy(T t)
261 : val(t)
262 {}
263 const T operator*() const _NOEXCEPT
264 {
265 return val;
266 }
267 const T* operator->() const _NOEXCEPT
268 {
269 return &val;
270 }
271 private:
272 T val;
273 };
274}
275
Kern Handae1570262015-09-25 00:42:38 -0700276template <size_t Rank, typename ValueType = size_t>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700277class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
278{
279 using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
280 friend Base;
Kern Handae1570262015-09-25 00:42:38 -0700281 template <size_t OtherRank, typename OtherValueType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700282 friend class index;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700283public:
284 using Base::rank;
285 using reference = typename Base::reference;
286 using const_reference = typename Base::const_reference;
287 using size_type = typename Base::value_type;
288 using value_type = typename Base::value_type;
289 _CONSTEXPR index() _NOEXCEPT : Base(){}
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700290 _CONSTEXPR index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {}
291 _CONSTEXPR index(std::initializer_list<value_type> il) : Base(il) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700292
293 _CONSTEXPR index(const index &) = default;
294
295 template <typename OtherValueType>
296 _CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
297 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700298 }
299 _CONSTEXPR static index shift_left(const index<rank+1, value_type>& other) _NOEXCEPT
300 {
Anna Gringauze1a864982015-09-14 18:55:06 -0700301 value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
302 return index(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700303 }
304
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 using Base::operator/;
316 using Base::operator*=;
317 using Base::operator/=;
318};
319
320template <typename ValueType>
321class index<1, ValueType>
322{
Kern Handae1570262015-09-25 00:42:38 -0700323 template <size_t, typename OtherValueType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700324 friend class index;
325public:
Kern Handae1570262015-09-25 00:42:38 -0700326 static const size_t rank = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700327 using reference = ValueType&;
328 using const_reference = const ValueType&;
329 using size_type = ValueType;
330 using value_type = ValueType;
331
332 _CONSTEXPR index() _NOEXCEPT : value(0)
333 {
334 }
335 _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
336 {
337 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700338 _CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
339 {
340 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700341 // Preconditions: il.size() == rank
342 _CONSTEXPR index(std::initializer_list<value_type> il)
343 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700344 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 -0700345 value = begin(il)[0];
346 }
347
348 _CONSTEXPR index(const index &) = default;
349
350 template <typename OtherValueType>
351 _CONSTEXPR index(const index<1, OtherValueType> & other)
352 {
353 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
354 value = static_cast<ValueType>(other.value);
355 }
356
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700357 _CONSTEXPR static index shift_left(const index<rank + 1, value_type>& other) _NOEXCEPT
358 {
359 return other.elems[1];
360 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700361 // Preconditions: component_idx < rank
362 _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
363 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700364 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700365 (void)(component_idx);
366 return value;
367 }
368 // Preconditions: component_idx < rank
369 _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
370 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700371 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700372 (void)(component_idx);
373 return value;
374 }
375 _CONSTEXPR bool operator==(const index& rhs) const _NOEXCEPT
376 {
377 return value == rhs.value;
378 }
379 _CONSTEXPR bool operator!=(const index& rhs) const _NOEXCEPT
380 {
381 return !(*this == rhs);
382 }
383 _CONSTEXPR index operator+() const _NOEXCEPT
384 {
385 return *this;
386 }
387 _CONSTEXPR index operator-() const _NOEXCEPT
388 {
389 return index(-value);
390 }
391 _CONSTEXPR index operator+(const index& rhs) const _NOEXCEPT
392 {
393 return index(value + rhs.value);
394 }
395 _CONSTEXPR index operator-(const index& rhs) const _NOEXCEPT
396 {
397 return index(value - rhs.value);
398 }
399 _CONSTEXPR index& operator+=(const index& rhs) _NOEXCEPT
400 {
401 value += rhs.value;
402 return *this;
403 }
404 _CONSTEXPR index& operator-=(const index& rhs) _NOEXCEPT
405 {
406 value -= rhs.value;
407 return *this;
408 }
409 _CONSTEXPR index& operator++() _NOEXCEPT
410 {
411 ++value;
412 return *this;
413 }
414 _CONSTEXPR index operator++(int) _NOEXCEPT
415 {
416 index ret = *this;
417 ++(*this);
418 return ret;
419 }
420 _CONSTEXPR index& operator--() _NOEXCEPT
421 {
422 --value;
423 return *this;
424 }
425 _CONSTEXPR index operator--(int) _NOEXCEPT
426 {
427 index ret = *this;
428 --(*this);
429 return ret;
430 }
431 _CONSTEXPR index operator*(value_type v) const _NOEXCEPT
432 {
433 return index(value * v);
434 }
435 _CONSTEXPR index operator/(value_type v) const _NOEXCEPT
436 {
437 return index(value / v);
438 }
439 _CONSTEXPR index& operator*=(value_type v) _NOEXCEPT
440 {
441 value *= v;
442 return *this;
443 }
444 _CONSTEXPR index& operator/=(value_type v) _NOEXCEPT
445 {
446 value /= v;
447 return *this;
448 }
449 friend _CONSTEXPR index operator*(value_type v, const index& rhs) _NOEXCEPT
450 {
451 return index(rhs * v);
452 }
453private:
454 value_type value;
455};
456
457#ifndef _MSC_VER
458
459struct static_bounds_dynamic_range_t
460{
461 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
462 constexpr operator T() const noexcept
463 {
464 return static_cast<T>(-1);
465 }
466
467 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
468 constexpr bool operator ==(T other) const noexcept
469 {
470 return static_cast<T>(-1) == other;
471 }
472
473 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
474 constexpr bool operator !=(T other) const noexcept
475 {
476 return static_cast<T>(-1) != other;
477 }
478
479};
480
481template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
482constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
483{
484 return right == left;
485}
486
487template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
488constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
489{
490 return right != left;
491}
492
493constexpr static_bounds_dynamic_range_t dynamic_range{};
494#else
495const char dynamic_range = -1;
496#endif
497
498struct generalized_mapping_tag {};
499struct contiguous_mapping_tag : generalized_mapping_tag {};
500
501namespace details
502{
503 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
504 struct StaticSizeHelperImpl
505 {
506 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
507 static const SizeType value = Fact1 * Fact2;
508 };
509
510 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
511 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
512 {
513 static const SizeType value = ConstBound;
514 };
515
516 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
517 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
518 {
519 static const SizeType value = ConstBound;
520 };
521
522 template <typename SizeType, SizeType ConstBound>
523 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
524 {
525 static const SizeType value = static_cast<SizeType>(ConstBound);
526 };
527
528 template <typename SizeType, SizeType Fact1, SizeType Fact2>
529 struct StaticSizeHelper
530 {
531 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
532 };
533
534
535 template <size_t Left, size_t Right>
536 struct LessThan
537 {
538 static const bool value = Left < Right;
539 };
540
541 template <typename SizeType, size_t... Ranges>
542 struct BoundsRanges {
Kern Handae1570262015-09-25 00:42:38 -0700543 static const size_t Depth = 0;
544 static const size_t DynamicNum = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700545 static const SizeType CurrentRange = 1;
546 static const SizeType TotalSize = 1;
547
548 BoundsRanges (const BoundsRanges &) = default;
549
550 // TODO : following signature is for work around VS bug
551 template <typename OtherType>
552 BoundsRanges (const OtherType &, bool firstLevel) {}
galikcab9bda2015-09-19 07:52:30 +0100553 BoundsRanges(const SizeType * const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700554 BoundsRanges() = default;
555
556
Kern Handae1570262015-09-25 00:42:38 -0700557 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700558 void serialize(T &) const {
559 }
Kern Handae1570262015-09-25 00:42:38 -0700560 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700561 SizeType linearize(const T &) const {
562 return 0;
563 }
Kern Handae1570262015-09-25 00:42:38 -0700564 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700565 ptrdiff_t contains(const T &) const {
566 return 0;
567 }
568
569 size_t totalSize() const _NOEXCEPT {
570 return TotalSize;
571 }
572
573 bool operator == (const BoundsRanges &) const _NOEXCEPT
574 {
575 return true;
576 }
577 };
578
579 template <typename SizeType, size_t... RestRanges>
580 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
581 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700582 static const size_t Depth = Base::Depth + 1;
583 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700584 static const SizeType CurrentRange = dynamic_range;
585 static const SizeType TotalSize = dynamic_range;
586 const SizeType m_bound;
587
588 BoundsRanges (const BoundsRanges &) = default;
589 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
590 {
591 fail_fast_assert(0 <= *arr);
592 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
593 }
594 BoundsRanges() : m_bound(0) {}
595
596 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
597 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) :
598 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
599 {
600 }
601
Kern Handae1570262015-09-25 00:42:38 -0700602 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700603 void serialize(T & arr) const {
604 arr[Dim] = elementNum();
605 this->Base::template serialize<T, Dim + 1>(arr);
606 }
Kern Handae1570262015-09-25 00:42:38 -0700607 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700608 SizeType linearize(const T & arr) const {
609 const size_t index = this->Base::totalSize() * arr[Dim];
610 fail_fast_assert(index < static_cast<size_t>(m_bound));
611 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
612 }
613
Kern Handae1570262015-09-25 00:42:38 -0700614 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700615 ptrdiff_t contains(const T & arr) const {
616 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
617 if (last == -1)
618 return -1;
619 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
620 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
621 }
622
623 size_t totalSize() const _NOEXCEPT {
624 return m_bound;
625 }
626
627 SizeType elementNum() const _NOEXCEPT {
628 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
629 }
630
Kern Handae1570262015-09-25 00:42:38 -0700631 SizeType elementNum(size_t dim) const _NOEXCEPT{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700632 if (dim > 0)
633 return this->Base::elementNum(dim - 1);
634 else
635 return elementNum();
636 }
637
638 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
639 {
640 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
641 }
642 };
643
644 template <typename SizeType, size_t CurRange, size_t... RestRanges>
645 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
646 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700647 static const size_t Depth = Base::Depth + 1;
648 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700649 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
650 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
651 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
652
653 BoundsRanges (const BoundsRanges &) = default;
654 BoundsRanges(const SizeType * const arr) : Base(arr) { }
655 BoundsRanges() = default;
656
657 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
658 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
659 {
660 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
661 }
662
Kern Handae1570262015-09-25 00:42:38 -0700663 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700664 void serialize(T & arr) const {
665 arr[Dim] = elementNum();
666 this->Base::template serialize<T, Dim + 1>(arr);
667 }
668
Kern Handae1570262015-09-25 00:42:38 -0700669 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700670 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700671 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700672 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
673 }
674
Kern Handae1570262015-09-25 00:42:38 -0700675 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700676 ptrdiff_t contains(const T & arr) const {
677 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
678 return -1;
679 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
680 if (last == -1)
681 return -1;
682 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
683 }
684
685 size_t totalSize() const _NOEXCEPT{
686 return CurrentRange * this->Base::totalSize();
687 }
688
689 SizeType elementNum() const _NOEXCEPT{
690 return CurrentRange;
691 }
692
Kern Handae1570262015-09-25 00:42:38 -0700693 SizeType elementNum(size_t dim) const _NOEXCEPT{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700694 if (dim > 0)
695 return this->Base::elementNum(dim - 1);
696 else
697 return elementNum();
698 }
699
700 bool operator == (const BoundsRanges & rhs) const _NOEXCEPT
701 {
702 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
703 }
704 };
705
706 template <typename SourceType, typename TargetType, size_t Rank>
707 struct BoundsRangeConvertible2;
708
709 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
710 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
711 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
712
713 template <size_t Rank, typename SourceType, typename TargetType>
714 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
715
716 template <typename SourceType, typename TargetType, size_t Rank>
717 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
718 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
719 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
720 {};
721
722 template <typename SourceType, typename TargetType>
723 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
724
725 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
726 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
727 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
728 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
729 {};
730 template <typename SourceType, typename TargetType>
731 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
732
733 template <typename TypeChain>
734 struct TypeListIndexer
735 {
736 const TypeChain & obj;
737 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700738 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700739 const TypeChain & getObj(std::true_type)
740 {
741 return obj;
742 }
Kern Handae1570262015-09-25 00:42:38 -0700743 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700744 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
745 {
746 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
747 }
Kern Handae1570262015-09-25 00:42:38 -0700748 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700749 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
750 {
751 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
752 }
753 };
754
755 template <typename TypeChain>
756 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
757 {
758 return TypeListIndexer<TypeChain>(obj);
759 }
760}
761
762template <typename IndexType>
763class bounds_iterator;
764
765template <typename SizeType, size_t... Ranges>
766class static_bounds {
767public:
768 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &empty) {
769 }
770};
771
772template <typename SizeType, size_t FirstRange, size_t... RestRanges>
773class static_bounds<SizeType, FirstRange, RestRanges...>
774{
775 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
776 static_assert(std::is_integral<SizeType>::value
777 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
778
779 MyRanges m_ranges;
780 _CONSTEXPR static_bounds(const MyRanges & range) : m_ranges(range) { }
781
782 template <typename SizeType2, size_t... Ranges2>
783 friend class static_bounds;
784public:
Kern Handae1570262015-09-25 00:42:38 -0700785 static const size_t rank = MyRanges::Depth;
786 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700787 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
788
789 using size_type = SizeType;
790 using index_type = index<rank, size_type>;
791 using iterator = bounds_iterator<index_type>;
792 using const_iterator = bounds_iterator<index_type>;
793 using difference_type = ptrdiff_t;
794 using sliced_type = static_bounds<SizeType, RestRanges...>;
795 using mapping_type = contiguous_mapping_tag;
796public:
797 _CONSTEXPR static_bounds(const static_bounds &) = default;
798
799 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
800 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
801 _CONSTEXPR static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
802 m_ranges(other.m_ranges)
803 {
804 }
805
806 _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
807 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700808 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
809 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 -0700810 }
811
812 _CONSTEXPR static_bounds() = default;
813
814 _CONSTEXPR static_bounds & operator = (const static_bounds & otherBounds)
815 {
816 new(&m_ranges) MyRanges (otherBounds.m_ranges);
817 return *this;
818 }
819
820 _CONSTEXPR sliced_type slice() const _NOEXCEPT
821 {
822 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
823 }
824
825 _CONSTEXPR size_type stride() const _NOEXCEPT
826 {
827 return rank > 1 ? slice().size() : 1;
828 }
829
830 _CONSTEXPR size_type size() const _NOEXCEPT
831 {
832 return static_cast<size_type>(m_ranges.totalSize());
833 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700834
835 _CONSTEXPR size_type total_size() const _NOEXCEPT
836 {
837 return static_cast<size_type>(m_ranges.totalSize());
838 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700839
840 _CONSTEXPR size_type linearize(const index_type & idx) const
841 {
842 return m_ranges.linearize(idx);
843 }
844
845 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
846 {
847 return m_ranges.contains(idx) != -1;
848 }
849
Kern Handae1570262015-09-25 00:42:38 -0700850 _CONSTEXPR size_type operator[](size_t index) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700851 {
852 return m_ranges.elementNum(index);
853 }
854
Kern Handae1570262015-09-25 00:42:38 -0700855 template <size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700856 _CONSTEXPR size_type extent() const _NOEXCEPT
857 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700858 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700859 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
860 }
861
862 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
863 {
864 index_type extents;
865 m_ranges.serialize(extents);
866 return extents;
867 }
868
869 template <typename OtherSizeTypes, size_t... Ranges>
870 _CONSTEXPR bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
871 {
872 return this->size() == rhs.size();
873 }
874
875 template <typename OtherSizeTypes, size_t... Ranges>
876 _CONSTEXPR bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const _NOEXCEPT
877 {
878 return !(*this == rhs);
879 }
880
881 _CONSTEXPR const_iterator begin() const _NOEXCEPT
882 {
883 return const_iterator(*this);
884 }
885
886 _CONSTEXPR const_iterator end() const _NOEXCEPT
887 {
888 index_type boundary;
889 m_ranges.serialize(boundary);
890 return const_iterator(*this, this->index_bounds());
891 }
892};
893
Kern Handae1570262015-09-25 00:42:38 -0700894template <size_t Rank, typename SizeType = size_t>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700895class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
896{
897 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
898 friend Base;
Kern Handae1570262015-09-25 00:42:38 -0700899 template <size_t OtherRank, typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700900 friend class strided_bounds;
901
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700902public:
903 using Base::rank;
904 using reference = typename Base::reference;
905 using const_reference = typename Base::const_reference;
906 using size_type = typename Base::value_type;
907 using difference_type = typename Base::value_type;
908 using value_type = typename Base::value_type;
909 using index_type = index<rank, size_type>;
910 using iterator = bounds_iterator<index_type>;
911 using const_iterator = bounds_iterator<index_type>;
912 static const int dynamic_rank = rank;
913 static const size_t static_size = dynamic_range;
914 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
915 using mapping_type = generalized_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700916 _CONSTEXPR strided_bounds(const strided_bounds &) = default;
917
918 template <typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700919 _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
920 : Base(other), m_strides(other.strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700921 {
922 }
923
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700924 _CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
925 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700926 {
Kern Handae1570262015-09-25 00:42:38 -0700927 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700928 Base::elems[i] = extents[i];
929 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700930 _CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
931 : Base(values), m_strides(std::move(strides))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700932 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700933 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700934 _CONSTEXPR index_type strides() const _NOEXCEPT
935 {
936 return m_strides;
937 }
938 _CONSTEXPR size_type total_size() const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700939 {
940 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700941 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700942 ret += (Base::elems[i] - 1) * m_strides[i];
943 return ret + 1;
944 }
945 _CONSTEXPR size_type size() const _NOEXCEPT
946 {
947 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700948 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700949 ret *= Base::elems[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700950 return ret;
951 }
952 _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
953 {
Kern Handae1570262015-09-25 00:42:38 -0700954 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700955 {
956 if (idx[i] < 0 || idx[i] >= Base::elems[i])
957 return false;
958 }
959 return true;
960 }
961 _CONSTEXPR size_type linearize(const index_type & idx) const
962 {
963 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700964 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700965 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700966 fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
967 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700968 }
969 return ret;
970 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700971 _CONSTEXPR size_type stride() const _NOEXCEPT
972 {
973 return m_strides[0];
974 }
975 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700976 _CONSTEXPR sliced_type slice() const
977 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700978 return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700979 }
Kern Handae1570262015-09-25 00:42:38 -0700980 template <size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 _CONSTEXPR size_type extent() const _NOEXCEPT
982 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700983 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700984 return Base::elems[Dim];
985 }
986 _CONSTEXPR index_type index_bounds() const _NOEXCEPT
987 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700988 return index_type(Base::elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700989 }
990 const_iterator begin() const _NOEXCEPT
991 {
992 return const_iterator{ *this };
993 }
994 const_iterator end() const _NOEXCEPT
995 {
996 return const_iterator{ *this, index_bounds() };
997 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700998private:
999 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001000};
1001
1002template <typename T>
1003struct is_bounds : std::integral_constant<bool, false> {};
1004template <typename SizeType, size_t... Ranges>
1005struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
Kern Handae1570262015-09-25 00:42:38 -07001006template <size_t Rank, typename SizeType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001007struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
1008
1009template <typename IndexType>
1010class bounds_iterator
1011 : public std::iterator<std::random_access_iterator_tag,
1012 IndexType,
1013 ptrdiff_t,
1014 const details::arrow_proxy<IndexType>,
1015 const IndexType>
1016{
1017private:
1018 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
1019public:
Kern Handae1570262015-09-25 00:42:38 -07001020 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001021 using typename Base::reference;
1022 using typename Base::pointer;
1023 using typename Base::difference_type;
1024 using typename Base::value_type;
1025 using index_type = value_type;
1026 using index_size_type = typename IndexType::size_type;
1027 template <typename Bounds>
1028 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) _NOEXCEPT
1029 : boundary(bnd.index_bounds())
1030 , curr( std::move(curr) )
1031 {
1032 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1033 }
1034 reference operator*() const _NOEXCEPT
1035 {
1036 return curr;
1037 }
1038 pointer operator->() const _NOEXCEPT
1039 {
1040 return details::arrow_proxy<value_type>{ curr };
1041 }
1042 bounds_iterator& operator++() _NOEXCEPT
1043 {
Kern Handae1570262015-09-25 00:42:38 -07001044 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001045 {
1046 if (++curr[i] < boundary[i])
1047 {
1048 return *this;
1049 }
1050 else
1051 {
1052 curr[i] = 0;
1053 }
1054 }
1055 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -07001056 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001057 {
1058 curr[i] = boundary[i];
1059 }
1060 return *this;
1061 }
1062 bounds_iterator operator++(int) _NOEXCEPT
1063 {
1064 auto ret = *this;
1065 ++(*this);
1066 return ret;
1067 }
1068 bounds_iterator& operator--() _NOEXCEPT
1069 {
Neil MacIntoshfb913932015-09-27 16:25:43 -07001070 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001071 {
1072 if (curr[i]-- > 0)
1073 {
1074 return *this;
1075 }
1076 else
1077 {
1078 curr[i] = boundary[i] - 1;
1079 }
1080 }
1081 // If we're here the preconditions were violated
1082 // "pre: there exists s such that r == ++s"
1083 fail_fast_assert(false);
1084 return *this;
1085 }
1086 bounds_iterator operator--(int) _NOEXCEPT
1087 {
1088 auto ret = *this;
1089 --(*this);
1090 return ret;
1091 }
1092 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1093 {
1094 bounds_iterator ret{ *this };
1095 return ret += n;
1096 }
1097 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1098 {
1099 auto linear_idx = linearize(curr) + n;
1100 value_type stride;
1101 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -07001102 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001103 {
1104 stride[i] = stride[i + 1] * boundary[i + 1];
1105 }
Kern Handae1570262015-09-25 00:42:38 -07001106 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001107 {
1108 curr[i] = linear_idx / stride[i];
1109 linear_idx = linear_idx % stride[i];
1110 }
1111 return *this;
1112 }
1113 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1114 {
1115 bounds_iterator ret{ *this };
1116 return ret -= n;
1117 }
1118 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1119 {
1120 return *this += -n;
1121 }
1122 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1123 {
1124 return linearize(curr) - linearize(rhs.curr);
1125 }
1126 reference operator[](difference_type n) const _NOEXCEPT
1127 {
1128 return *(*this + n);
1129 }
1130 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1131 {
1132 return curr == rhs.curr;
1133 }
1134 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1135 {
1136 return !(*this == rhs);
1137 }
1138 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1139 {
Kern Handae1570262015-09-25 00:42:38 -07001140 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001141 {
1142 if (curr[i] < rhs.curr[i])
1143 return true;
1144 }
1145 return false;
1146 }
1147 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1148 {
1149 return !(rhs < *this);
1150 }
1151 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1152 {
1153 return rhs < *this;
1154 }
1155 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1156 {
1157 return !(rhs > *this);
1158 }
1159 void swap(bounds_iterator& rhs) _NOEXCEPT
1160 {
1161 std::swap(boundary, rhs.boundary);
1162 std::swap(curr, rhs.curr);
1163 }
1164private:
1165 index_size_type linearize(const value_type& idx) const _NOEXCEPT
1166 {
1167 // TODO: Smarter impl.
1168 // Check if past-the-end
1169 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001170 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001171 {
1172 if (idx[i] != boundary[i])
1173 {
1174 pte = false;
1175 break;
1176 }
1177 }
1178 index_size_type multiplier = 1;
1179 index_size_type res = 0;
1180 if (pte)
1181 {
1182 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001183 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001184 {
1185 res += (idx[i] - 1) * multiplier;
1186 multiplier *= boundary[i];
1187 }
1188 }
1189 else
1190 {
Kern Handae1570262015-09-25 00:42:38 -07001191 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001192 {
1193 res += idx[i] * multiplier;
1194 multiplier *= boundary[i];
1195 }
1196 }
1197 return res;
1198 }
1199 value_type boundary;
1200 value_type curr;
1201};
1202
1203template <typename SizeType>
1204class bounds_iterator<index<1, SizeType>>
1205 : public std::iterator<std::random_access_iterator_tag,
1206 index<1, SizeType>,
1207 ptrdiff_t,
1208 const details::arrow_proxy<index<1, SizeType>>,
1209 const index<1, SizeType>>
1210{
1211 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>>;
1212
1213public:
1214 using typename Base::reference;
1215 using typename Base::pointer;
1216 using typename Base::difference_type;
1217 using typename Base::value_type;
1218 using index_type = value_type;
1219 using index_size_type = typename index_type::size_type;
1220
1221 template <typename Bounds>
1222 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) _NOEXCEPT
1223 : curr( std::move(curr) )
1224 {}
1225 reference operator*() const _NOEXCEPT
1226 {
1227 return curr;
1228 }
1229 pointer operator->() const _NOEXCEPT
1230 {
1231 return details::arrow_proxy<value_type>{ curr };
1232 }
1233 bounds_iterator& operator++() _NOEXCEPT
1234 {
1235 ++curr;
1236 return *this;
1237 }
1238 bounds_iterator operator++(int) _NOEXCEPT
1239 {
1240 auto ret = *this;
1241 ++(*this);
1242 return ret;
1243 }
1244 bounds_iterator& operator--() _NOEXCEPT
1245 {
1246 curr--;
1247 return *this;
1248 }
1249 bounds_iterator operator--(int) _NOEXCEPT
1250 {
1251 auto ret = *this;
1252 --(*this);
1253 return ret;
1254 }
1255 bounds_iterator operator+(difference_type n) const _NOEXCEPT
1256 {
1257 bounds_iterator ret{ *this };
1258 return ret += n;
1259 }
1260 bounds_iterator& operator+=(difference_type n) _NOEXCEPT
1261 {
1262 curr += n;
1263 return *this;
1264 }
1265 bounds_iterator operator-(difference_type n) const _NOEXCEPT
1266 {
1267 bounds_iterator ret{ *this };
1268 return ret -= n;
1269 }
1270 bounds_iterator& operator-=(difference_type n) _NOEXCEPT
1271 {
1272 return *this += -n;
1273 }
1274 difference_type operator-(const bounds_iterator& rhs) const _NOEXCEPT
1275 {
1276 return curr[0] - rhs.curr[0];
1277 }
1278 reference operator[](difference_type n) const _NOEXCEPT
1279 {
1280 return curr + n;
1281 }
1282 bool operator==(const bounds_iterator& rhs) const _NOEXCEPT
1283 {
1284 return curr == rhs.curr;
1285 }
1286 bool operator!=(const bounds_iterator& rhs) const _NOEXCEPT
1287 {
1288 return !(*this == rhs);
1289 }
1290 bool operator<(const bounds_iterator& rhs) const _NOEXCEPT
1291 {
1292 return curr[0] < rhs.curr[0];
1293 }
1294 bool operator<=(const bounds_iterator& rhs) const _NOEXCEPT
1295 {
1296 return !(rhs < *this);
1297 }
1298 bool operator>(const bounds_iterator& rhs) const _NOEXCEPT
1299 {
1300 return rhs < *this;
1301 }
1302 bool operator>=(const bounds_iterator& rhs) const _NOEXCEPT
1303 {
1304 return !(rhs > *this);
1305 }
1306 void swap(bounds_iterator& rhs) _NOEXCEPT
1307 {
1308 std::swap(curr, rhs.curr);
1309 }
1310private:
1311 value_type curr;
1312};
1313
1314template <typename IndexType>
1315bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) _NOEXCEPT
1316{
1317 return rhs + n;
1318}
1319
1320/*
1321** begin definitions of basic_array_view
1322*/
1323namespace details
1324{
1325 template <typename Bounds>
1326 _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
1327 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001328 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001329 }
1330
1331 // Make a stride vector from bounds, assuming continugous memory.
1332 template <typename Bounds>
1333 _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
1334 {
1335 auto extents = bnd.index_bounds();
1336 typename Bounds::index_type stride;
1337 stride[Bounds::rank - 1] = 1;
Neil MacIntoshfb913932015-09-27 16:25:43 -07001338 for (size_t i = Bounds::rank - 2; i >= 0; --i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001339 stride[i] = stride[i + 1] * extents[i + 1];
1340 return stride;
1341 }
1342
1343 template <typename BoundsSrc, typename BoundsDest>
1344 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1345 {
1346 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1347 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1348 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");
1349 fail_fast_assert(src.size() == dest.size());
1350 }
1351
1352
1353} // namespace details
1354
1355template <typename ArrayView>
1356class contiguous_array_view_iterator;
1357template <typename ArrayView>
1358class general_array_view_iterator;
1359enum class byte : std::uint8_t {};
1360
1361template <typename ValueType, typename BoundsType>
1362class basic_array_view
1363{
1364public:
Kern Handae1570262015-09-25 00:42:38 -07001365 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001366 using bounds_type = BoundsType;
1367 using size_type = typename bounds_type::size_type;
1368 using index_type = typename bounds_type::index_type;
1369 using value_type = ValueType;
1370 using pointer = ValueType*;
1371 using reference = ValueType&;
1372 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>>;
1373 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>>>;
1374 using reverse_iterator = std::reverse_iterator<iterator>;
1375 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1376 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1377
1378private:
1379 pointer m_pdata;
1380 bounds_type m_bounds;
1381
1382public:
1383 _CONSTEXPR bounds_type bounds() const _NOEXCEPT
1384 {
1385 return m_bounds;
1386 }
Kern Handae1570262015-09-25 00:42:38 -07001387 template <size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001388 _CONSTEXPR size_type extent() const _NOEXCEPT
1389 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001390 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001391 return m_bounds.template extent<Dim>();
1392 }
1393 _CONSTEXPR size_type size() const _NOEXCEPT
1394 {
1395 return m_bounds.size();
1396 }
1397 _CONSTEXPR reference operator[](const index_type& idx) const
1398 {
1399 return m_pdata[m_bounds.linearize(idx)];
1400 }
1401 _CONSTEXPR pointer data() const _NOEXCEPT
1402 {
1403 return m_pdata;
1404 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001405 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001406 _CONSTEXPR Ret operator[](size_type idx) const
1407 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001408 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001409 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001410
1411 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001412 return Ret {m_pdata + ridx, m_bounds.slice()};
1413 }
1414
1415 _CONSTEXPR operator bool () const _NOEXCEPT
1416 {
1417 return m_pdata != nullptr;
1418 }
1419
1420 _CONSTEXPR iterator begin() const
1421 {
1422 return iterator {this, true};
1423 }
1424 _CONSTEXPR iterator end() const
1425 {
1426 return iterator {this};
1427 }
1428 _CONSTEXPR const_iterator cbegin() const
1429 {
1430 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1431 }
1432 _CONSTEXPR const_iterator cend() const
1433 {
1434 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1435 }
1436
1437 _CONSTEXPR reverse_iterator rbegin() const
1438 {
1439 return reverse_iterator {end()};
1440 }
1441 _CONSTEXPR reverse_iterator rend() const
1442 {
1443 return reverse_iterator {begin()};
1444 }
1445 _CONSTEXPR const_reverse_iterator crbegin() const
1446 {
1447 return const_reverse_iterator {cend()};
1448 }
1449 _CONSTEXPR const_reverse_iterator crend() const
1450 {
1451 return const_reverse_iterator {cbegin()};
1452 }
1453
1454 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 -07001455 _CONSTEXPR bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001456 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001457 return m_bounds.size() == other.m_bounds.size() &&
1458 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001459 }
1460
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001461 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>>
1462 _CONSTEXPR bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1463 {
1464 return !(*this == other);
1465 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001466
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001467 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>>
1468 _CONSTEXPR bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1469 {
1470 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1471 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001472
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001473 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>>
1474 _CONSTEXPR bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1475 {
1476 return !(other < *this);
1477 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001478
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001479 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>>
1480 _CONSTEXPR bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1481 {
1482 return (other < *this);
1483 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001484
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001485 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>>
1486 _CONSTEXPR bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
1487 {
1488 return !(*this < other);
1489 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001490
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001491public:
1492 template <typename OtherValueType, typename OtherBounds,
1493 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1494 && std::is_convertible<OtherBounds, bounds_type>::value>>
1495 _CONSTEXPR basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) _NOEXCEPT
1496 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1497 {
1498 }
1499protected:
1500
1501 _CONSTEXPR basic_array_view(pointer data, bounds_type bound) _NOEXCEPT
1502 : m_pdata(data)
1503 , m_bounds(std::move(bound))
1504 {
1505 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1506 }
1507 template <typename T>
1508 _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
1509 : m_pdata(reinterpret_cast<pointer>(data))
1510 , m_bounds(std::move(bound))
1511 {
1512 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1513 }
1514 template <typename DestBounds>
1515 _CONSTEXPR basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1516 {
1517 details::verifyBoundsReshape(m_bounds, bounds);
1518 return {m_pdata, bounds};
1519 }
1520private:
1521
1522 friend iterator;
1523 friend const_iterator;
1524 template <typename ValueType2, typename BoundsType2>
1525 friend class basic_array_view;
1526};
1527
1528template <size_t DimSize = dynamic_range>
1529struct dim
1530{
1531 static const size_t value = DimSize;
1532};
1533template <>
1534struct dim<dynamic_range>
1535{
1536 static const size_t value = dynamic_range;
1537 const size_t dvalue;
1538 dim(size_t size) : dvalue(size) {}
1539};
1540
1541template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1542class array_view;
Kern Handae1570262015-09-25 00:42:38 -07001543template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001544class strided_array_view;
1545
1546namespace details
1547{
1548 template <typename T, typename = std::true_type>
1549 struct ArrayViewTypeTraits
1550 {
1551 using value_type = T;
1552 using size_type = size_t;
1553 };
1554
1555 template <typename Traits>
1556 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1557 {
1558 using value_type = typename Traits::array_view_traits::value_type;
1559 using size_type = typename Traits::array_view_traits::size_type;
1560 };
1561
1562 template <typename T, typename SizeType, size_t... Ranks>
1563 struct ArrayViewArrayTraits {
1564 using type = array_view<T, Ranks...>;
1565 using value_type = T;
1566 using bounds_type = static_bounds<SizeType, Ranks...>;
1567 using pointer = T*;
1568 using reference = T&;
1569 };
1570 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1571 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1572
1573 template <typename BoundsType>
1574 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1575 {
1576 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1577 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1578 }
1579 template <typename BoundsType>
1580 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1581 {
1582 fail_fast_assert(BoundsType::static_size == totalSize);
1583 return {};
1584 }
1585 template <typename BoundsType>
1586 BoundsType newBoundsHelper(size_t totalSize)
1587 {
1588 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1589 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1590 }
1591
1592 struct Sep{};
1593
1594 template <typename T, typename... Args>
1595 T static_as_array_view_helper(Sep, Args... args)
1596 {
1597 return T{static_cast<typename T::size_type>(args)...};
1598 }
1599 template <typename T, typename Arg, typename... Args>
1600 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)
1601 {
1602 return static_as_array_view_helper<T>(args...);
1603 }
1604 template <typename T, typename... Args>
1605 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1606 {
1607 return static_as_array_view_helper<T>(args..., val.dvalue);
1608 }
1609
1610 template <typename SizeType, typename ...Dimensions>
1611 struct static_as_array_view_static_bounds_helper
1612 {
1613 using type = static_bounds<SizeType, (Dimensions::value)...>;
1614 };
1615
1616 template <typename T>
1617 struct is_array_view_oracle : std::false_type
1618 {};
1619 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1620 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1621 {};
Kern Handae1570262015-09-25 00:42:38 -07001622 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001623 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1624 {};
1625 template <typename T>
1626 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1627 {};
1628
1629}
1630
1631
1632template <typename ValueType, typename SizeType>
1633struct array_view_options
1634{
1635 struct array_view_traits
1636 {
1637 using value_type = ValueType;
1638 using size_type = SizeType;
1639 };
1640};
1641
1642template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1643class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1644 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1645{
1646 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1647 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001648 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001649 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001650
1651public:
1652 using typename Base::bounds_type;
1653 using typename Base::size_type;
1654 using typename Base::pointer;
1655 using typename Base::value_type;
1656 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001657 using typename Base::iterator;
1658 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001659 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001660 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001661
1662public:
1663 // basic
1664 _CONSTEXPR array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1665 {
1666 }
1667
1668 _CONSTEXPR array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1669 {
1670 }
1671
Neil MacIntosh9b40a0a2015-08-27 19:49:27 -07001672 _CONSTEXPR array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001673 {
1674 fail_fast_assert(size == 0);
1675 }
1676
1677 // default
1678 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
1679 _CONSTEXPR array_view() : Base(nullptr, bounds_type())
1680 {
1681 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001682
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001683 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1684 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001685 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1686 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
1687 _CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001688 {
1689 }
1690
1691 // from n-dimensions static array
1692 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1693 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 -07001694 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Anna Gringauzee5b79d22015-09-14 16:38:25 -07001695 _CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001696 {
1697 }
1698
1699 // from n-dimensions static array with size
1700 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1701 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 -07001702 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
1703 _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001704 {
1705 fail_fast_assert(size <= N);
1706 }
1707
1708 // from std array
1709 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
1710 _CONSTEXPR array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1711 {
1712 }
1713
1714 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>>
1715 _CONSTEXPR array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
1716 {
1717 }
1718
1719
1720 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1721 template <typename Ptr,
1722 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1723 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
1724 _CONSTEXPR array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1725 {
1726 }
1727
1728 // from containers. It must has .size() and .data() two function signatures
1729 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1730 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001731 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001732 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1733 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001734 >
Anna Gringauze18cd9802015-09-14 16:34:26 -07001735 _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 -07001736 {
1737
1738 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001739
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001740 _CONSTEXPR array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001741
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001742 // convertible
1743 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1744 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1745 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1746 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1747 >
1748 _CONSTEXPR array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
1749
1750 // reshape
1751 template <typename... Dimensions2>
1752 _CONSTEXPR array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
1753 {
1754 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001755 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1757 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001758 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001759 }
1760
1761 // to bytes array
1762 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001763 _CONSTEXPR auto as_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001764 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)>
1765 {
1766 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001767 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001768 }
1769
1770 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001771 _CONSTEXPR auto as_writeable_bytes() const _NOEXCEPT ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001772 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)>
1773 {
1774 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001775 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001776 }
1777
Anna Gringauze18cd9802015-09-14 16:34:26 -07001778
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001779 // from bytes array
1780 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1781 _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))>
1782 {
1783 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1784 "Target type must be standard layout and its size must match the byte array size");
1785 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001786 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001787 }
1788
1789 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1790 _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))>
1791 {
1792 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1793 "Target type must be standard layout and its size must match the byte array size");
1794 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001795 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001796 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001797
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001798 // section on linear space
1799 template<size_t Count>
1800 _CONSTEXPR array_view<ValueTypeOpt, Count> first() const _NOEXCEPT
1801 {
1802 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1803 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 -07001804 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001805 }
1806
1807 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
1808 {
1809 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001810 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001811 }
1812
1813 template<size_t Count>
1814 _CONSTEXPR array_view<ValueTypeOpt, Count> last() const _NOEXCEPT
1815 {
1816 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1817 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001818 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001819 }
1820
1821 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
1822 {
1823 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001824 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001825 }
1826
1827 template<size_t Offset, size_t Count>
1828 _CONSTEXPR array_view<ValueTypeOpt, Count> sub() const _NOEXCEPT
1829 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001830 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");
1831 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 -07001832 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001833 }
1834
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001835 _CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const _NOEXCEPT
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001836 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001837 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1838 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001839 }
1840
1841 // size
1842 _CONSTEXPR size_type length() const _NOEXCEPT
1843 {
1844 return this->size();
1845 }
1846 _CONSTEXPR size_type used_length() const _NOEXCEPT
1847 {
1848 return length();
1849 }
1850 _CONSTEXPR size_type bytes() const _NOEXCEPT
1851 {
1852 return sizeof(value_type) * this->size();
1853 }
1854 _CONSTEXPR size_type used_bytes() const _NOEXCEPT
1855 {
1856 return bytes();
1857 }
1858
1859 // section
1860 _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
1861 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001862 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001863 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1864 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001865
Anna Gringauze1a864982015-09-14 18:55:06 -07001866 _CONSTEXPR reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001867 {
1868 return Base::operator[](idx);
1869 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001870
Anna Gringauze1a864982015-09-14 18:55:06 -07001871 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001872 _CONSTEXPR array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
1873 {
1874 auto ret = Base::operator[](idx);
1875 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001876 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001877
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001878 using Base::operator==;
1879 using Base::operator!=;
1880 using Base::operator<;
1881 using Base::operator<=;
1882 using Base::operator>;
1883 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001884};
1885
1886template <typename T, size_t... Dimensions>
1887_CONSTEXPR auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
1888{
1889 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1890}
1891
1892template <typename T>
1893_CONSTEXPR auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
1894{
1895 return {arr, len};
1896}
1897
1898template <typename T, size_t N>
1899_CONSTEXPR auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
1900{
1901 return {arr};
1902}
1903
1904template <typename T, size_t N>
1905_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &arr)
1906{
1907 return {arr};
1908}
1909
1910template <typename T, size_t N>
1911_CONSTEXPR array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
1912
1913template <typename T, size_t N>
1914_CONSTEXPR array_view<T, N> as_array_view(std::array<T, N> &arr)
1915{
1916 return {arr};
1917}
1918
1919template <typename T>
1920_CONSTEXPR array_view<T, dynamic_range> as_array_view(T *begin, T *end)
1921{
1922 return {begin, end};
1923}
1924
1925template <typename Cont>
1926_CONSTEXPR auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1927 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1928{
1929 return {arr.data(), arr.size()};
1930}
1931
1932template <typename Cont>
1933_CONSTEXPR auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
1934 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1935
Kern Handae1570262015-09-25 00:42:38 -07001936template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001937class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1938{
1939 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 -07001940
Kern Handae1570262015-09-25 00:42:38 -07001941 template<typename OtherValueOpt, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001942 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001943public:
1944 using Base::rank;
1945 using typename Base::bounds_type;
1946 using typename Base::size_type;
1947 using typename Base::pointer;
1948 using typename Base::value_type;
1949 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001950 using typename Base::iterator;
1951 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001952 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001953
1954 // from static array of size N
1955 template<size_type N>
1956 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1957 {
1958 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1959 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001960
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001961 // from raw data
1962 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001963 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001964 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001965 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001966
1967 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001968 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001969 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1970 {
1971 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1972 }
1973
1974 // convertible
1975 template <typename OtherValueTypeOpt,
1976 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1977 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1978 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1979 >
1980 _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 -07001981 {
1982 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001983
1984 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001985 template <typename OtherValueType>
1986 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 -07001987 {
1988 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1989 auto d = sizeof(OtherValueType) / sizeof(value_type);
1990
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001991 size_type size = this->bounds().total_size() / d;
1992 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 -07001993 }
1994
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001995 strided_array_view section(index_type origin, index_type extents) const
1996 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001997 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001998 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1999 }
2000
2001 _CONSTEXPR reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07002002 {
2003 return Base::operator[](idx);
2004 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002005
2006 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2007 _CONSTEXPR strided_array_view<value_type, rank-1> operator[](size_type idx) const
2008 {
2009 auto ret = Base::operator[](idx);
2010 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
2011 }
2012
2013private:
2014 static index_type resize_extent(const index_type& extent, size_t d)
2015 {
2016 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");
2017
2018 index_type ret = extent;
2019 ret[rank - 1] /= d;
2020
2021 return ret;
2022 }
2023
2024 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
2025 static index_type resize_stride(const index_type& strides, size_t d, void *p = 0)
2026 {
2027 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2028
2029 return strides;
2030 }
2031
2032 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2033 static index_type resize_stride(const index_type& strides, size_t d)
2034 {
2035 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2036 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");
2037
Neil MacIntoshfb913932015-09-27 16:25:43 -07002038 for (size_t i = rank - 2; i >= 0; --i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002039 {
2040 fail_fast_assert((strides[i] >= strides[i + 1]) && (strides[i] % strides[i + 1] == 0), "Only strided arrays with regular strides can be resized");
2041 }
2042
2043 index_type ret = strides / d;
2044 ret[rank - 1] = 1;
2045
2046 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002047 }
2048};
2049
2050template <typename ArrayView>
2051class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2052{
2053 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2054public:
2055 using typename Base::reference;
2056 using typename Base::pointer;
2057 using typename Base::difference_type;
2058private:
2059 template <typename ValueType, typename Bounds>
2060 friend class basic_array_view;
2061 pointer m_pdata;
2062 const ArrayView * m_validator;
2063 void validateThis() const
2064 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07002065 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 -07002066 }
2067 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2068 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2069public:
2070 reference operator*() const _NOEXCEPT
2071 {
2072 validateThis();
2073 return *m_pdata;
2074 }
2075 pointer operator->() const _NOEXCEPT
2076 {
2077 validateThis();
2078 return m_pdata;
2079 }
2080 contiguous_array_view_iterator& operator++() _NOEXCEPT
2081 {
2082 ++m_pdata;
2083 return *this;
2084 }
2085 contiguous_array_view_iterator operator++(int)_NOEXCEPT
2086 {
2087 auto ret = *this;
2088 ++(*this);
2089 return ret;
2090 }
2091 contiguous_array_view_iterator& operator--() _NOEXCEPT
2092 {
2093 --m_pdata;
2094 return *this;
2095 }
2096 contiguous_array_view_iterator operator--(int)_NOEXCEPT
2097 {
2098 auto ret = *this;
2099 --(*this);
2100 return ret;
2101 }
2102 contiguous_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2103 {
2104 contiguous_array_view_iterator ret{ *this };
2105 return ret += n;
2106 }
2107 contiguous_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2108 {
2109 m_pdata += n;
2110 return *this;
2111 }
2112 contiguous_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2113 {
2114 contiguous_array_view_iterator ret{ *this };
2115 return ret -= n;
2116 }
2117 contiguous_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2118 {
2119 return *this += -n;
2120 }
2121 difference_type operator-(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2122 {
2123 fail_fast_assert(m_validator == rhs.m_validator);
2124 return m_pdata - rhs.m_pdata;
2125 }
2126 reference operator[](difference_type n) const _NOEXCEPT
2127 {
2128 return *(*this + n);
2129 }
2130 bool operator==(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2131 {
2132 fail_fast_assert(m_validator == rhs.m_validator);
2133 return m_pdata == rhs.m_pdata;
2134 }
2135 bool operator!=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2136 {
2137 return !(*this == rhs);
2138 }
2139 bool operator<(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2140 {
2141 fail_fast_assert(m_validator == rhs.m_validator);
2142 return m_pdata < rhs.m_pdata;
2143 }
2144 bool operator<=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2145 {
2146 return !(rhs < *this);
2147 }
2148 bool operator>(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2149 {
2150 return rhs < *this;
2151 }
2152 bool operator>=(const contiguous_array_view_iterator& rhs) const _NOEXCEPT
2153 {
2154 return !(rhs > *this);
2155 }
2156 void swap(contiguous_array_view_iterator& rhs) _NOEXCEPT
2157 {
2158 std::swap(m_pdata, rhs.m_pdata);
2159 std::swap(m_validator, rhs.m_validator);
2160 }
2161};
2162
2163template <typename ArrayView>
2164contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2165{
2166 return rhs + n;
2167}
2168
2169template <typename ArrayView>
2170class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2171{
2172 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2173public:
2174 using typename Base::reference;
2175 using typename Base::pointer;
2176 using typename Base::difference_type;
2177 using typename Base::value_type;
2178private:
2179 template <typename ValueType, typename Bounds>
2180 friend class basic_array_view;
2181 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002182 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002183 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2184 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2185 {
2186 }
2187public:
2188 reference operator*() const _NOEXCEPT
2189 {
2190 return (*m_container)[*m_itr];
2191 }
2192 pointer operator->() const _NOEXCEPT
2193 {
2194 return &(*m_container)[*m_itr];
2195 }
2196 general_array_view_iterator& operator++() _NOEXCEPT
2197 {
2198 ++m_itr;
2199 return *this;
2200 }
2201 general_array_view_iterator operator++(int)_NOEXCEPT
2202 {
2203 auto ret = *this;
2204 ++(*this);
2205 return ret;
2206 }
2207 general_array_view_iterator& operator--() _NOEXCEPT
2208 {
2209 --m_itr;
2210 return *this;
2211 }
2212 general_array_view_iterator operator--(int)_NOEXCEPT
2213 {
2214 auto ret = *this;
2215 --(*this);
2216 return ret;
2217 }
2218 general_array_view_iterator operator+(difference_type n) const _NOEXCEPT
2219 {
2220 general_array_view_iterator ret{ *this };
2221 return ret += n;
2222 }
2223 general_array_view_iterator& operator+=(difference_type n) _NOEXCEPT
2224 {
2225 m_itr += n;
2226 return *this;
2227 }
2228 general_array_view_iterator operator-(difference_type n) const _NOEXCEPT
2229 {
2230 general_array_view_iterator ret{ *this };
2231 return ret -= n;
2232 }
2233 general_array_view_iterator& operator-=(difference_type n) _NOEXCEPT
2234 {
2235 return *this += -n;
2236 }
2237 difference_type operator-(const general_array_view_iterator& rhs) const _NOEXCEPT
2238 {
2239 fail_fast_assert(m_container == rhs.m_container);
2240 return m_itr - rhs.m_itr;
2241 }
2242 value_type operator[](difference_type n) const _NOEXCEPT
2243 {
2244 return (*m_container)[m_itr[n]];;
2245 }
2246 bool operator==(const general_array_view_iterator& rhs) const _NOEXCEPT
2247 {
2248 fail_fast_assert(m_container == rhs.m_container);
2249 return m_itr == rhs.m_itr;
2250 }
2251 bool operator !=(const general_array_view_iterator& rhs) const _NOEXCEPT
2252 {
2253 return !(*this == rhs);
2254 }
2255 bool operator<(const general_array_view_iterator& rhs) const _NOEXCEPT
2256 {
2257 fail_fast_assert(m_container == rhs.m_container);
2258 return m_itr < rhs.m_itr;
2259 }
2260 bool operator<=(const general_array_view_iterator& rhs) const _NOEXCEPT
2261 {
2262 return !(rhs < *this);
2263 }
2264 bool operator>(const general_array_view_iterator& rhs) const _NOEXCEPT
2265 {
2266 return rhs < *this;
2267 }
2268 bool operator>=(const general_array_view_iterator& rhs) const _NOEXCEPT
2269 {
2270 return !(rhs > *this);
2271 }
2272 void swap(general_array_view_iterator& rhs) _NOEXCEPT
2273 {
2274 std::swap(m_itr, rhs.m_itr);
2275 std::swap(m_container, rhs.m_container);
2276 }
2277};
2278
2279template <typename ArrayView>
2280general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) _NOEXCEPT
2281{
2282 return rhs + n;
2283}
2284
2285} // namespace Guide
2286
Gabriel Dos Reis65655da2015-09-21 03:09:33 -07002287#if defined(_MSC_VER) && _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002288#pragma warning(pop)
2289#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002290
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002291#pragma pop_macro("_NOEXCEPT")
Treb Connell51da1362015-09-24 18:08:34 -07002292
2293#endif // GSL_ARRAY_VIEW_H