blob: cc7ab23ed177dc99162567caed83fefdd6c961fc [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>
Anna Gringauze2cdedda2015-10-15 13:19:24 -070027#include <numeric>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070028#include <type_traits>
29#include <utility>
30#include <array>
31#include <iterator>
Kern Handac4f9b872015-09-25 17:01:29 -070032#include <algorithm>
Neil MacIntosh01868f22015-10-15 16:48:38 -070033#include <functional>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070034#include "fail_fast.h"
35
Neil MacIntoshd5316802015-09-30 21:54:08 -070036#ifdef _MSC_VER
37
38// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070039#pragma push_macro("constexpr")
40#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070041
42
43// VS 2013 workarounds
44#if _MSC_VER <= 1800
45
46// noexcept is not understood
47#ifndef GSL_THROWS_FOR_TESTING
48#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070049#endif
50
Neil MacIntoshd5316802015-09-30 21:54:08 -070051// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070052#pragma warning(push)
53#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntoshd5316802015-09-30 21:54:08 -070054
Neil MacIntosh9a297122015-09-14 15:11:07 -070055#endif // _MSC_VER <= 1800
56
Neil MacIntoshd5316802015-09-30 21:54:08 -070057#endif // _MSC_VER
58
59// In order to test the library, we need it to throw exceptions that we can catch
60#ifdef GSL_THROWS_FOR_TESTING
61#define noexcept /* nothing */
62#endif // GSL_THROWS_FOR_TESTING
63
64
Neil MacIntoshef626fd2015-09-29 16:41:37 -070065namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070066
67/*
68** begin definitions of index and bounds
69*/
70namespace details
71{
72 template <typename SizeType>
73 struct SizeTypeTraits
74 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -070075 static const SizeType max_value = std::is_signed<SizeType>::value ? static_cast<typename std::make_unsigned<SizeType>::type>(-1) / 2 : static_cast<SizeType>(-1);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070076 };
77
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070078 template <typename T>
79 class arrow_proxy
80 {
81 public:
82 explicit arrow_proxy(T t)
83 : val(t)
84 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -070085 const T operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070086 {
87 return val;
88 }
Neil MacIntoshd5316802015-09-30 21:54:08 -070089 const T* operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070090 {
91 return &val;
92 }
93 private:
94 T val;
95 };
96}
97
Kern Handae1570262015-09-25 00:42:38 -070098template <size_t Rank, typename ValueType = size_t>
Anna Gringauzedb384972015-10-05 12:34:23 -070099class index final
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700100{
Anna Gringauzedb384972015-10-05 12:34:23 -0700101 static_assert(std::is_integral<ValueType>::value, "ValueType must be an integral type!");
102 static_assert(Rank > 0, "Rank must be greater than 0!");
103
Kern Handae1570262015-09-25 00:42:38 -0700104 template <size_t OtherRank, typename OtherValueType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700105 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -0700106
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700107public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700108 static const size_t rank = Rank;
109 using value_type = std::remove_reference_t<ValueType>;
110 using reference = std::add_lvalue_reference_t<value_type>;
111 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700112
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700113 constexpr index() noexcept
114 {}
115
Anna Gringauzedb384972015-10-05 12:34:23 -0700116 constexpr index(const value_type(&values)[Rank]) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700117 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700118 std::copy(values, values + Rank, elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700119 }
120
Anna Gringauzedb384972015-10-05 12:34:23 -0700121 // Preconditions: il.size() == rank
122 constexpr index(std::initializer_list<value_type> il) noexcept
123 {
124 fail_fast_assert(il.size() == Rank, "The size of the initializer list must match the rank of the array");
125 std::copy(begin(il), end(il), elems);
126 }
127
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700128 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700129
130 // copy from index over smaller domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700131 template <typename OtherValueType,
132 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
133 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
134 constexpr index(const index<Rank, OtherValueType>& other) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700135 {
136 std::copy(other.elems, other.elems + Rank, elems);
137 }
138
139 // copy from index over larger domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700140 template <typename OtherValueType,
141 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
142 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
143 constexpr index(const index<Rank, OtherValueType>& other, void* ptr = 0) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700144 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700145 bool ok = std::accumulate(other.elems, other.elems + Rank, true,
146 [&](bool b, OtherValueType val) { return b && (val <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value)); }
147 );
Anna Gringauzedb384972015-10-05 12:34:23 -0700148
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700149 fail_fast_assert(ok, "other value must fit in the new domain");
150 std::transform(other.elems, other.elems + rank, elems, [&](OtherValueType val) { return static_cast<value_type>(val); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700151 }
152
153 constexpr index& operator=(const index& rhs) noexcept = default;
154
155 // Preconditions: component_idx < rank
156 constexpr reference operator[](size_t component_idx)
157 {
158 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
159 return elems[component_idx];
160 }
161
162 // Preconditions: component_idx < rank
163 constexpr const_reference operator[](size_t component_idx) const noexcept
164 {
165 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
166 return elems[component_idx];
167 }
168
169 constexpr bool operator==(const index& rhs) const noexcept
170 {
171 return std::equal(elems, elems + rank, rhs.elems);
172 }
173
174 constexpr bool operator!=(const index& rhs) const noexcept
175 {
176 return !(this == rhs);
177 }
178
179 constexpr index operator+() const noexcept
180 {
181 return *this;
182 }
183
184 constexpr index operator-() const noexcept
185 {
186 index ret = *this;
187 std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
188 return ret;
189 }
190
191 constexpr index operator+(const index& rhs) const noexcept
192 {
193 index ret = *this;
194 ret += rhs;
195 return ret;
196 }
197
198 constexpr index operator-(const index& rhs) const noexcept
199 {
200 index ret = *this;
201 ret -= rhs;
202 return ret;
203 }
204
205 constexpr index& operator+=(const index& rhs) noexcept
206 {
207 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<ValueType>{});
208 return *this;
209 }
210
211 constexpr index& operator-=(const index& rhs) noexcept
212 {
213 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<ValueType>{});
214 return *this;
215 }
216
217 constexpr index operator*(value_type v) const noexcept
218 {
219 index ret = *this;
220 ret *= v;
221 return ret;
222 }
223
224 constexpr index operator/(value_type v) const noexcept
225 {
226 index ret = *this;
227 ret /= v;
228 return ret;
229 }
230
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700231 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700232 {
233 return rhs * v;
234 }
235
236 constexpr index& operator*=(value_type v) noexcept
237 {
238 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<ValueType>{}(x, v); });
239 return *this;
240 }
241
242 constexpr index& operator/=(value_type v) noexcept
243 {
244 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<ValueType>{}(x, v); });
245 return *this;
246 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700247
Anna Gringauzedb384972015-10-05 12:34:23 -0700248private:
249 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700250};
251
252template <typename ValueType>
253class index<1, ValueType>
254{
Kern Handae1570262015-09-25 00:42:38 -0700255 template <size_t, typename OtherValueType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700256 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -0700257
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700258public:
Kern Handae1570262015-09-25 00:42:38 -0700259 static const size_t rank = 1;
Anna Gringauzedb384972015-10-05 12:34:23 -0700260 using value_type = std::remove_reference_t<ValueType>;
261 using reference = std::add_lvalue_reference_t<value_type>;
262 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700263
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700264 constexpr index() noexcept : value(0)
Anna Gringauzedb384972015-10-05 12:34:23 -0700265 {}
266
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700267 constexpr index(value_type e) noexcept : value(e)
Anna Gringauzedb384972015-10-05 12:34:23 -0700268 {}
269
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700270 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
271 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700272
Anna Gringauzedb384972015-10-05 12:34:23 -0700273 constexpr index(const index &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700274
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700275 template <typename OtherValueType,
276 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
277 typename Other = std::enable_if_t<Enabled, index<1, OtherValueType>>>
278 constexpr index(const index<1, OtherValueType>& other) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700279 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700280 value = static_cast<ValueType>(other.value);
281 }
282
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700283 template <typename OtherValueType,
284 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
285 typename Other = std::enable_if_t<Enabled, index<1, OtherValueType>>>
286 constexpr index(const index<1, OtherValueType>& other, void* ptr=0) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700287 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700288 fail_fast_assert(other.value <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value));
Anna Gringauzedb384972015-10-05 12:34:23 -0700289 value = static_cast<value_type>(other.value);
290 }
291
Anna Gringauzedb384972015-10-05 12:34:23 -0700292 // Preconditions: component_idx < 1
293 constexpr reference operator[](value_type component_idx) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700294 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700295 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700296 (void)(component_idx);
297 return value;
298 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700299 // Preconditions: component_idx < 1
300 constexpr const_reference operator[](value_type component_idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700301 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700302 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700303 (void)(component_idx);
304 return value;
305 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700306 constexpr bool operator==(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700307 {
308 return value == rhs.value;
309 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700310 constexpr bool operator!=(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700311 {
312 return !(*this == rhs);
313 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700314 constexpr index operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700315 {
316 return *this;
317 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700318 constexpr index operator-() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700319 {
320 return index(-value);
321 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700322 constexpr index operator+(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700323 {
324 return index(value + rhs.value);
325 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700326 constexpr index operator-(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700327 {
328 return index(value - rhs.value);
329 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700330 constexpr index& operator+=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700331 {
332 value += rhs.value;
333 return *this;
334 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700335 constexpr index& operator-=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700336 {
337 value -= rhs.value;
338 return *this;
339 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700340 constexpr index& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700341 {
342 ++value;
343 return *this;
344 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700345 constexpr index operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700346 {
347 index ret = *this;
348 ++(*this);
349 return ret;
350 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700351 constexpr index& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700352 {
353 --value;
354 return *this;
355 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700356 constexpr index operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700357 {
358 index ret = *this;
359 --(*this);
360 return ret;
361 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700362 constexpr index operator*(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700363 {
364 return index(value * v);
365 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700366 constexpr index operator/(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700367 {
368 return index(value / v);
369 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700370 constexpr index& operator*=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700371 {
372 value *= v;
373 return *this;
374 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700375 constexpr index& operator/=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700376 {
377 value /= v;
378 return *this;
379 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700380 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700381 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700382 return{ rhs * v };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700383 }
384private:
385 value_type value;
386};
387
388#ifndef _MSC_VER
389
390struct static_bounds_dynamic_range_t
391{
392 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
393 constexpr operator T() const noexcept
394 {
395 return static_cast<T>(-1);
396 }
397
398 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
399 constexpr bool operator ==(T other) const noexcept
400 {
401 return static_cast<T>(-1) == other;
402 }
403
404 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
405 constexpr bool operator !=(T other) const noexcept
406 {
407 return static_cast<T>(-1) != other;
408 }
409
410};
411
412template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
413constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
414{
415 return right == left;
416}
417
418template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
419constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
420{
421 return right != left;
422}
423
424constexpr static_bounds_dynamic_range_t dynamic_range{};
425#else
426const char dynamic_range = -1;
427#endif
428
429struct generalized_mapping_tag {};
430struct contiguous_mapping_tag : generalized_mapping_tag {};
431
432namespace details
433{
434 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
435 struct StaticSizeHelperImpl
436 {
437 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
438 static const SizeType value = Fact1 * Fact2;
439 };
440
441 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
442 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
443 {
444 static const SizeType value = ConstBound;
445 };
446
447 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
448 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
449 {
450 static const SizeType value = ConstBound;
451 };
452
453 template <typename SizeType, SizeType ConstBound>
454 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
455 {
456 static const SizeType value = static_cast<SizeType>(ConstBound);
457 };
458
459 template <typename SizeType, SizeType Fact1, SizeType Fact2>
460 struct StaticSizeHelper
461 {
462 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
463 };
464
465
466 template <size_t Left, size_t Right>
467 struct LessThan
468 {
469 static const bool value = Left < Right;
470 };
471
472 template <typename SizeType, size_t... Ranges>
473 struct BoundsRanges {
Kern Handae1570262015-09-25 00:42:38 -0700474 static const size_t Depth = 0;
475 static const size_t DynamicNum = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700476 static const SizeType CurrentRange = 1;
477 static const SizeType TotalSize = 1;
478
479 BoundsRanges (const BoundsRanges &) = default;
480
481 // TODO : following signature is for work around VS bug
482 template <typename OtherType>
Kosov Eugene3402b922015-09-28 21:20:02 +0300483 BoundsRanges (const OtherType &, bool /* firstLevel */) {}
galikcab9bda2015-09-19 07:52:30 +0100484 BoundsRanges(const SizeType * const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700485 BoundsRanges() = default;
486
487
Kern Handae1570262015-09-25 00:42:38 -0700488 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700489 void serialize(T &) const {
490 }
Kern Handae1570262015-09-25 00:42:38 -0700491 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700492 SizeType linearize(const T &) const {
493 return 0;
494 }
Kern Handae1570262015-09-25 00:42:38 -0700495 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700496 ptrdiff_t contains(const T &) const {
497 return 0;
498 }
499
Neil MacIntoshd5316802015-09-30 21:54:08 -0700500 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700501 return TotalSize;
502 }
503
Neil MacIntoshd5316802015-09-30 21:54:08 -0700504 bool operator == (const BoundsRanges &) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700505 {
506 return true;
507 }
508 };
509
510 template <typename SizeType, size_t... RestRanges>
511 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
512 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700513 static const size_t Depth = Base::Depth + 1;
514 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700515 static const SizeType CurrentRange = dynamic_range;
516 static const SizeType TotalSize = dynamic_range;
517 const SizeType m_bound;
518
519 BoundsRanges (const BoundsRanges &) = default;
520 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
521 {
522 fail_fast_assert(0 <= *arr);
523 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
524 }
525 BoundsRanges() : m_bound(0) {}
526
527 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
Kosov Eugene3402b922015-09-28 21:20:02 +0300528 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool /* firstLevel */ = true) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700529 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
530 {
531 }
532
Kern Handae1570262015-09-25 00:42:38 -0700533 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700534 void serialize(T & arr) const {
535 arr[Dim] = elementNum();
536 this->Base::template serialize<T, Dim + 1>(arr);
537 }
Kern Handae1570262015-09-25 00:42:38 -0700538 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700539 SizeType linearize(const T & arr) const {
540 const size_t index = this->Base::totalSize() * arr[Dim];
541 fail_fast_assert(index < static_cast<size_t>(m_bound));
542 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
543 }
544
Kern Handae1570262015-09-25 00:42:38 -0700545 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700546 ptrdiff_t contains(const T & arr) const {
547 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
548 if (last == -1)
549 return -1;
550 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
551 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
552 }
553
Neil MacIntoshd5316802015-09-30 21:54:08 -0700554 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700555 return m_bound;
556 }
557
Neil MacIntoshd5316802015-09-30 21:54:08 -0700558 SizeType elementNum() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700559 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
560 }
561
Neil MacIntoshd5316802015-09-30 21:54:08 -0700562 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700563 if (dim > 0)
564 return this->Base::elementNum(dim - 1);
565 else
566 return elementNum();
567 }
568
Neil MacIntoshd5316802015-09-30 21:54:08 -0700569 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700570 {
571 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
572 }
573 };
574
575 template <typename SizeType, size_t CurRange, size_t... RestRanges>
576 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
577 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700578 static const size_t Depth = Base::Depth + 1;
579 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700580 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
581 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
582 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
583
584 BoundsRanges (const BoundsRanges &) = default;
585 BoundsRanges(const SizeType * const arr) : Base(arr) { }
586 BoundsRanges() = default;
587
588 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
589 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
590 {
591 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
592 }
593
Kern Handae1570262015-09-25 00:42:38 -0700594 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700595 void serialize(T & arr) const {
596 arr[Dim] = elementNum();
597 this->Base::template serialize<T, Dim + 1>(arr);
598 }
599
Kern Handae1570262015-09-25 00:42:38 -0700600 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700601 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700602 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700603 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
604 }
605
Kern Handae1570262015-09-25 00:42:38 -0700606 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700607 ptrdiff_t contains(const T & arr) const {
608 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
609 return -1;
610 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
611 if (last == -1)
612 return -1;
613 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
614 }
615
Neil MacIntoshd5316802015-09-30 21:54:08 -0700616 size_t totalSize() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700617 return CurrentRange * this->Base::totalSize();
618 }
619
Neil MacIntoshd5316802015-09-30 21:54:08 -0700620 SizeType elementNum() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700621 return CurrentRange;
622 }
623
Neil MacIntoshd5316802015-09-30 21:54:08 -0700624 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700625 if (dim > 0)
626 return this->Base::elementNum(dim - 1);
627 else
628 return elementNum();
629 }
630
Neil MacIntoshd5316802015-09-30 21:54:08 -0700631 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700632 {
633 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
634 }
635 };
636
637 template <typename SourceType, typename TargetType, size_t Rank>
638 struct BoundsRangeConvertible2;
639
640 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
641 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
642 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
643
644 template <size_t Rank, typename SourceType, typename TargetType>
645 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
646
647 template <typename SourceType, typename TargetType, size_t Rank>
648 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
649 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
650 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
651 {};
652
653 template <typename SourceType, typename TargetType>
654 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
655
656 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
657 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
658 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
659 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
660 {};
661 template <typename SourceType, typename TargetType>
662 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
663
664 template <typename TypeChain>
665 struct TypeListIndexer
666 {
667 const TypeChain & obj;
668 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700669 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700670 const TypeChain & getObj(std::true_type)
671 {
672 return obj;
673 }
Kern Handae1570262015-09-25 00:42:38 -0700674 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700675 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
676 {
677 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
678 }
Kern Handae1570262015-09-25 00:42:38 -0700679 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700680 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
681 {
682 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
683 }
684 };
685
686 template <typename TypeChain>
687 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
688 {
689 return TypeListIndexer<TypeChain>(obj);
690 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700691
692 template <size_t Rank, typename ValueType, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1, ValueType>>>
693 constexpr Ret shift_left(const index<Rank, ValueType>& other) noexcept
694 {
695 Ret ret;
696 for (size_t i = 0; i < Rank - 1; ++i)
697 {
698 ret[i] = other[i + 1];
699 }
700 return ret;
701 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700702}
703
704template <typename IndexType>
705class bounds_iterator;
706
707template <typename SizeType, size_t... Ranges>
708class static_bounds {
709public:
Kosov Eugene3402b922015-09-28 21:20:02 +0300710 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700711 }
712};
713
714template <typename SizeType, size_t FirstRange, size_t... RestRanges>
715class static_bounds<SizeType, FirstRange, RestRanges...>
716{
717 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
718 static_assert(std::is_integral<SizeType>::value
719 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
720
721 MyRanges m_ranges;
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700722 constexpr static_bounds(const MyRanges & range) : m_ranges(range) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700723
724 template <typename SizeType2, size_t... Ranges2>
725 friend class static_bounds;
726public:
Kern Handae1570262015-09-25 00:42:38 -0700727 static const size_t rank = MyRanges::Depth;
728 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700729 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
730
731 using size_type = SizeType;
732 using index_type = index<rank, size_type>;
733 using iterator = bounds_iterator<index_type>;
734 using const_iterator = bounds_iterator<index_type>;
735 using difference_type = ptrdiff_t;
736 using sliced_type = static_bounds<SizeType, RestRanges...>;
737 using mapping_type = contiguous_mapping_tag;
738public:
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700739 constexpr static_bounds(const static_bounds &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700740
741 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
742 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700743 constexpr static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700744 m_ranges(other.m_ranges)
745 {
746 }
747
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700748 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700749 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700750 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
751 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 -0700752 }
753
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700754 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700755
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700756 constexpr static_bounds & operator = (const static_bounds & otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700757 {
758 new(&m_ranges) MyRanges (otherBounds.m_ranges);
759 return *this;
760 }
761
Neil MacIntoshd5316802015-09-30 21:54:08 -0700762 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700763 {
764 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
765 }
766
Neil MacIntoshd5316802015-09-30 21:54:08 -0700767 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700768 {
769 return rank > 1 ? slice().size() : 1;
770 }
771
Neil MacIntoshd5316802015-09-30 21:54:08 -0700772 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700773 {
774 return static_cast<size_type>(m_ranges.totalSize());
775 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700776
Neil MacIntoshd5316802015-09-30 21:54:08 -0700777 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700778 {
779 return static_cast<size_type>(m_ranges.totalSize());
780 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700781
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700782 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700783 {
784 return m_ranges.linearize(idx);
785 }
786
Neil MacIntoshd5316802015-09-30 21:54:08 -0700787 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700788 {
789 return m_ranges.contains(idx) != -1;
790 }
791
Neil MacIntoshd5316802015-09-30 21:54:08 -0700792 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700793 {
794 return m_ranges.elementNum(index);
795 }
796
Kern Handae1570262015-09-25 00:42:38 -0700797 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700798 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700799 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700800 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700801 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
802 }
803
Neil MacIntoshd5316802015-09-30 21:54:08 -0700804 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700805 {
Anna Gringauzefdf86432015-10-14 10:46:22 -0700806 size_type extents[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700807 m_ranges.serialize(extents);
Anna Gringauzedb384972015-10-05 12:34:23 -0700808 return{ extents };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700809 }
810
811 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700812 constexpr bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700813 {
814 return this->size() == rhs.size();
815 }
816
817 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700818 constexpr bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700819 {
820 return !(*this == rhs);
821 }
822
Neil MacIntoshd5316802015-09-30 21:54:08 -0700823 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700824 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700825 return const_iterator(*this);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700826 }
827
Neil MacIntoshd5316802015-09-30 21:54:08 -0700828 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700829 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700830 return const_iterator(*this, this->index_bounds());
831 }
832};
833
Kern Handae1570262015-09-25 00:42:38 -0700834template <size_t Rank, typename SizeType = size_t>
Anna Gringauzedb384972015-10-05 12:34:23 -0700835class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700836{
Kern Handae1570262015-09-25 00:42:38 -0700837 template <size_t OtherRank, typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700838 friend class strided_bounds;
839
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700840public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700841 static const size_t rank = Rank;
Anna Gringauzefdf86432015-10-14 10:46:22 -0700842 using reference = SizeType&;
843 using const_reference = const SizeType&;
844 using size_type = SizeType;
845 using difference_type = SizeType;
846 using value_type = SizeType;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700847 using index_type = index<rank, size_type>;
848 using iterator = bounds_iterator<index_type>;
849 using const_iterator = bounds_iterator<index_type>;
850 static const int dynamic_rank = rank;
851 static const size_t static_size = dynamic_range;
852 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
853 using mapping_type = generalized_mapping_tag;
Anna Gringauzedb384972015-10-05 12:34:23 -0700854 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700855
856 template <typename OtherSizeType>
Anna Gringauzedb384972015-10-05 12:34:23 -0700857 constexpr strided_bounds(const strided_bounds<rank, OtherSizeType> &other) noexcept
858 : m_extents(other.extents), m_strides(other.strides)
859 {}
860 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
861 : m_extents(extents), m_strides(strides)
862 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -0700863 constexpr index_type strides() const noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700864 {
865 return m_strides;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700866 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700867 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700868 {
869 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700870 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700871 {
872 ret += (m_extents[i] - 1) * m_strides[i];
873 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700874 return ret + 1;
875 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700876 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700877 {
878 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700879 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700880 {
881 ret *= m_extents[i];
882 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700883 return ret;
884 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700885 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700886 {
Kern Handae1570262015-09-25 00:42:38 -0700887 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700888 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700889 if (idx[i] < 0 || idx[i] >= m_extents[i])
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700890 return false;
891 }
892 return true;
893 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700894 constexpr size_type linearize(const index_type & idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700895 {
896 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700897 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700898 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700899 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700900 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700901 }
902 return ret;
903 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700904 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700905 {
906 return m_strides[0];
907 }
908 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700909 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700910 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700911 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700912 }
Kern Handae1570262015-09-25 00:42:38 -0700913 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700914 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700915 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700916 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Anna Gringauzedb384972015-10-05 12:34:23 -0700917 return m_extents[Dim];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700918 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700919 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700920 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700921 return m_extents;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700922 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700923 const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700924 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700925 return const_iterator{ *this };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700926 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700927 const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700928 {
929 return const_iterator{ *this, index_bounds() };
930 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700931private:
Anna Gringauzedb384972015-10-05 12:34:23 -0700932 index_type m_extents;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700933 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700934};
935
936template <typename T>
937struct is_bounds : std::integral_constant<bool, false> {};
938template <typename SizeType, size_t... Ranges>
939struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
Kern Handae1570262015-09-25 00:42:38 -0700940template <size_t Rank, typename SizeType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700941struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
942
943template <typename IndexType>
944class bounds_iterator
945 : public std::iterator<std::random_access_iterator_tag,
946 IndexType,
947 ptrdiff_t,
948 const details::arrow_proxy<IndexType>,
949 const IndexType>
950{
951private:
952 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
953public:
Kern Handae1570262015-09-25 00:42:38 -0700954 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700955 using typename Base::reference;
956 using typename Base::pointer;
957 using typename Base::difference_type;
958 using typename Base::value_type;
959 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700960 using index_size_type = typename IndexType::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700961 template <typename Bounds>
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700962 explicit bounds_iterator(const Bounds& bnd, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700963 : boundary(bnd.index_bounds())
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700964 , curr(std::move(curr))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700965 {
966 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
967 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700968 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700969 {
970 return curr;
971 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700972 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700973 {
974 return details::arrow_proxy<value_type>{ curr };
975 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700976 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700977 {
Kern Handae1570262015-09-25 00:42:38 -0700978 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700979 {
980 if (++curr[i] < boundary[i])
981 {
982 return *this;
983 }
984 else
985 {
986 curr[i] = 0;
987 }
988 }
989 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -0700990 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700991 {
992 curr[i] = boundary[i];
993 }
994 return *this;
995 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700996 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700997 {
998 auto ret = *this;
999 ++(*this);
1000 return ret;
1001 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001002 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001003 {
Neil MacIntoshfb913932015-09-27 16:25:43 -07001004 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001005 {
1006 if (curr[i]-- > 0)
1007 {
1008 return *this;
1009 }
1010 else
1011 {
1012 curr[i] = boundary[i] - 1;
1013 }
1014 }
1015 // If we're here the preconditions were violated
1016 // "pre: there exists s such that r == ++s"
1017 fail_fast_assert(false);
1018 return *this;
1019 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001020 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001021 {
1022 auto ret = *this;
1023 --(*this);
1024 return ret;
1025 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001026 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001027 {
1028 bounds_iterator ret{ *this };
1029 return ret += n;
1030 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001031 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001032 {
1033 auto linear_idx = linearize(curr) + n;
1034 value_type stride;
1035 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -07001036 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001037 {
1038 stride[i] = stride[i + 1] * boundary[i + 1];
1039 }
Kern Handae1570262015-09-25 00:42:38 -07001040 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001041 {
1042 curr[i] = linear_idx / stride[i];
1043 linear_idx = linear_idx % stride[i];
1044 }
1045 return *this;
1046 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001047 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001048 {
1049 bounds_iterator ret{ *this };
1050 return ret -= n;
1051 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001052 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001053 {
1054 return *this += -n;
1055 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001056 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001057 {
1058 return linearize(curr) - linearize(rhs.curr);
1059 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001060 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001061 {
1062 return *(*this + n);
1063 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001064 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001065 {
1066 return curr == rhs.curr;
1067 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001068 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001069 {
1070 return !(*this == rhs);
1071 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001072 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001073 {
Kern Handae1570262015-09-25 00:42:38 -07001074 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001075 {
1076 if (curr[i] < rhs.curr[i])
1077 return true;
1078 }
1079 return false;
1080 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001081 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001082 {
1083 return !(rhs < *this);
1084 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001085 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001086 {
1087 return rhs < *this;
1088 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001089 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001090 {
1091 return !(rhs > *this);
1092 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001093 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001094 {
1095 std::swap(boundary, rhs.boundary);
1096 std::swap(curr, rhs.curr);
1097 }
1098private:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001099 index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001100 {
1101 // TODO: Smarter impl.
1102 // Check if past-the-end
1103 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001104 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001105 {
1106 if (idx[i] != boundary[i])
1107 {
1108 pte = false;
1109 break;
1110 }
1111 }
1112 index_size_type multiplier = 1;
1113 index_size_type res = 0;
1114 if (pte)
1115 {
1116 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001117 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001118 {
1119 res += (idx[i] - 1) * multiplier;
1120 multiplier *= boundary[i];
1121 }
1122 }
1123 else
1124 {
Kern Handae1570262015-09-25 00:42:38 -07001125 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001126 {
1127 res += idx[i] * multiplier;
1128 multiplier *= boundary[i];
1129 }
1130 }
1131 return res;
1132 }
1133 value_type boundary;
1134 value_type curr;
1135};
1136
1137template <typename SizeType>
1138class bounds_iterator<index<1, SizeType>>
1139 : public std::iterator<std::random_access_iterator_tag,
1140 index<1, SizeType>,
1141 ptrdiff_t,
1142 const details::arrow_proxy<index<1, SizeType>>,
1143 const index<1, SizeType>>
1144{
1145 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>>;
1146
1147public:
1148 using typename Base::reference;
1149 using typename Base::pointer;
1150 using typename Base::difference_type;
1151 using typename Base::value_type;
1152 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -07001153 using index_size_type = typename index_type::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001154
1155 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001156 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001157 : curr( std::move(curr) )
1158 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -07001159 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001160 {
1161 return curr;
1162 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001163 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001164 {
1165 return details::arrow_proxy<value_type>{ curr };
1166 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001167 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001168 {
1169 ++curr;
1170 return *this;
1171 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001172 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001173 {
1174 auto ret = *this;
1175 ++(*this);
1176 return ret;
1177 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001178 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001179 {
1180 curr--;
1181 return *this;
1182 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001183 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001184 {
1185 auto ret = *this;
1186 --(*this);
1187 return ret;
1188 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001189 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001190 {
1191 bounds_iterator ret{ *this };
1192 return ret += n;
1193 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001194 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001195 {
1196 curr += n;
1197 return *this;
1198 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001199 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001200 {
1201 bounds_iterator ret{ *this };
1202 return ret -= n;
1203 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001204 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001205 {
1206 return *this += -n;
1207 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001208 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001209 {
1210 return curr[0] - rhs.curr[0];
1211 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001212 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001213 {
1214 return curr + n;
1215 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001216 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001217 {
1218 return curr == rhs.curr;
1219 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001220 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001221 {
1222 return !(*this == rhs);
1223 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001224 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001225 {
1226 return curr[0] < rhs.curr[0];
1227 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001228 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001229 {
1230 return !(rhs < *this);
1231 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001232 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001233 {
1234 return rhs < *this;
1235 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001236 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001237 {
1238 return !(rhs > *this);
1239 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001240 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001241 {
1242 std::swap(curr, rhs.curr);
1243 }
1244private:
1245 value_type curr;
1246};
1247
1248template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001249bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001250{
1251 return rhs + n;
1252}
1253
1254/*
1255** begin definitions of basic_array_view
1256*/
1257namespace details
1258{
1259 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001260 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
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001261 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001262 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001263 }
1264
Neil MacIntosh99746e22015-09-27 16:53:58 -07001265 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001266 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001267 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
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001268 {
1269 auto extents = bnd.index_bounds();
Anna Gringauzefdf86432015-10-14 10:46:22 -07001270 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001271
1272 stride[Bounds::rank - 1] = 1;
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001273 for (size_t i = 1; i < Bounds::rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -07001274 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001275 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
Anna Gringauzedb384972015-10-05 12:34:23 -07001276 }
1277 return{ stride };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001278 }
1279
1280 template <typename BoundsSrc, typename BoundsDest>
1281 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1282 {
1283 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1284 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1285 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");
1286 fail_fast_assert(src.size() == dest.size());
1287 }
1288
1289
1290} // namespace details
1291
1292template <typename ArrayView>
1293class contiguous_array_view_iterator;
1294template <typename ArrayView>
1295class general_array_view_iterator;
1296enum class byte : std::uint8_t {};
1297
1298template <typename ValueType, typename BoundsType>
1299class basic_array_view
1300{
1301public:
Kern Handae1570262015-09-25 00:42:38 -07001302 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001303 using bounds_type = BoundsType;
1304 using size_type = typename bounds_type::size_type;
1305 using index_type = typename bounds_type::index_type;
1306 using value_type = ValueType;
1307 using pointer = ValueType*;
1308 using reference = ValueType&;
1309 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>>;
1310 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>>>;
1311 using reverse_iterator = std::reverse_iterator<iterator>;
1312 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1313 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1314
1315private:
1316 pointer m_pdata;
1317 bounds_type m_bounds;
1318
1319public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001320 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001321 {
1322 return m_bounds;
1323 }
Kern Handae1570262015-09-25 00:42:38 -07001324 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001325 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001326 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001327 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001328 return m_bounds.template extent<Dim>();
1329 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001330 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001331 {
1332 return m_bounds.size();
1333 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001334 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001335 {
1336 return m_pdata[m_bounds.linearize(idx)];
1337 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001338 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001339 {
1340 return m_pdata;
1341 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001342 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001343 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001344 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001345 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001346 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001347
1348 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001349 return Ret {m_pdata + ridx, m_bounds.slice()};
1350 }
1351
Neil MacIntoshd5316802015-09-30 21:54:08 -07001352 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001353 {
1354 return m_pdata != nullptr;
1355 }
1356
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001357 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001358 {
1359 return iterator {this, true};
1360 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001361 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001362 {
1363 return iterator {this};
1364 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001365 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001366 {
1367 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1368 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001369 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001370 {
1371 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1372 }
1373
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001374 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001375 {
1376 return reverse_iterator {end()};
1377 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001378 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001379 {
1380 return reverse_iterator {begin()};
1381 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001382 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001383 {
1384 return const_reverse_iterator {cend()};
1385 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001386 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001387 {
1388 return const_reverse_iterator {cbegin()};
1389 }
1390
1391 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 MacIntoshd5316802015-09-30 21:54:08 -07001392 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001393 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001394 return m_bounds.size() == other.m_bounds.size() &&
1395 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001396 }
1397
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001398 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 MacIntoshd5316802015-09-30 21:54:08 -07001399 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001400 {
1401 return !(*this == other);
1402 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001403
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001404 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 MacIntoshd5316802015-09-30 21:54:08 -07001405 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001406 {
1407 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1408 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001409
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001410 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 MacIntoshd5316802015-09-30 21:54:08 -07001411 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001412 {
1413 return !(other < *this);
1414 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001415
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001416 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 MacIntoshd5316802015-09-30 21:54:08 -07001417 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001418 {
1419 return (other < *this);
1420 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001421
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001422 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 MacIntoshd5316802015-09-30 21:54:08 -07001423 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001424 {
1425 return !(*this < other);
1426 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001427
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001428public:
1429 template <typename OtherValueType, typename OtherBounds,
1430 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1431 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001432 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001433 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1434 {
1435 }
1436protected:
1437
Neil MacIntoshd5316802015-09-30 21:54:08 -07001438 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001439 : m_pdata(data)
1440 , m_bounds(std::move(bound))
1441 {
1442 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1443 }
1444 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001445 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
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001446 : m_pdata(reinterpret_cast<pointer>(data))
1447 , m_bounds(std::move(bound))
1448 {
1449 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1450 }
1451 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001452 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001453 {
1454 details::verifyBoundsReshape(m_bounds, bounds);
1455 return {m_pdata, bounds};
1456 }
1457private:
1458
1459 friend iterator;
1460 friend const_iterator;
1461 template <typename ValueType2, typename BoundsType2>
1462 friend class basic_array_view;
1463};
1464
1465template <size_t DimSize = dynamic_range>
1466struct dim
1467{
1468 static const size_t value = DimSize;
1469};
1470template <>
1471struct dim<dynamic_range>
1472{
1473 static const size_t value = dynamic_range;
1474 const size_t dvalue;
1475 dim(size_t size) : dvalue(size) {}
1476};
1477
1478template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1479class array_view;
Kern Handae1570262015-09-25 00:42:38 -07001480template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001481class strided_array_view;
1482
1483namespace details
1484{
1485 template <typename T, typename = std::true_type>
1486 struct ArrayViewTypeTraits
1487 {
1488 using value_type = T;
1489 using size_type = size_t;
1490 };
1491
1492 template <typename Traits>
1493 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1494 {
1495 using value_type = typename Traits::array_view_traits::value_type;
1496 using size_type = typename Traits::array_view_traits::size_type;
1497 };
1498
1499 template <typename T, typename SizeType, size_t... Ranks>
1500 struct ArrayViewArrayTraits {
1501 using type = array_view<T, Ranks...>;
1502 using value_type = T;
1503 using bounds_type = static_bounds<SizeType, Ranks...>;
1504 using pointer = T*;
1505 using reference = T&;
1506 };
1507 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1508 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1509
1510 template <typename BoundsType>
1511 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1512 {
1513 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1514 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1515 }
1516 template <typename BoundsType>
1517 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1518 {
1519 fail_fast_assert(BoundsType::static_size == totalSize);
1520 return {};
1521 }
1522 template <typename BoundsType>
1523 BoundsType newBoundsHelper(size_t totalSize)
1524 {
1525 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1526 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1527 }
1528
1529 struct Sep{};
1530
1531 template <typename T, typename... Args>
1532 T static_as_array_view_helper(Sep, Args... args)
1533 {
1534 return T{static_cast<typename T::size_type>(args)...};
1535 }
1536 template <typename T, typename Arg, typename... Args>
1537 std::enable_if_t<!std::is_same<Arg, dim<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T> static_as_array_view_helper(Arg, Args... args)
1538 {
1539 return static_as_array_view_helper<T>(args...);
1540 }
1541 template <typename T, typename... Args>
1542 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1543 {
1544 return static_as_array_view_helper<T>(args..., val.dvalue);
1545 }
1546
1547 template <typename SizeType, typename ...Dimensions>
1548 struct static_as_array_view_static_bounds_helper
1549 {
1550 using type = static_bounds<SizeType, (Dimensions::value)...>;
1551 };
1552
1553 template <typename T>
1554 struct is_array_view_oracle : std::false_type
1555 {};
1556 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1557 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1558 {};
Kern Handae1570262015-09-25 00:42:38 -07001559 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001560 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1561 {};
1562 template <typename T>
1563 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1564 {};
1565
1566}
1567
1568
1569template <typename ValueType, typename SizeType>
1570struct array_view_options
1571{
1572 struct array_view_traits
1573 {
1574 using value_type = ValueType;
1575 using size_type = SizeType;
1576 };
1577};
1578
1579template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1580class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1581 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1582{
1583 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1584 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001585 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001586 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001587
1588public:
1589 using typename Base::bounds_type;
1590 using typename Base::size_type;
1591 using typename Base::pointer;
1592 using typename Base::value_type;
1593 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001594 using typename Base::iterator;
1595 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001596 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001597 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001598
1599public:
1600 // basic
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001601 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001602 {
1603 }
1604
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001605 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001606 {
1607 }
1608
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001609 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001610 {
1611 fail_fast_assert(size == 0);
1612 }
1613
1614 // default
1615 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001616 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001617 {
1618 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001619
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001620 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1621 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001622 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1623 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001624 constexpr array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001625 {
1626 }
1627
1628 // from n-dimensions static array
1629 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1630 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 -07001631 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001632 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001633 {
1634 }
1635
1636 // from n-dimensions static array with size
1637 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1638 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 -07001639 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001640 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001641 {
1642 fail_fast_assert(size <= N);
1643 }
1644
1645 // from std array
1646 template <size_t N, typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<size_type, N>, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001647 constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001648 {
1649 }
1650
1651 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>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001652 constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<size_type, N>())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001653 {
1654 }
1655
1656
1657 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1658 template <typename Ptr,
1659 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1660 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001661 constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001662 {
1663 }
1664
1665 // from containers. It must has .size() and .data() two function signatures
1666 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1667 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001668 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001669 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1670 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001671 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001672 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 -07001673 {
1674
1675 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001676
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001677 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001678
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001679 // convertible
1680 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1681 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1682 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1683 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1684 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001685 constexpr array_view(const array_view<OtherValueTypeOpt, OtherDimensions...> &av) : Base(static_cast<const typename array_view<OtherValueTypeOpt, OtherDimensions...>::Base &>(av)) {} // static_cast is required
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001686
1687 // reshape
1688 template <typename... Dimensions2>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001689 constexpr array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001690 {
1691 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001692 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001693 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1694 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001695 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001696 }
1697
1698 // to bytes array
1699 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001700 constexpr auto as_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001701 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)>
1702 {
1703 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001704 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001705 }
1706
1707 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001708 constexpr auto as_writeable_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001709 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)>
1710 {
1711 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001712 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001713 }
1714
Anna Gringauze18cd9802015-09-14 16:34:26 -07001715
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001716 // from bytes array
1717 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001718 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))>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001719 {
1720 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1721 "Target type must be standard layout and its size must match the byte array size");
1722 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001723 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001724 }
1725
1726 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001727 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))>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001728 {
1729 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1730 "Target type must be standard layout and its size must match the byte array size");
1731 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001732 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001733 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001734
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001735 // section on linear space
1736 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001737 constexpr array_view<ValueTypeOpt, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001738 {
1739 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1740 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 -07001741 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001742 }
1743
Neil MacIntoshd5316802015-09-30 21:54:08 -07001744 constexpr array_view<ValueTypeOpt, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001745 {
1746 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001747 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001748 }
1749
1750 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001751 constexpr array_view<ValueTypeOpt, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001752 {
1753 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1754 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001755 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 }
1757
Neil MacIntoshd5316802015-09-30 21:54:08 -07001758 constexpr array_view<ValueTypeOpt, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001759 {
1760 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001761 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001762 }
1763
1764 template<size_t Offset, size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001765 constexpr array_view<ValueTypeOpt, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001766 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001767 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");
1768 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 -07001769 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001770 }
1771
Neil MacIntoshd5316802015-09-30 21:54:08 -07001772 constexpr array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001773 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001774 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1775 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001776 }
1777
1778 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001779 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001780 {
1781 return this->size();
1782 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001783 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001784 {
1785 return length();
1786 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001787 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001788 {
1789 return sizeof(value_type) * this->size();
1790 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001791 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001792 {
1793 return bytes();
1794 }
1795
1796 // section
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001797 constexpr strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001798 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001799 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001800 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1801 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001802
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001803 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001804 {
1805 return Base::operator[](idx);
1806 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001807
Anna Gringauze1a864982015-09-14 18:55:06 -07001808 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001809 constexpr array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001810 {
1811 auto ret = Base::operator[](idx);
1812 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001813 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001814
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001815 using Base::operator==;
1816 using Base::operator!=;
1817 using Base::operator<;
1818 using Base::operator<=;
1819 using Base::operator>;
1820 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001821};
1822
1823template <typename T, size_t... Dimensions>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001824constexpr auto as_array_view(T * const & ptr, dim<Dimensions>... args) -> array_view<std::remove_all_extents_t<T>, Dimensions...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001825{
1826 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1827}
1828
1829template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001830constexpr auto as_array_view (T * arr, size_t len) -> typename details::ArrayViewArrayTraits<T, size_t, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001831{
1832 return {arr, len};
1833}
1834
1835template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001836constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001837{
1838 return {arr};
1839}
1840
1841template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001842constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001843{
1844 return {arr};
1845}
1846
1847template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001848constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001849
1850template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001851constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001852{
1853 return {arr};
1854}
1855
1856template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001857constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001858{
1859 return {begin, end};
1860}
1861
1862template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001863constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001864 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1865{
1866 return {arr.data(), arr.size()};
1867}
1868
1869template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001870constexpr auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001871 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1872
Kern Handae1570262015-09-25 00:42:38 -07001873template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001874class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1875{
1876 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 -07001877
Kern Handae1570262015-09-25 00:42:38 -07001878 template<typename OtherValueOpt, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001879 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001880public:
1881 using Base::rank;
1882 using typename Base::bounds_type;
1883 using typename Base::size_type;
1884 using typename Base::pointer;
1885 using typename Base::value_type;
1886 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001887 using typename Base::iterator;
1888 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001889 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001890
1891 // from static array of size N
1892 template<size_type N>
1893 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1894 {
1895 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1896 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001897
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001898 // from raw data
1899 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001900 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001901 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001902 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001903
1904 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001905 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001906 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1907 {
1908 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1909 }
1910
1911 // convertible
1912 template <typename OtherValueTypeOpt,
1913 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1914 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1915 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1916 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001917 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 -07001918 {
1919 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001920
1921 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001922 template <typename OtherValueType>
1923 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 -07001924 {
1925 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1926 auto d = sizeof(OtherValueType) / sizeof(value_type);
1927
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001928 size_type size = this->bounds().total_size() / d;
1929 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 -07001930 }
1931
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001932 strided_array_view section(index_type origin, index_type extents) const
1933 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001934 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001935 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1936 }
1937
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001938 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001939 {
1940 return Base::operator[](idx);
1941 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001942
1943 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001944 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001945 {
1946 auto ret = Base::operator[](idx);
1947 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1948 }
1949
1950private:
1951 static index_type resize_extent(const index_type& extent, size_t d)
1952 {
1953 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");
1954
1955 index_type ret = extent;
1956 ret[rank - 1] /= d;
1957
1958 return ret;
1959 }
1960
1961 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Kosov Eugene3402b922015-09-28 21:20:02 +03001962 static index_type resize_stride(const index_type& strides, size_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001963 {
1964 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1965
1966 return strides;
1967 }
1968
1969 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1970 static index_type resize_stride(const index_type& strides, size_t d)
1971 {
1972 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1973 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");
1974
Neil MacIntosh99746e22015-09-27 16:53:58 -07001975 for (size_t i = rank - 1; i > 0; --i)
1976 fail_fast_assert((strides[i-1] >= strides[i]) && (strides[i-1] % strides[i] == 0), "Only strided arrays with regular strides can be resized");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001977
1978 index_type ret = strides / d;
1979 ret[rank - 1] = 1;
1980
1981 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001982 }
1983};
1984
1985template <typename ArrayView>
1986class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1987{
1988 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1989public:
1990 using typename Base::reference;
1991 using typename Base::pointer;
1992 using typename Base::difference_type;
1993private:
1994 template <typename ValueType, typename Bounds>
1995 friend class basic_array_view;
1996 pointer m_pdata;
1997 const ArrayView * m_validator;
1998 void validateThis() const
1999 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07002000 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 -07002001 }
2002 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2003 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2004public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002005 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002006 {
2007 validateThis();
2008 return *m_pdata;
2009 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002010 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002011 {
2012 validateThis();
2013 return m_pdata;
2014 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002015 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002016 {
2017 ++m_pdata;
2018 return *this;
2019 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002020 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002021 {
2022 auto ret = *this;
2023 ++(*this);
2024 return ret;
2025 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002026 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002027 {
2028 --m_pdata;
2029 return *this;
2030 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002031 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002032 {
2033 auto ret = *this;
2034 --(*this);
2035 return ret;
2036 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002037 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002038 {
2039 contiguous_array_view_iterator ret{ *this };
2040 return ret += n;
2041 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002042 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002043 {
2044 m_pdata += n;
2045 return *this;
2046 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002047 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002048 {
2049 contiguous_array_view_iterator ret{ *this };
2050 return ret -= n;
2051 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002052 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002053 {
2054 return *this += -n;
2055 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002056 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002057 {
2058 fail_fast_assert(m_validator == rhs.m_validator);
2059 return m_pdata - rhs.m_pdata;
2060 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002061 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002062 {
2063 return *(*this + n);
2064 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002065 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002066 {
2067 fail_fast_assert(m_validator == rhs.m_validator);
2068 return m_pdata == rhs.m_pdata;
2069 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002070 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002071 {
2072 return !(*this == rhs);
2073 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002074 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002075 {
2076 fail_fast_assert(m_validator == rhs.m_validator);
2077 return m_pdata < rhs.m_pdata;
2078 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002079 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002080 {
2081 return !(rhs < *this);
2082 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002083 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002084 {
2085 return rhs < *this;
2086 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002087 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002088 {
2089 return !(rhs > *this);
2090 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002091 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002092 {
2093 std::swap(m_pdata, rhs.m_pdata);
2094 std::swap(m_validator, rhs.m_validator);
2095 }
2096};
2097
2098template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002099contiguous_array_view_iterator<ArrayView> operator+(typename contiguous_array_view_iterator<ArrayView>::difference_type n, const contiguous_array_view_iterator<ArrayView>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002100{
2101 return rhs + n;
2102}
2103
2104template <typename ArrayView>
2105class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2106{
2107 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2108public:
2109 using typename Base::reference;
2110 using typename Base::pointer;
2111 using typename Base::difference_type;
2112 using typename Base::value_type;
2113private:
2114 template <typename ValueType, typename Bounds>
2115 friend class basic_array_view;
2116 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002117 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002118 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2119 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2120 {
2121 }
2122public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002123 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002124 {
2125 return (*m_container)[*m_itr];
2126 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002127 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002128 {
2129 return &(*m_container)[*m_itr];
2130 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002131 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002132 {
2133 ++m_itr;
2134 return *this;
2135 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002136 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002137 {
2138 auto ret = *this;
2139 ++(*this);
2140 return ret;
2141 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002142 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002143 {
2144 --m_itr;
2145 return *this;
2146 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002147 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002148 {
2149 auto ret = *this;
2150 --(*this);
2151 return ret;
2152 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002153 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002154 {
2155 general_array_view_iterator ret{ *this };
2156 return ret += n;
2157 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002158 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002159 {
2160 m_itr += n;
2161 return *this;
2162 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002163 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002164 {
2165 general_array_view_iterator ret{ *this };
2166 return ret -= n;
2167 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002168 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002169 {
2170 return *this += -n;
2171 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002172 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002173 {
2174 fail_fast_assert(m_container == rhs.m_container);
2175 return m_itr - rhs.m_itr;
2176 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002177 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002178 {
2179 return (*m_container)[m_itr[n]];;
2180 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002181 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002182 {
2183 fail_fast_assert(m_container == rhs.m_container);
2184 return m_itr == rhs.m_itr;
2185 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002186 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002187 {
2188 return !(*this == rhs);
2189 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002190 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002191 {
2192 fail_fast_assert(m_container == rhs.m_container);
2193 return m_itr < rhs.m_itr;
2194 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002195 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002196 {
2197 return !(rhs < *this);
2198 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002199 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002200 {
2201 return rhs < *this;
2202 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002203 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002204 {
2205 return !(rhs > *this);
2206 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002207 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002208 {
2209 std::swap(m_itr, rhs.m_itr);
2210 std::swap(m_container, rhs.m_container);
2211 }
2212};
2213
2214template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002215general_array_view_iterator<ArrayView> operator+(typename general_array_view_iterator<ArrayView>::difference_type n, const general_array_view_iterator<ArrayView>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002216{
2217 return rhs + n;
2218}
2219
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002220} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002221
Neil MacIntoshd5316802015-09-30 21:54:08 -07002222#ifdef _MSC_VER
2223
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002224#undef constexpr
2225#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002226
Neil MacIntoshd5316802015-09-30 21:54:08 -07002227#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002228#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002229
2230#ifndef GSL_THROWS_FOR_TESTING
2231#pragma undef noexcept
2232#endif // GSL_THROWS_FOR_TESTING
2233
Neil MacIntosh9a297122015-09-14 15:11:07 -07002234#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002235
Neil MacIntoshd5316802015-09-30 21:54:08 -07002236#endif // _MSC_VER
2237
2238#if defined(GSL_THROWS_FOR_TESTING)
2239#undef noexcept
2240#endif // GSL_THROWS_FOR_TESTING
2241
Treb Connell51da1362015-09-24 18:08:34 -07002242
2243#endif // GSL_ARRAY_VIEW_H