blob: c884c119af56f0d6ebf6dff94f4bd37abe57a16f [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 MacIntosha9dcbe02015-08-20 18:09:14 -070033#include "fail_fast.h"
34
Neil MacIntoshd5316802015-09-30 21:54:08 -070035#ifdef _MSC_VER
36
37// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070038#pragma push_macro("constexpr")
39#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070040
41
42// VS 2013 workarounds
43#if _MSC_VER <= 1800
44
45// noexcept is not understood
46#ifndef GSL_THROWS_FOR_TESTING
47#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070048#endif
49
Neil MacIntoshd5316802015-09-30 21:54:08 -070050// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070051#pragma warning(push)
52#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntoshd5316802015-09-30 21:54:08 -070053
Neil MacIntosh9a297122015-09-14 15:11:07 -070054#endif // _MSC_VER <= 1800
55
Neil MacIntoshd5316802015-09-30 21:54:08 -070056#endif // _MSC_VER
57
58// In order to test the library, we need it to throw exceptions that we can catch
59#ifdef GSL_THROWS_FOR_TESTING
60#define noexcept /* nothing */
61#endif // GSL_THROWS_FOR_TESTING
62
63
Neil MacIntoshef626fd2015-09-29 16:41:37 -070064namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070065
66/*
67** begin definitions of index and bounds
68*/
69namespace details
70{
71 template <typename SizeType>
72 struct SizeTypeTraits
73 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -070074 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 -070075 };
76
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070077 template <typename T>
78 class arrow_proxy
79 {
80 public:
81 explicit arrow_proxy(T t)
82 : val(t)
83 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -070084 const T operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070085 {
86 return val;
87 }
Neil MacIntoshd5316802015-09-30 21:54:08 -070088 const T* operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070089 {
90 return &val;
91 }
92 private:
93 T val;
94 };
95}
96
Kern Handae1570262015-09-25 00:42:38 -070097template <size_t Rank, typename ValueType = size_t>
Anna Gringauzedb384972015-10-05 12:34:23 -070098class index final
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070099{
Anna Gringauzedb384972015-10-05 12:34:23 -0700100 static_assert(std::is_integral<ValueType>::value, "ValueType must be an integral type!");
101 static_assert(Rank > 0, "Rank must be greater than 0!");
102
Kern Handae1570262015-09-25 00:42:38 -0700103 template <size_t OtherRank, typename OtherValueType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700104 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -0700105
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700106public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700107 static const size_t rank = Rank;
108 using value_type = std::remove_reference_t<ValueType>;
109 using reference = std::add_lvalue_reference_t<value_type>;
110 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700111
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700112 constexpr index() noexcept
113 {}
114
Anna Gringauzedb384972015-10-05 12:34:23 -0700115 constexpr index(const value_type(&values)[Rank]) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700116 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700117 std::copy(values, values + Rank, elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700118 }
119
Anna Gringauzedb384972015-10-05 12:34:23 -0700120 // Preconditions: il.size() == rank
121 constexpr index(std::initializer_list<value_type> il) noexcept
122 {
123 fail_fast_assert(il.size() == Rank, "The size of the initializer list must match the rank of the array");
124 std::copy(begin(il), end(il), elems);
125 }
126
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700127 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700128
129 // copy from index over smaller domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700130 template <typename OtherValueType,
131 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
132 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
133 constexpr index(const index<Rank, OtherValueType>& other) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700134 {
135 std::copy(other.elems, other.elems + Rank, elems);
136 }
137
138 // copy from index over larger domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700139 template <typename OtherValueType,
140 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
141 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
142 constexpr index(const index<Rank, OtherValueType>& other, void* ptr = 0) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700143 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700144 bool ok = std::accumulate(other.elems, other.elems + Rank, true,
145 [&](bool b, OtherValueType val) { return b && (val <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value)); }
146 );
Anna Gringauzedb384972015-10-05 12:34:23 -0700147
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700148 fail_fast_assert(ok, "other value must fit in the new domain");
149 std::transform(other.elems, other.elems + rank, elems, [&](OtherValueType val) { return static_cast<value_type>(val); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700150 }
151
152 constexpr index& operator=(const index& rhs) noexcept = default;
153
154 // Preconditions: component_idx < rank
155 constexpr reference operator[](size_t component_idx)
156 {
157 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
158 return elems[component_idx];
159 }
160
161 // Preconditions: component_idx < rank
162 constexpr const_reference operator[](size_t component_idx) const noexcept
163 {
164 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
165 return elems[component_idx];
166 }
167
168 constexpr bool operator==(const index& rhs) const noexcept
169 {
170 return std::equal(elems, elems + rank, rhs.elems);
171 }
172
173 constexpr bool operator!=(const index& rhs) const noexcept
174 {
175 return !(this == rhs);
176 }
177
178 constexpr index operator+() const noexcept
179 {
180 return *this;
181 }
182
183 constexpr index operator-() const noexcept
184 {
185 index ret = *this;
186 std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
187 return ret;
188 }
189
190 constexpr index operator+(const index& rhs) const noexcept
191 {
192 index ret = *this;
193 ret += rhs;
194 return ret;
195 }
196
197 constexpr index operator-(const index& rhs) const noexcept
198 {
199 index ret = *this;
200 ret -= rhs;
201 return ret;
202 }
203
204 constexpr index& operator+=(const index& rhs) noexcept
205 {
206 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<ValueType>{});
207 return *this;
208 }
209
210 constexpr index& operator-=(const index& rhs) noexcept
211 {
212 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<ValueType>{});
213 return *this;
214 }
215
216 constexpr index operator*(value_type v) const noexcept
217 {
218 index ret = *this;
219 ret *= v;
220 return ret;
221 }
222
223 constexpr index operator/(value_type v) const noexcept
224 {
225 index ret = *this;
226 ret /= v;
227 return ret;
228 }
229
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700230 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700231 {
232 return rhs * v;
233 }
234
235 constexpr index& operator*=(value_type v) noexcept
236 {
237 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<ValueType>{}(x, v); });
238 return *this;
239 }
240
241 constexpr index& operator/=(value_type v) noexcept
242 {
243 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<ValueType>{}(x, v); });
244 return *this;
245 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700246
Anna Gringauzedb384972015-10-05 12:34:23 -0700247private:
248 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700249};
250
251template <typename ValueType>
252class index<1, ValueType>
253{
Kern Handae1570262015-09-25 00:42:38 -0700254 template <size_t, typename OtherValueType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700255 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -0700256
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700257public:
Kern Handae1570262015-09-25 00:42:38 -0700258 static const size_t rank = 1;
Anna Gringauzedb384972015-10-05 12:34:23 -0700259 using value_type = std::remove_reference_t<ValueType>;
260 using reference = std::add_lvalue_reference_t<value_type>;
261 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700262
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700263 constexpr index() noexcept : value(0)
Anna Gringauzedb384972015-10-05 12:34:23 -0700264 {}
265
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700266 constexpr index(value_type e) noexcept : value(e)
Anna Gringauzedb384972015-10-05 12:34:23 -0700267 {}
268
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700269 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
270 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700271
Anna Gringauzedb384972015-10-05 12:34:23 -0700272 constexpr index(const index &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700273
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700274 template <typename OtherValueType,
275 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
276 typename Other = std::enable_if_t<Enabled, index<1, OtherValueType>>>
277 constexpr index(const index<1, OtherValueType>& other) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700278 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700279 value = static_cast<ValueType>(other.value);
280 }
281
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700282 template <typename OtherValueType,
283 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
284 typename Other = std::enable_if_t<Enabled, index<1, OtherValueType>>>
285 constexpr index(const index<1, OtherValueType>& other, void* ptr=0) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700286 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700287 fail_fast_assert(other.value <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value));
Anna Gringauzedb384972015-10-05 12:34:23 -0700288 value = static_cast<value_type>(other.value);
289 }
290
Anna Gringauzedb384972015-10-05 12:34:23 -0700291 // Preconditions: component_idx < 1
292 constexpr reference operator[](value_type component_idx) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700293 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700294 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700295 (void)(component_idx);
296 return value;
297 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700298 // Preconditions: component_idx < 1
299 constexpr const_reference operator[](value_type component_idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700300 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700301 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700302 (void)(component_idx);
303 return value;
304 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700305 constexpr bool operator==(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700306 {
307 return value == rhs.value;
308 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700309 constexpr bool operator!=(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700310 {
311 return !(*this == rhs);
312 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700313 constexpr index operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700314 {
315 return *this;
316 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700317 constexpr index operator-() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700318 {
319 return index(-value);
320 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700321 constexpr index operator+(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700322 {
323 return index(value + rhs.value);
324 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700325 constexpr index operator-(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700326 {
327 return index(value - rhs.value);
328 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700329 constexpr index& operator+=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700330 {
331 value += rhs.value;
332 return *this;
333 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700334 constexpr index& operator-=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700335 {
336 value -= rhs.value;
337 return *this;
338 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700339 constexpr index& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700340 {
341 ++value;
342 return *this;
343 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700344 constexpr index operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700345 {
346 index ret = *this;
347 ++(*this);
348 return ret;
349 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700350 constexpr index& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700351 {
352 --value;
353 return *this;
354 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700355 constexpr index operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700356 {
357 index ret = *this;
358 --(*this);
359 return ret;
360 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700361 constexpr index operator*(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700362 {
363 return index(value * v);
364 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700365 constexpr index operator/(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700366 {
367 return index(value / v);
368 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700369 constexpr index& operator*=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700370 {
371 value *= v;
372 return *this;
373 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700374 constexpr index& operator/=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700375 {
376 value /= v;
377 return *this;
378 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700379 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700380 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700381 return{ rhs * v };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700382 }
383private:
384 value_type value;
385};
386
387#ifndef _MSC_VER
388
389struct static_bounds_dynamic_range_t
390{
391 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
392 constexpr operator T() const noexcept
393 {
394 return static_cast<T>(-1);
395 }
396
397 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
398 constexpr bool operator ==(T other) const noexcept
399 {
400 return static_cast<T>(-1) == other;
401 }
402
403 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
404 constexpr bool operator !=(T other) const noexcept
405 {
406 return static_cast<T>(-1) != other;
407 }
408
409};
410
411template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
412constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
413{
414 return right == left;
415}
416
417template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
418constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
419{
420 return right != left;
421}
422
423constexpr static_bounds_dynamic_range_t dynamic_range{};
424#else
425const char dynamic_range = -1;
426#endif
427
428struct generalized_mapping_tag {};
429struct contiguous_mapping_tag : generalized_mapping_tag {};
430
431namespace details
432{
433 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
434 struct StaticSizeHelperImpl
435 {
436 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
437 static const SizeType value = Fact1 * Fact2;
438 };
439
440 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
441 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
442 {
443 static const SizeType value = ConstBound;
444 };
445
446 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
447 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
448 {
449 static const SizeType value = ConstBound;
450 };
451
452 template <typename SizeType, SizeType ConstBound>
453 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
454 {
455 static const SizeType value = static_cast<SizeType>(ConstBound);
456 };
457
458 template <typename SizeType, SizeType Fact1, SizeType Fact2>
459 struct StaticSizeHelper
460 {
461 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
462 };
463
464
465 template <size_t Left, size_t Right>
466 struct LessThan
467 {
468 static const bool value = Left < Right;
469 };
470
471 template <typename SizeType, size_t... Ranges>
472 struct BoundsRanges {
Kern Handae1570262015-09-25 00:42:38 -0700473 static const size_t Depth = 0;
474 static const size_t DynamicNum = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700475 static const SizeType CurrentRange = 1;
476 static const SizeType TotalSize = 1;
477
478 BoundsRanges (const BoundsRanges &) = default;
479
480 // TODO : following signature is for work around VS bug
481 template <typename OtherType>
Kosov Eugene3402b922015-09-28 21:20:02 +0300482 BoundsRanges (const OtherType &, bool /* firstLevel */) {}
galikcab9bda2015-09-19 07:52:30 +0100483 BoundsRanges(const SizeType * const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700484 BoundsRanges() = default;
485
486
Kern Handae1570262015-09-25 00:42:38 -0700487 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700488 void serialize(T &) const {
489 }
Kern Handae1570262015-09-25 00:42:38 -0700490 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700491 SizeType linearize(const T &) const {
492 return 0;
493 }
Kern Handae1570262015-09-25 00:42:38 -0700494 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700495 ptrdiff_t contains(const T &) const {
496 return 0;
497 }
498
Neil MacIntoshd5316802015-09-30 21:54:08 -0700499 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700500 return TotalSize;
501 }
502
Neil MacIntoshd5316802015-09-30 21:54:08 -0700503 bool operator == (const BoundsRanges &) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700504 {
505 return true;
506 }
507 };
508
509 template <typename SizeType, size_t... RestRanges>
510 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
511 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700512 static const size_t Depth = Base::Depth + 1;
513 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700514 static const SizeType CurrentRange = dynamic_range;
515 static const SizeType TotalSize = dynamic_range;
516 const SizeType m_bound;
517
518 BoundsRanges (const BoundsRanges &) = default;
519 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
520 {
521 fail_fast_assert(0 <= *arr);
522 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
523 }
524 BoundsRanges() : m_bound(0) {}
525
526 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
Kosov Eugene3402b922015-09-28 21:20:02 +0300527 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool /* firstLevel */ = true) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700528 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
529 {
530 }
531
Kern Handae1570262015-09-25 00:42:38 -0700532 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700533 void serialize(T & arr) const {
534 arr[Dim] = elementNum();
535 this->Base::template serialize<T, Dim + 1>(arr);
536 }
Kern Handae1570262015-09-25 00:42:38 -0700537 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700538 SizeType linearize(const T & arr) const {
539 const size_t index = this->Base::totalSize() * arr[Dim];
540 fail_fast_assert(index < static_cast<size_t>(m_bound));
541 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
542 }
543
Kern Handae1570262015-09-25 00:42:38 -0700544 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700545 ptrdiff_t contains(const T & arr) const {
546 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
547 if (last == -1)
548 return -1;
549 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
550 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
551 }
552
Neil MacIntoshd5316802015-09-30 21:54:08 -0700553 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700554 return m_bound;
555 }
556
Neil MacIntoshd5316802015-09-30 21:54:08 -0700557 SizeType elementNum() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700558 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
559 }
560
Neil MacIntoshd5316802015-09-30 21:54:08 -0700561 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700562 if (dim > 0)
563 return this->Base::elementNum(dim - 1);
564 else
565 return elementNum();
566 }
567
Neil MacIntoshd5316802015-09-30 21:54:08 -0700568 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700569 {
570 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
571 }
572 };
573
574 template <typename SizeType, size_t CurRange, size_t... RestRanges>
575 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
576 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700577 static const size_t Depth = Base::Depth + 1;
578 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700579 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
580 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
581 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
582
583 BoundsRanges (const BoundsRanges &) = default;
584 BoundsRanges(const SizeType * const arr) : Base(arr) { }
585 BoundsRanges() = default;
586
587 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
588 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
589 {
590 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
591 }
592
Kern Handae1570262015-09-25 00:42:38 -0700593 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700594 void serialize(T & arr) const {
595 arr[Dim] = elementNum();
596 this->Base::template serialize<T, Dim + 1>(arr);
597 }
598
Kern Handae1570262015-09-25 00:42:38 -0700599 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700600 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700601 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700602 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
603 }
604
Kern Handae1570262015-09-25 00:42:38 -0700605 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700606 ptrdiff_t contains(const T & arr) const {
607 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
608 return -1;
609 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
610 if (last == -1)
611 return -1;
612 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
613 }
614
Neil MacIntoshd5316802015-09-30 21:54:08 -0700615 size_t totalSize() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700616 return CurrentRange * this->Base::totalSize();
617 }
618
Neil MacIntoshd5316802015-09-30 21:54:08 -0700619 SizeType elementNum() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700620 return CurrentRange;
621 }
622
Neil MacIntoshd5316802015-09-30 21:54:08 -0700623 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700624 if (dim > 0)
625 return this->Base::elementNum(dim - 1);
626 else
627 return elementNum();
628 }
629
Neil MacIntoshd5316802015-09-30 21:54:08 -0700630 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700631 {
632 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
633 }
634 };
635
636 template <typename SourceType, typename TargetType, size_t Rank>
637 struct BoundsRangeConvertible2;
638
639 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
640 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
641 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
642
643 template <size_t Rank, typename SourceType, typename TargetType>
644 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
645
646 template <typename SourceType, typename TargetType, size_t Rank>
647 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
648 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
649 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
650 {};
651
652 template <typename SourceType, typename TargetType>
653 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
654
655 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
656 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
657 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
658 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
659 {};
660 template <typename SourceType, typename TargetType>
661 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
662
663 template <typename TypeChain>
664 struct TypeListIndexer
665 {
666 const TypeChain & obj;
667 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700668 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700669 const TypeChain & getObj(std::true_type)
670 {
671 return obj;
672 }
Kern Handae1570262015-09-25 00:42:38 -0700673 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700674 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
675 {
676 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
677 }
Kern Handae1570262015-09-25 00:42:38 -0700678 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700679 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
680 {
681 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
682 }
683 };
684
685 template <typename TypeChain>
686 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
687 {
688 return TypeListIndexer<TypeChain>(obj);
689 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700690
691 template <size_t Rank, typename ValueType, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1, ValueType>>>
692 constexpr Ret shift_left(const index<Rank, ValueType>& other) noexcept
693 {
694 Ret ret;
695 for (size_t i = 0; i < Rank - 1; ++i)
696 {
697 ret[i] = other[i + 1];
698 }
699 return ret;
700 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700701}
702
703template <typename IndexType>
704class bounds_iterator;
705
706template <typename SizeType, size_t... Ranges>
707class static_bounds {
708public:
Kosov Eugene3402b922015-09-28 21:20:02 +0300709 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700710 }
711};
712
713template <typename SizeType, size_t FirstRange, size_t... RestRanges>
714class static_bounds<SizeType, FirstRange, RestRanges...>
715{
716 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
717 static_assert(std::is_integral<SizeType>::value
718 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
719
720 MyRanges m_ranges;
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700721 constexpr static_bounds(const MyRanges & range) : m_ranges(range) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700722
723 template <typename SizeType2, size_t... Ranges2>
724 friend class static_bounds;
725public:
Kern Handae1570262015-09-25 00:42:38 -0700726 static const size_t rank = MyRanges::Depth;
727 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700728 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
729
730 using size_type = SizeType;
731 using index_type = index<rank, size_type>;
732 using iterator = bounds_iterator<index_type>;
733 using const_iterator = bounds_iterator<index_type>;
734 using difference_type = ptrdiff_t;
735 using sliced_type = static_bounds<SizeType, RestRanges...>;
736 using mapping_type = contiguous_mapping_tag;
737public:
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700738 constexpr static_bounds(const static_bounds &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700739
740 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
741 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700742 constexpr static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700743 m_ranges(other.m_ranges)
744 {
745 }
746
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700747 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700748 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700749 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
750 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 -0700751 }
752
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700753 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700754
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700755 constexpr static_bounds & operator = (const static_bounds & otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700756 {
757 new(&m_ranges) MyRanges (otherBounds.m_ranges);
758 return *this;
759 }
760
Neil MacIntoshd5316802015-09-30 21:54:08 -0700761 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700762 {
763 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
764 }
765
Neil MacIntoshd5316802015-09-30 21:54:08 -0700766 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700767 {
768 return rank > 1 ? slice().size() : 1;
769 }
770
Neil MacIntoshd5316802015-09-30 21:54:08 -0700771 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700772 {
773 return static_cast<size_type>(m_ranges.totalSize());
774 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700775
Neil MacIntoshd5316802015-09-30 21:54:08 -0700776 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700777 {
778 return static_cast<size_type>(m_ranges.totalSize());
779 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700780
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700781 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700782 {
783 return m_ranges.linearize(idx);
784 }
785
Neil MacIntoshd5316802015-09-30 21:54:08 -0700786 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700787 {
788 return m_ranges.contains(idx) != -1;
789 }
790
Neil MacIntoshd5316802015-09-30 21:54:08 -0700791 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700792 {
793 return m_ranges.elementNum(index);
794 }
795
Kern Handae1570262015-09-25 00:42:38 -0700796 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700797 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700798 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700799 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700800 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
801 }
802
Neil MacIntoshd5316802015-09-30 21:54:08 -0700803 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700804 {
Anna Gringauzefdf86432015-10-14 10:46:22 -0700805 size_type extents[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700806 m_ranges.serialize(extents);
Anna Gringauzedb384972015-10-05 12:34:23 -0700807 return{ extents };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700808 }
809
810 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700811 constexpr bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700812 {
813 return this->size() == rhs.size();
814 }
815
816 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700817 constexpr bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700818 {
819 return !(*this == rhs);
820 }
821
Neil MacIntoshd5316802015-09-30 21:54:08 -0700822 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700823 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700824 return const_iterator(*this);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700825 }
826
Neil MacIntoshd5316802015-09-30 21:54:08 -0700827 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700828 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700829 return const_iterator(*this, this->index_bounds());
830 }
831};
832
Kern Handae1570262015-09-25 00:42:38 -0700833template <size_t Rank, typename SizeType = size_t>
Anna Gringauzedb384972015-10-05 12:34:23 -0700834class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700835{
Kern Handae1570262015-09-25 00:42:38 -0700836 template <size_t OtherRank, typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700837 friend class strided_bounds;
838
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700839public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700840 static const size_t rank = Rank;
Anna Gringauzefdf86432015-10-14 10:46:22 -0700841 using reference = SizeType&;
842 using const_reference = const SizeType&;
843 using size_type = SizeType;
844 using difference_type = SizeType;
845 using value_type = SizeType;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700846 using index_type = index<rank, size_type>;
847 using iterator = bounds_iterator<index_type>;
848 using const_iterator = bounds_iterator<index_type>;
849 static const int dynamic_rank = rank;
850 static const size_t static_size = dynamic_range;
851 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
852 using mapping_type = generalized_mapping_tag;
Anna Gringauzedb384972015-10-05 12:34:23 -0700853 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700854
855 template <typename OtherSizeType>
Anna Gringauzedb384972015-10-05 12:34:23 -0700856 constexpr strided_bounds(const strided_bounds<rank, OtherSizeType> &other) noexcept
857 : m_extents(other.extents), m_strides(other.strides)
858 {}
859 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
860 : m_extents(extents), m_strides(strides)
861 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -0700862 constexpr index_type strides() const noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700863 {
864 return m_strides;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700865 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700866 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700867 {
868 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700869 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700870 {
871 ret += (m_extents[i] - 1) * m_strides[i];
872 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700873 return ret + 1;
874 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700875 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700876 {
877 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700878 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700879 {
880 ret *= m_extents[i];
881 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700882 return ret;
883 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700884 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700885 {
Kern Handae1570262015-09-25 00:42:38 -0700886 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700887 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700888 if (idx[i] < 0 || idx[i] >= m_extents[i])
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700889 return false;
890 }
891 return true;
892 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700893 constexpr size_type linearize(const index_type & idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700894 {
895 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700896 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700897 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700898 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700899 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700900 }
901 return ret;
902 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700903 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700904 {
905 return m_strides[0];
906 }
907 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700908 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700909 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700910 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700911 }
Kern Handae1570262015-09-25 00:42:38 -0700912 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700913 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700914 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700915 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Anna Gringauzedb384972015-10-05 12:34:23 -0700916 return m_extents[Dim];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700917 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700918 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700919 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700920 return m_extents;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700921 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700922 const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700923 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700924 return const_iterator{ *this };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700925 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700926 const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700927 {
928 return const_iterator{ *this, index_bounds() };
929 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700930private:
Anna Gringauzedb384972015-10-05 12:34:23 -0700931 index_type m_extents;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700932 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700933};
934
935template <typename T>
936struct is_bounds : std::integral_constant<bool, false> {};
937template <typename SizeType, size_t... Ranges>
938struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
Kern Handae1570262015-09-25 00:42:38 -0700939template <size_t Rank, typename SizeType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700940struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
941
942template <typename IndexType>
943class bounds_iterator
944 : public std::iterator<std::random_access_iterator_tag,
945 IndexType,
946 ptrdiff_t,
947 const details::arrow_proxy<IndexType>,
948 const IndexType>
949{
950private:
951 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
952public:
Kern Handae1570262015-09-25 00:42:38 -0700953 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700954 using typename Base::reference;
955 using typename Base::pointer;
956 using typename Base::difference_type;
957 using typename Base::value_type;
958 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700959 using index_size_type = typename IndexType::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700960 template <typename Bounds>
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700961 explicit bounds_iterator(const Bounds& bnd, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700962 : boundary(bnd.index_bounds())
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700963 , curr(std::move(curr))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700964 {
965 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
966 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700967 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700968 {
969 return curr;
970 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700971 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700972 {
973 return details::arrow_proxy<value_type>{ curr };
974 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700975 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700976 {
Kern Handae1570262015-09-25 00:42:38 -0700977 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700978 {
979 if (++curr[i] < boundary[i])
980 {
981 return *this;
982 }
983 else
984 {
985 curr[i] = 0;
986 }
987 }
988 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -0700989 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700990 {
991 curr[i] = boundary[i];
992 }
993 return *this;
994 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700995 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700996 {
997 auto ret = *this;
998 ++(*this);
999 return ret;
1000 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001001 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001002 {
Neil MacIntoshfb913932015-09-27 16:25:43 -07001003 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001004 {
1005 if (curr[i]-- > 0)
1006 {
1007 return *this;
1008 }
1009 else
1010 {
1011 curr[i] = boundary[i] - 1;
1012 }
1013 }
1014 // If we're here the preconditions were violated
1015 // "pre: there exists s such that r == ++s"
1016 fail_fast_assert(false);
1017 return *this;
1018 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001019 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001020 {
1021 auto ret = *this;
1022 --(*this);
1023 return ret;
1024 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001025 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001026 {
1027 bounds_iterator ret{ *this };
1028 return ret += n;
1029 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001030 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001031 {
1032 auto linear_idx = linearize(curr) + n;
1033 value_type stride;
1034 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -07001035 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001036 {
1037 stride[i] = stride[i + 1] * boundary[i + 1];
1038 }
Kern Handae1570262015-09-25 00:42:38 -07001039 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001040 {
1041 curr[i] = linear_idx / stride[i];
1042 linear_idx = linear_idx % stride[i];
1043 }
1044 return *this;
1045 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001046 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001047 {
1048 bounds_iterator ret{ *this };
1049 return ret -= n;
1050 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001051 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001052 {
1053 return *this += -n;
1054 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001055 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001056 {
1057 return linearize(curr) - linearize(rhs.curr);
1058 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001059 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001060 {
1061 return *(*this + n);
1062 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001063 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001064 {
1065 return curr == rhs.curr;
1066 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001067 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001068 {
1069 return !(*this == rhs);
1070 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001071 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001072 {
Kern Handae1570262015-09-25 00:42:38 -07001073 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001074 {
1075 if (curr[i] < rhs.curr[i])
1076 return true;
1077 }
1078 return false;
1079 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001080 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001081 {
1082 return !(rhs < *this);
1083 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001084 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001085 {
1086 return rhs < *this;
1087 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001088 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001089 {
1090 return !(rhs > *this);
1091 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001092 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001093 {
1094 std::swap(boundary, rhs.boundary);
1095 std::swap(curr, rhs.curr);
1096 }
1097private:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001098 index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001099 {
1100 // TODO: Smarter impl.
1101 // Check if past-the-end
1102 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001103 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001104 {
1105 if (idx[i] != boundary[i])
1106 {
1107 pte = false;
1108 break;
1109 }
1110 }
1111 index_size_type multiplier = 1;
1112 index_size_type res = 0;
1113 if (pte)
1114 {
1115 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001116 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001117 {
1118 res += (idx[i] - 1) * multiplier;
1119 multiplier *= boundary[i];
1120 }
1121 }
1122 else
1123 {
Kern Handae1570262015-09-25 00:42:38 -07001124 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001125 {
1126 res += idx[i] * multiplier;
1127 multiplier *= boundary[i];
1128 }
1129 }
1130 return res;
1131 }
1132 value_type boundary;
1133 value_type curr;
1134};
1135
1136template <typename SizeType>
1137class bounds_iterator<index<1, SizeType>>
1138 : public std::iterator<std::random_access_iterator_tag,
1139 index<1, SizeType>,
1140 ptrdiff_t,
1141 const details::arrow_proxy<index<1, SizeType>>,
1142 const index<1, SizeType>>
1143{
1144 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>>;
1145
1146public:
1147 using typename Base::reference;
1148 using typename Base::pointer;
1149 using typename Base::difference_type;
1150 using typename Base::value_type;
1151 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -07001152 using index_size_type = typename index_type::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001153
1154 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001155 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001156 : curr( std::move(curr) )
1157 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -07001158 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001159 {
1160 return curr;
1161 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001162 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001163 {
1164 return details::arrow_proxy<value_type>{ curr };
1165 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001166 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001167 {
1168 ++curr;
1169 return *this;
1170 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001171 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001172 {
1173 auto ret = *this;
1174 ++(*this);
1175 return ret;
1176 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001177 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001178 {
1179 curr--;
1180 return *this;
1181 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001182 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001183 {
1184 auto ret = *this;
1185 --(*this);
1186 return ret;
1187 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001188 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001189 {
1190 bounds_iterator ret{ *this };
1191 return ret += n;
1192 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001193 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001194 {
1195 curr += n;
1196 return *this;
1197 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001198 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001199 {
1200 bounds_iterator ret{ *this };
1201 return ret -= n;
1202 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001203 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001204 {
1205 return *this += -n;
1206 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001207 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001208 {
1209 return curr[0] - rhs.curr[0];
1210 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001211 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001212 {
1213 return curr + n;
1214 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001215 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001216 {
1217 return curr == rhs.curr;
1218 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001219 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001220 {
1221 return !(*this == rhs);
1222 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001223 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001224 {
1225 return curr[0] < rhs.curr[0];
1226 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001227 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001228 {
1229 return !(rhs < *this);
1230 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001231 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001232 {
1233 return rhs < *this;
1234 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001235 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001236 {
1237 return !(rhs > *this);
1238 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001239 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001240 {
1241 std::swap(curr, rhs.curr);
1242 }
1243private:
1244 value_type curr;
1245};
1246
1247template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001248bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001249{
1250 return rhs + n;
1251}
1252
1253/*
1254** begin definitions of basic_array_view
1255*/
1256namespace details
1257{
1258 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001259 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 -07001260 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001261 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001262 }
1263
Neil MacIntosh99746e22015-09-27 16:53:58 -07001264 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001265 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001266 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 -07001267 {
1268 auto extents = bnd.index_bounds();
Anna Gringauzefdf86432015-10-14 10:46:22 -07001269 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001270
1271 stride[Bounds::rank - 1] = 1;
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001272 for (size_t i = 1; i < Bounds::rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -07001273 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001274 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
Anna Gringauzedb384972015-10-05 12:34:23 -07001275 }
1276 return{ stride };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001277 }
1278
1279 template <typename BoundsSrc, typename BoundsDest>
1280 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1281 {
1282 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1283 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1284 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");
1285 fail_fast_assert(src.size() == dest.size());
1286 }
1287
1288
1289} // namespace details
1290
1291template <typename ArrayView>
1292class contiguous_array_view_iterator;
1293template <typename ArrayView>
1294class general_array_view_iterator;
1295enum class byte : std::uint8_t {};
1296
1297template <typename ValueType, typename BoundsType>
1298class basic_array_view
1299{
1300public:
Kern Handae1570262015-09-25 00:42:38 -07001301 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001302 using bounds_type = BoundsType;
1303 using size_type = typename bounds_type::size_type;
1304 using index_type = typename bounds_type::index_type;
1305 using value_type = ValueType;
1306 using pointer = ValueType*;
1307 using reference = ValueType&;
1308 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>>;
1309 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>>>;
1310 using reverse_iterator = std::reverse_iterator<iterator>;
1311 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1312 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1313
1314private:
1315 pointer m_pdata;
1316 bounds_type m_bounds;
1317
1318public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001319 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001320 {
1321 return m_bounds;
1322 }
Kern Handae1570262015-09-25 00:42:38 -07001323 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001324 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001325 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001326 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001327 return m_bounds.template extent<Dim>();
1328 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001329 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001330 {
1331 return m_bounds.size();
1332 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001333 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001334 {
1335 return m_pdata[m_bounds.linearize(idx)];
1336 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001337 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001338 {
1339 return m_pdata;
1340 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001341 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001342 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001343 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001344 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001345 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001346
1347 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001348 return Ret {m_pdata + ridx, m_bounds.slice()};
1349 }
1350
Neil MacIntoshd5316802015-09-30 21:54:08 -07001351 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001352 {
1353 return m_pdata != nullptr;
1354 }
1355
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001356 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001357 {
1358 return iterator {this, true};
1359 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001360 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001361 {
1362 return iterator {this};
1363 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001364 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001365 {
1366 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1367 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001368 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001369 {
1370 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1371 }
1372
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001373 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001374 {
1375 return reverse_iterator {end()};
1376 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001377 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001378 {
1379 return reverse_iterator {begin()};
1380 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001381 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001382 {
1383 return const_reverse_iterator {cend()};
1384 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001385 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001386 {
1387 return const_reverse_iterator {cbegin()};
1388 }
1389
1390 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 -07001391 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001392 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001393 return m_bounds.size() == other.m_bounds.size() &&
1394 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001395 }
1396
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001397 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 -07001398 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001399 {
1400 return !(*this == other);
1401 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001402
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001403 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 -07001404 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001405 {
1406 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1407 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001408
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001409 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 -07001410 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001411 {
1412 return !(other < *this);
1413 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001414
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001415 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 -07001416 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001417 {
1418 return (other < *this);
1419 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001420
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001421 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 -07001422 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001423 {
1424 return !(*this < other);
1425 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001426
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001427public:
1428 template <typename OtherValueType, typename OtherBounds,
1429 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1430 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001431 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001432 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1433 {
1434 }
1435protected:
1436
Neil MacIntoshd5316802015-09-30 21:54:08 -07001437 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001438 : m_pdata(data)
1439 , m_bounds(std::move(bound))
1440 {
1441 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1442 }
1443 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001444 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 -07001445 : m_pdata(reinterpret_cast<pointer>(data))
1446 , m_bounds(std::move(bound))
1447 {
1448 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1449 }
1450 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001451 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001452 {
1453 details::verifyBoundsReshape(m_bounds, bounds);
1454 return {m_pdata, bounds};
1455 }
1456private:
1457
1458 friend iterator;
1459 friend const_iterator;
1460 template <typename ValueType2, typename BoundsType2>
1461 friend class basic_array_view;
1462};
1463
1464template <size_t DimSize = dynamic_range>
1465struct dim
1466{
1467 static const size_t value = DimSize;
1468};
1469template <>
1470struct dim<dynamic_range>
1471{
1472 static const size_t value = dynamic_range;
1473 const size_t dvalue;
1474 dim(size_t size) : dvalue(size) {}
1475};
1476
1477template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1478class array_view;
Kern Handae1570262015-09-25 00:42:38 -07001479template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001480class strided_array_view;
1481
1482namespace details
1483{
1484 template <typename T, typename = std::true_type>
1485 struct ArrayViewTypeTraits
1486 {
1487 using value_type = T;
1488 using size_type = size_t;
1489 };
1490
1491 template <typename Traits>
1492 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1493 {
1494 using value_type = typename Traits::array_view_traits::value_type;
1495 using size_type = typename Traits::array_view_traits::size_type;
1496 };
1497
1498 template <typename T, typename SizeType, size_t... Ranks>
1499 struct ArrayViewArrayTraits {
1500 using type = array_view<T, Ranks...>;
1501 using value_type = T;
1502 using bounds_type = static_bounds<SizeType, Ranks...>;
1503 using pointer = T*;
1504 using reference = T&;
1505 };
1506 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1507 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1508
1509 template <typename BoundsType>
1510 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1511 {
1512 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1513 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1514 }
1515 template <typename BoundsType>
1516 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1517 {
1518 fail_fast_assert(BoundsType::static_size == totalSize);
1519 return {};
1520 }
1521 template <typename BoundsType>
1522 BoundsType newBoundsHelper(size_t totalSize)
1523 {
1524 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1525 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1526 }
1527
1528 struct Sep{};
1529
1530 template <typename T, typename... Args>
1531 T static_as_array_view_helper(Sep, Args... args)
1532 {
1533 return T{static_cast<typename T::size_type>(args)...};
1534 }
1535 template <typename T, typename Arg, typename... Args>
1536 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)
1537 {
1538 return static_as_array_view_helper<T>(args...);
1539 }
1540 template <typename T, typename... Args>
1541 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1542 {
1543 return static_as_array_view_helper<T>(args..., val.dvalue);
1544 }
1545
1546 template <typename SizeType, typename ...Dimensions>
1547 struct static_as_array_view_static_bounds_helper
1548 {
1549 using type = static_bounds<SizeType, (Dimensions::value)...>;
1550 };
1551
1552 template <typename T>
1553 struct is_array_view_oracle : std::false_type
1554 {};
1555 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1556 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1557 {};
Kern Handae1570262015-09-25 00:42:38 -07001558 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001559 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1560 {};
1561 template <typename T>
1562 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1563 {};
1564
1565}
1566
1567
1568template <typename ValueType, typename SizeType>
1569struct array_view_options
1570{
1571 struct array_view_traits
1572 {
1573 using value_type = ValueType;
1574 using size_type = SizeType;
1575 };
1576};
1577
1578template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1579class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1580 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1581{
1582 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1583 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001584 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001585 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001586
1587public:
1588 using typename Base::bounds_type;
1589 using typename Base::size_type;
1590 using typename Base::pointer;
1591 using typename Base::value_type;
1592 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001593 using typename Base::iterator;
1594 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001595 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001596 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001597
1598public:
1599 // basic
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001600 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001601 {
1602 }
1603
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001604 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001605 {
1606 }
1607
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001608 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001609 {
1610 fail_fast_assert(size == 0);
1611 }
1612
1613 // default
1614 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001615 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001616 {
1617 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001618
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001619 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1620 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001621 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1622 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001623 constexpr array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001624 {
1625 }
1626
1627 // from n-dimensions static array
1628 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1629 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 -07001630 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001631 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001632 {
1633 }
1634
1635 // from n-dimensions static array with size
1636 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1637 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 -07001638 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001639 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001640 {
1641 fail_fast_assert(size <= N);
1642 }
1643
1644 // from std array
1645 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 -07001646 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 -07001647 {
1648 }
1649
1650 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 -07001651 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 -07001652 {
1653 }
1654
1655
1656 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1657 template <typename Ptr,
1658 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1659 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001660 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 -07001661 {
1662 }
1663
1664 // from containers. It must has .size() and .data() two function signatures
1665 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1666 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001667 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001668 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1669 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001670 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001671 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 -07001672 {
1673
1674 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001675
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001676 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001677
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001678 // convertible
1679 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1680 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1681 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1682 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1683 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001684 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 -07001685
1686 // reshape
1687 template <typename... Dimensions2>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001688 constexpr array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001689 {
1690 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001691 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001692 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1693 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001694 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001695 }
1696
1697 // to bytes array
1698 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001699 constexpr auto as_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001700 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)>
1701 {
1702 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001703 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001704 }
1705
1706 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001707 constexpr auto as_writeable_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001708 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)>
1709 {
1710 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001711 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001712 }
1713
Anna Gringauze18cd9802015-09-14 16:34:26 -07001714
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001715 // from bytes array
1716 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 -07001717 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 -07001718 {
1719 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1720 "Target type must be standard layout and its size must match the byte array size");
1721 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001722 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001723 }
1724
1725 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 -07001726 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 -07001727 {
1728 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1729 "Target type must be standard layout and its size must match the byte array size");
1730 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001731 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001732 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001733
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001734 // section on linear space
1735 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001736 constexpr array_view<ValueTypeOpt, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001737 {
1738 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1739 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 -07001740 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001741 }
1742
Neil MacIntoshd5316802015-09-30 21:54:08 -07001743 constexpr array_view<ValueTypeOpt, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001744 {
1745 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001746 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001747 }
1748
1749 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001750 constexpr array_view<ValueTypeOpt, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001751 {
1752 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1753 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001754 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001755 }
1756
Neil MacIntoshd5316802015-09-30 21:54:08 -07001757 constexpr array_view<ValueTypeOpt, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001758 {
1759 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001760 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001761 }
1762
1763 template<size_t Offset, size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001764 constexpr array_view<ValueTypeOpt, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001765 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001766 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");
1767 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 -07001768 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001769 }
1770
Neil MacIntoshd5316802015-09-30 21:54:08 -07001771 constexpr array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001772 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001773 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1774 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001775 }
1776
1777 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001778 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001779 {
1780 return this->size();
1781 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001782 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001783 {
1784 return length();
1785 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001786 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001787 {
1788 return sizeof(value_type) * this->size();
1789 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001790 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001791 {
1792 return bytes();
1793 }
1794
1795 // section
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001796 constexpr strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001797 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001798 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001799 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1800 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001801
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001802 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001803 {
1804 return Base::operator[](idx);
1805 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001806
Anna Gringauze1a864982015-09-14 18:55:06 -07001807 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001808 constexpr array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001809 {
1810 auto ret = Base::operator[](idx);
1811 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001812 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001813
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001814 using Base::operator==;
1815 using Base::operator!=;
1816 using Base::operator<;
1817 using Base::operator<=;
1818 using Base::operator>;
1819 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001820};
1821
1822template <typename T, size_t... Dimensions>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001823constexpr 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 -07001824{
1825 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1826}
1827
1828template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001829constexpr 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 -07001830{
1831 return {arr, len};
1832}
1833
1834template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001835constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001836{
1837 return {arr};
1838}
1839
1840template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001841constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001842{
1843 return {arr};
1844}
1845
1846template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001847constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001848
1849template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001850constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001851{
1852 return {arr};
1853}
1854
1855template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001856constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001857{
1858 return {begin, end};
1859}
1860
1861template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001862constexpr 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 -07001863 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1864{
1865 return {arr.data(), arr.size()};
1866}
1867
1868template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001869constexpr 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 -07001870 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1871
Kern Handae1570262015-09-25 00:42:38 -07001872template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001873class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1874{
1875 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 -07001876
Kern Handae1570262015-09-25 00:42:38 -07001877 template<typename OtherValueOpt, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001878 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001879public:
1880 using Base::rank;
1881 using typename Base::bounds_type;
1882 using typename Base::size_type;
1883 using typename Base::pointer;
1884 using typename Base::value_type;
1885 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001886 using typename Base::iterator;
1887 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001888 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001889
1890 // from static array of size N
1891 template<size_type N>
1892 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1893 {
1894 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1895 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001896
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001897 // from raw data
1898 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001899 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001900 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001901 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001902
1903 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001904 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001905 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1906 {
1907 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1908 }
1909
1910 // convertible
1911 template <typename OtherValueTypeOpt,
1912 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1913 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1914 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1915 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001916 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 -07001917 {
1918 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001919
1920 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001921 template <typename OtherValueType>
1922 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 -07001923 {
1924 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1925 auto d = sizeof(OtherValueType) / sizeof(value_type);
1926
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001927 size_type size = this->bounds().total_size() / d;
1928 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 -07001929 }
1930
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001931 strided_array_view section(index_type origin, index_type extents) const
1932 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001933 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001934 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1935 }
1936
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001937 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001938 {
1939 return Base::operator[](idx);
1940 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001941
1942 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001943 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001944 {
1945 auto ret = Base::operator[](idx);
1946 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1947 }
1948
1949private:
1950 static index_type resize_extent(const index_type& extent, size_t d)
1951 {
1952 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");
1953
1954 index_type ret = extent;
1955 ret[rank - 1] /= d;
1956
1957 return ret;
1958 }
1959
1960 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Kosov Eugene3402b922015-09-28 21:20:02 +03001961 static index_type resize_stride(const index_type& strides, size_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001962 {
1963 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1964
1965 return strides;
1966 }
1967
1968 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1969 static index_type resize_stride(const index_type& strides, size_t d)
1970 {
1971 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1972 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");
1973
Neil MacIntosh99746e22015-09-27 16:53:58 -07001974 for (size_t i = rank - 1; i > 0; --i)
1975 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 -07001976
1977 index_type ret = strides / d;
1978 ret[rank - 1] = 1;
1979
1980 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001981 }
1982};
1983
1984template <typename ArrayView>
1985class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1986{
1987 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1988public:
1989 using typename Base::reference;
1990 using typename Base::pointer;
1991 using typename Base::difference_type;
1992private:
1993 template <typename ValueType, typename Bounds>
1994 friend class basic_array_view;
1995 pointer m_pdata;
1996 const ArrayView * m_validator;
1997 void validateThis() const
1998 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07001999 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 -07002000 }
2001 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2002 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2003public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002004 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002005 {
2006 validateThis();
2007 return *m_pdata;
2008 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002009 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002010 {
2011 validateThis();
2012 return m_pdata;
2013 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002014 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002015 {
2016 ++m_pdata;
2017 return *this;
2018 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002019 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002020 {
2021 auto ret = *this;
2022 ++(*this);
2023 return ret;
2024 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002025 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002026 {
2027 --m_pdata;
2028 return *this;
2029 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002030 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002031 {
2032 auto ret = *this;
2033 --(*this);
2034 return ret;
2035 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002036 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002037 {
2038 contiguous_array_view_iterator ret{ *this };
2039 return ret += n;
2040 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002041 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002042 {
2043 m_pdata += n;
2044 return *this;
2045 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002046 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002047 {
2048 contiguous_array_view_iterator ret{ *this };
2049 return ret -= n;
2050 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002051 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002052 {
2053 return *this += -n;
2054 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002055 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002056 {
2057 fail_fast_assert(m_validator == rhs.m_validator);
2058 return m_pdata - rhs.m_pdata;
2059 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002060 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002061 {
2062 return *(*this + n);
2063 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002064 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002065 {
2066 fail_fast_assert(m_validator == rhs.m_validator);
2067 return m_pdata == rhs.m_pdata;
2068 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002069 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002070 {
2071 return !(*this == rhs);
2072 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002073 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002074 {
2075 fail_fast_assert(m_validator == rhs.m_validator);
2076 return m_pdata < rhs.m_pdata;
2077 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002078 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002079 {
2080 return !(rhs < *this);
2081 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002082 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002083 {
2084 return rhs < *this;
2085 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002086 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002087 {
2088 return !(rhs > *this);
2089 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002090 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002091 {
2092 std::swap(m_pdata, rhs.m_pdata);
2093 std::swap(m_validator, rhs.m_validator);
2094 }
2095};
2096
2097template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002098contiguous_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 -07002099{
2100 return rhs + n;
2101}
2102
2103template <typename ArrayView>
2104class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2105{
2106 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2107public:
2108 using typename Base::reference;
2109 using typename Base::pointer;
2110 using typename Base::difference_type;
2111 using typename Base::value_type;
2112private:
2113 template <typename ValueType, typename Bounds>
2114 friend class basic_array_view;
2115 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002116 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002117 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2118 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2119 {
2120 }
2121public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002122 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002123 {
2124 return (*m_container)[*m_itr];
2125 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002126 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002127 {
2128 return &(*m_container)[*m_itr];
2129 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002130 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002131 {
2132 ++m_itr;
2133 return *this;
2134 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002135 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002136 {
2137 auto ret = *this;
2138 ++(*this);
2139 return ret;
2140 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002141 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002142 {
2143 --m_itr;
2144 return *this;
2145 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002146 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002147 {
2148 auto ret = *this;
2149 --(*this);
2150 return ret;
2151 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002152 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002153 {
2154 general_array_view_iterator ret{ *this };
2155 return ret += n;
2156 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002157 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002158 {
2159 m_itr += n;
2160 return *this;
2161 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002162 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002163 {
2164 general_array_view_iterator ret{ *this };
2165 return ret -= n;
2166 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002167 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002168 {
2169 return *this += -n;
2170 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002171 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002172 {
2173 fail_fast_assert(m_container == rhs.m_container);
2174 return m_itr - rhs.m_itr;
2175 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002176 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002177 {
2178 return (*m_container)[m_itr[n]];;
2179 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002180 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002181 {
2182 fail_fast_assert(m_container == rhs.m_container);
2183 return m_itr == rhs.m_itr;
2184 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002185 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002186 {
2187 return !(*this == rhs);
2188 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002189 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002190 {
2191 fail_fast_assert(m_container == rhs.m_container);
2192 return m_itr < rhs.m_itr;
2193 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002194 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002195 {
2196 return !(rhs < *this);
2197 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002198 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002199 {
2200 return rhs < *this;
2201 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002202 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002203 {
2204 return !(rhs > *this);
2205 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002206 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002207 {
2208 std::swap(m_itr, rhs.m_itr);
2209 std::swap(m_container, rhs.m_container);
2210 }
2211};
2212
2213template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002214general_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 -07002215{
2216 return rhs + n;
2217}
2218
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002219} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002220
Neil MacIntoshd5316802015-09-30 21:54:08 -07002221#ifdef _MSC_VER
2222
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002223#undef constexpr
2224#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002225
Neil MacIntoshd5316802015-09-30 21:54:08 -07002226#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002227#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002228
2229#ifndef GSL_THROWS_FOR_TESTING
2230#pragma undef noexcept
2231#endif // GSL_THROWS_FOR_TESTING
2232
Neil MacIntosh9a297122015-09-14 15:11:07 -07002233#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002234
Neil MacIntoshd5316802015-09-30 21:54:08 -07002235#endif // _MSC_VER
2236
2237#if defined(GSL_THROWS_FOR_TESTING)
2238#undef noexcept
2239#endif // GSL_THROWS_FOR_TESTING
2240
Treb Connell51da1362015-09-24 18:08:34 -07002241
2242#endif // GSL_ARRAY_VIEW_H