blob: 4d5d46a9da176bc76eda35917a90909689580b5e [file] [log] [blame]
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
Treb Connell51da1362015-09-24 18:08:34 -070019#ifndef GSL_ARRAY_VIEW_H
20#define GSL_ARRAY_VIEW_H
21
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070022#include <new>
23#include <stdexcept>
24#include <cstddef>
25#include <cstdint>
26#include <limits>
Anna Gringauze2cdedda2015-10-15 13:19:24 -070027#include <numeric>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070028#include <type_traits>
29#include <utility>
30#include <array>
31#include <iterator>
Kern Handac4f9b872015-09-25 17:01:29 -070032#include <algorithm>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070033#include "fail_fast.h"
34
Neil MacIntoshd5316802015-09-30 21:54:08 -070035#ifdef _MSC_VER
36
37// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070038#pragma push_macro("constexpr")
39#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070040
41
42// VS 2013 workarounds
43#if _MSC_VER <= 1800
44
45// noexcept is not understood
46#ifndef GSL_THROWS_FOR_TESTING
47#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070048#endif
49
Neil MacIntoshd5316802015-09-30 21:54:08 -070050// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070051#pragma warning(push)
52#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntoshd5316802015-09-30 21:54:08 -070053
Neil MacIntosh9a297122015-09-14 15:11:07 -070054#endif // _MSC_VER <= 1800
55
Neil MacIntoshd5316802015-09-30 21:54:08 -070056#endif // _MSC_VER
57
58// In order to test the library, we need it to throw exceptions that we can catch
59#ifdef GSL_THROWS_FOR_TESTING
60#define noexcept /* nothing */
61#endif // GSL_THROWS_FOR_TESTING
62
63
Neil MacIntoshef626fd2015-09-29 16:41:37 -070064namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070065
66/*
67** begin definitions of index and bounds
68*/
69namespace details
70{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070071 template <typename T>
72 class arrow_proxy
73 {
74 public:
75 explicit arrow_proxy(T t)
76 : val(t)
77 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -070078 const T operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070079 {
80 return val;
81 }
Neil MacIntoshd5316802015-09-30 21:54:08 -070082 const T* operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070083 {
84 return &val;
85 }
86 private:
87 T val;
88 };
89}
90
Neil MacIntoshf45fedb2015-10-15 14:29:35 -070091template <size_t Rank>
Anna Gringauzedb384972015-10-05 12:34:23 -070092class index final
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070093{
Anna Gringauzedb384972015-10-05 12:34:23 -070094 static_assert(Rank > 0, "Rank must be greater than 0!");
95
Neil MacIntoshf45fedb2015-10-15 14:29:35 -070096 template <size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -070097 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -070098
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070099public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700100 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700101 using value_type = std::ptrdiff_t;
102 using size_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700103 using reference = std::add_lvalue_reference_t<value_type>;
104 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700105
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700106 constexpr index() noexcept
107 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700108
Anna Gringauzedb384972015-10-05 12:34:23 -0700109 constexpr index(const value_type(&values)[Rank]) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700110 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700111 std::copy(values, values + Rank, elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700112 }
113
Anna Gringauzedb384972015-10-05 12:34:23 -0700114 // Preconditions: il.size() == rank
115 constexpr index(std::initializer_list<value_type> il) noexcept
116 {
117 fail_fast_assert(il.size() == Rank, "The size of the initializer list must match the rank of the array");
118 std::copy(begin(il), end(il), elems);
119 }
120
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700121 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700122 constexpr index& operator=(const index& rhs) noexcept = default;
123
124 // Preconditions: component_idx < rank
125 constexpr reference operator[](size_t component_idx)
126 {
127 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
128 return elems[component_idx];
129 }
130
131 // Preconditions: component_idx < rank
132 constexpr const_reference operator[](size_t component_idx) const noexcept
133 {
134 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
135 return elems[component_idx];
136 }
137
138 constexpr bool operator==(const index& rhs) const noexcept
139 {
140 return std::equal(elems, elems + rank, rhs.elems);
141 }
142
143 constexpr bool operator!=(const index& rhs) const noexcept
144 {
145 return !(this == rhs);
146 }
147
148 constexpr index operator+() const noexcept
149 {
150 return *this;
151 }
152
153 constexpr index operator-() const noexcept
154 {
155 index ret = *this;
156 std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
157 return ret;
158 }
159
160 constexpr index operator+(const index& rhs) const noexcept
161 {
162 index ret = *this;
163 ret += rhs;
164 return ret;
165 }
166
167 constexpr index operator-(const index& rhs) const noexcept
168 {
169 index ret = *this;
170 ret -= rhs;
171 return ret;
172 }
173
174 constexpr index& operator+=(const index& rhs) noexcept
175 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700176 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
Anna Gringauzedb384972015-10-05 12:34:23 -0700177 return *this;
178 }
179
180 constexpr index& operator-=(const index& rhs) noexcept
181 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700182 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
Anna Gringauzedb384972015-10-05 12:34:23 -0700183 return *this;
184 }
185
186 constexpr index operator*(value_type v) const noexcept
187 {
188 index ret = *this;
189 ret *= v;
190 return ret;
191 }
192
193 constexpr index operator/(value_type v) const noexcept
194 {
195 index ret = *this;
196 ret /= v;
197 return ret;
198 }
199
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700200 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700201 {
202 return rhs * v;
203 }
204
205 constexpr index& operator*=(value_type v) noexcept
206 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700207 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700208 return *this;
209 }
210
211 constexpr index& operator/=(value_type v) noexcept
212 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700213 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700214 return *this;
215 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700216
Anna Gringauzedb384972015-10-05 12:34:23 -0700217private:
218 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700219};
220
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700221template<>
222class index<1>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700223{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700224public:
Kern Handae1570262015-09-25 00:42:38 -0700225 static const size_t rank = 1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700226 using size_type = std::ptrdiff_t;
227 using value_type = std::ptrdiff_t;
228 using reference = std::add_lvalue_reference_t<std::ptrdiff_t>;
229 using const_reference = const std::ptrdiff_t&;//std::add_const_t<std::add_lvalue_reference_t<std::ptrdiff_t>>;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700230
231 template <size_t>
232 friend class index;
233
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700234 constexpr index() noexcept : value(0)
235 {}
236
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700237 constexpr index(value_type e) noexcept : value(e)
Anna Gringauzedb384972015-10-05 12:34:23 -0700238 {}
239
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700240 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
241 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700242
Anna Gringauzedb384972015-10-05 12:34:23 -0700243 constexpr index(const index &) noexcept = default;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700244
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700245 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700246 constexpr reference operator[](size_type component_idx) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700247 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700248 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700249 return value;
250 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700251
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700252 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700253 constexpr const_reference operator[](size_type component_idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700254 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700255 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700256 return value;
257 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700258
Neil MacIntoshd5316802015-09-30 21:54:08 -0700259 constexpr bool operator==(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700260 {
261 return value == rhs.value;
262 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700263
Neil MacIntoshd5316802015-09-30 21:54:08 -0700264 constexpr bool operator!=(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700265 {
266 return !(*this == rhs);
267 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700268
269 constexpr index operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700270 {
271 return *this;
272 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700273
274 constexpr index operator-() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700275 {
276 return index(-value);
277 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700278
279 constexpr index operator+(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700280 {
281 return index(value + rhs.value);
282 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700283
284 constexpr index operator-(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700285 {
286 return index(value - rhs.value);
287 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700288
289 constexpr index& operator+=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700290 {
291 value += rhs.value;
292 return *this;
293 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700294
295 constexpr index& operator-=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700296 {
297 value -= rhs.value;
298 return *this;
299 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700300
301 constexpr index& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700302 {
303 ++value;
304 return *this;
305 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700306
307 constexpr index operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700308 {
309 index ret = *this;
310 ++(*this);
311 return ret;
312 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700313
314 constexpr index& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700315 {
316 --value;
317 return *this;
318 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700319
320 constexpr index operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700321 {
322 index ret = *this;
323 --(*this);
324 return ret;
325 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700326
327 constexpr index operator*(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700328 {
329 return index(value * v);
330 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700331
332 constexpr index operator/(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700333 {
334 return index(value / v);
335 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700336
337 constexpr index& operator*=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700338 {
339 value *= v;
340 return *this;
341 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700342
343 constexpr index& operator/=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700344 {
345 value /= v;
346 return *this;
347 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700348
349 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700350 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700351 return{ rhs * v };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700352 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700353
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700354private:
355 value_type value;
356};
357
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700358const std::ptrdiff_t dynamic_range = -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700359
360struct generalized_mapping_tag {};
361struct contiguous_mapping_tag : generalized_mapping_tag {};
362
363namespace details
364{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700365
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700366 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700367 struct LessThan
368 {
369 static const bool value = Left < Right;
370 };
371
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700372 template <std::ptrdiff_t... Ranges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700373 struct BoundsRanges {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700374 using size_type = std::ptrdiff_t;
375 static const size_type Depth = 0;
376 static const size_type DynamicNum = 0;
377 static const size_type CurrentRange = 1;
378 static const size_type TotalSize = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700379
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700380 // TODO : following signature is for work around VS bug
381 template <typename OtherRange>
382 BoundsRanges(const OtherRange&, bool /* firstLevel */)
383 {}
384
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700385 BoundsRanges (const BoundsRanges&) = default;
386 BoundsRanges(const size_type* const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700387 BoundsRanges() = default;
388
389
Kern Handae1570262015-09-25 00:42:38 -0700390 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700391 void serialize(T&) const
392 {}
393
Kern Handae1570262015-09-25 00:42:38 -0700394 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700395 size_type linearize(const T&) const
396 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700397 return 0;
398 }
399
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700400 template <typename T, size_t Dim>
401 bool contains(const T&) const
402 {
403 return 0;
404 }
405
406 size_type totalSize() const noexcept
407 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700408 return TotalSize;
409 }
410
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700411 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700412 {
413 return true;
414 }
415 };
416
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700417 template <std::ptrdiff_t... RestRanges>
418 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
419 using Base = BoundsRanges <RestRanges... >;
420 using size_type = Base::size_type;
Kern Handae1570262015-09-25 00:42:38 -0700421 static const size_t Depth = Base::Depth + 1;
422 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700423 static const size_type CurrentRange = dynamic_range;
424 static const size_type TotalSize = dynamic_range;
425 const size_type m_bound;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700426
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700427 BoundsRanges (const BoundsRanges&) = default;
428
429 BoundsRanges(const size_type* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700430 {
431 fail_fast_assert(0 <= *arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700432 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700433
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700434 BoundsRanges() : m_bound(0) {}
435
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700436 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
437 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
438 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
439 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700440
Kern Handae1570262015-09-25 00:42:38 -0700441 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700442 void serialize(T& arr) const
443 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700444 arr[Dim] = elementNum();
445 this->Base::template serialize<T, Dim + 1>(arr);
446 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700447
Kern Handae1570262015-09-25 00:42:38 -0700448 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700449 size_type linearize(const T& arr) const
450 {
451 const size_type index = this->Base::totalSize() * arr[Dim];
452 fail_fast_assert(index < m_bound);
453 return index + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700454 }
455
Kern Handae1570262015-09-25 00:42:38 -0700456 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700457 size_type contains(const T & arr) const
458 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700459 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
460 if (last == -1)
461 return -1;
462 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700463 return cur < m_bound ? cur + last : -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700464 }
465
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700466 size_type totalSize() const noexcept
467 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700468 return m_bound;
469 }
470
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700471 size_type elementNum() const noexcept
472 {
473 return totalSize() / this->Base::totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700474 }
475
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700476 size_type elementNum(size_t dim) const noexcept
477 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700478 if (dim > 0)
479 return this->Base::elementNum(dim - 1);
480 else
481 return elementNum();
482 }
483
Neil MacIntoshd5316802015-09-30 21:54:08 -0700484 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700485 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700486 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700487 }
488 };
489
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700490 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
491 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
492 {
493 using Base = BoundsRanges <RestRanges... >;
494 using size_type = Base::size_type;
Kern Handae1570262015-09-25 00:42:38 -0700495 static const size_t Depth = Base::Depth + 1;
496 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700497 static const size_type CurrentRange = CurRange;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700498 static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700499
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700500 BoundsRanges (const BoundsRanges&) = default;
501 BoundsRanges(const size_type* const arr) : Base(arr) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700502 BoundsRanges() = default;
503
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700504 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
505 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>&other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700506 {
507 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
508 }
509
Kern Handae1570262015-09-25 00:42:38 -0700510 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700511 void serialize(T& arr) const
512 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700513 arr[Dim] = elementNum();
514 this->Base::template serialize<T, Dim + 1>(arr);
515 }
516
Kern Handae1570262015-09-25 00:42:38 -0700517 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700518 size_type linearize(const T& arr) const
519 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700520 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700521 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700522 }
523
Kern Handae1570262015-09-25 00:42:38 -0700524 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700525 size_type contains(const T& arr) const
526 {
527 if (arr[Dim] >= CurrentRange)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700528 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700529 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700530 if (last == -1)
531 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700532 return this->Base::totalSize() * arr[Dim] + last;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700533 }
534
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700535 size_type totalSize() const noexcept
536 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700537 return CurrentRange * this->Base::totalSize();
538 }
539
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700540 size_type elementNum() const noexcept
541 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700542 return CurrentRange;
543 }
544
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700545 size_type elementNum(size_t dim) const noexcept
546 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700547 if (dim > 0)
548 return this->Base::elementNum(dim - 1);
549 else
550 return elementNum();
551 }
552
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700553 bool operator== (const BoundsRanges& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700554 {
555 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
556 }
557 };
558
559 template <typename SourceType, typename TargetType, size_t Rank>
560 struct BoundsRangeConvertible2;
561
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700562 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
563 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
564
565 template <size_t Rank, typename SourceType, typename TargetType>
566 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
567
568 template <typename SourceType, typename TargetType, size_t Rank>
569 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
570 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
571 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
572 {};
573
574 template <typename SourceType, typename TargetType>
575 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
576
577 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
578 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
579 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
580 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
581 {};
582 template <typename SourceType, typename TargetType>
583 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
584
585 template <typename TypeChain>
586 struct TypeListIndexer
587 {
588 const TypeChain & obj;
589 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700590 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700591 const TypeChain & getObj(std::true_type)
592 {
593 return obj;
594 }
Kern Handae1570262015-09-25 00:42:38 -0700595 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700596 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
597 {
598 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
599 }
Kern Handae1570262015-09-25 00:42:38 -0700600 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700601 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
602 {
603 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
604 }
605 };
606
607 template <typename TypeChain>
608 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
609 {
610 return TypeListIndexer<TypeChain>(obj);
611 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700612
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700613 template <size_t Rank, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
614 constexpr Ret shift_left(const index<Rank>& other) noexcept
Anna Gringauzefdf86432015-10-14 10:46:22 -0700615 {
616 Ret ret;
617 for (size_t i = 0; i < Rank - 1; ++i)
618 {
619 ret[i] = other[i + 1];
620 }
621 return ret;
622 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700623}
624
625template <typename IndexType>
626class bounds_iterator;
627
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700628template <std::ptrdiff_t... Ranges>
629class static_bounds
630{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700631public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700632 static_bounds(const details::BoundsRanges<Ranges...>&) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700633 }
634};
635
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700636template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
637class static_bounds<FirstRange, RestRanges...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700638{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700639 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700640
641 MyRanges m_ranges;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700642 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
643 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700644
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700645 template <std::ptrdiff_t... OtherRanges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700646 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700647
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700648public:
Kern Handae1570262015-09-25 00:42:38 -0700649 static const size_t rank = MyRanges::Depth;
650 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700651 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700652
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700653 using size_type = std::ptrdiff_t;
654 using index_type = index<rank>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700655 using iterator = bounds_iterator<index_type>;
656 using const_iterator = bounds_iterator<index_type>;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700657 using difference_type = std::ptrdiff_t;
658 using sliced_type = static_bounds<RestRanges...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700659 using mapping_type = contiguous_mapping_tag;
660public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700661 constexpr static_bounds(const static_bounds&) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700662
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700663 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
664 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
665 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
666 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700667
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700668 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700669 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700670 fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700671 fail_fast_assert(m_ranges.totalSize() <= PTRDIFF_MAX, "Size of the range is larger than the max element of the size type");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700672 }
673
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700674 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700675
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700676 constexpr static_bounds& operator=(const static_bounds& otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700677 {
678 new(&m_ranges) MyRanges (otherBounds.m_ranges);
679 return *this;
680 }
681
Neil MacIntoshd5316802015-09-30 21:54:08 -0700682 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700683 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700684 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700685 }
686
Neil MacIntoshd5316802015-09-30 21:54:08 -0700687 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700688 {
689 return rank > 1 ? slice().size() : 1;
690 }
691
Neil MacIntoshd5316802015-09-30 21:54:08 -0700692 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700693 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700694 return m_ranges.totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700695 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700696
Neil MacIntoshd5316802015-09-30 21:54:08 -0700697 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700698 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700699 return m_ranges.totalSize();
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700700 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700701
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700702 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700703 {
704 return m_ranges.linearize(idx);
705 }
706
Neil MacIntoshd5316802015-09-30 21:54:08 -0700707 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700708 {
709 return m_ranges.contains(idx) != -1;
710 }
711
Neil MacIntoshd5316802015-09-30 21:54:08 -0700712 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700713 {
714 return m_ranges.elementNum(index);
715 }
716
Kern Handae1570262015-09-25 00:42:38 -0700717 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700718 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700719 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700720 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700721 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
722 }
723
Neil MacIntoshd5316802015-09-30 21:54:08 -0700724 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700725 {
Anna Gringauzefdf86432015-10-14 10:46:22 -0700726 size_type extents[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700727 m_ranges.serialize(extents);
Anna Gringauzedb384972015-10-05 12:34:23 -0700728 return{ extents };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700729 }
730
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700731 template <std::ptrdiff_t... Ranges>
732 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700733 {
734 return this->size() == rhs.size();
735 }
736
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700737 template <std::ptrdiff_t... Ranges>
738 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700739 {
740 return !(*this == rhs);
741 }
742
Neil MacIntoshd5316802015-09-30 21:54:08 -0700743 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700744 {
745 return const_iterator(*this);
746 }
747
Neil MacIntoshd5316802015-09-30 21:54:08 -0700748 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700749 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700750 return const_iterator(*this, this->index_bounds());
751 }
752};
753
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700754template <size_t Rank>
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700755class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700756{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700757 template <size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700758 friend class strided_bounds;
759
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700760public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700761 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700762 using value_type = std::ptrdiff_t;
763 using reference = std::add_lvalue_reference_t<value_type>;
764 using const_reference = std::add_const_t<reference>;
765 using size_type = value_type;
766 using difference_type = value_type;
767 using index_type = index<rank>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700768 using iterator = bounds_iterator<index_type>;
769 using const_iterator = bounds_iterator<index_type>;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700770 static const size_t dynamic_rank = rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700771 static const value_type static_size = dynamic_range;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700772 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
773 using mapping_type = generalized_mapping_tag;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700774
Anna Gringauzedb384972015-10-05 12:34:23 -0700775 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700776
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700777 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700778 : m_extents(values), m_strides(std::move(strides))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700779 {}
780
Anna Gringauzedb384972015-10-05 12:34:23 -0700781 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
782 : m_extents(extents), m_strides(strides)
783 {}
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700784
Neil MacIntoshd5316802015-09-30 21:54:08 -0700785 constexpr index_type strides() const noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700786 {
787 return m_strides;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700788 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700789
Neil MacIntoshd5316802015-09-30 21:54:08 -0700790 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700791 {
792 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700793 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700794 {
795 ret += (m_extents[i] - 1) * m_strides[i];
796 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700797 return ret + 1;
798 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700799
800 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700801 {
802 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700803 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700804 {
805 ret *= m_extents[i];
806 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700807 return ret;
808 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700809
810 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700811 {
Kern Handae1570262015-09-25 00:42:38 -0700812 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700813 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700814 if (idx[i] < 0 || idx[i] >= m_extents[i])
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700815 return false;
816 }
817 return true;
818 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700819
820 constexpr size_type linearize(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700821 {
822 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700823 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700824 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700825 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700826 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700827 }
828 return ret;
829 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700830
831 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700832 {
833 return m_strides[0];
834 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700835
836 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700837 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700838 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700839 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700840 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700841
842 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700843 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700844 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700845 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Anna Gringauzedb384972015-10-05 12:34:23 -0700846 return m_extents[Dim];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700847 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700848
849 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700850 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700851 return m_extents;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700852 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700853
854 const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700855 {
856 return const_iterator{ *this };
857 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700858
859 const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700860 {
861 return const_iterator{ *this, index_bounds() };
862 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700863
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700864private:
Anna Gringauzedb384972015-10-05 12:34:23 -0700865 index_type m_extents;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700866 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700867};
868
869template <typename T>
870struct is_bounds : std::integral_constant<bool, false> {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700871template <std::ptrdiff_t... Ranges>
872struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> {};
873template <size_t Rank>
874struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700875
876template <typename IndexType>
877class bounds_iterator
878 : public std::iterator<std::random_access_iterator_tag,
879 IndexType,
880 ptrdiff_t,
881 const details::arrow_proxy<IndexType>,
882 const IndexType>
883{
884private:
885 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
886public:
Kern Handae1570262015-09-25 00:42:38 -0700887 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700888 using typename Base::reference;
889 using typename Base::pointer;
890 using typename Base::difference_type;
891 using typename Base::value_type;
892 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700893 using index_size_type = typename IndexType::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700894 template <typename Bounds>
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700895 explicit bounds_iterator(const Bounds& bnd, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700896 : boundary(bnd.index_bounds())
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700897 , curr(std::move(curr))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700898 {
899 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
900 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700901 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700902 {
903 return curr;
904 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700905 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700906 {
907 return details::arrow_proxy<value_type>{ curr };
908 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700909 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700910 {
Kern Handae1570262015-09-25 00:42:38 -0700911 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700912 {
913 if (++curr[i] < boundary[i])
914 {
915 return *this;
916 }
917 else
918 {
919 curr[i] = 0;
920 }
921 }
922 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -0700923 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700924 {
925 curr[i] = boundary[i];
926 }
927 return *this;
928 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700929 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700930 {
931 auto ret = *this;
932 ++(*this);
933 return ret;
934 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700935 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700936 {
Neil MacIntoshfb913932015-09-27 16:25:43 -0700937 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700938 {
939 if (curr[i]-- > 0)
940 {
941 return *this;
942 }
943 else
944 {
945 curr[i] = boundary[i] - 1;
946 }
947 }
948 // If we're here the preconditions were violated
949 // "pre: there exists s such that r == ++s"
950 fail_fast_assert(false);
951 return *this;
952 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700953 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700954 {
955 auto ret = *this;
956 --(*this);
957 return ret;
958 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700959 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700960 {
961 bounds_iterator ret{ *this };
962 return ret += n;
963 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700964 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700965 {
966 auto linear_idx = linearize(curr) + n;
967 value_type stride;
968 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -0700969 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700970 {
971 stride[i] = stride[i + 1] * boundary[i + 1];
972 }
Kern Handae1570262015-09-25 00:42:38 -0700973 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700974 {
975 curr[i] = linear_idx / stride[i];
976 linear_idx = linear_idx % stride[i];
977 }
978 return *this;
979 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700980 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 {
982 bounds_iterator ret{ *this };
983 return ret -= n;
984 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700985 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700986 {
987 return *this += -n;
988 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700989 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700990 {
991 return linearize(curr) - linearize(rhs.curr);
992 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700993 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700994 {
995 return *(*this + n);
996 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700997 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700998 {
999 return curr == rhs.curr;
1000 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001001 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001002 {
1003 return !(*this == rhs);
1004 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001005 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001006 {
Kern Handae1570262015-09-25 00:42:38 -07001007 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001008 {
1009 if (curr[i] < rhs.curr[i])
1010 return true;
1011 }
1012 return false;
1013 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001014 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001015 {
1016 return !(rhs < *this);
1017 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001018 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001019 {
1020 return rhs < *this;
1021 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001022 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001023 {
1024 return !(rhs > *this);
1025 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001026 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001027 {
1028 std::swap(boundary, rhs.boundary);
1029 std::swap(curr, rhs.curr);
1030 }
1031private:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001032 index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001033 {
1034 // TODO: Smarter impl.
1035 // Check if past-the-end
1036 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001037 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001038 {
1039 if (idx[i] != boundary[i])
1040 {
1041 pte = false;
1042 break;
1043 }
1044 }
1045 index_size_type multiplier = 1;
1046 index_size_type res = 0;
1047 if (pte)
1048 {
1049 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001050 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001051 {
1052 res += (idx[i] - 1) * multiplier;
1053 multiplier *= boundary[i];
1054 }
1055 }
1056 else
1057 {
Kern Handae1570262015-09-25 00:42:38 -07001058 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001059 {
1060 res += idx[i] * multiplier;
1061 multiplier *= boundary[i];
1062 }
1063 }
1064 return res;
1065 }
1066 value_type boundary;
1067 value_type curr;
1068};
1069
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001070template <>
1071class bounds_iterator<index<1>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001072 : public std::iterator<std::random_access_iterator_tag,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001073 index<1>,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001074 ptrdiff_t,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001075 const details::arrow_proxy<index<1>>,
1076 const index<1>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001077{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001078 using Base = std::iterator<std::random_access_iterator_tag, index<1>, std::ptrdiff_t, const details::arrow_proxy<index<1>>, const index<1>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001079
1080public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001081 using Base::reference;
1082 using Base::pointer;
1083 using Base::difference_type;
1084 using Base::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001085 using index_type = value_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001086 using index_size_type = index_type::size_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001087
1088 template <typename Bounds>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001089 explicit bounds_iterator(const Bounds&, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001090 : curr( std::move(curr) )
1091 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -07001092 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001093 {
1094 return curr;
1095 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001096 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001097 {
1098 return details::arrow_proxy<value_type>{ curr };
1099 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001100 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001101 {
1102 ++curr;
1103 return *this;
1104 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001105 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001106 {
1107 auto ret = *this;
1108 ++(*this);
1109 return ret;
1110 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001111 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001112 {
1113 curr--;
1114 return *this;
1115 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001116 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001117 {
1118 auto ret = *this;
1119 --(*this);
1120 return ret;
1121 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001122 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001123 {
1124 bounds_iterator ret{ *this };
1125 return ret += n;
1126 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001127 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001128 {
1129 curr += n;
1130 return *this;
1131 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001132 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001133 {
1134 bounds_iterator ret{ *this };
1135 return ret -= n;
1136 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001137 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001138 {
1139 return *this += -n;
1140 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001141 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001142 {
1143 return curr[0] - rhs.curr[0];
1144 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001145 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001146 {
1147 return curr + n;
1148 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001149 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001150 {
1151 return curr == rhs.curr;
1152 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001153 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001154 {
1155 return !(*this == rhs);
1156 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001157 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001158 {
1159 return curr[0] < rhs.curr[0];
1160 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001161 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001162 {
1163 return !(rhs < *this);
1164 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001165 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001166 {
1167 return rhs < *this;
1168 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001169 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001170 {
1171 return !(rhs > *this);
1172 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001173 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001174 {
1175 std::swap(curr, rhs.curr);
1176 }
1177private:
1178 value_type curr;
1179};
1180
1181template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001182bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001183{
1184 return rhs + n;
1185}
1186
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001187//
1188// begin definitions of basic_array_view
1189//
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001190namespace details
1191{
1192 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001193 constexpr std::enable_if_t<std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001194 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001195 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001196 }
1197
Neil MacIntosh99746e22015-09-27 16:53:58 -07001198 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001199 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001200 constexpr std::enable_if_t<std::is_same<typename Bounds::mapping_type, contiguous_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001201 {
1202 auto extents = bnd.index_bounds();
Anna Gringauzefdf86432015-10-14 10:46:22 -07001203 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001204
1205 stride[Bounds::rank - 1] = 1;
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001206 for (size_t i = 1; i < Bounds::rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -07001207 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001208 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
Anna Gringauzedb384972015-10-05 12:34:23 -07001209 }
1210 return{ stride };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001211 }
1212
1213 template <typename BoundsSrc, typename BoundsDest>
1214 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1215 {
1216 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1217 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1218 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");
1219 fail_fast_assert(src.size() == dest.size());
1220 }
1221
1222
1223} // namespace details
1224
1225template <typename ArrayView>
1226class contiguous_array_view_iterator;
1227template <typename ArrayView>
1228class general_array_view_iterator;
1229enum class byte : std::uint8_t {};
1230
1231template <typename ValueType, typename BoundsType>
1232class basic_array_view
1233{
1234public:
Kern Handae1570262015-09-25 00:42:38 -07001235 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001236 using bounds_type = BoundsType;
1237 using size_type = typename bounds_type::size_type;
1238 using index_type = typename bounds_type::index_type;
1239 using value_type = ValueType;
1240 using pointer = ValueType*;
1241 using reference = ValueType&;
1242 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>>;
1243 using const_iterator = std::conditional_t<std::is_same<typename BoundsType::mapping_type, contiguous_mapping_tag>::value, contiguous_array_view_iterator<basic_array_view<const ValueType, BoundsType>>, general_array_view_iterator<basic_array_view<const ValueType, BoundsType>>>;
1244 using reverse_iterator = std::reverse_iterator<iterator>;
1245 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1246 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1247
1248private:
1249 pointer m_pdata;
1250 bounds_type m_bounds;
1251
1252public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001253 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001254 {
1255 return m_bounds;
1256 }
Kern Handae1570262015-09-25 00:42:38 -07001257 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001258 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001259 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001260 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001261 return m_bounds.template extent<Dim>();
1262 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001263 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001264 {
1265 return m_bounds.size();
1266 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001267 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001268 {
1269 return m_pdata[m_bounds.linearize(idx)];
1270 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001271 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001272 {
1273 return m_pdata;
1274 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001275 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001276 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001277 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001278 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001279 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001280
1281 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001282 return Ret {m_pdata + ridx, m_bounds.slice()};
1283 }
1284
Neil MacIntoshd5316802015-09-30 21:54:08 -07001285 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001286 {
1287 return m_pdata != nullptr;
1288 }
1289
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001290 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001291 {
1292 return iterator {this, true};
1293 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001294 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001295 {
1296 return iterator {this};
1297 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001298 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001299 {
1300 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1301 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001302 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001303 {
1304 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1305 }
1306
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001307 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001308 {
1309 return reverse_iterator {end()};
1310 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001311 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001312 {
1313 return reverse_iterator {begin()};
1314 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001315 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001316 {
1317 return const_reverse_iterator {cend()};
1318 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001319 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001320 {
1321 return const_reverse_iterator {cbegin()};
1322 }
1323
1324 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001325 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001326 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001327 return m_bounds.size() == other.m_bounds.size() &&
1328 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001329 }
1330
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001331 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001332 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001333 {
1334 return !(*this == other);
1335 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001336
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001337 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001338 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001339 {
1340 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1341 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001342
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001343 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001344 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001345 {
1346 return !(other < *this);
1347 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001348
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001349 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001350 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001351 {
1352 return (other < *this);
1353 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001354
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001355 template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001356 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001357 {
1358 return !(*this < other);
1359 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001360
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001361public:
1362 template <typename OtherValueType, typename OtherBounds,
1363 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1364 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001365 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001366 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1367 {
1368 }
1369protected:
1370
Neil MacIntoshd5316802015-09-30 21:54:08 -07001371 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001372 : m_pdata(data)
1373 , m_bounds(std::move(bound))
1374 {
1375 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1376 }
1377 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001378 constexpr basic_array_view(T *data, std::enable_if_t<std::is_same<value_type, std::remove_all_extents_t<T>>::value, bounds_type> bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001379 : m_pdata(reinterpret_cast<pointer>(data))
1380 , m_bounds(std::move(bound))
1381 {
1382 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1383 }
1384 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001385 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001386 {
1387 details::verifyBoundsReshape(m_bounds, bounds);
1388 return {m_pdata, bounds};
1389 }
1390private:
1391
1392 friend iterator;
1393 friend const_iterator;
1394 template <typename ValueType2, typename BoundsType2>
1395 friend class basic_array_view;
1396};
1397
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001398template <std::ptrdiff_t DimSize = dynamic_range>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001399struct dim
1400{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001401 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001402};
1403template <>
1404struct dim<dynamic_range>
1405{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001406 static const std::ptrdiff_t value = dynamic_range;
1407 const std::ptrdiff_t dvalue;
1408 dim(std::ptrdiff_t size) : dvalue(size) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001409};
1410
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001411template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001412class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001413
1414template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001415class strided_array_view;
1416
1417namespace details
1418{
1419 template <typename T, typename = std::true_type>
1420 struct ArrayViewTypeTraits
1421 {
1422 using value_type = T;
1423 using size_type = size_t;
1424 };
1425
1426 template <typename Traits>
1427 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1428 {
1429 using value_type = typename Traits::array_view_traits::value_type;
1430 using size_type = typename Traits::array_view_traits::size_type;
1431 };
1432
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001433 template <typename T, size_t... Ranks>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001434 struct ArrayViewArrayTraits {
1435 using type = array_view<T, Ranks...>;
1436 using value_type = T;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001437 using bounds_type = static_bounds<Ranks...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001438 using pointer = T*;
1439 using reference = T&;
1440 };
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001441 template <typename T, size_t N, size_t... Ranks>
1442 struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001443
1444 template <typename BoundsType>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001445 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001446 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001447 fail_fast_assert(totalSize <= PTRDIFF_MAX);
1448 return BoundsType{totalSize};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001449 }
1450 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001451 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001452 {
1453 fail_fast_assert(BoundsType::static_size == totalSize);
1454 return {};
1455 }
1456 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001457 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001458 {
1459 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1460 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1461 }
1462
1463 struct Sep{};
1464
1465 template <typename T, typename... Args>
1466 T static_as_array_view_helper(Sep, Args... args)
1467 {
1468 return T{static_cast<typename T::size_type>(args)...};
1469 }
1470 template <typename T, typename Arg, typename... Args>
1471 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)
1472 {
1473 return static_as_array_view_helper<T>(args...);
1474 }
1475 template <typename T, typename... Args>
1476 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1477 {
1478 return static_as_array_view_helper<T>(args..., val.dvalue);
1479 }
1480
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001481 template <typename ...Dimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001482 struct static_as_array_view_static_bounds_helper
1483 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001484 using type = static_bounds<(Dimensions::value)...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001485 };
1486
1487 template <typename T>
1488 struct is_array_view_oracle : std::false_type
1489 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001490
1491 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001492 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1493 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001494
1495 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001496 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1497 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001498
1499 template <typename T>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001500 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1501 {};
1502
1503}
1504
1505
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001506template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1507class array_view : public basic_array_view <ValueType, static_bounds <FirstDimension, RestDimensions...>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001508{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001509 template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001510 friend class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001511
1512 using Base = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001513
1514public:
1515 using typename Base::bounds_type;
1516 using typename Base::size_type;
1517 using typename Base::pointer;
1518 using typename Base::value_type;
1519 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001520 using typename Base::iterator;
1521 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001522 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001523 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001524
1525public:
1526 // basic
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001527 constexpr array_view(pointer ptr, size_type size) : Base(ptr, bounds_type{ size })
1528 {}
1529
1530 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1531 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001532
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001533 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001534 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001535
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001536 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001537 {
1538 fail_fast_assert(size == 0);
1539 }
1540
1541 // default
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001542 template <size_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001543 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001544 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001545
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001546 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001547 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001548 /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001549 constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size})
1550 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001551
1552 // from n-dimensions static array
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001553 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
1554 typename = std::enable_if_t<std::is_convertible<Helper::value_type(*)[], typename Base::value_type(*)[]>::value>>
1555 constexpr array_view (T (&arr)[N]) : Base(arr, Helper::bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001556 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001557
1558 // from n-dimensions static array with size
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001559 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
1560 typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type(*)[], typename Base::value_type(*)[]>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001561 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001562 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001563 {
1564 fail_fast_assert(size <= N);
1565 }
1566
1567 // from std array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001568 template <size_t N,
1569 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value>
1570 >
1571 constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1572 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001573
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001574 template <size_t N,
1575 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value
1576 && std::is_const<value_type>::value>
1577 >
1578 constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1579 {}
1580
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001581 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1582 template <typename Ptr,
1583 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001584 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>
1585 > // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001586 constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001587 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001588
1589 // from containers. It must has .size() and .data() two function signatures
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001590 template <typename Cont, typename DataType = typename Cont::value_type,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001591 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001592 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001593 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001594 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001595 constexpr array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001596 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001597
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001598 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001599
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001600 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001601 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
1602 typename BaseType = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>,
1603 typename OtherBaseType = basic_array_view<OtherValueType, static_bounds<OtherDimensions...>>,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001604 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1605 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001606 constexpr array_view(const array_view<OtherValueType, OtherDimensions...> &av)
1607 : Base(static_cast<const typename array_view<OtherValueType, OtherDimensions...>::Base&>(av))
1608 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001609
1610 // reshape
1611 template <typename... Dimensions2>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001612 constexpr array_view<ValueType, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001613 {
1614 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001615 using BoundsType = typename array_view<ValueType, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001616 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1617 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001618 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001619 }
1620
1621 // to bytes array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001622 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1623 constexpr auto as_bytes() const noexcept -> array_view<const byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001624 {
1625 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001626 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001627 }
1628
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001629 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1630 constexpr auto as_writeable_bytes() const noexcept -> array_view<byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001631 {
1632 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001633 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001634 }
1635
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001636 // from bytes array
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001637 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
1638 constexpr auto as_array_view() const noexcept -> array_view<const U, dynamic_range, (Base::bounds_type::static_size != dynamic_range ? static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001639 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001640 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0),
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001641 "Target type must be standard layout and its size must match the byte array size");
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001642 fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
1643 return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001644 }
1645
1646 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001647 constexpr auto as_array_view() const noexcept -> array_view<U, dynamic_range, (Base::bounds_type::static_size != dynamic_range ? static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001648 {
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001649 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0),
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001650 "Target type must be standard layout and its size must match the byte array size");
1651 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001652 return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001653 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001654
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001655 // section on linear space
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001656 template<std::ptrdiff_t Count>
1657 constexpr array_view<ValueType, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001658 {
1659 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1660 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
Anna Gringauze18cd9802015-09-14 16:34:26 -07001661 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001662 }
1663
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001664 constexpr array_view<ValueType, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001665 {
1666 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001667 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001668 }
1669
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001670 template<std::ptrdiff_t Count>
1671 constexpr array_view<ValueType, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001672 {
1673 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1674 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001675 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001676 }
1677
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001678 constexpr array_view<ValueType, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001679 {
1680 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001681 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001682 }
1683
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001684 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
1685 constexpr array_view<ValueType, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001686 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001687 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");
1688 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size()));
Anna Gringauze18cd9802015-09-14 16:34:26 -07001689 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001690 }
1691
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001692 constexpr array_view<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001693 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001694 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1695 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001696 }
1697
1698 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001699 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001700 {
1701 return this->size();
1702 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001703
Neil MacIntoshd5316802015-09-30 21:54:08 -07001704 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001705 {
1706 return length();
1707 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001708
Neil MacIntoshd5316802015-09-30 21:54:08 -07001709 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001710 {
1711 return sizeof(value_type) * this->size();
1712 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001713
Neil MacIntoshd5316802015-09-30 21:54:08 -07001714 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001715 {
1716 return bytes();
1717 }
1718
1719 // section
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001720 constexpr strided_array_view<ValueType, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001721 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001722 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001723 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001724 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001725
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001726 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001727 {
1728 return Base::operator[](idx);
1729 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001730
Anna Gringauze1a864982015-09-14 18:55:06 -07001731 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001732 constexpr array_view<ValueType, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001733 {
1734 auto ret = Base::operator[](idx);
1735 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001736 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001737
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001738 using Base::operator==;
1739 using Base::operator!=;
1740 using Base::operator<;
1741 using Base::operator<=;
1742 using Base::operator>;
1743 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001744};
1745
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001746template <typename T, std::ptrdiff_t... Dimensions>
1747constexpr 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 -07001748{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001749 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 -07001750}
1751
1752template <typename T>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001753constexpr auto as_array_view (T* arr, std::ptrdiff_t len) -> typename details::ArrayViewArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001754{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001755 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756}
1757
1758template <typename T, size_t N>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001759constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001760{
1761 return {arr};
1762}
1763
1764template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001765constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001766{
1767 return {arr};
1768}
1769
1770template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001771constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001772
1773template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001774constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001775{
1776 return {arr};
1777}
1778
1779template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001780constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001781{
1782 return {begin, end};
1783}
1784
1785template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001786constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001787 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1788{
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001789 fail_fast_assert(arr.size() < PTRDIFF_MAX);
1790 return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001791}
1792
1793template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001794constexpr auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001795 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1796
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001797template <typename ValueType, size_t Rank>
1798class strided_array_view : public basic_array_view<ValueType, strided_bounds<Rank>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001799{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001800 using Base = basic_array_view<ValueType, strided_bounds<Rank>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001801
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001802 template<typename OtherValue, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001803 friend class strided_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001804
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001805public:
1806 using Base::rank;
1807 using typename Base::bounds_type;
1808 using typename Base::size_type;
1809 using typename Base::pointer;
1810 using typename Base::value_type;
1811 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001812 using typename Base::iterator;
1813 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001814 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001815
1816 // from static array of size N
1817 template<size_type N>
1818 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1819 {
1820 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1821 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001822
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001823 // from raw data
1824 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001825 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001826 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001827 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001828
1829 // from array view
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001830 template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
1831 strided_array_view(array_view<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001832 {
1833 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1834 }
1835
1836 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001837 template <typename OtherValueType,
1838 typename BaseType = basic_array_view<ValueType, strided_bounds<Rank>>,
1839 typename OtherBaseType = basic_array_view<OtherValueType, strided_bounds<Rank>>,
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001840 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1841 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001842 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
1843 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001844
1845 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001846 template <typename OtherValueType>
1847 strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001848 {
1849 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001850 auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001851
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001852 size_type size = this->bounds().total_size() / d;
1853 return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001854 }
1855
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001856 strided_array_view section(index_type origin, index_type extents) const
1857 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001858 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001859 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1860 }
1861
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001862 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001863 {
1864 return Base::operator[](idx);
1865 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001866
1867 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001868 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001869 {
1870 auto ret = Base::operator[](idx);
1871 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1872 }
1873
1874private:
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001875 static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001876 {
1877 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");
1878
1879 index_type ret = extent;
1880 ret[rank - 1] /= d;
1881
1882 return ret;
1883 }
1884
1885 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001886 static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001887 {
1888 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1889
1890 return strides;
1891 }
1892
1893 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001894 static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001895 {
1896 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1897 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");
1898
Neil MacIntosh99746e22015-09-27 16:53:58 -07001899 for (size_t i = rank - 1; i > 0; --i)
1900 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 -07001901
1902 index_type ret = strides / d;
1903 ret[rank - 1] = 1;
1904
1905 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001906 }
1907};
1908
1909template <typename ArrayView>
1910class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1911{
1912 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1913public:
1914 using typename Base::reference;
1915 using typename Base::pointer;
1916 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001917
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001918private:
1919 template <typename ValueType, typename Bounds>
1920 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001921
1922 pointer m_pdata;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001923 const ArrayView * m_validator;
1924 void validateThis() const
1925 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07001926 fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), "iterator is out of range of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001927 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001928
1929 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001930 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001931
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001932public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001933 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001934 {
1935 validateThis();
1936 return *m_pdata;
1937 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001938 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001939 {
1940 validateThis();
1941 return m_pdata;
1942 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001943 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001944 {
1945 ++m_pdata;
1946 return *this;
1947 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001948 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001949 {
1950 auto ret = *this;
1951 ++(*this);
1952 return ret;
1953 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001954 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001955 {
1956 --m_pdata;
1957 return *this;
1958 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001959 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001960 {
1961 auto ret = *this;
1962 --(*this);
1963 return ret;
1964 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001965 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001966 {
1967 contiguous_array_view_iterator ret{ *this };
1968 return ret += n;
1969 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001970 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001971 {
1972 m_pdata += n;
1973 return *this;
1974 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001975 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001976 {
1977 contiguous_array_view_iterator ret{ *this };
1978 return ret -= n;
1979 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001980 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001981 {
1982 return *this += -n;
1983 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001984 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001985 {
1986 fail_fast_assert(m_validator == rhs.m_validator);
1987 return m_pdata - rhs.m_pdata;
1988 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001989 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001990 {
1991 return *(*this + n);
1992 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001993 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001994 {
1995 fail_fast_assert(m_validator == rhs.m_validator);
1996 return m_pdata == rhs.m_pdata;
1997 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001998 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001999 {
2000 return !(*this == rhs);
2001 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002002 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002003 {
2004 fail_fast_assert(m_validator == rhs.m_validator);
2005 return m_pdata < rhs.m_pdata;
2006 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002007 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002008 {
2009 return !(rhs < *this);
2010 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002011 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002012 {
2013 return rhs < *this;
2014 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002015 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002016 {
2017 return !(rhs > *this);
2018 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002019 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002020 {
2021 std::swap(m_pdata, rhs.m_pdata);
2022 std::swap(m_validator, rhs.m_validator);
2023 }
2024};
2025
2026template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002027contiguous_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 -07002028{
2029 return rhs + n;
2030}
2031
2032template <typename ArrayView>
2033class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2034{
2035 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2036public:
2037 using typename Base::reference;
2038 using typename Base::pointer;
2039 using typename Base::difference_type;
2040 using typename Base::value_type;
2041private:
2042 template <typename ValueType, typename Bounds>
2043 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07002044
2045 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002046 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07002047
2048 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2049 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2050 {}
2051
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002052public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002053 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002054 {
2055 return (*m_container)[*m_itr];
2056 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002057 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002058 {
2059 return &(*m_container)[*m_itr];
2060 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002061 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002062 {
2063 ++m_itr;
2064 return *this;
2065 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002066 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002067 {
2068 auto ret = *this;
2069 ++(*this);
2070 return ret;
2071 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002072 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002073 {
2074 --m_itr;
2075 return *this;
2076 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002077 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002078 {
2079 auto ret = *this;
2080 --(*this);
2081 return ret;
2082 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002083 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002084 {
2085 general_array_view_iterator ret{ *this };
2086 return ret += n;
2087 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002088 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002089 {
2090 m_itr += n;
2091 return *this;
2092 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002093 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002094 {
2095 general_array_view_iterator ret{ *this };
2096 return ret -= n;
2097 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002098 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002099 {
2100 return *this += -n;
2101 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002102 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002103 {
2104 fail_fast_assert(m_container == rhs.m_container);
2105 return m_itr - rhs.m_itr;
2106 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002107 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002108 {
2109 return (*m_container)[m_itr[n]];;
2110 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002111 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002112 {
2113 fail_fast_assert(m_container == rhs.m_container);
2114 return m_itr == rhs.m_itr;
2115 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002116 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002117 {
2118 return !(*this == rhs);
2119 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002120 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002121 {
2122 fail_fast_assert(m_container == rhs.m_container);
2123 return m_itr < rhs.m_itr;
2124 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002125 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002126 {
2127 return !(rhs < *this);
2128 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002129 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002130 {
2131 return rhs < *this;
2132 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002133 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002134 {
2135 return !(rhs > *this);
2136 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002137 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002138 {
2139 std::swap(m_itr, rhs.m_itr);
2140 std::swap(m_container, rhs.m_container);
2141 }
2142};
2143
2144template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002145general_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 -07002146{
2147 return rhs + n;
2148}
2149
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002150} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002151
Neil MacIntoshd5316802015-09-30 21:54:08 -07002152#ifdef _MSC_VER
2153
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002154#undef constexpr
2155#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002156
Neil MacIntoshd5316802015-09-30 21:54:08 -07002157#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002158#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002159
2160#ifndef GSL_THROWS_FOR_TESTING
2161#pragma undef noexcept
2162#endif // GSL_THROWS_FOR_TESTING
2163
Neil MacIntosh9a297122015-09-14 15:11:07 -07002164#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002165
Neil MacIntoshd5316802015-09-30 21:54:08 -07002166#endif // _MSC_VER
2167
2168#if defined(GSL_THROWS_FOR_TESTING)
2169#undef noexcept
2170#endif // GSL_THROWS_FOR_TESTING
2171
Treb Connell51da1362015-09-24 18:08:34 -07002172
2173#endif // GSL_ARRAY_VIEW_H