blob: 046cbf8bfb01d865b0be7e27ff5f73ae31f49868 [file] [log] [blame]
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
Treb Connell51da1362015-09-24 18:08:34 -070019#ifndef GSL_ARRAY_VIEW_H
20#define GSL_ARRAY_VIEW_H
21
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070022#include <new>
23#include <stdexcept>
24#include <cstddef>
25#include <cstdint>
26#include <limits>
27#include <type_traits>
28#include <utility>
29#include <array>
30#include <iterator>
Kern Handac4f9b872015-09-25 17:01:29 -070031#include <algorithm>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070032#include "fail_fast.h"
33
Neil MacIntoshd5316802015-09-30 21:54:08 -070034#ifdef _MSC_VER
35
36// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070037#pragma push_macro("constexpr")
38#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070039
40
41// VS 2013 workarounds
42#if _MSC_VER <= 1800
43
44// noexcept is not understood
45#ifndef GSL_THROWS_FOR_TESTING
46#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070047#endif
48
Neil MacIntoshd5316802015-09-30 21:54:08 -070049// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070050#pragma warning(push)
51#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntoshd5316802015-09-30 21:54:08 -070052
Neil MacIntosh9a297122015-09-14 15:11:07 -070053#endif // _MSC_VER <= 1800
54
Neil MacIntoshd5316802015-09-30 21:54:08 -070055#endif // _MSC_VER
56
57// In order to test the library, we need it to throw exceptions that we can catch
58#ifdef GSL_THROWS_FOR_TESTING
59#define noexcept /* nothing */
60#endif // GSL_THROWS_FOR_TESTING
61
62
Neil MacIntoshef626fd2015-09-29 16:41:37 -070063namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070064
65/*
66** begin definitions of index and bounds
67*/
68namespace details
69{
70 template <typename SizeType>
71 struct SizeTypeTraits
72 {
73 static const size_t max_value = std::is_signed<SizeType>::value ? static_cast<typename std::make_unsigned<SizeType>::type>(-1) / 2 : static_cast<SizeType>(-1);
74 };
75
76
Kern Handae1570262015-09-25 00:42:38 -070077 template <typename ConcreteType, typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070078 class coordinate_facade
79 {
80 static_assert(std::is_integral<ValueType>::value
Neil MacIntosh761554f2015-09-29 16:54:00 -070081 && sizeof(ValueType) <= sizeof(size_t), "ValueType must be an integral type!");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070082 static_assert(Rank > 0, "Rank must be greater than 0!");
83
Kern Handae1570262015-09-25 00:42:38 -070084 template <typename OtherConcreteType, typename OtherValueType, size_t OtherRank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070085 friend class coordinate_facade;
86 public:
87 using reference = ValueType&;
88 using const_reference = const ValueType&;
89 using value_type = ValueType;
Kern Handae1570262015-09-25 00:42:38 -070090 static const size_t rank = Rank;
Neil MacIntoshd5316802015-09-30 21:54:08 -070091 constexpr coordinate_facade() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070092 {
93 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -070094 }
Neil MacIntoshd5316802015-09-30 21:54:08 -070095 constexpr coordinate_facade(const value_type(&values)[rank]) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -070096 {
97 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Kern Handae1570262015-09-25 00:42:38 -070098 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -070099 elems[i] = values[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700100 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700101 constexpr coordinate_facade(value_type e0) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700102 {
103 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
104 static_assert(rank == 1, "This constructor can only be used with rank == 1.");
105 elems[0] = e0;
106 }
107 // Preconditions: il.size() == rank
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700108 constexpr coordinate_facade(std::initializer_list<value_type> il)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700109 {
110 static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700111 fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array");
Kern Handae1570262015-09-25 00:42:38 -0700112 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700113 {
114 elems[i] = begin(il)[i];
115 }
116 }
117
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700118 constexpr coordinate_facade(const coordinate_facade & other) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700119
120 template <typename OtherConcreteType, typename OtherValueType>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700121 constexpr coordinate_facade(const coordinate_facade<OtherConcreteType, OtherValueType, Rank> & other)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700122 {
Kern Handae1570262015-09-25 00:42:38 -0700123 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700124 {
125 fail_fast_assert(static_cast<size_t>(other.elems[i]) <= SizeTypeTraits<value_type>::max_value);
126 elems[i] = static_cast<value_type>(other.elems[i]);
127 }
128 }
129 protected:
130 coordinate_facade& operator=(const coordinate_facade& rhs) = default;
131 // Preconditions: component_idx < rank
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700132 constexpr reference operator[](size_t component_idx)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700133 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700134 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700135 return elems[component_idx];
136 }
137 // Preconditions: component_idx < rank
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700138 constexpr const_reference operator[](size_t component_idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700139 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700140 fail_fast_assert(component_idx < rank, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700141 return elems[component_idx];
142 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700143 constexpr bool operator==(const ConcreteType& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700144 {
Kern Handac4f9b872015-09-25 17:01:29 -0700145 return std::equal(elems, elems + rank, rhs.elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700146 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700147 constexpr bool operator!=(const ConcreteType& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700148 {
149 return !(to_concrete() == rhs);
150 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700151 constexpr ConcreteType operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700152 {
153 return to_concrete();
154 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700155 constexpr ConcreteType operator-() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700156 {
157 ConcreteType ret = to_concrete();
Kern Handaf1be21a2015-09-27 23:25:20 +0000158 std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700159 return ret;
160 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700161 constexpr ConcreteType operator+(const ConcreteType& rhs) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700162 {
163 ConcreteType ret = to_concrete();
164 ret += rhs;
165 return ret;
166 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700167 constexpr ConcreteType operator-(const ConcreteType& rhs) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700168 {
169 ConcreteType ret = to_concrete();
170 ret -= rhs;
171 return ret;
172 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700173 constexpr ConcreteType& operator+=(const ConcreteType& rhs)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700174 {
Kern Handae1570262015-09-25 00:42:38 -0700175 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700176 elems[i] += rhs.elems[i];
177 return to_concrete();
178 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700179 constexpr ConcreteType& operator-=(const ConcreteType& rhs)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700180 {
Kern Handae1570262015-09-25 00:42:38 -0700181 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700182 elems[i] -= rhs.elems[i];
183 return to_concrete();
184 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700185 constexpr ConcreteType& operator++()
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700186 {
187 static_assert(rank == 1, "This operator can only be used with rank == 1.");
188 ++elems[0];
189 return to_concrete();
190 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700191 constexpr ConcreteType operator++(int)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700192 {
193 static_assert(rank == 1, "This operator can only be used with rank == 1.");
194 ConcreteType ret = to_concrete();
195 ++(*this);
196 return ret;
197 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700198 constexpr ConcreteType& operator--()
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700199 {
200 static_assert(rank == 1, "This operator can only be used with rank == 1.");
201 --elems[0];
202 return to_concrete();
203 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700204 constexpr ConcreteType operator--(int)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700205 {
206 static_assert(rank == 1, "This operator can only be used with rank == 1.");
207 ConcreteType ret = to_concrete();
208 --(*this);
209 return ret;
210 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700211 constexpr ConcreteType operator*(value_type v) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700212 {
213 ConcreteType ret = to_concrete();
214 ret *= v;
215 return ret;
216 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700217 constexpr ConcreteType operator/(value_type v) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700218 {
219 ConcreteType ret = to_concrete();
220 ret /= v;
221 return ret;
222 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700223 friend constexpr ConcreteType operator*(value_type v, const ConcreteType& rhs)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700224 {
225 return rhs * v;
226 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700227 constexpr ConcreteType& operator*=(value_type v)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700228 {
Kern Handae1570262015-09-25 00:42:38 -0700229 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700230 elems[i] *= v;
231 return to_concrete();
232 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700233 constexpr ConcreteType& operator/=(value_type v)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700234 {
Kern Handae1570262015-09-25 00:42:38 -0700235 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700236 elems[i] /= v;
237 return to_concrete();
238 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700239 value_type elems[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700240 private:
Neil MacIntoshd5316802015-09-30 21:54:08 -0700241 constexpr const ConcreteType& to_concrete() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700242 {
243 return static_cast<const ConcreteType&>(*this);
244 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700245 constexpr ConcreteType& to_concrete() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700246 {
247 return static_cast<ConcreteType&>(*this);
248 }
249 };
250 template <typename T>
251 class arrow_proxy
252 {
253 public:
254 explicit arrow_proxy(T t)
255 : val(t)
256 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -0700257 const T operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700258 {
259 return val;
260 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700261 const T* operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700262 {
263 return &val;
264 }
265 private:
266 T val;
267 };
268}
269
Kern Handae1570262015-09-25 00:42:38 -0700270template <size_t Rank, typename ValueType = size_t>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700271class index : private details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>
272{
273 using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
274 friend Base;
Kern Handae1570262015-09-25 00:42:38 -0700275 template <size_t OtherRank, typename OtherValueType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700276 friend class index;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700277public:
278 using Base::rank;
279 using reference = typename Base::reference;
280 using const_reference = typename Base::const_reference;
281 using size_type = typename Base::value_type;
282 using value_type = typename Base::value_type;
Neil MacIntoshd5316802015-09-30 21:54:08 -0700283 constexpr index() noexcept : Base(){}
284 constexpr index(const value_type (&values)[rank]) noexcept : Base(values) {}
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700285 constexpr index(std::initializer_list<value_type> il) : Base(il) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700286
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700287 constexpr index(const index &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700288
289 template <typename OtherValueType>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700290 constexpr index(const index<Rank, OtherValueType> &other) : Base(other)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700291 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700292 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700293 constexpr static index shift_left(const index<rank+1, value_type>& other) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700294 {
Anna Gringauze1a864982015-09-14 18:55:06 -0700295 value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
296 return index(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700297 }
298
299 using Base::operator[];
300 using Base::operator==;
301 using Base::operator!=;
302 using Base::operator+;
303 using Base::operator-;
304 using Base::operator+=;
305 using Base::operator-=;
306 using Base::operator++;
307 using Base::operator--;
308 using Base::operator*;
309 using Base::operator/;
310 using Base::operator*=;
311 using Base::operator/=;
312};
313
314template <typename ValueType>
315class index<1, ValueType>
316{
Kern Handae1570262015-09-25 00:42:38 -0700317 template <size_t, typename OtherValueType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700318 friend class index;
319public:
Kern Handae1570262015-09-25 00:42:38 -0700320 static const size_t rank = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700321 using reference = ValueType&;
322 using const_reference = const ValueType&;
323 using size_type = ValueType;
324 using value_type = ValueType;
325
Neil MacIntoshd5316802015-09-30 21:54:08 -0700326 constexpr index() noexcept : value(0)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700327 {
328 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700329 constexpr index(value_type e0) noexcept : value(e0)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700330 {
331 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700332 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700333 {
334 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700335 // Preconditions: il.size() == rank
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700336 constexpr index(std::initializer_list<value_type> il)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700337 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700338 fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700339 value = begin(il)[0];
340 }
341
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700342 constexpr index(const index &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700343
344 template <typename OtherValueType>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700345 constexpr index(const index<1, OtherValueType> & other)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700346 {
347 fail_fast_assert(other.value <= details::SizeTypeTraits<ValueType>::max_value);
348 value = static_cast<ValueType>(other.value);
349 }
350
Neil MacIntoshd5316802015-09-30 21:54:08 -0700351 constexpr static index shift_left(const index<rank + 1, value_type>& other) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700352 {
353 return other.elems[1];
354 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700355 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700356 constexpr reference operator[](size_type component_idx) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700357 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700358 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700359 (void)(component_idx);
360 return value;
361 }
362 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700363 constexpr const_reference operator[](size_type component_idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700364 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700365 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700366 (void)(component_idx);
367 return value;
368 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700369 constexpr bool operator==(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700370 {
371 return value == rhs.value;
372 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700373 constexpr bool operator!=(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700374 {
375 return !(*this == rhs);
376 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700377 constexpr index operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700378 {
379 return *this;
380 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700381 constexpr index operator-() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700382 {
383 return index(-value);
384 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700385 constexpr index operator+(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700386 {
387 return index(value + rhs.value);
388 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700389 constexpr index operator-(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700390 {
391 return index(value - rhs.value);
392 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700393 constexpr index& operator+=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700394 {
395 value += rhs.value;
396 return *this;
397 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700398 constexpr index& operator-=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700399 {
400 value -= rhs.value;
401 return *this;
402 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700403 constexpr index& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700404 {
405 ++value;
406 return *this;
407 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700408 constexpr index operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700409 {
410 index ret = *this;
411 ++(*this);
412 return ret;
413 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700414 constexpr index& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700415 {
416 --value;
417 return *this;
418 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700419 constexpr index operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700420 {
421 index ret = *this;
422 --(*this);
423 return ret;
424 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700425 constexpr index operator*(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700426 {
427 return index(value * v);
428 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700429 constexpr index operator/(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700430 {
431 return index(value / v);
432 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700433 constexpr index& operator*=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700434 {
435 value *= v;
436 return *this;
437 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700438 constexpr index& operator/=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700439 {
440 value /= v;
441 return *this;
442 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700443 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700444 {
445 return index(rhs * v);
446 }
447private:
448 value_type value;
449};
450
451#ifndef _MSC_VER
452
453struct static_bounds_dynamic_range_t
454{
455 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
456 constexpr operator T() const noexcept
457 {
458 return static_cast<T>(-1);
459 }
460
461 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
462 constexpr bool operator ==(T other) const noexcept
463 {
464 return static_cast<T>(-1) == other;
465 }
466
467 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
468 constexpr bool operator !=(T other) const noexcept
469 {
470 return static_cast<T>(-1) != other;
471 }
472
473};
474
475template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
476constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
477{
478 return right == left;
479}
480
481template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
482constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
483{
484 return right != left;
485}
486
487constexpr static_bounds_dynamic_range_t dynamic_range{};
488#else
489const char dynamic_range = -1;
490#endif
491
492struct generalized_mapping_tag {};
493struct contiguous_mapping_tag : generalized_mapping_tag {};
494
495namespace details
496{
497 template <typename SizeType, SizeType Fact1, SizeType Fact2, SizeType ConstBound>
498 struct StaticSizeHelperImpl
499 {
500 static_assert(static_cast<size_t>(Fact1) * static_cast<size_t>(Fact2) <= SizeTypeTraits<SizeType>::max_value, "Value out of the range of SizeType");
501 static const SizeType value = Fact1 * Fact2;
502 };
503
504 template <typename SizeType, SizeType Fact1, SizeType ConstBound>
505 struct StaticSizeHelperImpl<SizeType, Fact1, ConstBound, ConstBound>
506 {
507 static const SizeType value = ConstBound;
508 };
509
510 template <typename SizeType, SizeType Fact2, SizeType ConstBound>
511 struct StaticSizeHelperImpl<SizeType, ConstBound, Fact2, ConstBound>
512 {
513 static const SizeType value = ConstBound;
514 };
515
516 template <typename SizeType, SizeType ConstBound>
517 struct StaticSizeHelperImpl<SizeType, ConstBound, ConstBound, ConstBound>
518 {
519 static const SizeType value = static_cast<SizeType>(ConstBound);
520 };
521
522 template <typename SizeType, SizeType Fact1, SizeType Fact2>
523 struct StaticSizeHelper
524 {
525 static const SizeType value = StaticSizeHelperImpl<SizeType, static_cast<SizeType>(Fact1), static_cast<SizeType>(Fact2), static_cast<SizeType>(dynamic_range)>::value;
526 };
527
528
529 template <size_t Left, size_t Right>
530 struct LessThan
531 {
532 static const bool value = Left < Right;
533 };
534
535 template <typename SizeType, size_t... Ranges>
536 struct BoundsRanges {
Kern Handae1570262015-09-25 00:42:38 -0700537 static const size_t Depth = 0;
538 static const size_t DynamicNum = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700539 static const SizeType CurrentRange = 1;
540 static const SizeType TotalSize = 1;
541
542 BoundsRanges (const BoundsRanges &) = default;
543
544 // TODO : following signature is for work around VS bug
545 template <typename OtherType>
Kosov Eugene3402b922015-09-28 21:20:02 +0300546 BoundsRanges (const OtherType &, bool /* firstLevel */) {}
galikcab9bda2015-09-19 07:52:30 +0100547 BoundsRanges(const SizeType * const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700548 BoundsRanges() = default;
549
550
Kern Handae1570262015-09-25 00:42:38 -0700551 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700552 void serialize(T &) const {
553 }
Kern Handae1570262015-09-25 00:42:38 -0700554 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700555 SizeType linearize(const T &) const {
556 return 0;
557 }
Kern Handae1570262015-09-25 00:42:38 -0700558 template <typename T, size_t Dim>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700559 ptrdiff_t contains(const T &) const {
560 return 0;
561 }
562
Neil MacIntoshd5316802015-09-30 21:54:08 -0700563 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700564 return TotalSize;
565 }
566
Neil MacIntoshd5316802015-09-30 21:54:08 -0700567 bool operator == (const BoundsRanges &) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700568 {
569 return true;
570 }
571 };
572
573 template <typename SizeType, size_t... RestRanges>
574 struct BoundsRanges <SizeType, dynamic_range, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
575 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700576 static const size_t Depth = Base::Depth + 1;
577 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700578 static const SizeType CurrentRange = dynamic_range;
579 static const SizeType TotalSize = dynamic_range;
580 const SizeType m_bound;
581
582 BoundsRanges (const BoundsRanges &) = default;
583 BoundsRanges(const SizeType * const arr) : Base(arr + 1), m_bound(static_cast<SizeType>(*arr * this->Base::totalSize()))
584 {
585 fail_fast_assert(0 <= *arr);
586 fail_fast_assert(*arr * this->Base::totalSize() <= details::SizeTypeTraits<SizeType>::max_value);
587 }
588 BoundsRanges() : m_bound(0) {}
589
590 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
Kosov Eugene3402b922015-09-28 21:20:02 +0300591 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool /* firstLevel */ = true) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700592 Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false), m_bound (static_cast<SizeType>(other.totalSize()))
593 {
594 }
595
Kern Handae1570262015-09-25 00:42:38 -0700596 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700597 void serialize(T & arr) const {
598 arr[Dim] = elementNum();
599 this->Base::template serialize<T, Dim + 1>(arr);
600 }
Kern Handae1570262015-09-25 00:42:38 -0700601 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700602 SizeType linearize(const T & arr) const {
603 const size_t index = this->Base::totalSize() * arr[Dim];
604 fail_fast_assert(index < static_cast<size_t>(m_bound));
605 return static_cast<SizeType>(index) + this->Base::template linearize<T, Dim + 1>(arr);
606 }
607
Kern Handae1570262015-09-25 00:42:38 -0700608 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700609 ptrdiff_t contains(const T & arr) const {
610 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
611 if (last == -1)
612 return -1;
613 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
614 return static_cast<size_t>(cur) < static_cast<size_t>(m_bound) ? cur + last : -1;
615 }
616
Neil MacIntoshd5316802015-09-30 21:54:08 -0700617 size_t totalSize() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700618 return m_bound;
619 }
620
Neil MacIntoshd5316802015-09-30 21:54:08 -0700621 SizeType elementNum() const noexcept {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700622 return static_cast<SizeType>(totalSize() / this->Base::totalSize());
623 }
624
Neil MacIntoshd5316802015-09-30 21:54:08 -0700625 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700626 if (dim > 0)
627 return this->Base::elementNum(dim - 1);
628 else
629 return elementNum();
630 }
631
Neil MacIntoshd5316802015-09-30 21:54:08 -0700632 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700633 {
634 return m_bound == rhs.m_bound && static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
635 }
636 };
637
638 template <typename SizeType, size_t CurRange, size_t... RestRanges>
639 struct BoundsRanges <SizeType, CurRange, RestRanges...> : BoundsRanges<SizeType, RestRanges...>{
640 using Base = BoundsRanges <SizeType, RestRanges... >;
Kern Handae1570262015-09-25 00:42:38 -0700641 static const size_t Depth = Base::Depth + 1;
642 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700643 static const SizeType CurrentRange = static_cast<SizeType>(CurRange);
644 static const SizeType TotalSize = StaticSizeHelper<SizeType, Base::TotalSize, CurrentRange>::value;
645 static_assert (CurRange <= SizeTypeTraits<SizeType>::max_value, "CurRange must be smaller than SizeType limits");
646
647 BoundsRanges (const BoundsRanges &) = default;
648 BoundsRanges(const SizeType * const arr) : Base(arr) { }
649 BoundsRanges() = default;
650
651 template <typename OtherSizeType, size_t OtherRange, size_t... RestOtherRanges>
652 BoundsRanges(const BoundsRanges<OtherSizeType, OtherRange, RestOtherRanges...> &other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<OtherSizeType, RestOtherRanges...>&>(other), false)
653 {
654 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
655 }
656
Kern Handae1570262015-09-25 00:42:38 -0700657 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700658 void serialize(T & arr) const {
659 arr[Dim] = elementNum();
660 this->Base::template serialize<T, Dim + 1>(arr);
661 }
662
Kern Handae1570262015-09-25 00:42:38 -0700663 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700664 SizeType linearize(const T & arr) const {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700665 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700666 return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
667 }
668
Kern Handae1570262015-09-25 00:42:38 -0700669 template <typename T, size_t Dim = 0>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700670 ptrdiff_t contains(const T & arr) const {
671 if (static_cast<size_t>(arr[Dim]) >= CurrentRange)
672 return -1;
673 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
674 if (last == -1)
675 return -1;
676 return static_cast<ptrdiff_t>(this->Base::totalSize() * arr[Dim]) + last;
677 }
678
Neil MacIntoshd5316802015-09-30 21:54:08 -0700679 size_t totalSize() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700680 return CurrentRange * this->Base::totalSize();
681 }
682
Neil MacIntoshd5316802015-09-30 21:54:08 -0700683 SizeType elementNum() const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700684 return CurrentRange;
685 }
686
Neil MacIntoshd5316802015-09-30 21:54:08 -0700687 SizeType elementNum(size_t dim) const noexcept{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700688 if (dim > 0)
689 return this->Base::elementNum(dim - 1);
690 else
691 return elementNum();
692 }
693
Neil MacIntoshd5316802015-09-30 21:54:08 -0700694 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700695 {
696 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
697 }
698 };
699
700 template <typename SourceType, typename TargetType, size_t Rank>
701 struct BoundsRangeConvertible2;
702
703 // TODO: I have to rewrite BoundsRangeConvertible into following way to workaround VS 2013 bugs
704 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
705 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
706
707 template <size_t Rank, typename SourceType, typename TargetType>
708 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
709
710 template <typename SourceType, typename TargetType, size_t Rank>
711 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
712 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
713 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
714 {};
715
716 template <typename SourceType, typename TargetType>
717 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
718
719 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
720 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
721 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
722 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
723 {};
724 template <typename SourceType, typename TargetType>
725 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
726
727 template <typename TypeChain>
728 struct TypeListIndexer
729 {
730 const TypeChain & obj;
731 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700732 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700733 const TypeChain & getObj(std::true_type)
734 {
735 return obj;
736 }
Kern Handae1570262015-09-25 00:42:38 -0700737 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700738 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
739 {
740 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
741 }
Kern Handae1570262015-09-25 00:42:38 -0700742 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700743 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
744 {
745 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
746 }
747 };
748
749 template <typename TypeChain>
750 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
751 {
752 return TypeListIndexer<TypeChain>(obj);
753 }
754}
755
756template <typename IndexType>
757class bounds_iterator;
758
759template <typename SizeType, size_t... Ranges>
760class static_bounds {
761public:
Kosov Eugene3402b922015-09-28 21:20:02 +0300762 static_bounds(const details::BoundsRanges<SizeType, Ranges...> &) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700763 }
764};
765
766template <typename SizeType, size_t FirstRange, size_t... RestRanges>
767class static_bounds<SizeType, FirstRange, RestRanges...>
768{
769 using MyRanges = details::BoundsRanges <SizeType, FirstRange, RestRanges... >;
770 static_assert(std::is_integral<SizeType>::value
771 && details::SizeTypeTraits<SizeType>::max_value <= SIZE_MAX, "SizeType must be an integral type and its numeric limits must be smaller than SIZE_MAX");
772
773 MyRanges m_ranges;
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700774 constexpr static_bounds(const MyRanges & range) : m_ranges(range) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700775
776 template <typename SizeType2, size_t... Ranges2>
777 friend class static_bounds;
778public:
Kern Handae1570262015-09-25 00:42:38 -0700779 static const size_t rank = MyRanges::Depth;
780 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700781 static const SizeType static_size = static_cast<SizeType>(MyRanges::TotalSize);
782
783 using size_type = SizeType;
784 using index_type = index<rank, size_type>;
785 using iterator = bounds_iterator<index_type>;
786 using const_iterator = bounds_iterator<index_type>;
787 using difference_type = ptrdiff_t;
788 using sliced_type = static_bounds<SizeType, RestRanges...>;
789 using mapping_type = contiguous_mapping_tag;
790public:
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700791 constexpr static_bounds(const static_bounds &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700792
793 template <typename OtherSizeType, size_t... Ranges, typename Dummy = std::enable_if_t<
794 details::BoundsRangeConvertible<details::BoundsRanges<OtherSizeType, Ranges...>, details::BoundsRanges <SizeType, FirstRange, RestRanges... >>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700795 constexpr static_bounds(const static_bounds<OtherSizeType, Ranges...> &other):
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700796 m_ranges(other.m_ranges)
797 {
798 }
799
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700800 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700801 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700802 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
803 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 -0700804 }
805
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700806 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700807
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700808 constexpr static_bounds & operator = (const static_bounds & otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700809 {
810 new(&m_ranges) MyRanges (otherBounds.m_ranges);
811 return *this;
812 }
813
Neil MacIntoshd5316802015-09-30 21:54:08 -0700814 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700815 {
816 return sliced_type{static_cast<const details::BoundsRanges<SizeType, RestRanges...> &>(m_ranges)};
817 }
818
Neil MacIntoshd5316802015-09-30 21:54:08 -0700819 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700820 {
821 return rank > 1 ? slice().size() : 1;
822 }
823
Neil MacIntoshd5316802015-09-30 21:54:08 -0700824 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700825 {
826 return static_cast<size_type>(m_ranges.totalSize());
827 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700828
Neil MacIntoshd5316802015-09-30 21:54:08 -0700829 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700830 {
831 return static_cast<size_type>(m_ranges.totalSize());
832 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700833
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700834 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700835 {
836 return m_ranges.linearize(idx);
837 }
838
Neil MacIntoshd5316802015-09-30 21:54:08 -0700839 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700840 {
841 return m_ranges.contains(idx) != -1;
842 }
843
Neil MacIntoshd5316802015-09-30 21:54:08 -0700844 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700845 {
846 return m_ranges.elementNum(index);
847 }
848
Kern Handae1570262015-09-25 00:42:38 -0700849 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700850 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700851 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700852 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700853 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
854 }
855
Neil MacIntoshd5316802015-09-30 21:54:08 -0700856 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700857 {
858 index_type extents;
859 m_ranges.serialize(extents);
860 return extents;
861 }
862
863 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700864 constexpr bool operator == (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700865 {
866 return this->size() == rhs.size();
867 }
868
869 template <typename OtherSizeTypes, size_t... Ranges>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700870 constexpr bool operator != (const static_bounds<OtherSizeTypes, Ranges...> & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700871 {
872 return !(*this == rhs);
873 }
874
Neil MacIntoshd5316802015-09-30 21:54:08 -0700875 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700876 {
877 return const_iterator(*this);
878 }
879
Neil MacIntoshd5316802015-09-30 21:54:08 -0700880 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700881 {
882 index_type boundary;
883 m_ranges.serialize(boundary);
884 return const_iterator(*this, this->index_bounds());
885 }
886};
887
Kern Handae1570262015-09-25 00:42:38 -0700888template <size_t Rank, typename SizeType = size_t>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700889class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>
890{
891 using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
892 friend Base;
Kern Handae1570262015-09-25 00:42:38 -0700893 template <size_t OtherRank, typename OtherSizeType>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700894 friend class strided_bounds;
895
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700896public:
897 using Base::rank;
898 using reference = typename Base::reference;
899 using const_reference = typename Base::const_reference;
900 using size_type = typename Base::value_type;
901 using difference_type = typename Base::value_type;
902 using value_type = typename Base::value_type;
903 using index_type = index<rank, size_type>;
904 using iterator = bounds_iterator<index_type>;
905 using const_iterator = bounds_iterator<index_type>;
906 static const int dynamic_rank = rank;
907 static const size_t static_size = dynamic_range;
908 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
909 using mapping_type = generalized_mapping_tag;
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700910 constexpr strided_bounds(const strided_bounds &) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700911
912 template <typename OtherSizeType>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700913 constexpr strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700914 : Base(other), m_strides(other.strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700915 {
916 }
917
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700918 constexpr strided_bounds(const index_type &extents, const index_type &strides)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700919 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700920 {
Kern Handae1570262015-09-25 00:42:38 -0700921 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700922 Base::elems[i] = extents[i];
923 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700924 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700925 : Base(values), m_strides(std::move(strides))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700926 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700927 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700928 constexpr index_type strides() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700929 {
930 return m_strides;
931 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700932 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700933 {
934 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700935 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700936 ret += (Base::elems[i] - 1) * m_strides[i];
937 return ret + 1;
938 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700939 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700940 {
941 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700942 for (size_t i = 0; i < rank; ++i)
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700943 ret *= Base::elems[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700944 return ret;
945 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700946 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700947 {
Kern Handae1570262015-09-25 00:42:38 -0700948 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700949 {
950 if (idx[i] < 0 || idx[i] >= Base::elems[i])
951 return false;
952 }
953 return true;
954 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700955 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700956 {
957 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700958 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700959 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700960 fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
961 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700962 }
963 return ret;
964 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700965 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700966 {
967 return m_strides[0];
968 }
969 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700970 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700971 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700972 return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700973 }
Kern Handae1570262015-09-25 00:42:38 -0700974 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700975 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700976 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700977 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700978 return Base::elems[Dim];
979 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700980 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700982 return index_type(Base::elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700983 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700984 const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700985 {
986 return const_iterator{ *this };
987 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700988 const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700989 {
990 return const_iterator{ *this, index_bounds() };
991 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700992private:
993 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700994};
995
996template <typename T>
997struct is_bounds : std::integral_constant<bool, false> {};
998template <typename SizeType, size_t... Ranges>
999struct is_bounds<static_bounds<SizeType, Ranges...>> : std::integral_constant<bool, true> {};
Kern Handae1570262015-09-25 00:42:38 -07001000template <size_t Rank, typename SizeType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001001struct is_bounds<strided_bounds<Rank, SizeType>> : std::integral_constant<bool, true> {};
1002
1003template <typename IndexType>
1004class bounds_iterator
1005 : public std::iterator<std::random_access_iterator_tag,
1006 IndexType,
1007 ptrdiff_t,
1008 const details::arrow_proxy<IndexType>,
1009 const IndexType>
1010{
1011private:
1012 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
1013public:
Kern Handae1570262015-09-25 00:42:38 -07001014 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001015 using typename Base::reference;
1016 using typename Base::pointer;
1017 using typename Base::difference_type;
1018 using typename Base::value_type;
1019 using index_type = value_type;
1020 using index_size_type = typename IndexType::size_type;
1021 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001022 explicit bounds_iterator(const Bounds & bnd, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001023 : boundary(bnd.index_bounds())
1024 , curr( std::move(curr) )
1025 {
1026 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
1027 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001028 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001029 {
1030 return curr;
1031 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001032 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001033 {
1034 return details::arrow_proxy<value_type>{ curr };
1035 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001036 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001037 {
Kern Handae1570262015-09-25 00:42:38 -07001038 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001039 {
1040 if (++curr[i] < boundary[i])
1041 {
1042 return *this;
1043 }
1044 else
1045 {
1046 curr[i] = 0;
1047 }
1048 }
1049 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -07001050 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001051 {
1052 curr[i] = boundary[i];
1053 }
1054 return *this;
1055 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001056 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001057 {
1058 auto ret = *this;
1059 ++(*this);
1060 return ret;
1061 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001062 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001063 {
Neil MacIntoshfb913932015-09-27 16:25:43 -07001064 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001065 {
1066 if (curr[i]-- > 0)
1067 {
1068 return *this;
1069 }
1070 else
1071 {
1072 curr[i] = boundary[i] - 1;
1073 }
1074 }
1075 // If we're here the preconditions were violated
1076 // "pre: there exists s such that r == ++s"
1077 fail_fast_assert(false);
1078 return *this;
1079 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001080 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001081 {
1082 auto ret = *this;
1083 --(*this);
1084 return ret;
1085 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001086 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001087 {
1088 bounds_iterator ret{ *this };
1089 return ret += n;
1090 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001091 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001092 {
1093 auto linear_idx = linearize(curr) + n;
1094 value_type stride;
1095 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -07001096 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001097 {
1098 stride[i] = stride[i + 1] * boundary[i + 1];
1099 }
Kern Handae1570262015-09-25 00:42:38 -07001100 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001101 {
1102 curr[i] = linear_idx / stride[i];
1103 linear_idx = linear_idx % stride[i];
1104 }
1105 return *this;
1106 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001107 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001108 {
1109 bounds_iterator ret{ *this };
1110 return ret -= n;
1111 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001112 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001113 {
1114 return *this += -n;
1115 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001116 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001117 {
1118 return linearize(curr) - linearize(rhs.curr);
1119 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001120 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001121 {
1122 return *(*this + n);
1123 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001124 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001125 {
1126 return curr == rhs.curr;
1127 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001128 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001129 {
1130 return !(*this == rhs);
1131 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001132 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001133 {
Kern Handae1570262015-09-25 00:42:38 -07001134 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001135 {
1136 if (curr[i] < rhs.curr[i])
1137 return true;
1138 }
1139 return false;
1140 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001141 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001142 {
1143 return !(rhs < *this);
1144 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001145 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001146 {
1147 return rhs < *this;
1148 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001149 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001150 {
1151 return !(rhs > *this);
1152 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001153 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001154 {
1155 std::swap(boundary, rhs.boundary);
1156 std::swap(curr, rhs.curr);
1157 }
1158private:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001159 index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001160 {
1161 // TODO: Smarter impl.
1162 // Check if past-the-end
1163 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001164 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001165 {
1166 if (idx[i] != boundary[i])
1167 {
1168 pte = false;
1169 break;
1170 }
1171 }
1172 index_size_type multiplier = 1;
1173 index_size_type res = 0;
1174 if (pte)
1175 {
1176 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001177 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001178 {
1179 res += (idx[i] - 1) * multiplier;
1180 multiplier *= boundary[i];
1181 }
1182 }
1183 else
1184 {
Kern Handae1570262015-09-25 00:42:38 -07001185 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001186 {
1187 res += idx[i] * multiplier;
1188 multiplier *= boundary[i];
1189 }
1190 }
1191 return res;
1192 }
1193 value_type boundary;
1194 value_type curr;
1195};
1196
1197template <typename SizeType>
1198class bounds_iterator<index<1, SizeType>>
1199 : public std::iterator<std::random_access_iterator_tag,
1200 index<1, SizeType>,
1201 ptrdiff_t,
1202 const details::arrow_proxy<index<1, SizeType>>,
1203 const index<1, SizeType>>
1204{
1205 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>>;
1206
1207public:
1208 using typename Base::reference;
1209 using typename Base::pointer;
1210 using typename Base::difference_type;
1211 using typename Base::value_type;
1212 using index_type = value_type;
1213 using index_size_type = typename index_type::size_type;
1214
1215 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001216 explicit bounds_iterator(const Bounds &, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001217 : curr( std::move(curr) )
1218 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -07001219 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001220 {
1221 return curr;
1222 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001223 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001224 {
1225 return details::arrow_proxy<value_type>{ curr };
1226 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001227 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001228 {
1229 ++curr;
1230 return *this;
1231 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001232 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001233 {
1234 auto ret = *this;
1235 ++(*this);
1236 return ret;
1237 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001238 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001239 {
1240 curr--;
1241 return *this;
1242 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001243 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001244 {
1245 auto ret = *this;
1246 --(*this);
1247 return ret;
1248 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001249 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001250 {
1251 bounds_iterator ret{ *this };
1252 return ret += n;
1253 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001254 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001255 {
1256 curr += n;
1257 return *this;
1258 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001259 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001260 {
1261 bounds_iterator ret{ *this };
1262 return ret -= n;
1263 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001264 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001265 {
1266 return *this += -n;
1267 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001268 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001269 {
1270 return curr[0] - rhs.curr[0];
1271 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001272 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001273 {
1274 return curr + n;
1275 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001276 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001277 {
1278 return curr == rhs.curr;
1279 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001280 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001281 {
1282 return !(*this == rhs);
1283 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001284 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001285 {
1286 return curr[0] < rhs.curr[0];
1287 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001288 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001289 {
1290 return !(rhs < *this);
1291 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001292 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001293 {
1294 return rhs < *this;
1295 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001296 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001297 {
1298 return !(rhs > *this);
1299 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001300 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001301 {
1302 std::swap(curr, rhs.curr);
1303 }
1304private:
1305 value_type curr;
1306};
1307
1308template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001309bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001310{
1311 return rhs + n;
1312}
1313
1314/*
1315** begin definitions of basic_array_view
1316*/
1317namespace details
1318{
1319 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001320 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 -07001321 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001322 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001323 }
1324
Neil MacIntosh99746e22015-09-27 16:53:58 -07001325 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001326 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001327 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 -07001328 {
1329 auto extents = bnd.index_bounds();
1330 typename Bounds::index_type stride;
1331 stride[Bounds::rank - 1] = 1;
Neil MacIntosh99746e22015-09-27 16:53:58 -07001332 for (size_t i = Bounds::rank - 1; Bounds::rank > 1 && i > 0; --i)
1333 stride[i-1] = stride[i] * extents[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001334 return stride;
1335 }
1336
1337 template <typename BoundsSrc, typename BoundsDest>
1338 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1339 {
1340 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1341 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1342 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");
1343 fail_fast_assert(src.size() == dest.size());
1344 }
1345
1346
1347} // namespace details
1348
1349template <typename ArrayView>
1350class contiguous_array_view_iterator;
1351template <typename ArrayView>
1352class general_array_view_iterator;
1353enum class byte : std::uint8_t {};
1354
1355template <typename ValueType, typename BoundsType>
1356class basic_array_view
1357{
1358public:
Kern Handae1570262015-09-25 00:42:38 -07001359 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001360 using bounds_type = BoundsType;
1361 using size_type = typename bounds_type::size_type;
1362 using index_type = typename bounds_type::index_type;
1363 using value_type = ValueType;
1364 using pointer = ValueType*;
1365 using reference = ValueType&;
1366 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>>;
1367 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>>>;
1368 using reverse_iterator = std::reverse_iterator<iterator>;
1369 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1370 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1371
1372private:
1373 pointer m_pdata;
1374 bounds_type m_bounds;
1375
1376public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001377 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001378 {
1379 return m_bounds;
1380 }
Kern Handae1570262015-09-25 00:42:38 -07001381 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001382 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001383 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001384 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001385 return m_bounds.template extent<Dim>();
1386 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001387 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001388 {
1389 return m_bounds.size();
1390 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001391 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001392 {
1393 return m_pdata[m_bounds.linearize(idx)];
1394 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001395 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001396 {
1397 return m_pdata;
1398 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001399 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001400 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001401 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001402 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001403 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001404
1405 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001406 return Ret {m_pdata + ridx, m_bounds.slice()};
1407 }
1408
Neil MacIntoshd5316802015-09-30 21:54:08 -07001409 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001410 {
1411 return m_pdata != nullptr;
1412 }
1413
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001414 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001415 {
1416 return iterator {this, true};
1417 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001418 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001419 {
1420 return iterator {this};
1421 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001422 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001423 {
1424 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1425 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001426 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001427 {
1428 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1429 }
1430
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001431 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001432 {
1433 return reverse_iterator {end()};
1434 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001435 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001436 {
1437 return reverse_iterator {begin()};
1438 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001439 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001440 {
1441 return const_reverse_iterator {cend()};
1442 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001443 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001444 {
1445 return const_reverse_iterator {cbegin()};
1446 }
1447
1448 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 -07001449 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001450 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001451 return m_bounds.size() == other.m_bounds.size() &&
1452 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001453 }
1454
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001455 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 -07001456 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001457 {
1458 return !(*this == other);
1459 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001460
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001461 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001462 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001463 {
1464 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1465 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001466
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001467 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001468 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001469 {
1470 return !(other < *this);
1471 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001472
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001473 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001474 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001475 {
1476 return (other < *this);
1477 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001478
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001479 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001480 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001481 {
1482 return !(*this < other);
1483 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001484
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001485public:
1486 template <typename OtherValueType, typename OtherBounds,
1487 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1488 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001489 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001490 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1491 {
1492 }
1493protected:
1494
Neil MacIntoshd5316802015-09-30 21:54:08 -07001495 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001496 : m_pdata(data)
1497 , m_bounds(std::move(bound))
1498 {
1499 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1500 }
1501 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001502 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 -07001503 : m_pdata(reinterpret_cast<pointer>(data))
1504 , m_bounds(std::move(bound))
1505 {
1506 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1507 }
1508 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001509 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001510 {
1511 details::verifyBoundsReshape(m_bounds, bounds);
1512 return {m_pdata, bounds};
1513 }
1514private:
1515
1516 friend iterator;
1517 friend const_iterator;
1518 template <typename ValueType2, typename BoundsType2>
1519 friend class basic_array_view;
1520};
1521
1522template <size_t DimSize = dynamic_range>
1523struct dim
1524{
1525 static const size_t value = DimSize;
1526};
1527template <>
1528struct dim<dynamic_range>
1529{
1530 static const size_t value = dynamic_range;
1531 const size_t dvalue;
1532 dim(size_t size) : dvalue(size) {}
1533};
1534
1535template <typename ValueTypeOpt, size_t FirstDimension = dynamic_range, size_t... RestDimensions>
1536class array_view;
Kern Handae1570262015-09-25 00:42:38 -07001537template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001538class strided_array_view;
1539
1540namespace details
1541{
1542 template <typename T, typename = std::true_type>
1543 struct ArrayViewTypeTraits
1544 {
1545 using value_type = T;
1546 using size_type = size_t;
1547 };
1548
1549 template <typename Traits>
1550 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1551 {
1552 using value_type = typename Traits::array_view_traits::value_type;
1553 using size_type = typename Traits::array_view_traits::size_type;
1554 };
1555
1556 template <typename T, typename SizeType, size_t... Ranks>
1557 struct ArrayViewArrayTraits {
1558 using type = array_view<T, Ranks...>;
1559 using value_type = T;
1560 using bounds_type = static_bounds<SizeType, Ranks...>;
1561 using pointer = T*;
1562 using reference = T&;
1563 };
1564 template <typename T, typename SizeType, size_t N, size_t... Ranks>
1565 struct ArrayViewArrayTraits<T[N], SizeType, Ranks...> : ArrayViewArrayTraits<T, SizeType, Ranks..., N> {};
1566
1567 template <typename BoundsType>
1568 BoundsType newBoundsHelperImpl(size_t totalSize, std::true_type) // dynamic size
1569 {
1570 fail_fast_assert(totalSize <= details::SizeTypeTraits<typename BoundsType::size_type>::max_value);
1571 return BoundsType{static_cast<typename BoundsType::size_type>(totalSize)};
1572 }
1573 template <typename BoundsType>
1574 BoundsType newBoundsHelperImpl(size_t totalSize, std::false_type) // static size
1575 {
1576 fail_fast_assert(BoundsType::static_size == totalSize);
1577 return {};
1578 }
1579 template <typename BoundsType>
1580 BoundsType newBoundsHelper(size_t totalSize)
1581 {
1582 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1583 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1584 }
1585
1586 struct Sep{};
1587
1588 template <typename T, typename... Args>
1589 T static_as_array_view_helper(Sep, Args... args)
1590 {
1591 return T{static_cast<typename T::size_type>(args)...};
1592 }
1593 template <typename T, typename Arg, typename... Args>
1594 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)
1595 {
1596 return static_as_array_view_helper<T>(args...);
1597 }
1598 template <typename T, typename... Args>
1599 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1600 {
1601 return static_as_array_view_helper<T>(args..., val.dvalue);
1602 }
1603
1604 template <typename SizeType, typename ...Dimensions>
1605 struct static_as_array_view_static_bounds_helper
1606 {
1607 using type = static_bounds<SizeType, (Dimensions::value)...>;
1608 };
1609
1610 template <typename T>
1611 struct is_array_view_oracle : std::false_type
1612 {};
1613 template <typename ValueType, size_t FirstDimension, size_t... RestDimensions>
1614 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1615 {};
Kern Handae1570262015-09-25 00:42:38 -07001616 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001617 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1618 {};
1619 template <typename T>
1620 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1621 {};
1622
1623}
1624
1625
1626template <typename ValueType, typename SizeType>
1627struct array_view_options
1628{
1629 struct array_view_traits
1630 {
1631 using value_type = ValueType;
1632 using size_type = SizeType;
1633 };
1634};
1635
1636template <typename ValueTypeOpt, size_t FirstDimension, size_t... RestDimensions>
1637class array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
1638 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>
1639{
1640 template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
1641 friend class array_view;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001642 using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001643 static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001644
1645public:
1646 using typename Base::bounds_type;
1647 using typename Base::size_type;
1648 using typename Base::pointer;
1649 using typename Base::value_type;
1650 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001651 using typename Base::iterator;
1652 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001653 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001654 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001655
1656public:
1657 // basic
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001658 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001659 {
1660 }
1661
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001662 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001663 {
1664 }
1665
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001666 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001667 {
1668 fail_fast_assert(size == 0);
1669 }
1670
1671 // default
1672 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001673 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001674 {
1675 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001676
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001677 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1678 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
Anna Gringauze18cd9802015-09-14 16:34:26 -07001679 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
1680 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001681 constexpr array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001682 {
1683 }
1684
1685 // from n-dimensions static array
1686 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
1687 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 -07001688 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001689 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001690 {
1691 }
1692
1693 // from n-dimensions static array with size
1694 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
1695 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 -07001696 && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001697 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001698 {
1699 fail_fast_assert(size <= N);
1700 }
1701
1702 // from std array
1703 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 -07001704 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 -07001705 {
1706 }
1707
1708 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 -07001709 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 -07001710 {
1711 }
1712
1713
1714 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1715 template <typename Ptr,
1716 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1717 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>> // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001718 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 -07001719 {
1720 }
1721
1722 // from containers. It must has .size() and .data() two function signatures
1723 template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
1724 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001725 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001726 && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value
1727 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001728 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001729 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 -07001730 {
1731
1732 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001733
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001734 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001735
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001736 // convertible
1737 template <typename OtherValueTypeOpt, size_t... OtherDimensions,
1738 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>,
1739 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, static_bounds<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type, OtherDimensions...>>,
1740 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1741 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001742 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 -07001743
1744 // reshape
1745 template <typename... Dimensions2>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001746 constexpr array_view<ValueTypeOpt, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001747 {
1748 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001749 using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001750 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1751 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001752 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001753 }
1754
1755 // to bytes array
1756 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001757 constexpr auto as_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001758 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)>
1759 {
1760 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001761 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001762 }
1763
1764 template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001765 constexpr auto as_writeable_bytes() const noexcept ->
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001766 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)>
1767 {
1768 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001769 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001770 }
1771
Anna Gringauze18cd9802015-09-14 16:34:26 -07001772
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001773 // from bytes array
1774 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 -07001775 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 -07001776 {
1777 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1778 "Target type must be standard layout and its size must match the byte array size");
1779 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001780 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001781 }
1782
1783 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 -07001784 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 -07001785 {
1786 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1787 "Target type must be standard layout and its size must match the byte array size");
1788 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001789 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001790 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001791
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001792 // section on linear space
1793 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001794 constexpr array_view<ValueTypeOpt, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001795 {
1796 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1797 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 -07001798 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001799 }
1800
Neil MacIntoshd5316802015-09-30 21:54:08 -07001801 constexpr array_view<ValueTypeOpt, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802 {
1803 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001804 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001805 }
1806
1807 template<size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001808 constexpr array_view<ValueTypeOpt, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001809 {
1810 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1811 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001812 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001813 }
1814
Neil MacIntoshd5316802015-09-30 21:54:08 -07001815 constexpr array_view<ValueTypeOpt, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001816 {
1817 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001818 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001819 }
1820
1821 template<size_t Offset, size_t Count>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001822 constexpr array_view<ValueTypeOpt, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001823 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001824 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");
1825 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 -07001826 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001827 }
1828
Neil MacIntoshd5316802015-09-30 21:54:08 -07001829 constexpr array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001830 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001831 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1832 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001833 }
1834
1835 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001836 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001837 {
1838 return this->size();
1839 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001840 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001841 {
1842 return length();
1843 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001844 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001845 {
1846 return sizeof(value_type) * this->size();
1847 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001848 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001849 {
1850 return bytes();
1851 }
1852
1853 // section
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001854 constexpr strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001855 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001856 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001857 return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
1858 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001859
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001860 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001861 {
1862 return Base::operator[](idx);
1863 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001864
Anna Gringauze1a864982015-09-14 18:55:06 -07001865 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001866 constexpr array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001867 {
1868 auto ret = Base::operator[](idx);
1869 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001870 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001871
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001872 using Base::operator==;
1873 using Base::operator!=;
1874 using Base::operator<;
1875 using Base::operator<=;
1876 using Base::operator>;
1877 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001878};
1879
1880template <typename T, size_t... Dimensions>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001881constexpr 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 -07001882{
1883 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<size_t, Dimensions...>>(args..., details::Sep{})};
1884}
1885
1886template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001887constexpr 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 -07001888{
1889 return {arr, len};
1890}
1891
1892template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001893constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, size_t, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001894{
1895 return {arr};
1896}
1897
1898template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001899constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001900{
1901 return {arr};
1902}
1903
1904template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001905constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001906
1907template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001908constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001909{
1910 return {arr};
1911}
1912
1913template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001914constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001915{
1916 return {begin, end};
1917}
1918
1919template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001920constexpr 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 -07001921 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1922{
1923 return {arr.data(), arr.size()};
1924}
1925
1926template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001927constexpr 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 -07001928 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1929
Kern Handae1570262015-09-25 00:42:38 -07001930template <typename ValueTypeOpt, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001931class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
1932{
1933 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 -07001934
Kern Handae1570262015-09-25 00:42:38 -07001935 template<typename OtherValueOpt, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001936 friend class strided_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001937public:
1938 using Base::rank;
1939 using typename Base::bounds_type;
1940 using typename Base::size_type;
1941 using typename Base::pointer;
1942 using typename Base::value_type;
1943 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001944 using typename Base::iterator;
1945 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001946 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001947
1948 // from static array of size N
1949 template<size_type N>
1950 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1951 {
1952 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1953 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001954
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001955 // from raw data
1956 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001957 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001958 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001959 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001960
1961 // from array view
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001962 template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001963 strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1964 {
1965 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1966 }
1967
1968 // convertible
1969 template <typename OtherValueTypeOpt,
1970 typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
1971 typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
1972 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1973 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001974 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 -07001975 {
1976 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001977
1978 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001979 template <typename OtherValueType>
1980 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 -07001981 {
1982 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1983 auto d = sizeof(OtherValueType) / sizeof(value_type);
1984
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001985 size_type size = this->bounds().total_size() / d;
1986 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 -07001987 }
1988
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001989 strided_array_view section(index_type origin, index_type extents) const
1990 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001991 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001992 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1993 }
1994
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001995 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001996 {
1997 return Base::operator[](idx);
1998 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001999
2000 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002001 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002002 {
2003 auto ret = Base::operator[](idx);
2004 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
2005 }
2006
2007private:
2008 static index_type resize_extent(const index_type& extent, size_t d)
2009 {
2010 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");
2011
2012 index_type ret = extent;
2013 ret[rank - 1] /= d;
2014
2015 return ret;
2016 }
2017
2018 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Kosov Eugene3402b922015-09-28 21:20:02 +03002019 static index_type resize_stride(const index_type& strides, size_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002020 {
2021 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2022
2023 return strides;
2024 }
2025
2026 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
2027 static index_type resize_stride(const index_type& strides, size_t d)
2028 {
2029 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
2030 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");
2031
Neil MacIntosh99746e22015-09-27 16:53:58 -07002032 for (size_t i = rank - 1; i > 0; --i)
2033 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 -07002034
2035 index_type ret = strides / d;
2036 ret[rank - 1] = 1;
2037
2038 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002039 }
2040};
2041
2042template <typename ArrayView>
2043class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2044{
2045 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2046public:
2047 using typename Base::reference;
2048 using typename Base::pointer;
2049 using typename Base::difference_type;
2050private:
2051 template <typename ValueType, typename Bounds>
2052 friend class basic_array_view;
2053 pointer m_pdata;
2054 const ArrayView * m_validator;
2055 void validateThis() const
2056 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07002057 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 -07002058 }
2059 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
2060 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
2061public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002062 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002063 {
2064 validateThis();
2065 return *m_pdata;
2066 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002067 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002068 {
2069 validateThis();
2070 return m_pdata;
2071 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002072 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002073 {
2074 ++m_pdata;
2075 return *this;
2076 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002077 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002078 {
2079 auto ret = *this;
2080 ++(*this);
2081 return ret;
2082 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002083 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002084 {
2085 --m_pdata;
2086 return *this;
2087 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002088 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002089 {
2090 auto ret = *this;
2091 --(*this);
2092 return ret;
2093 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002094 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002095 {
2096 contiguous_array_view_iterator ret{ *this };
2097 return ret += n;
2098 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002099 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002100 {
2101 m_pdata += n;
2102 return *this;
2103 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002104 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002105 {
2106 contiguous_array_view_iterator ret{ *this };
2107 return ret -= n;
2108 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002109 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002110 {
2111 return *this += -n;
2112 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002113 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002114 {
2115 fail_fast_assert(m_validator == rhs.m_validator);
2116 return m_pdata - rhs.m_pdata;
2117 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002118 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002119 {
2120 return *(*this + n);
2121 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002122 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002123 {
2124 fail_fast_assert(m_validator == rhs.m_validator);
2125 return m_pdata == rhs.m_pdata;
2126 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002127 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002128 {
2129 return !(*this == rhs);
2130 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002131 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002132 {
2133 fail_fast_assert(m_validator == rhs.m_validator);
2134 return m_pdata < rhs.m_pdata;
2135 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002136 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002137 {
2138 return !(rhs < *this);
2139 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002140 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002141 {
2142 return rhs < *this;
2143 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002144 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002145 {
2146 return !(rhs > *this);
2147 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002148 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002149 {
2150 std::swap(m_pdata, rhs.m_pdata);
2151 std::swap(m_validator, rhs.m_validator);
2152 }
2153};
2154
2155template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002156contiguous_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 -07002157{
2158 return rhs + n;
2159}
2160
2161template <typename ArrayView>
2162class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2163{
2164 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2165public:
2166 using typename Base::reference;
2167 using typename Base::pointer;
2168 using typename Base::difference_type;
2169 using typename Base::value_type;
2170private:
2171 template <typename ValueType, typename Bounds>
2172 friend class basic_array_view;
2173 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002174 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002175 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2176 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2177 {
2178 }
2179public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002180 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002181 {
2182 return (*m_container)[*m_itr];
2183 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002184 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002185 {
2186 return &(*m_container)[*m_itr];
2187 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002188 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002189 {
2190 ++m_itr;
2191 return *this;
2192 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002193 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002194 {
2195 auto ret = *this;
2196 ++(*this);
2197 return ret;
2198 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002199 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002200 {
2201 --m_itr;
2202 return *this;
2203 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002204 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002205 {
2206 auto ret = *this;
2207 --(*this);
2208 return ret;
2209 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002210 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002211 {
2212 general_array_view_iterator ret{ *this };
2213 return ret += n;
2214 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002215 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002216 {
2217 m_itr += n;
2218 return *this;
2219 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002220 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002221 {
2222 general_array_view_iterator ret{ *this };
2223 return ret -= n;
2224 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002225 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002226 {
2227 return *this += -n;
2228 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002229 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002230 {
2231 fail_fast_assert(m_container == rhs.m_container);
2232 return m_itr - rhs.m_itr;
2233 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002234 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002235 {
2236 return (*m_container)[m_itr[n]];;
2237 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002238 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002239 {
2240 fail_fast_assert(m_container == rhs.m_container);
2241 return m_itr == rhs.m_itr;
2242 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002243 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002244 {
2245 return !(*this == rhs);
2246 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002247 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002248 {
2249 fail_fast_assert(m_container == rhs.m_container);
2250 return m_itr < rhs.m_itr;
2251 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002252 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002253 {
2254 return !(rhs < *this);
2255 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002256 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002257 {
2258 return rhs < *this;
2259 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002260 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002261 {
2262 return !(rhs > *this);
2263 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002264 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002265 {
2266 std::swap(m_itr, rhs.m_itr);
2267 std::swap(m_container, rhs.m_container);
2268 }
2269};
2270
2271template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002272general_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 -07002273{
2274 return rhs + n;
2275}
2276
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002277} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002278
Neil MacIntoshd5316802015-09-30 21:54:08 -07002279#ifdef _MSC_VER
2280
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002281#undef constexpr
2282#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002283
Neil MacIntoshd5316802015-09-30 21:54:08 -07002284#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002285#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002286
2287#ifndef GSL_THROWS_FOR_TESTING
2288#pragma undef noexcept
2289#endif // GSL_THROWS_FOR_TESTING
2290
Neil MacIntosh9a297122015-09-14 15:11:07 -07002291#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002292
Neil MacIntoshd5316802015-09-30 21:54:08 -07002293#endif // _MSC_VER
2294
2295#if defined(GSL_THROWS_FOR_TESTING)
2296#undef noexcept
2297#endif // GSL_THROWS_FOR_TESTING
2298
Treb Connell51da1362015-09-24 18:08:34 -07002299
2300#endif // GSL_ARRAY_VIEW_H