blob: bca973abe03c79d4f78dbf45b531731b31bb86bb [file] [log] [blame]
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
Treb Connell51da1362015-09-24 18:08:34 -070019#ifndef GSL_ARRAY_VIEW_H
20#define GSL_ARRAY_VIEW_H
21
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070022#include <new>
23#include <stdexcept>
24#include <cstddef>
25#include <cstdint>
26#include <limits>
Anna Gringauze2cdedda2015-10-15 13:19:24 -070027#include <numeric>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070028#include <type_traits>
29#include <utility>
30#include <array>
31#include <iterator>
Kern Handac4f9b872015-09-25 17:01:29 -070032#include <algorithm>
Neil MacIntosh01868f22015-10-15 16:48:38 -070033#include <functional>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070034#include "fail_fast.h"
35
Neil MacIntoshd5316802015-09-30 21:54:08 -070036#ifdef _MSC_VER
37
38// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070039#pragma push_macro("constexpr")
40#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070041
42
43// VS 2013 workarounds
44#if _MSC_VER <= 1800
45
Neil MacIntoshe9a96022015-11-03 18:56:55 -080046#pragma push_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG")
47#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
48
49
Neil MacIntoshd5316802015-09-30 21:54:08 -070050// noexcept is not understood
51#ifndef GSL_THROWS_FOR_TESTING
52#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070053#endif
54
Neil MacIntoshd5316802015-09-30 21:54:08 -070055// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070056#pragma warning(push)
57#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntoshd5316802015-09-30 21:54:08 -070058
Neil MacIntosh9a297122015-09-14 15:11:07 -070059#endif // _MSC_VER <= 1800
60
Neil MacIntoshd5316802015-09-30 21:54:08 -070061#endif // _MSC_VER
62
63// In order to test the library, we need it to throw exceptions that we can catch
64#ifdef GSL_THROWS_FOR_TESTING
65#define noexcept /* nothing */
66#endif // GSL_THROWS_FOR_TESTING
67
68
Neil MacIntoshef626fd2015-09-29 16:41:37 -070069namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070070
71/*
72** begin definitions of index and bounds
73*/
74namespace details
75{
76 template <typename SizeType>
77 struct SizeTypeTraits
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070078 {
Anna Gringauze5f26dda2015-10-16 17:30:48 -070079 static const SizeType max_value = std::numeric_limits<SizeType>::max();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070080 };
Anna Gringauze1c208b32015-10-16 17:40:57 -070081
82
83 template<typename... Ts>
84 class are_integral : public std::integral_constant<bool, true> {};
85
86 template<typename T, typename... Ts>
87 class are_integral<T, Ts...> : public std::integral_constant<bool, std::is_integral<T>::value && are_integral<Ts...>::value> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070088}
89
Neil MacIntoshf45fedb2015-10-15 14:29:35 -070090template <size_t Rank>
Anna Gringauzedb384972015-10-05 12:34:23 -070091class index final
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070092{
Anna Gringauzedb384972015-10-05 12:34:23 -070093 static_assert(Rank > 0, "Rank must be greater than 0!");
94
Neil MacIntoshf45fedb2015-10-15 14:29:35 -070095 template <size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -070096 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -070097
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070098public:
Anna Gringauzedb384972015-10-05 12:34:23 -070099 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700100 using value_type = std::ptrdiff_t;
101 using size_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700102 using reference = std::add_lvalue_reference_t<value_type>;
103 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700104
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700105 constexpr index() noexcept
106 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700107
Anna Gringauzedb384972015-10-05 12:34:23 -0700108 constexpr index(const value_type(&values)[Rank]) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700109 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700110 std::copy(values, values + Rank, elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700111 }
112
Neil MacIntoshe9a96022015-11-03 18:56:55 -0800113#ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
114 template<typename T, typename... Ts,
115 typename = std::enable_if_t<((sizeof...(Ts) + 1) == Rank) &&
116 std::is_integral<T>::value &&
117 details::are_integral<Ts...>::value>>
118 constexpr index(T t, Ts... ds) : index({ static_cast<value_type>(t), static_cast<value_type>(ds)... })
119 {}
120#else
121 template<typename... Ts,
122 typename = std::enable_if_t<(sizeof...(Ts) == Rank) && details::are_integral<Ts...>::value>>
123 constexpr index(Ts... ds) noexcept : elems{ static_cast<value_type>(ds)... }
124 {}
125#endif
Anna Gringauzedb384972015-10-05 12:34:23 -0700126
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700127 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700128
Anna Gringauzedb384972015-10-05 12:34:23 -0700129 constexpr index& operator=(const index& rhs) noexcept = default;
130
131 // Preconditions: component_idx < rank
132 constexpr reference operator[](size_t component_idx)
133 {
134 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
135 return elems[component_idx];
136 }
137
138 // Preconditions: component_idx < rank
139 constexpr const_reference operator[](size_t component_idx) const noexcept
140 {
141 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
142 return elems[component_idx];
143 }
144
145 constexpr bool operator==(const index& rhs) const noexcept
146 {
147 return std::equal(elems, elems + rank, rhs.elems);
148 }
149
150 constexpr bool operator!=(const index& rhs) const noexcept
151 {
152 return !(this == rhs);
153 }
154
155 constexpr index operator+() const noexcept
156 {
157 return *this;
158 }
159
160 constexpr index operator-() const noexcept
161 {
162 index ret = *this;
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000163 std::transform(ret, ret + rank, ret, std::negate<value_type>{});
Anna Gringauzedb384972015-10-05 12:34:23 -0700164 return ret;
165 }
166
167 constexpr index operator+(const index& rhs) const noexcept
168 {
169 index ret = *this;
170 ret += rhs;
171 return ret;
172 }
173
174 constexpr index operator-(const index& rhs) const noexcept
175 {
176 index ret = *this;
177 ret -= rhs;
178 return ret;
179 }
180
181 constexpr index& operator+=(const index& rhs) noexcept
182 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700183 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
Anna Gringauzedb384972015-10-05 12:34:23 -0700184 return *this;
185 }
186
187 constexpr index& operator-=(const index& rhs) noexcept
188 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700189 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
Anna Gringauzedb384972015-10-05 12:34:23 -0700190 return *this;
191 }
192
193 constexpr index operator*(value_type v) const noexcept
194 {
195 index ret = *this;
196 ret *= v;
197 return ret;
198 }
199
200 constexpr index operator/(value_type v) const noexcept
201 {
202 index ret = *this;
203 ret /= v;
204 return ret;
205 }
206
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700207 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700208 {
209 return rhs * v;
210 }
211
212 constexpr index& operator*=(value_type v) noexcept
213 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700214 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700215 return *this;
216 }
217
218 constexpr index& operator/=(value_type v) noexcept
219 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700220 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700221 return *this;
222 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700223
Anna Gringauzedb384972015-10-05 12:34:23 -0700224private:
225 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700226};
227
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700228#ifndef _MSC_VER
229
230struct static_bounds_dynamic_range_t
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700231{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700232 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
233 constexpr operator T() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700234 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700235 return static_cast<T>(-1);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700236 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700237
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700238 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
239 constexpr bool operator ==(T other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700240 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700241 return static_cast<T>(-1) == other;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700242 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700243
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700244 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
245 constexpr bool operator !=(T other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700246 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700247 return static_cast<T>(-1) != other;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700248 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700249
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700250};
251
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700252template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
253constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
254{
255 return right == left;
256}
257
258template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
259constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
260{
261 return right != left;
262}
263
264constexpr static_bounds_dynamic_range_t dynamic_range{};
265#else
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700266const std::ptrdiff_t dynamic_range = -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700267#endif
268
269struct generalized_mapping_tag {};
270struct contiguous_mapping_tag : generalized_mapping_tag {};
271
272namespace details
273{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700274
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700275 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700276 struct LessThan
277 {
278 static const bool value = Left < Right;
279 };
280
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700281 template <std::ptrdiff_t... Ranges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700282 struct BoundsRanges {
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000283 using size_type = std::ptrdiff_t;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700284 static const size_type Depth = 0;
285 static const size_type DynamicNum = 0;
286 static const size_type CurrentRange = 1;
287 static const size_type TotalSize = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700288
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700289 // TODO : following signature is for work around VS bug
290 template <typename OtherRange>
291 BoundsRanges(const OtherRange&, bool /* firstLevel */)
292 {}
293
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700294 BoundsRanges (const BoundsRanges&) = default;
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000295 BoundsRanges(const std::ptrdiff_t* const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700296 BoundsRanges() = default;
297
298
Kern Handae1570262015-09-25 00:42:38 -0700299 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700300 void serialize(T&) const
301 {}
302
Kern Handae1570262015-09-25 00:42:38 -0700303 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700304 size_type linearize(const T&) const
305 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700306 return 0;
307 }
308
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700309 template <typename T, size_t Dim>
310 bool contains(const T&) const
311 {
312 return 0;
313 }
314
315 size_type totalSize() const noexcept
316 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700317 return TotalSize;
318 }
319
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700320 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700321 {
322 return true;
323 }
324 };
325
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700326 template <std::ptrdiff_t... RestRanges>
327 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
328 using Base = BoundsRanges <RestRanges... >;
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000329 using size_type = std::ptrdiff_t;
Kern Handae1570262015-09-25 00:42:38 -0700330 static const size_t Depth = Base::Depth + 1;
331 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700332 static const size_type CurrentRange = dynamic_range;
333 static const size_type TotalSize = dynamic_range;
334 const size_type m_bound;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700335
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700336 BoundsRanges (const BoundsRanges&) = default;
337
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000338 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700339 {
340 fail_fast_assert(0 <= *arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700341 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700342
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700343 BoundsRanges() : m_bound(0) {}
344
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700345 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000346 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
347 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
348 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700349
Kern Handae1570262015-09-25 00:42:38 -0700350 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700351 void serialize(T& arr) const
352 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700353 arr[Dim] = elementNum();
354 this->Base::template serialize<T, Dim + 1>(arr);
355 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700356
Kern Handae1570262015-09-25 00:42:38 -0700357 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700358 size_type linearize(const T& arr) const
359 {
360 const size_type index = this->Base::totalSize() * arr[Dim];
361 fail_fast_assert(index < m_bound);
362 return index + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700363 }
364
Kern Handae1570262015-09-25 00:42:38 -0700365 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700366 size_type contains(const T & arr) const
367 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700368 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
369 if (last == -1)
370 return -1;
371 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700372 return cur < m_bound ? cur + last : -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700373 }
374
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700375 size_type totalSize() const noexcept
376 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700377 return m_bound;
378 }
379
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700380 size_type elementNum() const noexcept
381 {
382 return totalSize() / this->Base::totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700383 }
384
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700385 size_type elementNum(size_t dim) const noexcept
386 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700387 if (dim > 0)
388 return this->Base::elementNum(dim - 1);
389 else
390 return elementNum();
391 }
392
Neil MacIntoshd5316802015-09-30 21:54:08 -0700393 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700394 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700395 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700396 }
397 };
398
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700399 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
400 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
401 {
402 using Base = BoundsRanges <RestRanges... >;
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000403 using size_type = std::ptrdiff_t;
Kern Handae1570262015-09-25 00:42:38 -0700404 static const size_t Depth = Base::Depth + 1;
405 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700406 static const size_type CurrentRange = CurRange;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700407 static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700408
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700409 BoundsRanges (const BoundsRanges&) = default;
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000410 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700411 BoundsRanges() = default;
412
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700413 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
414 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>&other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700415 {
416 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
417 }
418
Kern Handae1570262015-09-25 00:42:38 -0700419 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700420 void serialize(T& arr) const
421 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700422 arr[Dim] = elementNum();
423 this->Base::template serialize<T, Dim + 1>(arr);
424 }
425
Kern Handae1570262015-09-25 00:42:38 -0700426 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700427 size_type linearize(const T& arr) const
428 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700429 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700430 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700431 }
432
Kern Handae1570262015-09-25 00:42:38 -0700433 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700434 size_type contains(const T& arr) const
435 {
436 if (arr[Dim] >= CurrentRange)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700437 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700438 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700439 if (last == -1)
440 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700441 return this->Base::totalSize() * arr[Dim] + last;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700442 }
443
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700444 size_type totalSize() const noexcept
445 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700446 return CurrentRange * this->Base::totalSize();
447 }
448
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700449 size_type elementNum() const noexcept
450 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700451 return CurrentRange;
452 }
453
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700454 size_type elementNum(size_t dim) const noexcept
455 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700456 if (dim > 0)
457 return this->Base::elementNum(dim - 1);
458 else
459 return elementNum();
460 }
461
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700462 bool operator== (const BoundsRanges& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700463 {
464 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
465 }
466 };
467
468 template <typename SourceType, typename TargetType, size_t Rank>
469 struct BoundsRangeConvertible2;
470
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700471 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
472 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
473
474 template <size_t Rank, typename SourceType, typename TargetType>
475 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
476
477 template <typename SourceType, typename TargetType, size_t Rank>
478 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
479 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
480 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
481 {};
482
483 template <typename SourceType, typename TargetType>
484 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
485
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000486 template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700487 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
488 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000489 && (!LessThan<SourceType::CurrentRange, TargetType::CurrentRange>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700490 {};
491 template <typename SourceType, typename TargetType>
492 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
493
494 template <typename TypeChain>
495 struct TypeListIndexer
496 {
497 const TypeChain & obj;
498 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700499 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700500 const TypeChain & getObj(std::true_type)
501 {
502 return obj;
503 }
Kern Handae1570262015-09-25 00:42:38 -0700504 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700505 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
506 {
507 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
508 }
Kern Handae1570262015-09-25 00:42:38 -0700509 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700510 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
511 {
512 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
513 }
514 };
515
516 template <typename TypeChain>
517 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
518 {
519 return TypeListIndexer<TypeChain>(obj);
520 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700521
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700522 template <size_t Rank, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
523 constexpr Ret shift_left(const index<Rank>& other) noexcept
Anna Gringauzefdf86432015-10-14 10:46:22 -0700524 {
archshiftb3957172015-11-02 11:47:14 -0800525 Ret ret{};
Anna Gringauzefdf86432015-10-14 10:46:22 -0700526 for (size_t i = 0; i < Rank - 1; ++i)
527 {
528 ret[i] = other[i + 1];
529 }
530 return ret;
531 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700532}
533
534template <typename IndexType>
535class bounds_iterator;
536
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700537template <std::ptrdiff_t... Ranges>
538class static_bounds
539{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700540public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700541 static_bounds(const details::BoundsRanges<Ranges...>&) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700542 }
543};
544
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700545template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
546class static_bounds<FirstRange, RestRanges...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700547{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700548 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700549
550 MyRanges m_ranges;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700551 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
552 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700553
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700554 template <std::ptrdiff_t... OtherRanges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700555 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700556
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700557public:
Kern Handae1570262015-09-25 00:42:38 -0700558 static const size_t rank = MyRanges::Depth;
559 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700560 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700561
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700562 using size_type = std::ptrdiff_t;
563 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700564 using const_index_type = std::add_const_t<index_type>;
565 using iterator = bounds_iterator<const_index_type>;
566 using const_iterator = bounds_iterator<const_index_type>;
567 using difference_type = std::ptrdiff_t;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700568 using sliced_type = static_bounds<RestRanges...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700569 using mapping_type = contiguous_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700570
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700571 constexpr static_bounds(const static_bounds&) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700572
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700573 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
574 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
575 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
576 {}
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700577
Neil MacIntosh41517ff2015-11-04 02:11:49 +0000578 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges((const std::ptrdiff_t*)il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700579 {
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700580 fail_fast_assert((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) || MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700581 fail_fast_assert(m_ranges.totalSize() <= PTRDIFF_MAX, "Size of the range is larger than the max element of the size type");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700582 }
583
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700584 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700585
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700586 constexpr static_bounds& operator=(const static_bounds& otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700587 {
588 new(&m_ranges) MyRanges (otherBounds.m_ranges);
589 return *this;
590 }
591
Neil MacIntoshd5316802015-09-30 21:54:08 -0700592 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700593 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700594 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700595 }
596
Neil MacIntoshd5316802015-09-30 21:54:08 -0700597 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700598 {
599 return rank > 1 ? slice().size() : 1;
600 }
601
Neil MacIntoshd5316802015-09-30 21:54:08 -0700602 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700603 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700604 return m_ranges.totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700605 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700606
Neil MacIntoshd5316802015-09-30 21:54:08 -0700607 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700608 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700609 return m_ranges.totalSize();
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700610 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700611
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700612 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700613 {
614 return m_ranges.linearize(idx);
615 }
616
Neil MacIntoshd5316802015-09-30 21:54:08 -0700617 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700618 {
619 return m_ranges.contains(idx) != -1;
620 }
621
Neil MacIntoshd5316802015-09-30 21:54:08 -0700622 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700623 {
624 return m_ranges.elementNum(index);
625 }
626
Kern Handae1570262015-09-25 00:42:38 -0700627 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700628 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700629 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700630 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700631 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
632 }
633
Neil MacIntoshd5316802015-09-30 21:54:08 -0700634 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700635 {
Anna Gringauzefdf86432015-10-14 10:46:22 -0700636 size_type extents[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700637 m_ranges.serialize(extents);
Anna Gringauzedb384972015-10-05 12:34:23 -0700638 return{ extents };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700639 }
640
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700641 template <std::ptrdiff_t... Ranges>
642 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700643 {
644 return this->size() == rhs.size();
645 }
646
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700647 template <std::ptrdiff_t... Ranges>
648 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700649 {
650 return !(*this == rhs);
651 }
652
Neil MacIntoshd5316802015-09-30 21:54:08 -0700653 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700654 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700655 return const_iterator(*this, index_type{});
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700656 }
657
Neil MacIntoshd5316802015-09-30 21:54:08 -0700658 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700659 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700660 return const_iterator(*this, this->index_bounds());
661 }
662};
663
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700664template <size_t Rank>
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700665class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700666{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700667 template <size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700668 friend class strided_bounds;
669
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700670public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700671 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700672 using value_type = std::ptrdiff_t;
673 using reference = std::add_lvalue_reference_t<value_type>;
674 using const_reference = std::add_const_t<reference>;
675 using size_type = value_type;
676 using difference_type = value_type;
677 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700678 using const_index_type = std::add_const_t<index_type>;
679 using iterator = bounds_iterator<const_index_type>;
680 using const_iterator = bounds_iterator<const_index_type>;
681 static const value_type dynamic_rank = rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700682 static const value_type static_size = dynamic_range;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700683 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
684 using mapping_type = generalized_mapping_tag;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700685
Anna Gringauzedb384972015-10-05 12:34:23 -0700686 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700687
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700688 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700689 : m_extents(values), m_strides(std::move(strides))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700690 {}
691
Anna Gringauzedb384972015-10-05 12:34:23 -0700692 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
693 : m_extents(extents), m_strides(strides)
694 {}
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700695
Neil MacIntoshd5316802015-09-30 21:54:08 -0700696 constexpr index_type strides() const noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700697 {
698 return m_strides;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700699 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700700
Neil MacIntoshd5316802015-09-30 21:54:08 -0700701 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700702 {
703 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700704 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700705 {
706 ret += (m_extents[i] - 1) * m_strides[i];
707 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700708 return ret + 1;
709 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700710
711 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700712 {
713 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700714 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700715 {
716 ret *= m_extents[i];
717 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700718 return ret;
719 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700720
721 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700722 {
Kern Handae1570262015-09-25 00:42:38 -0700723 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700724 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700725 if (idx[i] < 0 || idx[i] >= m_extents[i])
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700726 return false;
727 }
728 return true;
729 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700730
731 constexpr size_type linearize(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700732 {
733 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700734 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700735 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700736 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700737 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700738 }
739 return ret;
740 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700741
742 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700743 {
744 return m_strides[0];
745 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700746
747 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700748 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700749 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700750 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700751 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700752
753 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700754 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700755 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700756 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Anna Gringauzedb384972015-10-05 12:34:23 -0700757 return m_extents[Dim];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700758 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700759
760 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700761 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700762 return m_extents;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700763 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700764 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700765 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700766 return const_iterator{ *this, index_type{} };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700767 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700768
Anna Gringauzea4654a42015-10-16 12:15:22 -0700769 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700770 {
771 return const_iterator{ *this, index_bounds() };
772 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700773
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700774private:
Anna Gringauzedb384972015-10-05 12:34:23 -0700775 index_type m_extents;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700776 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700777};
778
779template <typename T>
780struct is_bounds : std::integral_constant<bool, false> {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700781template <std::ptrdiff_t... Ranges>
782struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> {};
783template <size_t Rank>
784struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700785
786template <typename IndexType>
Anna Gringauzea4654a42015-10-16 12:15:22 -0700787class bounds_iterator: public std::iterator<std::random_access_iterator_tag, IndexType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700788{
789private:
Anna Gringauzea4654a42015-10-16 12:15:22 -0700790 using Base = std::iterator <std::random_access_iterator_tag, IndexType>;
791
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700792public:
Kern Handae1570262015-09-25 00:42:38 -0700793 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700794 using typename Base::reference;
795 using typename Base::pointer;
796 using typename Base::difference_type;
797 using typename Base::value_type;
798 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700799 using index_size_type = typename IndexType::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700800 template <typename Bounds>
Anna Gringauzea4654a42015-10-16 12:15:22 -0700801 explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
802 : boundary(bnd.index_bounds()), curr(std::move(curr))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700803 {
804 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
805 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700806
807 constexpr reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700808 {
809 return curr;
810 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700811
812 constexpr pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700813 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700814 return &curr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700815 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700816
817 constexpr bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700818 {
Kern Handae1570262015-09-25 00:42:38 -0700819 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700820 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700821 if (curr[i] < boundary[i] - 1)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700822 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700823 curr[i]++;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700824 return *this;
825 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700826 curr[i] = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700827 }
828 // If we're here we've wrapped over - set to past-the-end.
Anna Gringauzea4654a42015-10-16 12:15:22 -0700829 curr = boundary;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700830 return *this;
831 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700832
833 constexpr bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700834 {
835 auto ret = *this;
836 ++(*this);
837 return ret;
838 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700839
840 constexpr bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700841 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700842 if (!less(curr, boundary))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700843 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700844 // if at the past-the-end, set to last element
845 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700846 {
847 curr[i] = boundary[i] - 1;
848 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700849 return *this;
850 }
851 for (size_t i = rank; i-- > 0;)
852 {
853 if (curr[i] >= 1)
854 {
855 curr[i]--;
856 return *this;
857 }
858 curr[i] = boundary[i] - 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700859 }
860 // If we're here the preconditions were violated
861 // "pre: there exists s such that r == ++s"
862 fail_fast_assert(false);
863 return *this;
864 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700865
866 constexpr bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700867 {
868 auto ret = *this;
869 --(*this);
870 return ret;
871 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700872
873 constexpr bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700874 {
875 bounds_iterator ret{ *this };
876 return ret += n;
877 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700878
879 constexpr bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700880 {
881 auto linear_idx = linearize(curr) + n;
archshiftb3957172015-11-02 11:47:14 -0800882 std::remove_const_t<value_type> stride = 0;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700883 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -0700884 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700885 {
886 stride[i] = stride[i + 1] * boundary[i + 1];
887 }
Kern Handae1570262015-09-25 00:42:38 -0700888 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700889 {
890 curr[i] = linear_idx / stride[i];
891 linear_idx = linear_idx % stride[i];
892 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700893 fail_fast_assert(!less(curr, index_type{}) && !less(boundary, curr), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700894 return *this;
895 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700896
897 constexpr bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700898 {
899 bounds_iterator ret{ *this };
900 return ret -= n;
901 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700902
903 constexpr bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700904 {
905 return *this += -n;
906 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700907
908 constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700909 {
910 return linearize(curr) - linearize(rhs.curr);
911 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700912
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700913 constexpr value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700914 {
915 return *(*this + n);
916 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700917
918 constexpr bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700919 {
920 return curr == rhs.curr;
921 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700922
923 constexpr bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700924 {
925 return !(*this == rhs);
926 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700927
928 constexpr bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700929 {
Anna Gringauzea4654a42015-10-16 12:15:22 -0700930 return less(curr, rhs.curr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700931 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700932
933 constexpr bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700934 {
935 return !(rhs < *this);
936 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700937
938 constexpr bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700939 {
940 return rhs < *this;
941 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700942
943 constexpr bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700944 {
945 return !(rhs > *this);
946 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700947
Neil MacIntoshd5316802015-09-30 21:54:08 -0700948 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700949 {
950 std::swap(boundary, rhs.boundary);
951 std::swap(curr, rhs.curr);
952 }
953private:
Anna Gringauzea4654a42015-10-16 12:15:22 -0700954 constexpr bool less(index_type& one, index_type& other) const noexcept
955 {
956 for (size_t i = 0; i < rank; ++i)
957 {
958 if (one[i] < other[i])
959 return true;
960 }
961 return false;
962 }
963
964 constexpr index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700965 {
966 // TODO: Smarter impl.
967 // Check if past-the-end
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700968 index_size_type multiplier = 1;
969 index_size_type res = 0;
Anna Gringauzea4654a42015-10-16 12:15:22 -0700970 if (!less(idx, boundary))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700971 {
972 res = 1;
Kern Handae1570262015-09-25 00:42:38 -0700973 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700974 {
975 res += (idx[i] - 1) * multiplier;
976 multiplier *= boundary[i];
977 }
978 }
979 else
980 {
Kern Handae1570262015-09-25 00:42:38 -0700981 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700982 {
983 res += idx[i] * multiplier;
984 multiplier *= boundary[i];
985 }
986 }
987 return res;
988 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700989
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700990 value_type boundary;
Anna Gringauzea4654a42015-10-16 12:15:22 -0700991 std::remove_const_t<value_type> curr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700992};
993
994template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700995bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700996{
997 return rhs + n;
998}
999
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001000//
1001// begin definitions of basic_array_view
1002//
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001003namespace details
1004{
1005 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001006 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 -07001007 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001008 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001009 }
1010
Neil MacIntosh99746e22015-09-27 16:53:58 -07001011 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001012 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001013 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 -07001014 {
1015 auto extents = bnd.index_bounds();
Anna Gringauzefdf86432015-10-14 10:46:22 -07001016 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001017
1018 stride[Bounds::rank - 1] = 1;
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001019 for (size_t i = 1; i < Bounds::rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -07001020 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001021 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
Anna Gringauzedb384972015-10-05 12:34:23 -07001022 }
1023 return{ stride };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001024 }
1025
1026 template <typename BoundsSrc, typename BoundsDest>
1027 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1028 {
1029 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1030 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1031 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");
1032 fail_fast_assert(src.size() == dest.size());
1033 }
1034
1035
1036} // namespace details
1037
1038template <typename ArrayView>
1039class contiguous_array_view_iterator;
1040template <typename ArrayView>
1041class general_array_view_iterator;
1042enum class byte : std::uint8_t {};
1043
1044template <typename ValueType, typename BoundsType>
1045class basic_array_view
1046{
1047public:
Kern Handae1570262015-09-25 00:42:38 -07001048 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001049 using bounds_type = BoundsType;
1050 using size_type = typename bounds_type::size_type;
1051 using index_type = typename bounds_type::index_type;
1052 using value_type = ValueType;
Anna Gringauzea4654a42015-10-16 12:15:22 -07001053 using const_value_type = std::add_const_t<value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001054 using pointer = ValueType*;
1055 using reference = ValueType&;
1056 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>>;
Anna Gringauzea4654a42015-10-16 12:15:22 -07001057 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_value_type, BoundsType>>, general_array_view_iterator<basic_array_view<const_value_type, BoundsType>>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001058 using reverse_iterator = std::reverse_iterator<iterator>;
1059 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1060 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1061
1062private:
1063 pointer m_pdata;
1064 bounds_type m_bounds;
1065
1066public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001067 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001068 {
1069 return m_bounds;
1070 }
Kern Handae1570262015-09-25 00:42:38 -07001071 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001072 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001073 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001074 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001075 return m_bounds.template extent<Dim>();
1076 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001077 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001078 {
1079 return m_bounds.size();
1080 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001081 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001082 {
1083 return m_pdata[m_bounds.linearize(idx)];
1084 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001085 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001086 {
1087 return m_pdata;
1088 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001089 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001090 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001091 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001092 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001093 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001094
1095 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001096 return Ret {m_pdata + ridx, m_bounds.slice()};
1097 }
1098
Neil MacIntoshd5316802015-09-30 21:54:08 -07001099 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001100 {
1101 return m_pdata != nullptr;
1102 }
1103
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001104 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001105 {
1106 return iterator {this, true};
1107 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001108 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001109 {
Anna Gringauzea4654a42015-10-16 12:15:22 -07001110 return iterator {this, false};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001111 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001112 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001113 {
1114 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1115 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001116 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001117 {
Anna Gringauzea4654a42015-10-16 12:15:22 -07001118 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), false};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001119 }
1120
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001121 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001122 {
1123 return reverse_iterator {end()};
1124 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001125 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001126 {
1127 return reverse_iterator {begin()};
1128 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001129 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001130 {
1131 return const_reverse_iterator {cend()};
1132 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001133 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001134 {
1135 return const_reverse_iterator {cbegin()};
1136 }
1137
1138 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 -07001139 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001140 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001141 return m_bounds.size() == other.m_bounds.size() &&
1142 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001143 }
1144
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001145 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 -07001146 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001147 {
1148 return !(*this == other);
1149 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001150
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001151 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 -07001152 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001153 {
1154 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1155 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001156
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001157 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 -07001158 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001159 {
1160 return !(other < *this);
1161 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001162
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001163 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 -07001164 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001165 {
1166 return (other < *this);
1167 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001168
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001169 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 -07001170 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001171 {
1172 return !(*this < other);
1173 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001174
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001175public:
1176 template <typename OtherValueType, typename OtherBounds,
1177 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1178 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001179 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001180 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1181 {
1182 }
1183protected:
1184
Neil MacIntoshd5316802015-09-30 21:54:08 -07001185 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001186 : m_pdata(data)
1187 , m_bounds(std::move(bound))
1188 {
1189 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1190 }
1191 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001192 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 -07001193 : m_pdata(reinterpret_cast<pointer>(data))
1194 , m_bounds(std::move(bound))
1195 {
1196 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1197 }
1198 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001199 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001200 {
1201 details::verifyBoundsReshape(m_bounds, bounds);
1202 return {m_pdata, bounds};
1203 }
1204private:
1205
1206 friend iterator;
1207 friend const_iterator;
1208 template <typename ValueType2, typename BoundsType2>
1209 friend class basic_array_view;
1210};
1211
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001212template <std::ptrdiff_t DimSize = dynamic_range>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001213struct dim
1214{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001215 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001216};
1217template <>
1218struct dim<dynamic_range>
1219{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001220 static const std::ptrdiff_t value = dynamic_range;
1221 const std::ptrdiff_t dvalue;
1222 dim(std::ptrdiff_t size) : dvalue(size) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001223};
1224
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001225template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001226class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001227
1228template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001229class strided_array_view;
1230
1231namespace details
1232{
1233 template <typename T, typename = std::true_type>
1234 struct ArrayViewTypeTraits
1235 {
1236 using value_type = T;
1237 using size_type = size_t;
1238 };
1239
1240 template <typename Traits>
1241 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1242 {
1243 using value_type = typename Traits::array_view_traits::value_type;
1244 using size_type = typename Traits::array_view_traits::size_type;
1245 };
1246
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001247 template <typename T, std::ptrdiff_t... Ranks>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001248 struct ArrayViewArrayTraits {
1249 using type = array_view<T, Ranks...>;
1250 using value_type = T;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001251 using bounds_type = static_bounds<Ranks...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001252 using pointer = T*;
1253 using reference = T&;
1254 };
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001255 template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001256 struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001257
1258 template <typename BoundsType>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001259 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001260 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001261 fail_fast_assert(totalSize <= PTRDIFF_MAX);
1262 return BoundsType{totalSize};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001263 }
1264 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001265 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001266 {
1267 fail_fast_assert(BoundsType::static_size == totalSize);
1268 return {};
1269 }
1270 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001271 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001272 {
1273 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1274 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1275 }
1276
1277 struct Sep{};
1278
1279 template <typename T, typename... Args>
1280 T static_as_array_view_helper(Sep, Args... args)
1281 {
1282 return T{static_cast<typename T::size_type>(args)...};
1283 }
1284 template <typename T, typename Arg, typename... Args>
1285 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)
1286 {
1287 return static_as_array_view_helper<T>(args...);
1288 }
1289 template <typename T, typename... Args>
1290 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1291 {
1292 return static_as_array_view_helper<T>(args..., val.dvalue);
1293 }
1294
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001295 template <typename ...Dimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001296 struct static_as_array_view_static_bounds_helper
1297 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001298 using type = static_bounds<(Dimensions::value)...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001299 };
1300
1301 template <typename T>
1302 struct is_array_view_oracle : std::false_type
1303 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001304
1305 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001306 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1307 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001308
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001309 template <typename ValueType, std::ptrdiff_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001310 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1311 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001312
1313 template <typename T>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001314 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1315 {};
1316
1317}
1318
1319
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001320template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1321class array_view : public basic_array_view <ValueType, static_bounds <FirstDimension, RestDimensions...>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001322{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001323 template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001324 friend class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001325
1326 using Base = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001327
1328public:
1329 using typename Base::bounds_type;
1330 using typename Base::size_type;
1331 using typename Base::pointer;
1332 using typename Base::value_type;
1333 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001334 using typename Base::iterator;
1335 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001336 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001337 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001338
1339public:
1340 // basic
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001341 constexpr array_view(pointer ptr, size_type size) : Base(ptr, bounds_type{ size })
1342 {}
1343
1344 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1345 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001346
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001347 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001348 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001349
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001350 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001351 {
1352 fail_fast_assert(size == 0);
1353 }
1354
1355 // default
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001356 template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001357 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001358 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001359
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001360 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001361 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001362 /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001363 constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size})
1364 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001365
1366 // from n-dimensions static array
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001367 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001368 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>>
1369 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001370 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001371
1372 // from n-dimensions static array with size
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001373 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001374 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001375 >
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001376 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{size})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001377 {
1378 fail_fast_assert(size <= N);
1379 }
1380
1381 // from std array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001382 template <size_t N,
1383 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value>
1384 >
1385 constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1386 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001387
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001388 template <size_t N,
1389 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value
1390 && std::is_const<value_type>::value>
1391 >
1392 constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1393 {}
1394
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001395 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1396 template <typename Ptr,
1397 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001398 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>
1399 > // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001400 constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001401 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001402
1403 // from containers. It must has .size() and .data() two function signatures
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001404 template <typename Cont, typename DataType = typename Cont::value_type,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001405 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001406 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001407 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001408 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001409 constexpr array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001410 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001411
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001412 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001413
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001414 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001415 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
1416 typename BaseType = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>,
1417 typename OtherBaseType = basic_array_view<OtherValueType, static_bounds<OtherDimensions...>>,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001418 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1419 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001420 constexpr array_view(const array_view<OtherValueType, OtherDimensions...> &av)
1421 : Base(static_cast<const typename array_view<OtherValueType, OtherDimensions...>::Base&>(av))
1422 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001423
1424 // reshape
Neil MacIntosh14d50a62015-11-03 12:44:09 -08001425 // DimCount here is a workaround for a bug in MSVC 2015
1426 template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), typename = std::enable_if_t<(DimCount > 0)>>
1427 constexpr array_view<ValueType, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001428 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001429 using BoundsType = typename array_view<ValueType, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001430 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1431 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001432 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001433 }
1434
1435 // to bytes array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001436 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001437 auto as_bytes() const noexcept -> array_view<const byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001438 {
1439 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001440 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001441 }
1442
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001443 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001444 auto as_writeable_bytes() const noexcept -> array_view<byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001445 {
1446 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001447 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001448 }
1449
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001450 // from bytes array
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001451 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001452 constexpr auto as_array_view() const noexcept -> array_view<const U, (Base::bounds_type::static_size != dynamic_range ? static_cast<std::ptrdiff_t>(static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U)) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001453 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001454 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0),
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001455 "Target type must be standard layout and its size must match the byte array size");
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001456 fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
1457 return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001458 }
1459
Neil MacIntosha4fa2b32015-10-28 16:53:53 -07001460 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001461 constexpr auto as_array_view() const noexcept -> array_view<U, (Base::bounds_type::static_size != dynamic_range ? static_cast<ptrdiff_t>(static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U)) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001462 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001463 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0),
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001464 "Target type must be standard layout and its size must match the byte array size");
1465 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001466 return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001467 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001468
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001469 // section on linear space
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001470 template<std::ptrdiff_t Count>
1471 constexpr array_view<ValueType, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001472 {
1473 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1474 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 -07001475 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001476 }
1477
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001478 constexpr array_view<ValueType, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001479 {
1480 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001481 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001482 }
1483
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001484 template<std::ptrdiff_t Count>
1485 constexpr array_view<ValueType, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001486 {
1487 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1488 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001489 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001490 }
1491
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001492 constexpr array_view<ValueType, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001493 {
1494 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001495 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001496 }
1497
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001498 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
1499 constexpr array_view<ValueType, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001500 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001501 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");
1502 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 -07001503 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001504 }
1505
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001506 constexpr array_view<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001507 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001508 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1509 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001510 }
1511
1512 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001513 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001514 {
1515 return this->size();
1516 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001517
Neil MacIntoshd5316802015-09-30 21:54:08 -07001518 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001519 {
1520 return length();
1521 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001522
Neil MacIntoshd5316802015-09-30 21:54:08 -07001523 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001524 {
1525 return sizeof(value_type) * this->size();
1526 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001527
Neil MacIntoshd5316802015-09-30 21:54:08 -07001528 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001529 {
1530 return bytes();
1531 }
1532
1533 // section
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001534 constexpr strided_array_view<ValueType, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001535 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001536 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001537 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001538 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001539
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001540 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001541 {
1542 return Base::operator[](idx);
1543 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001544
Anna Gringauze1a864982015-09-14 18:55:06 -07001545 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001546 constexpr array_view<ValueType, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001547 {
1548 auto ret = Base::operator[](idx);
1549 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001550 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001551
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001552 using Base::operator==;
1553 using Base::operator!=;
1554 using Base::operator<;
1555 using Base::operator<=;
1556 using Base::operator>;
1557 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001558};
1559
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001560template <typename T, std::ptrdiff_t... Dimensions>
1561constexpr 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 -07001562{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001563 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_array_view_helper<static_bounds<Dimensions...>>(args..., details::Sep{})};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001564}
1565
1566template <typename T>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001567constexpr auto as_array_view (T* arr, std::ptrdiff_t len) -> typename details::ArrayViewArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001568{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001569 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001570}
1571
1572template <typename T, size_t N>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001573constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001574{
1575 return {arr};
1576}
1577
1578template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001579constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001580{
1581 return {arr};
1582}
1583
1584template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001585constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001586
1587template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001588constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001589{
1590 return {arr};
1591}
1592
1593template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001594constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001595{
1596 return {begin, end};
1597}
1598
1599template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001600constexpr 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 -07001601 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1602{
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001603 fail_fast_assert(arr.size() < PTRDIFF_MAX);
1604 return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001605}
1606
1607template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001608constexpr 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 -07001609 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1610
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001611template <typename ValueType, size_t Rank>
1612class strided_array_view : public basic_array_view<ValueType, strided_bounds<Rank>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001613{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001614 using Base = basic_array_view<ValueType, strided_bounds<Rank>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001615
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001616 template<typename OtherValue, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001617 friend class strided_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001618
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001619public:
1620 using Base::rank;
1621 using typename Base::bounds_type;
1622 using typename Base::size_type;
1623 using typename Base::pointer;
1624 using typename Base::value_type;
1625 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001626 using typename Base::iterator;
1627 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001628 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001629
1630 // from static array of size N
1631 template<size_type N>
1632 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1633 {
1634 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1635 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001636
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001637 // from raw data
1638 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001639 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001640 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001641 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001642
1643 // from array view
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001644 template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
1645 strided_array_view(array_view<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001646 {
1647 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1648 }
1649
1650 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001651 template <typename OtherValueType,
1652 typename BaseType = basic_array_view<ValueType, strided_bounds<Rank>>,
1653 typename OtherBaseType = basic_array_view<OtherValueType, strided_bounds<Rank>>,
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001654 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1655 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001656 constexpr strided_array_view(const strided_array_view<OtherValueType, Rank> &av) : Base(static_cast<const typename strided_array_view<OtherValueType, Rank>::Base &>(av)) // static_cast is required
1657 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001658
1659 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001660 template <typename OtherValueType>
1661 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 -07001662 {
1663 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001664 auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001665
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001666 size_type size = this->bounds().total_size() / d;
1667 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 -07001668 }
1669
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001670 strided_array_view section(index_type origin, index_type extents) const
1671 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001672 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001673 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1674 }
1675
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001676 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001677 {
1678 return Base::operator[](idx);
1679 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001680
1681 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001682 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001683 {
1684 auto ret = Base::operator[](idx);
1685 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1686 }
1687
1688private:
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001689 static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001690 {
1691 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");
1692
1693 index_type ret = extent;
1694 ret[rank - 1] /= d;
1695
1696 return ret;
1697 }
1698
1699 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001700 static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001701 {
1702 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1703
1704 return strides;
1705 }
1706
1707 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001708 static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001709 {
1710 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1711 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");
1712
Neil MacIntosh99746e22015-09-27 16:53:58 -07001713 for (size_t i = rank - 1; i > 0; --i)
1714 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 -07001715
1716 index_type ret = strides / d;
1717 ret[rank - 1] = 1;
1718
1719 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001720 }
1721};
1722
1723template <typename ArrayView>
1724class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1725{
1726 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1727public:
1728 using typename Base::reference;
1729 using typename Base::pointer;
1730 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001731
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001732private:
1733 template <typename ValueType, typename Bounds>
1734 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001735
1736 pointer m_pdata;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001737 const ArrayView * m_validator;
1738 void validateThis() const
1739 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07001740 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 -07001741 }
Anna Gringauzea4654a42015-10-16 12:15:22 -07001742 contiguous_array_view_iterator (const ArrayView *container, bool isbegin) :
1743 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001744public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001745 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001746 {
1747 validateThis();
1748 return *m_pdata;
1749 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001750 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001751 {
1752 validateThis();
1753 return m_pdata;
1754 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001755 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 {
1757 ++m_pdata;
1758 return *this;
1759 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001760 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001761 {
1762 auto ret = *this;
1763 ++(*this);
1764 return ret;
1765 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001766 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001767 {
1768 --m_pdata;
1769 return *this;
1770 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001771 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001772 {
1773 auto ret = *this;
1774 --(*this);
1775 return ret;
1776 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001777 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001778 {
1779 contiguous_array_view_iterator ret{ *this };
1780 return ret += n;
1781 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001782 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001783 {
1784 m_pdata += n;
1785 return *this;
1786 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001787 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001788 {
1789 contiguous_array_view_iterator ret{ *this };
1790 return ret -= n;
1791 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001792 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001793 {
1794 return *this += -n;
1795 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001796 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001797 {
1798 fail_fast_assert(m_validator == rhs.m_validator);
1799 return m_pdata - rhs.m_pdata;
1800 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001801 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802 {
1803 return *(*this + n);
1804 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001805 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001806 {
1807 fail_fast_assert(m_validator == rhs.m_validator);
1808 return m_pdata == rhs.m_pdata;
1809 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001810 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001811 {
1812 return !(*this == rhs);
1813 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001814 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001815 {
1816 fail_fast_assert(m_validator == rhs.m_validator);
1817 return m_pdata < rhs.m_pdata;
1818 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001819 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001820 {
1821 return !(rhs < *this);
1822 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001823 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001824 {
1825 return rhs < *this;
1826 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001827 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001828 {
1829 return !(rhs > *this);
1830 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001831 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001832 {
1833 std::swap(m_pdata, rhs.m_pdata);
1834 std::swap(m_validator, rhs.m_validator);
1835 }
1836};
1837
1838template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001839contiguous_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 -07001840{
1841 return rhs + n;
1842}
1843
1844template <typename ArrayView>
1845class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1846{
1847 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1848public:
1849 using typename Base::reference;
1850 using typename Base::pointer;
1851 using typename Base::difference_type;
1852 using typename Base::value_type;
1853private:
1854 template <typename ValueType, typename Bounds>
1855 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001856
1857 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001858 typename ArrayView::bounds_type::iterator m_itr;
Anna Gringauzea4654a42015-10-16 12:15:22 -07001859 general_array_view_iterator(const ArrayView *container, bool isbegin) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001860 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
Neil MacIntosha4fa2b32015-10-28 16:53:53 -07001861 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001862public:
Anna Gringauzea4654a42015-10-16 12:15:22 -07001863 reference operator*() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001864 {
1865 return (*m_container)[*m_itr];
1866 }
Anna Gringauzea4654a42015-10-16 12:15:22 -07001867 pointer operator->() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001868 {
1869 return &(*m_container)[*m_itr];
1870 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001871 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001872 {
1873 ++m_itr;
1874 return *this;
1875 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001876 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001877 {
1878 auto ret = *this;
1879 ++(*this);
1880 return ret;
1881 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001882 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001883 {
1884 --m_itr;
1885 return *this;
1886 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001887 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001888 {
1889 auto ret = *this;
1890 --(*this);
1891 return ret;
1892 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001893 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001894 {
1895 general_array_view_iterator ret{ *this };
1896 return ret += n;
1897 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001898 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001899 {
1900 m_itr += n;
1901 return *this;
1902 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001903 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001904 {
1905 general_array_view_iterator ret{ *this };
1906 return ret -= n;
1907 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001908 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001909 {
1910 return *this += -n;
1911 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001912 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001913 {
1914 fail_fast_assert(m_container == rhs.m_container);
1915 return m_itr - rhs.m_itr;
1916 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001917 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001918 {
1919 return (*m_container)[m_itr[n]];;
1920 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001921 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001922 {
1923 fail_fast_assert(m_container == rhs.m_container);
1924 return m_itr == rhs.m_itr;
1925 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001926 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001927 {
1928 return !(*this == rhs);
1929 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001930 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001931 {
1932 fail_fast_assert(m_container == rhs.m_container);
1933 return m_itr < rhs.m_itr;
1934 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001935 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001936 {
1937 return !(rhs < *this);
1938 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001939 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001940 {
1941 return rhs < *this;
1942 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001943 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001944 {
1945 return !(rhs > *this);
1946 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001947 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001948 {
1949 std::swap(m_itr, rhs.m_itr);
1950 std::swap(m_container, rhs.m_container);
1951 }
1952};
1953
1954template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001955general_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 -07001956{
1957 return rhs + n;
1958}
1959
Neil MacIntoshef626fd2015-09-29 16:41:37 -07001960} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001961
Neil MacIntoshd5316802015-09-30 21:54:08 -07001962#ifdef _MSC_VER
1963
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001964#undef constexpr
1965#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001966
Neil MacIntoshd5316802015-09-30 21:54:08 -07001967#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07001968#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07001969
1970#ifndef GSL_THROWS_FOR_TESTING
1971#pragma undef noexcept
1972#endif // GSL_THROWS_FOR_TESTING
1973
Neil MacIntoshe9a96022015-11-03 18:56:55 -08001974#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
1975#pragma pop_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG")
1976
1977
Neil MacIntosh9a297122015-09-14 15:11:07 -07001978#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07001979
Neil MacIntoshd5316802015-09-30 21:54:08 -07001980#endif // _MSC_VER
1981
1982#if defined(GSL_THROWS_FOR_TESTING)
1983#undef noexcept
1984#endif // GSL_THROWS_FOR_TESTING
1985
Treb Connell51da1362015-09-24 18:08:34 -07001986
1987#endif // GSL_ARRAY_VIEW_H