blob: cbc33be950944eac649739e0f1124bb828e261a7 [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{
Neil MacIntosh68064d62015-11-03 19:17:11 -080076 template <typename SizeType>
77 struct SizeTypeTraits
78 {
79 static const SizeType max_value = std::numeric_limits<SizeType>::max();
80 };
Anna Gringauze1c208b32015-10-16 17:40:57 -070081
82
Neil MacIntosh68064d62015-11-03 19:17:11 -080083 template<typename... Ts>
84 class are_integral : public std::integral_constant<bool, true> {};
Anna Gringauze1c208b32015-10-16 17:40:57 -070085
Neil MacIntosh68064d62015-11-03 19:17:11 -080086 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{
Neil MacIntosh68064d62015-11-03 19:17:11 -080093 static_assert(Rank > 0, "Rank must be greater than 0!");
Anna Gringauzedb384972015-10-05 12:34:23 -070094
Neil MacIntosh68064d62015-11-03 19:17:11 -080095 template <size_t OtherRank>
96 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -070097
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070098public:
Neil MacIntosh68064d62015-11-03 19:17:11 -080099 static const size_t rank = Rank;
100 using value_type = std::ptrdiff_t;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700101 using size_type = value_type;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800102 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
Neil MacIntosh68064d62015-11-03 19:17:11 -0800105 constexpr index() noexcept
106 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700107
Neil MacIntosh68064d62015-11-03 19:17:11 -0800108 constexpr index(const value_type(&values)[Rank]) noexcept
109 {
110 std::copy(values, values + Rank, elems);
111 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700112
Neil MacIntoshe9a96022015-11-03 18:56:55 -0800113#ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
Neil MacIntosh68064d62015-11-03 19:17:11 -0800114 template<typename T, typename... Ts,
Neil MacIntoshe9a96022015-11-03 18:56:55 -0800115 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
Neil MacIntosh68064d62015-11-03 19:17:11 -0800121 template<typename... Ts,
Neil MacIntoshe9a96022015-11-03 18:56:55 -0800122 typename = std::enable_if_t<(sizeof...(Ts) == Rank) && details::are_integral<Ts...>::value>>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800123 constexpr index(Ts... ds) noexcept : elems{ static_cast<value_type>(ds)... }
Neil MacIntoshe9a96022015-11-03 18:56:55 -0800124 {}
125#endif
Anna Gringauzedb384972015-10-05 12:34:23 -0700126
Neil MacIntosh68064d62015-11-03 19:17:11 -0800127 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700128
Neil MacIntosh68064d62015-11-03 19:17:11 -0800129 constexpr index& operator=(const index& rhs) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700130
Neil MacIntosh68064d62015-11-03 19:17:11 -0800131 // 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 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700137
Neil MacIntosh68064d62015-11-03 19:17:11 -0800138 // 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 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700144
Neil MacIntosh68064d62015-11-03 19:17:11 -0800145 constexpr bool operator==(const index& rhs) const noexcept
146 {
147 return std::equal(elems, elems + rank, rhs.elems);
148 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700149
Neil MacIntosh68064d62015-11-03 19:17:11 -0800150 constexpr bool operator!=(const index& rhs) const noexcept
151 {
152 return !(this == rhs);
153 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700154
Neil MacIntosh68064d62015-11-03 19:17:11 -0800155 constexpr index operator+() const noexcept
156 {
157 return *this;
158 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700159
Neil MacIntosh68064d62015-11-03 19:17:11 -0800160 constexpr index operator-() const noexcept
161 {
162 index ret = *this;
163 std::transform(ret, ret + rank, ret, std::negate<value_type>{});
164 return ret;
165 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700166
Neil MacIntosh68064d62015-11-03 19:17:11 -0800167 constexpr index operator+(const index& rhs) const noexcept
168 {
169 index ret = *this;
170 ret += rhs;
171 return ret;
172 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700173
Neil MacIntosh68064d62015-11-03 19:17:11 -0800174 constexpr index operator-(const index& rhs) const noexcept
175 {
176 index ret = *this;
177 ret -= rhs;
178 return ret;
179 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700180
Neil MacIntosh68064d62015-11-03 19:17:11 -0800181 constexpr index& operator+=(const index& rhs) noexcept
182 {
183 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
184 return *this;
185 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700186
Neil MacIntosh68064d62015-11-03 19:17:11 -0800187 constexpr index& operator-=(const index& rhs) noexcept
188 {
189 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
190 return *this;
191 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700192
Neil MacIntosh68064d62015-11-03 19:17:11 -0800193 constexpr index operator*(value_type v) const noexcept
194 {
195 index ret = *this;
196 ret *= v;
197 return ret;
198 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700199
Neil MacIntosh68064d62015-11-03 19:17:11 -0800200 constexpr index operator/(value_type v) const noexcept
201 {
202 index ret = *this;
203 ret /= v;
204 return ret;
205 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700206
Neil MacIntosh68064d62015-11-03 19:17:11 -0800207 friend constexpr index operator*(value_type v, const index& rhs) noexcept
208 {
209 return rhs * v;
210 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700211
Neil MacIntosh68064d62015-11-03 19:17:11 -0800212 constexpr index& operator*=(value_type v) noexcept
213 {
214 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
215 return *this;
216 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700217
Neil MacIntosh68064d62015-11-03 19:17:11 -0800218 constexpr index& operator/=(value_type v) noexcept
219 {
220 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
221 return *this;
222 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700223
Anna Gringauzedb384972015-10-05 12:34:23 -0700224private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800225 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 MacIntosh68064d62015-11-03 19:17:11 -0800232 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
233 constexpr operator T() const noexcept
234 {
235 return static_cast<T>(-1);
236 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700237
Neil MacIntosh68064d62015-11-03 19:17:11 -0800238 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
239 constexpr bool operator ==(T other) const noexcept
240 {
241 return static_cast<T>(-1) == other;
242 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700243
Neil MacIntosh68064d62015-11-03 19:17:11 -0800244 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
245 constexpr bool operator !=(T other) const noexcept
246 {
247 return static_cast<T>(-1) != other;
248 }
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{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800255 return right == left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700256}
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{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800261 return right != left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700262}
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 MacIntosh68064d62015-11-03 19:17:11 -0800275 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
276 struct LessThan
277 {
278 static const bool value = Left < Right;
279 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700280
Neil MacIntosh68064d62015-11-03 19:17:11 -0800281 template <std::ptrdiff_t... Ranges>
282 struct BoundsRanges {
283 using size_type = std::ptrdiff_t;
284 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>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800291 BoundsRanges(const OtherRange&, bool /* firstLevel */)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700292 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800293
294 BoundsRanges (const BoundsRanges&) = default;
295 BoundsRanges(const std::ptrdiff_t* const) { }
296 BoundsRanges() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700297
298
Neil MacIntosh68064d62015-11-03 19:17:11 -0800299 template <typename T, size_t Dim>
300 void serialize(T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700301 {}
302
Neil MacIntosh68064d62015-11-03 19:17:11 -0800303 template <typename T, size_t Dim>
304 size_type linearize(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700305 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800306 return 0;
307 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700308
Neil MacIntosh68064d62015-11-03 19:17:11 -0800309 template <typename T, size_t Dim>
310 bool contains(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700311 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800312 return 0;
313 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700314
Neil MacIntosh68064d62015-11-03 19:17:11 -0800315 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700316 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800317 return TotalSize;
318 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700319
Neil MacIntosh68064d62015-11-03 19:17:11 -0800320 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700321 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800322 return true;
323 }
324 };
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700325
Neil MacIntosh68064d62015-11-03 19:17:11 -0800326 template <std::ptrdiff_t... RestRanges>
327 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
328 using Base = BoundsRanges <RestRanges... >;
329 using size_type = std::ptrdiff_t;
330 static const size_t Depth = Base::Depth + 1;
331 static const size_t DynamicNum = Base::DynamicNum + 1;
332 static const size_type CurrentRange = dynamic_range;
333 static const size_type TotalSize = dynamic_range;
334 const size_type m_bound;
335
336 BoundsRanges (const BoundsRanges&) = default;
337
338 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
339 {
340 fail_fast_assert(0 <= *arr);
341 }
342
343 BoundsRanges() : m_bound(0) {}
344
345 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
346 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
347 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
348 {}
349
350 template <typename T, size_t Dim = 0>
351 void serialize(T& arr) const
352 {
353 arr[Dim] = elementNum();
354 this->Base::template serialize<T, Dim + 1>(arr);
355 }
356
357 template <typename T, size_t Dim = 0>
358 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700359 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800360 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);
363 }
364
365 template <typename T, size_t Dim = 0>
366 size_type contains(const T & arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700367 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800368 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];
372 return cur < m_bound ? cur + last : -1;
373 }
374
375 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700376 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800377 return m_bound;
378 }
379
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700380 size_type elementNum() const noexcept
381 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800382 return totalSize() / this->Base::totalSize();
383 }
384
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700385 size_type elementNum(size_t dim) const noexcept
386 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800387 if (dim > 0)
388 return this->Base::elementNum(dim - 1);
389 else
390 return elementNum();
391 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700392
Neil MacIntosh68064d62015-11-03 19:17:11 -0800393 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700394 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800395 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
396 }
397 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700398
Neil MacIntosh68064d62015-11-03 19:17:11 -0800399 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
400 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
401 {
402 using Base = BoundsRanges <RestRanges... >;
403 using size_type = std::ptrdiff_t;
404 static const size_t Depth = Base::Depth + 1;
405 static const size_t DynamicNum = Base::DynamicNum;
406 static const size_type CurrentRange = CurRange;
407 static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
408
409 BoundsRanges (const BoundsRanges&) = default;
410 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) { }
411 BoundsRanges() = default;
412
413 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)
415 {
416 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
417 }
418
419 template <typename T, size_t Dim = 0>
420 void serialize(T& arr) const
421 {
422 arr[Dim] = elementNum();
423 this->Base::template serialize<T, Dim + 1>(arr);
424 }
425
426 template <typename T, size_t Dim = 0>
427 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700428 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800429 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
430 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
431 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700432
Neil MacIntosh68064d62015-11-03 19:17:11 -0800433 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700434 size_type contains(const T& arr) const
435 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800436 if (arr[Dim] >= CurrentRange)
437 return -1;
438 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
439 if (last == -1)
440 return -1;
441 return this->Base::totalSize() * arr[Dim] + last;
442 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700443
Neil MacIntosh68064d62015-11-03 19:17:11 -0800444 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700445 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800446 return CurrentRange * this->Base::totalSize();
447 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700448
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700449 size_type elementNum() const noexcept
450 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800451 return CurrentRange;
452 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700453
Neil MacIntosh68064d62015-11-03 19:17:11 -0800454 size_type elementNum(size_t dim) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700455 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800456 if (dim > 0)
457 return this->Base::elementNum(dim - 1);
458 else
459 return elementNum();
460 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700461
Neil MacIntosh68064d62015-11-03 19:17:11 -0800462 bool operator== (const BoundsRanges& rhs) const noexcept
463 {
464 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
465 }
466 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700467
Neil MacIntosh68064d62015-11-03 19:17:11 -0800468 template <typename SourceType, typename TargetType, size_t Rank>
469 struct BoundsRangeConvertible2;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700470
Neil MacIntosh68064d62015-11-03 19:17:11 -0800471 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;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700473
Neil MacIntosh68064d62015-11-03 19:17:11 -0800474 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 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700482
Neil MacIntosh68064d62015-11-03 19:17:11 -0800483 template <typename SourceType, typename TargetType>
484 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700485
Neil MacIntosh68064d62015-11-03 19:17:11 -0800486 template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth>
487 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
488 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
489 && (!LessThan<SourceType::CurrentRange, TargetType::CurrentRange>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
490 {};
491 template <typename SourceType, typename TargetType>
492 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700493
Neil MacIntosh68064d62015-11-03 19:17:11 -0800494 template <typename TypeChain>
495 struct TypeListIndexer
496 {
497 const TypeChain & obj;
498 TypeListIndexer(const TypeChain & obj) :obj(obj){}
499 template<size_t N>
500 const TypeChain & getObj(std::true_type)
501 {
502 return obj;
503 }
504 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
505 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 }
509 template <size_t N>
510 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 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700515
Neil MacIntosh68064d62015-11-03 19:17:11 -0800516 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 MacIntosh68064d62015-11-03 19:17:11 -0800522 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
524 {
525 Ret ret{};
526 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 MacIntosh68064d62015-11-03 19:17:11 -0800541 static_bounds(const details::BoundsRanges<Ranges...>&) {
542 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700543};
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 MacIntosh68064d62015-11-03 19:17:11 -0800548 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700549
Neil MacIntosh68064d62015-11-03 19:17:11 -0800550 MyRanges m_ranges;
551 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700552 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800553
554 template <std::ptrdiff_t... OtherRanges>
555 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700556
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700557public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800558 static const size_t rank = MyRanges::Depth;
559 static const size_t dynamic_rank = MyRanges::DynamicNum;
560 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700561
Neil MacIntosh68064d62015-11-03 19:17:11 -0800562 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 MacIntosh68064d62015-11-03 19:17:11 -0800568 using sliced_type = static_bounds<RestRanges...>;
569 using mapping_type = contiguous_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700570
Neil MacIntosh68064d62015-11-03 19:17:11 -0800571 constexpr static_bounds(const static_bounds&) = default;
572
573 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
574 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700575 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
576 {}
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700577
Neil MacIntosh68064d62015-11-03 19:17:11 -0800578 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges((const std::ptrdiff_t*)il.begin())
579 {
580 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");
581 fail_fast_assert(m_ranges.totalSize() <= PTRDIFF_MAX, "Size of the range is larger than the max element of the size type");
582 }
583
584 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700585
Neil MacIntosh68064d62015-11-03 19:17:11 -0800586 constexpr static_bounds& operator=(const static_bounds& otherBounds)
587 {
588 new(&m_ranges) MyRanges (otherBounds.m_ranges);
589 return *this;
590 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700591
Neil MacIntosh68064d62015-11-03 19:17:11 -0800592 constexpr sliced_type slice() const noexcept
593 {
594 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
595 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700596
Neil MacIntosh68064d62015-11-03 19:17:11 -0800597 constexpr size_type stride() const noexcept
598 {
599 return rank > 1 ? slice().size() : 1;
600 }
601
602 constexpr size_type size() const noexcept
603 {
604 return m_ranges.totalSize();
605 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700606
Neil MacIntosh68064d62015-11-03 19:17:11 -0800607 constexpr size_type total_size() const noexcept
608 {
609 return m_ranges.totalSize();
610 }
611
612 constexpr size_type linearize(const index_type & idx) const
613 {
614 return m_ranges.linearize(idx);
615 }
616
617 constexpr bool contains(const index_type& idx) const noexcept
618 {
619 return m_ranges.contains(idx) != -1;
620 }
621
622 constexpr size_type operator[](size_t index) const noexcept
623 {
624 return m_ranges.elementNum(index);
625 }
626
627 template <size_t Dim = 0>
628 constexpr size_type extent() const noexcept
629 {
630 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
631 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
632 }
633
634 constexpr index_type index_bounds() const noexcept
635 {
636 size_type extents[rank] = {};
637 m_ranges.serialize(extents);
638 return{ extents };
639 }
640
641 template <std::ptrdiff_t... Ranges>
642 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
643 {
644 return this->size() == rhs.size();
645 }
646
647 template <std::ptrdiff_t... Ranges>
648 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
649 {
650 return !(*this == rhs);
651 }
652
653 constexpr const_iterator begin() const noexcept
654 {
655 return const_iterator(*this, index_type{});
656 }
657
658 constexpr const_iterator end() const noexcept
659 {
660 return const_iterator(*this, this->index_bounds());
661 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700662};
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 MacIntosh68064d62015-11-03 19:17:11 -0800667 template <size_t OtherRank>
668 friend class strided_bounds;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700669
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700670public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800671 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700672 using value_type = std::ptrdiff_t;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800673 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 MacIntosh68064d62015-11-03 19:17:11 -0800682 static const value_type static_size = dynamic_range;
683 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
Neil MacIntosh68064d62015-11-03 19:17:11 -0800686 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
Neil MacIntosh68064d62015-11-03 19:17:11 -0800692 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 MacIntosh68064d62015-11-03 19:17:11 -0800696 constexpr index_type strides() const noexcept
697 {
698 return m_strides;
699 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700700
Neil MacIntosh68064d62015-11-03 19:17:11 -0800701 constexpr size_type total_size() const noexcept
702 {
703 size_type ret = 0;
704 for (size_t i = 0; i < rank; ++i)
705 {
706 ret += (m_extents[i] - 1) * m_strides[i];
707 }
708 return ret + 1;
709 }
710
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700711 constexpr size_type size() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800712 {
713 size_type ret = 1;
714 for (size_t i = 0; i < rank; ++i)
715 {
716 ret *= m_extents[i];
717 }
718 return ret;
719 }
720
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700721 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800722 {
723 for (size_t i = 0; i < rank; ++i)
724 {
725 if (idx[i] < 0 || idx[i] >= m_extents[i])
726 return false;
727 }
728 return true;
729 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700730
Neil MacIntosh68064d62015-11-03 19:17:11 -0800731 constexpr size_type linearize(const index_type& idx) const noexcept
732 {
733 size_type ret = 0;
734 for (size_t i = 0; i < rank; i++)
735 {
736 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
737 ret += idx[i] * m_strides[i];
738 }
739 return ret;
740 }
741
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700742 constexpr size_type stride() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800743 {
744 return m_strides[0];
745 }
746
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700747 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800748 constexpr sliced_type slice() const
749 {
750 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
751 }
752
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700753 template <size_t Dim = 0>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800754 constexpr size_type extent() const noexcept
755 {
756 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
757 return m_extents[Dim];
758 }
759
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700760 constexpr index_type index_bounds() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800761 {
762 return m_extents;
763 }
764 constexpr const_iterator begin() const noexcept
765 {
766 return const_iterator{ *this, index_type{} };
767 }
768
769 constexpr const_iterator end() const noexcept
770 {
771 return const_iterator{ *this, index_bounds() };
772 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700773
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700774private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800775 index_type m_extents;
776 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:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800790 using Base = std::iterator <std::random_access_iterator_tag, IndexType>;
Anna Gringauzea4654a42015-10-16 12:15:22 -0700791
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700792public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800793 static const size_t rank = IndexType::rank;
794 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;
799 using index_size_type = typename IndexType::value_type;
800 template <typename Bounds>
801 explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
802 : boundary(bnd.index_bounds()), curr(std::move(curr))
803 {
804 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
805 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700806
Neil MacIntosh68064d62015-11-03 19:17:11 -0800807 constexpr reference operator*() const noexcept
808 {
809 return curr;
810 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700811
Neil MacIntosh68064d62015-11-03 19:17:11 -0800812 constexpr pointer operator->() const noexcept
813 {
814 return &curr;
815 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700816
Neil MacIntosh68064d62015-11-03 19:17:11 -0800817 constexpr bounds_iterator& operator++() noexcept
818 {
819 for (size_t i = rank; i-- > 0;)
820 {
821 if (curr[i] < boundary[i] - 1)
822 {
823 curr[i]++;
824 return *this;
825 }
826 curr[i] = 0;
827 }
828 // If we're here we've wrapped over - set to past-the-end.
829 curr = boundary;
830 return *this;
831 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700832
Neil MacIntosh68064d62015-11-03 19:17:11 -0800833 constexpr bounds_iterator operator++(int) noexcept
834 {
835 auto ret = *this;
836 ++(*this);
837 return ret;
838 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700839
Neil MacIntosh68064d62015-11-03 19:17:11 -0800840 constexpr bounds_iterator& operator--() noexcept
841 {
842 if (!less(curr, boundary))
843 {
844 // if at the past-the-end, set to last element
845 for (size_t i = 0; i < rank; ++i)
846 {
847 curr[i] = boundary[i] - 1;
848 }
849 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;
859 }
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
Neil MacIntosh68064d62015-11-03 19:17:11 -0800866 constexpr bounds_iterator operator--(int) noexcept
867 {
868 auto ret = *this;
869 --(*this);
870 return ret;
871 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700872
Neil MacIntosh68064d62015-11-03 19:17:11 -0800873 constexpr bounds_iterator operator+(difference_type n) const noexcept
874 {
875 bounds_iterator ret{ *this };
876 return ret += n;
877 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700878
Neil MacIntosh68064d62015-11-03 19:17:11 -0800879 constexpr bounds_iterator& operator+=(difference_type n) noexcept
880 {
881 auto linear_idx = linearize(curr) + n;
882 std::remove_const_t<value_type> stride = 0;
883 stride[rank - 1] = 1;
884 for (size_t i = rank - 1; i-- > 0;)
885 {
886 stride[i] = stride[i + 1] * boundary[i + 1];
887 }
888 for (size_t i = 0; i < rank; ++i)
889 {
890 curr[i] = linear_idx / stride[i];
891 linear_idx = linear_idx % stride[i];
892 }
893 fail_fast_assert(!less(curr, index_type{}) && !less(boundary, curr), "index is out of bounds of the array");
894 return *this;
895 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700896
Neil MacIntosh68064d62015-11-03 19:17:11 -0800897 constexpr bounds_iterator operator-(difference_type n) const noexcept
898 {
899 bounds_iterator ret{ *this };
900 return ret -= n;
901 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700902
Neil MacIntosh68064d62015-11-03 19:17:11 -0800903 constexpr bounds_iterator& operator-=(difference_type n) noexcept
904 {
905 return *this += -n;
906 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700907
Neil MacIntosh68064d62015-11-03 19:17:11 -0800908 constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
909 {
910 return linearize(curr) - linearize(rhs.curr);
911 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700912
Neil MacIntosh68064d62015-11-03 19:17:11 -0800913 constexpr value_type operator[](difference_type n) const noexcept
914 {
915 return *(*this + n);
916 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700917
Neil MacIntosh68064d62015-11-03 19:17:11 -0800918 constexpr bool operator==(const bounds_iterator& rhs) const noexcept
919 {
920 return curr == rhs.curr;
921 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700922
Neil MacIntosh68064d62015-11-03 19:17:11 -0800923 constexpr bool operator!=(const bounds_iterator& rhs) const noexcept
924 {
925 return !(*this == rhs);
926 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700927
Neil MacIntosh68064d62015-11-03 19:17:11 -0800928 constexpr bool operator<(const bounds_iterator& rhs) const noexcept
929 {
930 return less(curr, rhs.curr);
931 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700932
Neil MacIntosh68064d62015-11-03 19:17:11 -0800933 constexpr bool operator<=(const bounds_iterator& rhs) const noexcept
934 {
935 return !(rhs < *this);
936 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700937
Neil MacIntosh68064d62015-11-03 19:17:11 -0800938 constexpr bool operator>(const bounds_iterator& rhs) const noexcept
939 {
940 return rhs < *this;
941 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700942
Neil MacIntosh68064d62015-11-03 19:17:11 -0800943 constexpr bool operator>=(const bounds_iterator& rhs) const noexcept
944 {
945 return !(rhs > *this);
946 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700947
Neil MacIntosh68064d62015-11-03 19:17:11 -0800948 void swap(bounds_iterator& rhs) noexcept
949 {
950 std::swap(boundary, rhs.boundary);
951 std::swap(curr, rhs.curr);
952 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700953private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800954 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 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700963
Neil MacIntosh68064d62015-11-03 19:17:11 -0800964 constexpr index_size_type linearize(const value_type& idx) const noexcept
965 {
966 // TODO: Smarter impl.
967 // Check if past-the-end
968 index_size_type multiplier = 1;
969 index_size_type res = 0;
970 if (!less(idx, boundary))
971 {
972 res = 1;
973 for (size_t i = rank; i-- > 0;)
974 {
975 res += (idx[i] - 1) * multiplier;
976 multiplier *= boundary[i];
977 }
978 }
979 else
980 {
981 for (size_t i = rank; i-- > 0;)
982 {
983 res += idx[i] * multiplier;
984 multiplier *= boundary[i];
985 }
986 }
987 return res;
988 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700989
Neil MacIntosh68064d62015-11-03 19:17:11 -0800990 value_type boundary;
991 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{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800997 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700998}
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{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001005 template <typename Bounds>
1006 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
1007 {
1008 return bnd.strides();
1009 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001010
Neil MacIntosh68064d62015-11-03 19:17:11 -08001011 // Make a stride vector from bounds, assuming contiguous memory.
1012 template <typename Bounds>
1013 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
1014 {
1015 auto extents = bnd.index_bounds();
1016 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001017
Neil MacIntosh68064d62015-11-03 19:17:11 -08001018 stride[Bounds::rank - 1] = 1;
1019 for (size_t i = 1; i < Bounds::rank; ++i)
1020 {
1021 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
1022 }
1023 return{ stride };
1024 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001025
Neil MacIntosh68064d62015-11-03 19:17:11 -08001026 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 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001034
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:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001048 static const size_t rank = BoundsType::rank;
1049 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;
1053 using const_value_type = std::add_const_t<value_type>;
1054 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>>;
1057 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>>>;
1058 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>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001061
1062private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001063 pointer m_pdata;
1064 bounds_type m_bounds;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001065
1066public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001067 constexpr bounds_type bounds() const noexcept
1068 {
1069 return m_bounds;
1070 }
1071 template <size_t Dim = 0>
1072 constexpr size_type extent() const noexcept
1073 {
1074 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
1075 return m_bounds.template extent<Dim>();
1076 }
1077 constexpr size_type size() const noexcept
1078 {
1079 return m_bounds.size();
1080 }
1081 constexpr reference operator[](const index_type& idx) const
1082 {
1083 return m_pdata[m_bounds.linearize(idx)];
1084 }
1085 constexpr pointer data() const noexcept
1086 {
1087 return m_pdata;
1088 }
1089 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
1090 constexpr Ret operator[](size_type idx) const
1091 {
1092 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
1093 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001094
Neil MacIntosh68064d62015-11-03 19:17:11 -08001095 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
1096 return Ret {m_pdata + ridx, m_bounds.slice()};
1097 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001098
Neil MacIntosh68064d62015-11-03 19:17:11 -08001099 constexpr operator bool () const noexcept
1100 {
1101 return m_pdata != nullptr;
1102 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001103
Neil MacIntosh68064d62015-11-03 19:17:11 -08001104 constexpr iterator begin() const
1105 {
1106 return iterator {this, true};
1107 }
1108 constexpr iterator end() const
1109 {
1110 return iterator {this, false};
1111 }
1112 constexpr const_iterator cbegin() const
1113 {
1114 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1115 }
1116 constexpr const_iterator cend() const
1117 {
1118 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), false};
1119 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001120
Neil MacIntosh68064d62015-11-03 19:17:11 -08001121 constexpr reverse_iterator rbegin() const
1122 {
1123 return reverse_iterator {end()};
1124 }
1125 constexpr reverse_iterator rend() const
1126 {
1127 return reverse_iterator {begin()};
1128 }
1129 constexpr const_reverse_iterator crbegin() const
1130 {
1131 return const_reverse_iterator {cend()};
1132 }
1133 constexpr const_reverse_iterator crend() const
1134 {
1135 return const_reverse_iterator {cbegin()};
1136 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001137
Neil MacIntosh68064d62015-11-03 19:17:11 -08001138 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>>
1139 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1140 {
1141 return m_bounds.size() == other.m_bounds.size() &&
1142 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
1143 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001144
Neil MacIntosh68064d62015-11-03 19:17:11 -08001145 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>>
1146 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1147 {
1148 return !(*this == other);
1149 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001150
Neil MacIntosh68064d62015-11-03 19:17:11 -08001151 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>>
1152 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1153 {
1154 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1155 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001156
Neil MacIntosh68064d62015-11-03 19:17:11 -08001157 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>>
1158 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1159 {
1160 return !(other < *this);
1161 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001162
Neil MacIntosh68064d62015-11-03 19:17:11 -08001163 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>>
1164 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1165 {
1166 return (other < *this);
1167 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001168
Neil MacIntosh68064d62015-11-03 19:17:11 -08001169 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>>
1170 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
1171 {
1172 return !(*this < other);
1173 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001174
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001175public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001176 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>>
1179 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
1180 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1181 {
1182 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001183protected:
1184
Neil MacIntosh68064d62015-11-03 19:17:11 -08001185 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
1186 : 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>
1192 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
1193 : 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>
1199 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
1200 {
1201 details::verifyBoundsReshape(m_bounds, bounds);
1202 return {m_pdata, bounds};
1203 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001204private:
1205
Neil MacIntosh68064d62015-11-03 19:17:11 -08001206 friend iterator;
1207 friend const_iterator;
1208 template <typename ValueType2, typename BoundsType2>
1209 friend class basic_array_view;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001210};
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 MacIntosh68064d62015-11-03 19:17:11 -08001215 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001216};
1217template <>
1218struct dim<dynamic_range>
1219{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001220 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{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001233 template <typename T, typename = std::true_type>
1234 struct ArrayViewTypeTraits
1235 {
1236 using value_type = T;
1237 using size_type = size_t;
1238 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001239
Neil MacIntosh68064d62015-11-03 19:17:11 -08001240 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 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001246
Neil MacIntosh68064d62015-11-03 19:17:11 -08001247 template <typename T, std::ptrdiff_t... Ranks>
1248 struct ArrayViewArrayTraits {
1249 using type = array_view<T, Ranks...>;
1250 using value_type = T;
1251 using bounds_type = static_bounds<Ranks...>;
1252 using pointer = T*;
1253 using reference = T&;
1254 };
1255 template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
1256 struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001257
Neil MacIntosh68064d62015-11-03 19:17:11 -08001258 template <typename BoundsType>
1259 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
1260 {
1261 fail_fast_assert(totalSize <= PTRDIFF_MAX);
1262 return BoundsType{totalSize};
1263 }
1264 template <typename BoundsType>
1265 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
1266 {
1267 fail_fast_assert(BoundsType::static_size == totalSize);
1268 return {};
1269 }
1270 template <typename BoundsType>
1271 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
1272 {
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 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001294
Neil MacIntosh68064d62015-11-03 19:17:11 -08001295 template <typename ...Dimensions>
1296 struct static_as_array_view_static_bounds_helper
1297 {
1298 using type = static_bounds<(Dimensions::value)...>;
1299 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001300
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001301 template <typename T>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001302 struct is_array_view_oracle : std::false_type
1303 {};
1304
1305 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1306 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1307 {};
1308
1309 template <typename ValueType, std::ptrdiff_t Rank>
1310 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1311 {};
1312
1313 template <typename T>
1314 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1315 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001316
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 MacIntosh68064d62015-11-03 19:17:11 -08001323 template <typename ValueType2, std::ptrdiff_t FirstDimension2,
1324 std::ptrdiff_t... RestDimensions2>
1325 friend class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001326
Neil MacIntosh68064d62015-11-03 19:17:11 -08001327 using Base = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001328
1329public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001330 using typename Base::bounds_type;
1331 using typename Base::size_type;
1332 using typename Base::pointer;
1333 using typename Base::value_type;
1334 using typename Base::index_type;
1335 using typename Base::iterator;
1336 using typename Base::const_iterator;
1337 using typename Base::reference;
1338 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001339
1340public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001341 // basic
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001342 constexpr array_view(pointer ptr, size_type size) : Base(ptr, bounds_type{ size })
1343 {}
1344
1345 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1346 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001347
Neil MacIntosh68064d62015-11-03 19:17:11 -08001348 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
1349 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001350
Neil MacIntosh68064d62015-11-03 19:17:11 -08001351 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
1352 {
1353 fail_fast_assert(size == 0);
1354 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001355
Neil MacIntosh68064d62015-11-03 19:17:11 -08001356 // default
1357 template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
1358 constexpr array_view() : Base(nullptr, bounds_type())
1359 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001360
Neil MacIntosh68064d62015-11-03 19:17:11 -08001361 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
1362 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001363 /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001364 constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size})
1365 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001366
Neil MacIntosh68064d62015-11-03 19:17:11 -08001367 // from n-dimensions static array
1368 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001369 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001370 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
1371 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001372
Neil MacIntosh68064d62015-11-03 19:17:11 -08001373 // from n-dimensions static array with size
1374 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
1375 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001376 >
Neil MacIntosh41517ff2015-11-04 02:11:49 +00001377 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{size})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001378 {
1379 fail_fast_assert(size <= N);
1380 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001381
Neil MacIntosh68064d62015-11-03 19:17:11 -08001382 // from std array
1383 template <size_t N,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001384 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value>
1385 >
Neil MacIntosh68064d62015-11-03 19:17:11 -08001386 constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1387 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001388
Neil MacIntosh68064d62015-11-03 19:17:11 -08001389 template <size_t N,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001390 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value
1391 && std::is_const<value_type>::value>
1392 >
Neil MacIntosh68064d62015-11-03 19:17:11 -08001393 constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1394 {}
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001395
Neil MacIntosh68064d62015-11-03 19:17:11 -08001396 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1397 template <typename Ptr,
1398 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
1399 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001400 > // remove literal 0 case
Neil MacIntosh68064d62015-11-03 19:17:11 -08001401 constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
1402 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001403
Neil MacIntosh68064d62015-11-03 19:17:11 -08001404 // from containers. It must has .size() and .data() two function signatures
1405 template <typename Cont, typename DataType = typename Cont::value_type,
1406 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
1407 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
1408 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
1409 >
1410 constexpr array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
1411 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001412
Neil MacIntosh68064d62015-11-03 19:17:11 -08001413 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001414
Neil MacIntosh68064d62015-11-03 19:17:11 -08001415 // convertible
1416 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
1417 typename BaseType = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>,
1418 typename OtherBaseType = basic_array_view<OtherValueType, static_bounds<OtherDimensions...>>,
1419 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1420 >
1421 constexpr array_view(const array_view<OtherValueType, OtherDimensions...> &av)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001422 : Base(static_cast<const typename array_view<OtherValueType, OtherDimensions...>::Base&>(av))
1423 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001424
Neil MacIntosh68064d62015-11-03 19:17:11 -08001425 // reshape
Neil MacIntosh14d50a62015-11-03 12:44:09 -08001426 // DimCount here is a workaround for a bug in MSVC 2015
1427 template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), typename = std::enable_if_t<(DimCount > 0)>>
1428 constexpr array_view<ValueType, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001429 {
1430 using BoundsType = typename array_view<ValueType, (Dimensions2::value)...>::bounds_type;
1431 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1432 details::verifyBoundsReshape(this->bounds(), tobounds);
1433 return {this->data(), tobounds};
1434 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001435
Neil MacIntosh68064d62015-11-03 19:17:11 -08001436 // to bytes array
1437 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1438 auto as_bytes() const noexcept -> array_view<const byte>
1439 {
1440 static_assert(Enabled, "The value_type of array_view must be standarded layout");
1441 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
1442 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001443
Neil MacIntosh68064d62015-11-03 19:17:11 -08001444 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1445 auto as_writeable_bytes() const noexcept -> array_view<byte>
1446 {
1447 static_assert(Enabled, "The value_type of array_view must be standarded layout");
1448 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
1449 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001450
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001451 // from bytes array
Neil MacIntosh68064d62015-11-03 19:17:11 -08001452 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1453 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)>
1454 {
1455 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),
1456 "Target type must be standard layout and its size must match the byte array size");
1457 fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
1458 return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1459 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001460
Neil MacIntosh68064d62015-11-03 19:17:11 -08001461 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1462 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)>
1463 {
1464 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),
1465 "Target type must be standard layout and its size must match the byte array size");
1466 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
1467 return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1468 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001469
Neil MacIntosh68064d62015-11-03 19:17:11 -08001470 // section on linear space
1471 template<std::ptrdiff_t Count>
1472 constexpr array_view<ValueType, Count> first() const noexcept
1473 {
1474 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1475 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
1476 return { this->data(), Count };
1477 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001478
Neil MacIntosh68064d62015-11-03 19:17:11 -08001479 constexpr array_view<ValueType, dynamic_range> first(size_type count) const noexcept
1480 {
1481 fail_fast_assert(count <= this->size());
1482 return { this->data(), count };
1483 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001484
Neil MacIntosh68064d62015-11-03 19:17:11 -08001485 template<std::ptrdiff_t Count>
1486 constexpr array_view<ValueType, Count> last() const noexcept
1487 {
1488 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1489 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
1490 return { this->data() + this->size() - Count, Count };
1491 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001492
Neil MacIntosh68064d62015-11-03 19:17:11 -08001493 constexpr array_view<ValueType, dynamic_range> last(size_type count) const noexcept
1494 {
1495 fail_fast_assert(count <= this->size());
1496 return { this->data() + this->size() - count, count };
1497 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001498
Neil MacIntosh68064d62015-11-03 19:17:11 -08001499 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
1500 constexpr array_view<ValueType, Count> sub() const noexcept
1501 {
1502 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");
1503 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size()));
1504 return { this->data() + Offset, Count };
1505 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001506
Neil MacIntosh68064d62015-11-03 19:17:11 -08001507 constexpr array_view<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
1508 {
1509 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1510 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
1511 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001512
Neil MacIntosh68064d62015-11-03 19:17:11 -08001513 // size
1514 constexpr size_type length() const noexcept
1515 {
1516 return this->size();
1517 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001518
Neil MacIntosh68064d62015-11-03 19:17:11 -08001519 constexpr size_type used_length() const noexcept
1520 {
1521 return length();
1522 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001523
Neil MacIntosh68064d62015-11-03 19:17:11 -08001524 constexpr size_type bytes() const noexcept
1525 {
1526 return sizeof(value_type) * this->size();
1527 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001528
Neil MacIntosh68064d62015-11-03 19:17:11 -08001529 constexpr size_type used_bytes() const noexcept
1530 {
1531 return bytes();
1532 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001533
Neil MacIntosh68064d62015-11-03 19:17:11 -08001534 // section
1535 constexpr strided_array_view<ValueType, rank> section(index_type origin, index_type extents) const
1536 {
1537 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
1538 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} };
1539 }
1540
1541 constexpr reference operator[](const index_type& idx) const
1542 {
1543 return Base::operator[](idx);
1544 }
1545
1546 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1547 constexpr array_view<ValueType, RestDimensions...> operator[](size_type idx) const
1548 {
1549 auto ret = Base::operator[](idx);
1550 return{ ret.data(), ret.bounds() };
1551 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001552
Neil MacIntosh68064d62015-11-03 19:17:11 -08001553 using Base::operator==;
1554 using Base::operator!=;
1555 using Base::operator<;
1556 using Base::operator<=;
1557 using Base::operator>;
1558 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001559};
1560
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001561template <typename T, std::ptrdiff_t... Dimensions>
1562constexpr 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 -07001563{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001564 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 -07001565}
1566
1567template <typename T>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001568constexpr auto as_array_view (T* arr, std::ptrdiff_t len) -> typename details::ArrayViewArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001569{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001570 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001571}
1572
1573template <typename T, size_t N>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001574constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001575{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001576 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001577}
1578
1579template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001580constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001581{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001582 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001583}
1584
1585template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001586constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001587
1588template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001589constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001590{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001591 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001592}
1593
1594template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001595constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001596{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001597 return {begin, end};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001598}
1599
1600template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001601constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosh68064d62015-11-03 19:17:11 -08001602 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001603{
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001604 fail_fast_assert(arr.size() < PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001605 return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001606}
1607
1608template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001609constexpr auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosh68064d62015-11-03 19:17:11 -08001610 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001611
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001612template <typename ValueType, size_t Rank>
1613class strided_array_view : public basic_array_view<ValueType, strided_bounds<Rank>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001614{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001615 using Base = basic_array_view<ValueType, strided_bounds<Rank>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001616
Neil MacIntosh68064d62015-11-03 19:17:11 -08001617 template<typename OtherValue, size_t OtherRank>
1618 friend class strided_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001619
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001620public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001621 using Base::rank;
1622 using typename Base::bounds_type;
1623 using typename Base::size_type;
1624 using typename Base::pointer;
1625 using typename Base::value_type;
1626 using typename Base::index_type;
1627 using typename Base::iterator;
1628 using typename Base::const_iterator;
1629 using typename Base::reference;
1630
1631 // from static array of size N
1632 template<size_type N>
1633 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1634 {
1635 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1636 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001637
Neil MacIntosh68064d62015-11-03 19:17:11 -08001638 // from raw data
1639 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
1640 {
1641 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
1642 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001643
Neil MacIntosh68064d62015-11-03 19:17:11 -08001644 // from array view
1645 template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
1646 strided_array_view(array_view<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
1647 {
1648 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1649 }
1650
1651 // convertible
1652 template <typename OtherValueType,
1653 typename BaseType = basic_array_view<ValueType, strided_bounds<Rank>>,
1654 typename OtherBaseType = basic_array_view<OtherValueType, strided_bounds<Rank>>,
1655 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1656 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001657 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
1658 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001659
Neil MacIntosh68064d62015-11-03 19:17:11 -08001660 // convert from bytes
1661 template <typename OtherValueType>
1662 strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const
1663 {
1664 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1665 auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001666
Neil MacIntosh68064d62015-11-03 19:17:11 -08001667 size_type size = this->bounds().total_size() / d;
1668 return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
1669 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001670
Neil MacIntosh68064d62015-11-03 19:17:11 -08001671 strided_array_view section(index_type origin, index_type extents) const
1672 {
1673 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
1674 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1675 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001676
Neil MacIntosh68064d62015-11-03 19:17:11 -08001677 constexpr reference operator[](const index_type& idx) const
1678 {
1679 return Base::operator[](idx);
1680 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001681
Neil MacIntosh68064d62015-11-03 19:17:11 -08001682 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1683 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
1684 {
1685 auto ret = Base::operator[](idx);
1686 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1687 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001688
1689private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001690 static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
1691 {
1692 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");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001693
Neil MacIntosh68064d62015-11-03 19:17:11 -08001694 index_type ret = extent;
1695 ret[rank - 1] /= d;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001696
Neil MacIntosh68064d62015-11-03 19:17:11 -08001697 return ret;
1698 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001699
Neil MacIntosh68064d62015-11-03 19:17:11 -08001700 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
1701 static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
1702 {
1703 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001704
Neil MacIntosh68064d62015-11-03 19:17:11 -08001705 return strides;
1706 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001707
Neil MacIntosh68064d62015-11-03 19:17:11 -08001708 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1709 static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
1710 {
1711 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1712 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");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001713
Neil MacIntosh68064d62015-11-03 19:17:11 -08001714 for (size_t i = rank - 1; i > 0; --i)
1715 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 -07001716
Neil MacIntosh68064d62015-11-03 19:17:11 -08001717 index_type ret = strides / d;
1718 ret[rank - 1] = 1;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001719
Neil MacIntosh68064d62015-11-03 19:17:11 -08001720 return ret;
1721 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001722};
1723
1724template <typename ArrayView>
1725class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1726{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001727 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001728public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001729 using typename Base::reference;
1730 using typename Base::pointer;
1731 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001732
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001733private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001734 template <typename ValueType, typename Bounds>
1735 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001736
1737 pointer m_pdata;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001738 const ArrayView * m_validator;
1739 void validateThis() const
1740 {
1741 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");
1742 }
1743 contiguous_array_view_iterator (const ArrayView *container, bool isbegin) :
1744 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001745public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001746 reference operator*() const noexcept
1747 {
1748 validateThis();
1749 return *m_pdata;
1750 }
1751 pointer operator->() const noexcept
1752 {
1753 validateThis();
1754 return m_pdata;
1755 }
1756 contiguous_array_view_iterator& operator++() noexcept
1757 {
1758 ++m_pdata;
1759 return *this;
1760 }
1761 contiguous_array_view_iterator operator++(int)noexcept
1762 {
1763 auto ret = *this;
1764 ++(*this);
1765 return ret;
1766 }
1767 contiguous_array_view_iterator& operator--() noexcept
1768 {
1769 --m_pdata;
1770 return *this;
1771 }
1772 contiguous_array_view_iterator operator--(int)noexcept
1773 {
1774 auto ret = *this;
1775 --(*this);
1776 return ret;
1777 }
1778 contiguous_array_view_iterator operator+(difference_type n) const noexcept
1779 {
1780 contiguous_array_view_iterator ret{ *this };
1781 return ret += n;
1782 }
1783 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
1784 {
1785 m_pdata += n;
1786 return *this;
1787 }
1788 contiguous_array_view_iterator operator-(difference_type n) const noexcept
1789 {
1790 contiguous_array_view_iterator ret{ *this };
1791 return ret -= n;
1792 }
1793 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
1794 {
1795 return *this += -n;
1796 }
1797 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
1798 {
1799 fail_fast_assert(m_validator == rhs.m_validator);
1800 return m_pdata - rhs.m_pdata;
1801 }
1802 reference operator[](difference_type n) const noexcept
1803 {
1804 return *(*this + n);
1805 }
1806 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
1807 {
1808 fail_fast_assert(m_validator == rhs.m_validator);
1809 return m_pdata == rhs.m_pdata;
1810 }
1811 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
1812 {
1813 return !(*this == rhs);
1814 }
1815 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
1816 {
1817 fail_fast_assert(m_validator == rhs.m_validator);
1818 return m_pdata < rhs.m_pdata;
1819 }
1820 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
1821 {
1822 return !(rhs < *this);
1823 }
1824 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
1825 {
1826 return rhs < *this;
1827 }
1828 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
1829 {
1830 return !(rhs > *this);
1831 }
1832 void swap(contiguous_array_view_iterator& rhs) noexcept
1833 {
1834 std::swap(m_pdata, rhs.m_pdata);
1835 std::swap(m_validator, rhs.m_validator);
1836 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001837};
1838
1839template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001840contiguous_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 -07001841{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001842 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001843}
1844
1845template <typename ArrayView>
1846class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1847{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001848 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001849public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001850 using typename Base::reference;
1851 using typename Base::pointer;
1852 using typename Base::difference_type;
1853 using typename Base::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001854private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001855 template <typename ValueType, typename Bounds>
1856 friend class basic_array_view;
1857
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001858 const ArrayView * m_container;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001859 typename ArrayView::bounds_type::iterator m_itr;
1860 general_array_view_iterator(const ArrayView *container, bool isbegin) :
1861 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
1862 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001863public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001864 reference operator*() noexcept
1865 {
1866 return (*m_container)[*m_itr];
1867 }
1868 pointer operator->() noexcept
1869 {
1870 return &(*m_container)[*m_itr];
1871 }
1872 general_array_view_iterator& operator++() noexcept
1873 {
1874 ++m_itr;
1875 return *this;
1876 }
1877 general_array_view_iterator operator++(int)noexcept
1878 {
1879 auto ret = *this;
1880 ++(*this);
1881 return ret;
1882 }
1883 general_array_view_iterator& operator--() noexcept
1884 {
1885 --m_itr;
1886 return *this;
1887 }
1888 general_array_view_iterator operator--(int)noexcept
1889 {
1890 auto ret = *this;
1891 --(*this);
1892 return ret;
1893 }
1894 general_array_view_iterator operator+(difference_type n) const noexcept
1895 {
1896 general_array_view_iterator ret{ *this };
1897 return ret += n;
1898 }
1899 general_array_view_iterator& operator+=(difference_type n) noexcept
1900 {
1901 m_itr += n;
1902 return *this;
1903 }
1904 general_array_view_iterator operator-(difference_type n) const noexcept
1905 {
1906 general_array_view_iterator ret{ *this };
1907 return ret -= n;
1908 }
1909 general_array_view_iterator& operator-=(difference_type n) noexcept
1910 {
1911 return *this += -n;
1912 }
1913 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
1914 {
1915 fail_fast_assert(m_container == rhs.m_container);
1916 return m_itr - rhs.m_itr;
1917 }
1918 value_type operator[](difference_type n) const noexcept
1919 {
1920 return (*m_container)[m_itr[n]];;
1921 }
1922 bool operator==(const general_array_view_iterator& rhs) const noexcept
1923 {
1924 fail_fast_assert(m_container == rhs.m_container);
1925 return m_itr == rhs.m_itr;
1926 }
1927 bool operator !=(const general_array_view_iterator& rhs) const noexcept
1928 {
1929 return !(*this == rhs);
1930 }
1931 bool operator<(const general_array_view_iterator& rhs) const noexcept
1932 {
1933 fail_fast_assert(m_container == rhs.m_container);
1934 return m_itr < rhs.m_itr;
1935 }
1936 bool operator<=(const general_array_view_iterator& rhs) const noexcept
1937 {
1938 return !(rhs < *this);
1939 }
1940 bool operator>(const general_array_view_iterator& rhs) const noexcept
1941 {
1942 return rhs < *this;
1943 }
1944 bool operator>=(const general_array_view_iterator& rhs) const noexcept
1945 {
1946 return !(rhs > *this);
1947 }
1948 void swap(general_array_view_iterator& rhs) noexcept
1949 {
1950 std::swap(m_itr, rhs.m_itr);
1951 std::swap(m_container, rhs.m_container);
1952 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001953};
1954
1955template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001956general_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 -07001957{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001958 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001959}
1960
Neil MacIntoshef626fd2015-09-29 16:41:37 -07001961} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001962
Neil MacIntoshd5316802015-09-30 21:54:08 -07001963#ifdef _MSC_VER
1964
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001965#undef constexpr
1966#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001967
Neil MacIntoshd5316802015-09-30 21:54:08 -07001968#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07001969#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07001970
1971#ifndef GSL_THROWS_FOR_TESTING
1972#pragma undef noexcept
1973#endif // GSL_THROWS_FOR_TESTING
1974
Neil MacIntoshe9a96022015-11-03 18:56:55 -08001975#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
1976#pragma pop_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG")
1977
1978
Neil MacIntosh9a297122015-09-14 15:11:07 -07001979#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07001980
Neil MacIntoshd5316802015-09-30 21:54:08 -07001981#endif // _MSC_VER
1982
1983#if defined(GSL_THROWS_FOR_TESTING)
1984#undef noexcept
1985#endif // GSL_THROWS_FOR_TESTING
1986
Treb Connell51da1362015-09-24 18:08:34 -07001987
1988#endif // GSL_ARRAY_VIEW_H