blob: 4211637c3220419e8f9f64c0ca3c3537b4f5b464 [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;
101 using value_type = std::remove_reference_t<ValueType>;
102 using reference = std::add_lvalue_reference_t<value_type>;
103 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700104
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700105 constexpr index() noexcept
106 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700107
Anna Gringauzedb384972015-10-05 12:34:23 -0700108 constexpr index(const value_type(&values)[Rank]) noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700109 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700110 std::copy(values, values + Rank, elems);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700111 }
112
Anna Gringauzedb384972015-10-05 12:34:23 -0700113 // Preconditions: il.size() == rank
114 constexpr index(std::initializer_list<value_type> il) noexcept
115 {
116 fail_fast_assert(il.size() == Rank, "The size of the initializer list must match the rank of the array");
117 std::copy(begin(il), end(il), elems);
118 }
119
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700120 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700121
122 // copy from index over smaller domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700123 template <typename OtherValueType,
124 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
125 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
126 constexpr index(const index<Rank, OtherValueType>& other) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700127 {
128 std::copy(other.elems, other.elems + Rank, elems);
129 }
130
131 // copy from index over larger domain
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700132 template <typename OtherValueType,
133 bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
134 typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
135 constexpr index(const index<Rank, OtherValueType>& other, void* ptr = 0) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700136 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700137 bool ok = std::accumulate(other.elems, other.elems + Rank, true,
138 [&](bool b, OtherValueType val) { return b && (val <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value)); }
139 );
Anna Gringauzedb384972015-10-05 12:34:23 -0700140
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700141 fail_fast_assert(ok, "other value must fit in the new domain");
142 std::transform(other.elems, other.elems + rank, elems, [&](OtherValueType val) { return static_cast<value_type>(val); });
Anna Gringauzedb384972015-10-05 12:34:23 -0700143 }
144
145 constexpr index& operator=(const index& rhs) noexcept = default;
146
147 // Preconditions: component_idx < rank
148 constexpr reference operator[](size_t component_idx)
149 {
150 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
151 return elems[component_idx];
152 }
153
154 // Preconditions: component_idx < rank
155 constexpr const_reference operator[](size_t component_idx) const noexcept
156 {
157 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
158 return elems[component_idx];
159 }
160
161 constexpr bool operator==(const index& rhs) const noexcept
162 {
163 return std::equal(elems, elems + rank, rhs.elems);
164 }
165
166 constexpr bool operator!=(const index& rhs) const noexcept
167 {
168 return !(this == rhs);
169 }
170
171 constexpr index operator+() const noexcept
172 {
173 return *this;
174 }
175
176 constexpr index operator-() const noexcept
177 {
178 index ret = *this;
179 std::transform(ret, ret + rank, ret, std::negate<ValueType>{});
180 return ret;
181 }
182
183 constexpr index operator+(const index& rhs) const noexcept
184 {
185 index ret = *this;
186 ret += rhs;
187 return ret;
188 }
189
190 constexpr index operator-(const index& rhs) const noexcept
191 {
192 index ret = *this;
193 ret -= rhs;
194 return ret;
195 }
196
197 constexpr index& operator+=(const index& rhs) noexcept
198 {
199 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<ValueType>{});
200 return *this;
201 }
202
203 constexpr index& operator-=(const index& rhs) noexcept
204 {
205 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<ValueType>{});
206 return *this;
207 }
208
209 constexpr index operator*(value_type v) const noexcept
210 {
211 index ret = *this;
212 ret *= v;
213 return ret;
214 }
215
216 constexpr index operator/(value_type v) const noexcept
217 {
218 index ret = *this;
219 ret /= v;
220 return ret;
221 }
222
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700223 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700224 {
225 return rhs * v;
226 }
227
228 constexpr index& operator*=(value_type v) noexcept
229 {
230 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<ValueType>{}(x, v); });
231 return *this;
232 }
233
234 constexpr index& operator/=(value_type v) noexcept
235 {
236 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<ValueType>{}(x, v); });
237 return *this;
238 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700239
Anna Gringauzedb384972015-10-05 12:34:23 -0700240private:
241 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700242};
243
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700244template<>
245class index<1>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700246{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700247public:
Kern Handae1570262015-09-25 00:42:38 -0700248 static const size_t rank = 1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700249 using size_type = std::ptrdiff_t;
250 using value_type = std::ptrdiff_t;
251 using reference = std::add_lvalue_reference_t<std::ptrdiff_t>;
252 using const_reference = const std::ptrdiff_t&;//std::add_const_t<std::add_lvalue_reference_t<std::ptrdiff_t>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700253
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700254 constexpr index() noexcept : value(0)
255 {}
256
Neil MacIntoshd5316802015-09-30 21:54:08 -0700257 constexpr index(value_type e0) noexcept : value(e0)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700258 {}
259
260 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
261 {}
262
263 // Preconditions: il.size() == rank
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700264 constexpr index(std::initializer_list<value_type> il)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700265 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700266 fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700267 value = begin(il)[0];
268 }
269
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700270 template <size_t, typename OtherValueType>
271 friend class index;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700272
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700273 constexpr index() noexcept : value(0)
Anna Gringauzedb384972015-10-05 12:34:23 -0700274 {}
275
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700276 constexpr index(value_type e) noexcept : value(e)
Anna Gringauzedb384972015-10-05 12:34:23 -0700277 {}
278
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700279 constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
280 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700281
Anna Gringauzedb384972015-10-05 12:34:23 -0700282 constexpr index(const index &) noexcept = default;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700283
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700284 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700285 constexpr reference operator[](size_type component_idx) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700286 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700287 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700288 return value;
289 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700290
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700291 // Preconditions: component_idx < rank
Neil MacIntoshd5316802015-09-30 21:54:08 -0700292 constexpr const_reference operator[](size_type component_idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700293 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700294 fail_fast_assert(component_idx == 0, "Component index must be less than rank");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700295 return value;
296 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700297
Neil MacIntoshd5316802015-09-30 21:54:08 -0700298 constexpr bool operator==(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700299 {
300 return value == rhs.value;
301 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700302
Neil MacIntoshd5316802015-09-30 21:54:08 -0700303 constexpr bool operator!=(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700304 {
305 return !(*this == rhs);
306 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700307
308 constexpr index operator+() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700309 {
310 return *this;
311 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700312
313 constexpr index operator-() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700314 {
315 return index(-value);
316 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700317
318 constexpr index operator+(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700319 {
320 return index(value + rhs.value);
321 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700322
323 constexpr index operator-(const index& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700324 {
325 return index(value - rhs.value);
326 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700327
328 constexpr index& operator+=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700329 {
330 value += rhs.value;
331 return *this;
332 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700333
334 constexpr index& operator-=(const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700335 {
336 value -= rhs.value;
337 return *this;
338 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700339
340 constexpr index& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700341 {
342 ++value;
343 return *this;
344 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700345
346 constexpr index operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700347 {
348 index ret = *this;
349 ++(*this);
350 return ret;
351 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700352
353 constexpr index& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700354 {
355 --value;
356 return *this;
357 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700358
359 constexpr index operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700360 {
361 index ret = *this;
362 --(*this);
363 return ret;
364 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700365
366 constexpr index operator*(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700367 {
368 return index(value * v);
369 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700370
371 constexpr index operator/(value_type v) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700372 {
373 return index(value / v);
374 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700375
376 constexpr index& operator*=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700377 {
378 value *= v;
379 return *this;
380 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700381
382 constexpr index& operator/=(value_type v) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700383 {
384 value /= v;
385 return *this;
386 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700387
388 friend constexpr index operator*(value_type v, const index& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700389 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700390 return{ rhs * v };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700391 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700392
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700393private:
394 value_type value;
395};
396
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700397const std::ptrdiff_t dynamic_range = -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700398
399struct generalized_mapping_tag {};
400struct contiguous_mapping_tag : generalized_mapping_tag {};
401
402namespace details
403{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700404
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700405 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700406 struct LessThan
407 {
408 static const bool value = Left < Right;
409 };
410
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700411 template <std::ptrdiff_t... Ranges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700412 struct BoundsRanges {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700413 using size_type = std::ptrdiff_t;
414 static const size_type Depth = 0;
415 static const size_type DynamicNum = 0;
416 static const size_type CurrentRange = 1;
417 static const size_type TotalSize = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700418
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700419 BoundsRanges (const BoundsRanges&) = default;
420 BoundsRanges(const size_type* const) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700421 BoundsRanges() = default;
422
423
Kern Handae1570262015-09-25 00:42:38 -0700424 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700425 void serialize(T&) const
426 {}
427
Kern Handae1570262015-09-25 00:42:38 -0700428 template <typename T, size_t Dim>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700429 size_type linearize(const T&) const
430 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700431 return 0;
432 }
433
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700434 template <typename T, size_t Dim>
435 bool contains(const T&) const
436 {
437 return 0;
438 }
439
440 size_type totalSize() const noexcept
441 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700442 return TotalSize;
443 }
444
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700445 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700446 {
447 return true;
448 }
449 };
450
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700451 template <std::ptrdiff_t... RestRanges>
452 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
453 using Base = BoundsRanges <RestRanges... >;
454 using size_type = Base::size_type;
Kern Handae1570262015-09-25 00:42:38 -0700455 static const size_t Depth = Base::Depth + 1;
456 static const size_t DynamicNum = Base::DynamicNum + 1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700457 static const size_type CurrentRange = dynamic_range;
458 static const size_type TotalSize = dynamic_range;
459 const size_type m_bound;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700460
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700461 BoundsRanges (const BoundsRanges&) = default;
462
463 BoundsRanges(const size_type* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700464 {
465 fail_fast_assert(0 <= *arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700466 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700467
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700468 BoundsRanges() : m_bound(0) {}
469
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700470 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
471 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
472 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
473 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700474
Kern Handae1570262015-09-25 00:42:38 -0700475 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700476 void serialize(T& arr) const
477 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700478 arr[Dim] = elementNum();
479 this->Base::template serialize<T, Dim + 1>(arr);
480 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700481
Kern Handae1570262015-09-25 00:42:38 -0700482 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700483 size_type linearize(const T& arr) const
484 {
485 const size_type index = this->Base::totalSize() * arr[Dim];
486 fail_fast_assert(index < m_bound);
487 return index + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700488 }
489
Kern Handae1570262015-09-25 00:42:38 -0700490 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700491 size_type contains(const T & arr) const
492 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700493 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
494 if (last == -1)
495 return -1;
496 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700497 return cur < m_bound ? cur + last : -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700498 }
499
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700500 size_type totalSize() const noexcept
501 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700502 return m_bound;
503 }
504
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700505 size_type elementNum() const noexcept
506 {
507 return totalSize() / this->Base::totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700508 }
509
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700510 size_type elementNum(size_t dim) const noexcept
511 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700512 if (dim > 0)
513 return this->Base::elementNum(dim - 1);
514 else
515 return elementNum();
516 }
517
Neil MacIntoshd5316802015-09-30 21:54:08 -0700518 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700519 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700520 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700521 }
522 };
523
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700524 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
525 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
526 {
527 using Base = BoundsRanges <RestRanges... >;
528 using size_type = Base::size_type;
Kern Handae1570262015-09-25 00:42:38 -0700529 static const size_t Depth = Base::Depth + 1;
530 static const size_t DynamicNum = Base::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700531 static const size_type CurrentRange = CurRange;
532 static const size_type TotalSize = CurrentRange;
533 static_assert (CurRange <= PTRDIFF_MAX, "CurRange must be smaller than std::ptrdiff_t limits");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700534
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700535 BoundsRanges (const BoundsRanges&) = default;
536 BoundsRanges(const size_type* const arr) : Base(arr) { }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700537 BoundsRanges() = default;
538
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700539 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
540 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>&other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700541 {
542 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
543 }
544
Kern Handae1570262015-09-25 00:42:38 -0700545 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700546 void serialize(T& arr) const
547 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700548 arr[Dim] = elementNum();
549 this->Base::template serialize<T, Dim + 1>(arr);
550 }
551
Kern Handae1570262015-09-25 00:42:38 -0700552 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700553 size_type linearize(const T& arr) const
554 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700555 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700556 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700557 }
558
Kern Handae1570262015-09-25 00:42:38 -0700559 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700560 size_type contains(const T& arr) const
561 {
562 if (arr[Dim] >= CurrentRange)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700563 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700564 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700565 if (last == -1)
566 return -1;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700567 return this->Base::totalSize() * arr[Dim] + last;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700568 }
569
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700570 size_type totalSize() const noexcept
571 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700572 return CurrentRange * this->Base::totalSize();
573 }
574
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700575 size_type elementNum() const noexcept
576 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700577 return CurrentRange;
578 }
579
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700580 size_type elementNum(size_t dim) const noexcept
581 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700582 if (dim > 0)
583 return this->Base::elementNum(dim - 1);
584 else
585 return elementNum();
586 }
587
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700588 bool operator== (const BoundsRanges& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700589 {
590 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
591 }
592 };
593
594 template <typename SourceType, typename TargetType, size_t Rank>
595 struct BoundsRangeConvertible2;
596
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700597 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
598 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
599
600 template <size_t Rank, typename SourceType, typename TargetType>
601 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
602
603 template <typename SourceType, typename TargetType, size_t Rank>
604 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
605 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
606 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
607 {};
608
609 template <typename SourceType, typename TargetType>
610 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
611
612 template <typename SourceType, typename TargetType, size_t Rank = TargetType::Depth>
613 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
614 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
615 && (!LessThan<size_t(SourceType::CurrentRange), size_t(TargetType::CurrentRange)>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
616 {};
617 template <typename SourceType, typename TargetType>
618 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
619
620 template <typename TypeChain>
621 struct TypeListIndexer
622 {
623 const TypeChain & obj;
624 TypeListIndexer(const TypeChain & obj) :obj(obj){}
Kern Handae1570262015-09-25 00:42:38 -0700625 template<size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700626 const TypeChain & getObj(std::true_type)
627 {
628 return obj;
629 }
Kern Handae1570262015-09-25 00:42:38 -0700630 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700631 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
632 {
633 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
634 }
Kern Handae1570262015-09-25 00:42:38 -0700635 template <size_t N>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700636 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
637 {
638 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
639 }
640 };
641
642 template <typename TypeChain>
643 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
644 {
645 return TypeListIndexer<TypeChain>(obj);
646 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700647
648 template <size_t Rank, typename ValueType, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1, ValueType>>>
649 constexpr Ret shift_left(const index<Rank, ValueType>& other) noexcept
650 {
651 Ret ret;
652 for (size_t i = 0; i < Rank - 1; ++i)
653 {
654 ret[i] = other[i + 1];
655 }
656 return ret;
657 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700658}
659
660template <typename IndexType>
661class bounds_iterator;
662
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700663template <std::ptrdiff_t... Ranges>
664class static_bounds
665{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700666public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700667 static_bounds(const details::BoundsRanges<Ranges...>&) {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700668 }
669};
670
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700671template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
672class static_bounds<FirstRange, RestRanges...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700673{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700674 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700675
676 MyRanges m_ranges;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700677 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
678 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700679
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700680 template <std::ptrdiff_t... OtherRanges>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700681 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700682
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700683public:
Kern Handae1570262015-09-25 00:42:38 -0700684 static const size_t rank = MyRanges::Depth;
685 static const size_t dynamic_rank = MyRanges::DynamicNum;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700686 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700687
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700688 using size_type = std::ptrdiff_t;
689 using index_type = index<rank>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700690 using iterator = bounds_iterator<index_type>;
691 using const_iterator = bounds_iterator<index_type>;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700692 using difference_type = std::ptrdiff_t;
693 using sliced_type = static_bounds<RestRanges...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700694 using mapping_type = contiguous_mapping_tag;
695public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700696 constexpr static_bounds(const static_bounds&) = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700697
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700698 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
699 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
700 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
701 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700702
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700703 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700704 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700705 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 -0700706 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 -0700707 }
708
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700709 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700710
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700711 constexpr static_bounds& operator=(const static_bounds& otherBounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700712 {
713 new(&m_ranges) MyRanges (otherBounds.m_ranges);
714 return *this;
715 }
716
Neil MacIntoshd5316802015-09-30 21:54:08 -0700717 constexpr sliced_type slice() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700718 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700719 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700720 }
721
Neil MacIntoshd5316802015-09-30 21:54:08 -0700722 constexpr size_type stride() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700723 {
724 return rank > 1 ? slice().size() : 1;
725 }
726
Neil MacIntoshd5316802015-09-30 21:54:08 -0700727 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700728 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700729 return m_ranges.totalSize();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700730 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700731
Neil MacIntoshd5316802015-09-30 21:54:08 -0700732 constexpr size_type total_size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700733 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700734 return m_ranges.totalSize();
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700735 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700736
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700737 constexpr size_type linearize(const index_type & idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700738 {
739 return m_ranges.linearize(idx);
740 }
741
Neil MacIntoshd5316802015-09-30 21:54:08 -0700742 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700743 {
744 return m_ranges.contains(idx) != -1;
745 }
746
Neil MacIntoshd5316802015-09-30 21:54:08 -0700747 constexpr size_type operator[](size_t index) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700748 {
749 return m_ranges.elementNum(index);
750 }
751
Kern Handae1570262015-09-25 00:42:38 -0700752 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700753 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700754 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700755 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700756 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
757 }
758
Neil MacIntoshd5316802015-09-30 21:54:08 -0700759 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700760 {
Anna Gringauzefdf86432015-10-14 10:46:22 -0700761 size_type extents[rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700762 m_ranges.serialize(extents);
Anna Gringauzedb384972015-10-05 12:34:23 -0700763 return{ extents };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700764 }
765
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700766 template <std::ptrdiff_t... Ranges>
767 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700768 {
769 return this->size() == rhs.size();
770 }
771
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700772 template <std::ptrdiff_t... Ranges>
773 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700774 {
775 return !(*this == rhs);
776 }
777
Neil MacIntoshd5316802015-09-30 21:54:08 -0700778 constexpr const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700779 {
780 return const_iterator(*this);
781 }
782
Neil MacIntoshd5316802015-09-30 21:54:08 -0700783 constexpr const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700784 {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700785 return const_iterator(*this, this->index_bounds());
786 }
787};
788
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700789template <size_t Rank>
790class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700791{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700792 using Base = details::coordinate_facade<strided_bounds<Rank>, Rank>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700793 friend Base;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700794
795 template <size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700796 friend class strided_bounds;
797
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700798public:
Anna Gringauzedb384972015-10-05 12:34:23 -0700799 static const size_t rank = Rank;
Anna Gringauzefdf86432015-10-14 10:46:22 -0700800 using reference = SizeType&;
801 using const_reference = const SizeType&;
802 using size_type = SizeType;
803 using difference_type = SizeType;
804 using value_type = SizeType;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700805 using index_type = index<rank, size_type>;
806 using iterator = bounds_iterator<index_type>;
807 using const_iterator = bounds_iterator<index_type>;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700808 static const size_t dynamic_rank = rank;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700809 static const std::ptrdiff_t static_size = dynamic_range;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700810 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
811 using mapping_type = generalized_mapping_tag;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700812
Anna Gringauzedb384972015-10-05 12:34:23 -0700813 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700814
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700815 constexpr strided_bounds(const index_type& extents, const index_type& strides)
816 : m_strides(strides)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700817 {
Kern Handae1570262015-09-25 00:42:38 -0700818 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700819 Base::elems[i] = extents[i];
820 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700821
822 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
823 : Base(values), m_strides(std::move(strides))
824 {}
825
Anna Gringauzedb384972015-10-05 12:34:23 -0700826 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
827 : m_extents(extents), m_strides(strides)
828 {}
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700829
Neil MacIntoshd5316802015-09-30 21:54:08 -0700830 constexpr index_type strides() const noexcept
Anna Gringauzedb384972015-10-05 12:34:23 -0700831 {
832 return m_strides;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700833 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700834
Neil MacIntoshd5316802015-09-30 21:54:08 -0700835 constexpr size_type total_size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700836 {
837 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700838 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700839 {
840 ret += (m_extents[i] - 1) * m_strides[i];
841 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700842 return ret + 1;
843 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700844
845 constexpr size_type size() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700846 {
847 size_type ret = 1;
Kern Handae1570262015-09-25 00:42:38 -0700848 for (size_t i = 0; i < rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -0700849 {
850 ret *= m_extents[i];
851 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700852 return ret;
853 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700854
855 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700856 {
Kern Handae1570262015-09-25 00:42:38 -0700857 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700858 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700859 if (idx[i] < 0 || idx[i] >= m_extents[i])
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700860 return false;
861 }
862 return true;
863 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700864
865 constexpr size_type linearize(const index_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700866 {
867 size_type ret = 0;
Kern Handae1570262015-09-25 00:42:38 -0700868 for (size_t i = 0; i < rank; i++)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700869 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700870 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700871 ret += idx[i] * m_strides[i];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700872 }
873 return ret;
874 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700875
876 constexpr size_type stride() const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700877 {
878 return m_strides[0];
879 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700880
881 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -0700882 constexpr sliced_type slice() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700883 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700884 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700885 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700886
887 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700888 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700889 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700890 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
Anna Gringauzedb384972015-10-05 12:34:23 -0700891 return m_extents[Dim];
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700892 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700893
894 constexpr index_type index_bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700895 {
Anna Gringauzedb384972015-10-05 12:34:23 -0700896 return m_extents;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700897 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700898
899 const_iterator begin() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700900 {
901 return const_iterator{ *this };
902 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700903
904 const_iterator end() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700905 {
906 return const_iterator{ *this, index_bounds() };
907 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700908
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700909private:
Anna Gringauzedb384972015-10-05 12:34:23 -0700910 index_type m_extents;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700911 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700912};
913
914template <typename T>
915struct is_bounds : std::integral_constant<bool, false> {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700916template <std::ptrdiff_t... Ranges>
917struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> {};
918template <size_t Rank>
919struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700920
921template <typename IndexType>
922class bounds_iterator
923 : public std::iterator<std::random_access_iterator_tag,
924 IndexType,
925 ptrdiff_t,
926 const details::arrow_proxy<IndexType>,
927 const IndexType>
928{
929private:
930 using Base = std::iterator <std::random_access_iterator_tag, IndexType, ptrdiff_t, const details::arrow_proxy<IndexType>, const IndexType>;
931public:
Kern Handae1570262015-09-25 00:42:38 -0700932 static const size_t rank = IndexType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700933 using typename Base::reference;
934 using typename Base::pointer;
935 using typename Base::difference_type;
936 using typename Base::value_type;
937 using index_type = value_type;
Anna Gringauzedb384972015-10-05 12:34:23 -0700938 using index_size_type = typename IndexType::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700939 template <typename Bounds>
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700940 explicit bounds_iterator(const Bounds& bnd, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700941 : boundary(bnd.index_bounds())
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700942 , curr(std::move(curr))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700943 {
944 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
945 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700946 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700947 {
948 return curr;
949 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700950 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700951 {
952 return details::arrow_proxy<value_type>{ curr };
953 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700954 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700955 {
Kern Handae1570262015-09-25 00:42:38 -0700956 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700957 {
958 if (++curr[i] < boundary[i])
959 {
960 return *this;
961 }
962 else
963 {
964 curr[i] = 0;
965 }
966 }
967 // If we're here we've wrapped over - set to past-the-end.
Kern Handae1570262015-09-25 00:42:38 -0700968 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700969 {
970 curr[i] = boundary[i];
971 }
972 return *this;
973 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700974 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700975 {
976 auto ret = *this;
977 ++(*this);
978 return ret;
979 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700980 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700981 {
Neil MacIntoshfb913932015-09-27 16:25:43 -0700982 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700983 {
984 if (curr[i]-- > 0)
985 {
986 return *this;
987 }
988 else
989 {
990 curr[i] = boundary[i] - 1;
991 }
992 }
993 // If we're here the preconditions were violated
994 // "pre: there exists s such that r == ++s"
995 fail_fast_assert(false);
996 return *this;
997 }
Neil MacIntoshd5316802015-09-30 21:54:08 -0700998 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700999 {
1000 auto ret = *this;
1001 --(*this);
1002 return ret;
1003 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001004 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001005 {
1006 bounds_iterator ret{ *this };
1007 return ret += n;
1008 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001009 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001010 {
1011 auto linear_idx = linearize(curr) + n;
1012 value_type stride;
1013 stride[rank - 1] = 1;
Kern Handae1570262015-09-25 00:42:38 -07001014 for (size_t i = rank - 1; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001015 {
1016 stride[i] = stride[i + 1] * boundary[i + 1];
1017 }
Kern Handae1570262015-09-25 00:42:38 -07001018 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001019 {
1020 curr[i] = linear_idx / stride[i];
1021 linear_idx = linear_idx % stride[i];
1022 }
1023 return *this;
1024 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001025 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001026 {
1027 bounds_iterator ret{ *this };
1028 return ret -= n;
1029 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001030 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001031 {
1032 return *this += -n;
1033 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001034 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001035 {
1036 return linearize(curr) - linearize(rhs.curr);
1037 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001038 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001039 {
1040 return *(*this + n);
1041 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001042 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001043 {
1044 return curr == rhs.curr;
1045 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001046 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001047 {
1048 return !(*this == rhs);
1049 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001050 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001051 {
Kern Handae1570262015-09-25 00:42:38 -07001052 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001053 {
1054 if (curr[i] < rhs.curr[i])
1055 return true;
1056 }
1057 return false;
1058 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001059 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001060 {
1061 return !(rhs < *this);
1062 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001063 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001064 {
1065 return rhs < *this;
1066 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001067 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001068 {
1069 return !(rhs > *this);
1070 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001071 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001072 {
1073 std::swap(boundary, rhs.boundary);
1074 std::swap(curr, rhs.curr);
1075 }
1076private:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001077 index_size_type linearize(const value_type& idx) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001078 {
1079 // TODO: Smarter impl.
1080 // Check if past-the-end
1081 bool pte = true;
Kern Handae1570262015-09-25 00:42:38 -07001082 for (size_t i = 0; i < rank; ++i)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001083 {
1084 if (idx[i] != boundary[i])
1085 {
1086 pte = false;
1087 break;
1088 }
1089 }
1090 index_size_type multiplier = 1;
1091 index_size_type res = 0;
1092 if (pte)
1093 {
1094 res = 1;
Kern Handae1570262015-09-25 00:42:38 -07001095 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001096 {
1097 res += (idx[i] - 1) * multiplier;
1098 multiplier *= boundary[i];
1099 }
1100 }
1101 else
1102 {
Kern Handae1570262015-09-25 00:42:38 -07001103 for (size_t i = rank; i-- > 0;)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001104 {
1105 res += idx[i] * multiplier;
1106 multiplier *= boundary[i];
1107 }
1108 }
1109 return res;
1110 }
1111 value_type boundary;
1112 value_type curr;
1113};
1114
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001115template <>
1116class bounds_iterator<index<1>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001117 : public std::iterator<std::random_access_iterator_tag,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001118 index<1>,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001119 ptrdiff_t,
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001120 const details::arrow_proxy<index<1>>,
1121 const index<1>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001122{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001123 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 -07001124
1125public:
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001126 using Base::reference;
1127 using Base::pointer;
1128 using Base::difference_type;
1129 using Base::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001130 using index_type = value_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001131 using index_size_type = index_type::size_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001132
1133 template <typename Bounds>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001134 explicit bounds_iterator(const Bounds&, value_type curr = value_type{}) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001135 : curr( std::move(curr) )
1136 {}
Neil MacIntoshd5316802015-09-30 21:54:08 -07001137 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001138 {
1139 return curr;
1140 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001141 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001142 {
1143 return details::arrow_proxy<value_type>{ curr };
1144 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001145 bounds_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001146 {
1147 ++curr;
1148 return *this;
1149 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001150 bounds_iterator operator++(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001151 {
1152 auto ret = *this;
1153 ++(*this);
1154 return ret;
1155 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001156 bounds_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001157 {
1158 curr--;
1159 return *this;
1160 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001161 bounds_iterator operator--(int) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001162 {
1163 auto ret = *this;
1164 --(*this);
1165 return ret;
1166 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001167 bounds_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001168 {
1169 bounds_iterator ret{ *this };
1170 return ret += n;
1171 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001172 bounds_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001173 {
1174 curr += n;
1175 return *this;
1176 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001177 bounds_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001178 {
1179 bounds_iterator ret{ *this };
1180 return ret -= n;
1181 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001182 bounds_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001183 {
1184 return *this += -n;
1185 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001186 difference_type operator-(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001187 {
1188 return curr[0] - rhs.curr[0];
1189 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001190 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001191 {
1192 return curr + n;
1193 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001194 bool operator==(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001195 {
1196 return curr == rhs.curr;
1197 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001198 bool operator!=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001199 {
1200 return !(*this == rhs);
1201 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001202 bool operator<(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001203 {
1204 return curr[0] < rhs.curr[0];
1205 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001206 bool operator<=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001207 {
1208 return !(rhs < *this);
1209 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001210 bool operator>(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001211 {
1212 return rhs < *this;
1213 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001214 bool operator>=(const bounds_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001215 {
1216 return !(rhs > *this);
1217 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001218 void swap(bounds_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001219 {
1220 std::swap(curr, rhs.curr);
1221 }
1222private:
1223 value_type curr;
1224};
1225
1226template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001227bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001228{
1229 return rhs + n;
1230}
1231
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001232//
1233// begin definitions of basic_array_view
1234//
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001235namespace details
1236{
1237 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001238 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 -07001239 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001240 return bnd.strides();
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001241 }
1242
Neil MacIntosh99746e22015-09-27 16:53:58 -07001243 // Make a stride vector from bounds, assuming contiguous memory.
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001244 template <typename Bounds>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001245 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 -07001246 {
1247 auto extents = bnd.index_bounds();
Anna Gringauzefdf86432015-10-14 10:46:22 -07001248 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001249
1250 stride[Bounds::rank - 1] = 1;
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001251 for (size_t i = 1; i < Bounds::rank; ++i)
Anna Gringauzedb384972015-10-05 12:34:23 -07001252 {
Anna Gringauze546f8cc2015-10-05 21:04:56 -07001253 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
Anna Gringauzedb384972015-10-05 12:34:23 -07001254 }
1255 return{ stride };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001256 }
1257
1258 template <typename BoundsSrc, typename BoundsDest>
1259 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1260 {
1261 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1262 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1263 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");
1264 fail_fast_assert(src.size() == dest.size());
1265 }
1266
1267
1268} // namespace details
1269
1270template <typename ArrayView>
1271class contiguous_array_view_iterator;
1272template <typename ArrayView>
1273class general_array_view_iterator;
1274enum class byte : std::uint8_t {};
1275
1276template <typename ValueType, typename BoundsType>
1277class basic_array_view
1278{
1279public:
Kern Handae1570262015-09-25 00:42:38 -07001280 static const size_t rank = BoundsType::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001281 using bounds_type = BoundsType;
1282 using size_type = typename bounds_type::size_type;
1283 using index_type = typename bounds_type::index_type;
1284 using value_type = ValueType;
1285 using pointer = ValueType*;
1286 using reference = ValueType&;
1287 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>>;
1288 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>>>;
1289 using reverse_iterator = std::reverse_iterator<iterator>;
1290 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1291 using sliced_type = std::conditional_t<rank == 1, value_type, basic_array_view<value_type, typename BoundsType::sliced_type>>;
1292
1293private:
1294 pointer m_pdata;
1295 bounds_type m_bounds;
1296
1297public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001298 constexpr bounds_type bounds() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001299 {
1300 return m_bounds;
1301 }
Kern Handae1570262015-09-25 00:42:38 -07001302 template <size_t Dim = 0>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001303 constexpr size_type extent() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001304 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001305 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001306 return m_bounds.template extent<Dim>();
1307 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001308 constexpr size_type size() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001309 {
1310 return m_bounds.size();
1311 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001312 constexpr reference operator[](const index_type& idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001313 {
1314 return m_pdata[m_bounds.linearize(idx)];
1315 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001316 constexpr pointer data() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001317 {
1318 return m_pdata;
1319 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001320 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001321 constexpr Ret operator[](size_type idx) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001322 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001323 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001324 const size_type ridx = idx * m_bounds.stride();
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001325
1326 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001327 return Ret {m_pdata + ridx, m_bounds.slice()};
1328 }
1329
Neil MacIntoshd5316802015-09-30 21:54:08 -07001330 constexpr operator bool () const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001331 {
1332 return m_pdata != nullptr;
1333 }
1334
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001335 constexpr iterator begin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001336 {
1337 return iterator {this, true};
1338 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001339 constexpr iterator end() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001340 {
1341 return iterator {this};
1342 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001343 constexpr const_iterator cbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001344 {
1345 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true};
1346 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001347 constexpr const_iterator cend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001348 {
1349 return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this)};
1350 }
1351
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001352 constexpr reverse_iterator rbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001353 {
1354 return reverse_iterator {end()};
1355 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001356 constexpr reverse_iterator rend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001357 {
1358 return reverse_iterator {begin()};
1359 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001360 constexpr const_reverse_iterator crbegin() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001361 {
1362 return const_reverse_iterator {cend()};
1363 }
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001364 constexpr const_reverse_iterator crend() const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001365 {
1366 return const_reverse_iterator {cbegin()};
1367 }
1368
1369 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 -07001370 constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001371 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001372 return m_bounds.size() == other.m_bounds.size() &&
1373 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001374 }
1375
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001376 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 -07001377 constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001378 {
1379 return !(*this == other);
1380 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001381
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001382 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 -07001383 constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001384 {
1385 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1386 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001387
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001388 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 -07001389 constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001390 {
1391 return !(other < *this);
1392 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001393
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001394 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 -07001395 constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001396 {
1397 return (other < *this);
1398 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001399
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001400 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 -07001401 constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001402 {
1403 return !(*this < other);
1404 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001405
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001406public:
1407 template <typename OtherValueType, typename OtherBounds,
1408 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
1409 && std::is_convertible<OtherBounds, bounds_type>::value>>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001410 constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001411 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
1412 {
1413 }
1414protected:
1415
Neil MacIntoshd5316802015-09-30 21:54:08 -07001416 constexpr basic_array_view(pointer data, bounds_type bound) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001417 : m_pdata(data)
1418 , m_bounds(std::move(bound))
1419 {
1420 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1421 }
1422 template <typename T>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001423 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 -07001424 : m_pdata(reinterpret_cast<pointer>(data))
1425 , m_bounds(std::move(bound))
1426 {
1427 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1428 }
1429 template <typename DestBounds>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001430 constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001431 {
1432 details::verifyBoundsReshape(m_bounds, bounds);
1433 return {m_pdata, bounds};
1434 }
1435private:
1436
1437 friend iterator;
1438 friend const_iterator;
1439 template <typename ValueType2, typename BoundsType2>
1440 friend class basic_array_view;
1441};
1442
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001443template <std::ptrdiff_t DimSize = dynamic_range>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001444struct dim
1445{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001446 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001447};
1448template <>
1449struct dim<dynamic_range>
1450{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001451 static const std::ptrdiff_t value = dynamic_range;
1452 const std::ptrdiff_t dvalue;
1453 dim(std::ptrdiff_t size) : dvalue(size) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001454};
1455
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001456template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001457class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001458
1459template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001460class strided_array_view;
1461
1462namespace details
1463{
1464 template <typename T, typename = std::true_type>
1465 struct ArrayViewTypeTraits
1466 {
1467 using value_type = T;
1468 using size_type = size_t;
1469 };
1470
1471 template <typename Traits>
1472 struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::array_view_traits &>::type>
1473 {
1474 using value_type = typename Traits::array_view_traits::value_type;
1475 using size_type = typename Traits::array_view_traits::size_type;
1476 };
1477
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001478 template <typename T, size_t... Ranks>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001479 struct ArrayViewArrayTraits {
1480 using type = array_view<T, Ranks...>;
1481 using value_type = T;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001482 using bounds_type = static_bounds<Ranks...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001483 using pointer = T*;
1484 using reference = T&;
1485 };
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001486 template <typename T, size_t N, size_t... Ranks>
1487 struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001488
1489 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001490 BoundsType newBoundsHelperImpl(std::ptrdiff_t , std::true_type) // dynamic size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001491 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001492 fail_fast_assert(totalSize <= PTRDIFF_MAX);
1493 return BoundsType{totalSize};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001494 }
1495 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001496 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001497 {
1498 fail_fast_assert(BoundsType::static_size == totalSize);
1499 return {};
1500 }
1501 template <typename BoundsType>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001502 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001503 {
1504 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1505 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1506 }
1507
1508 struct Sep{};
1509
1510 template <typename T, typename... Args>
1511 T static_as_array_view_helper(Sep, Args... args)
1512 {
1513 return T{static_cast<typename T::size_type>(args)...};
1514 }
1515 template <typename T, typename Arg, typename... Args>
1516 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)
1517 {
1518 return static_as_array_view_helper<T>(args...);
1519 }
1520 template <typename T, typename... Args>
1521 T static_as_array_view_helper(dim<dynamic_range> val, Args ... args)
1522 {
1523 return static_as_array_view_helper<T>(args..., val.dvalue);
1524 }
1525
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001526 template <typename ...Dimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001527 struct static_as_array_view_static_bounds_helper
1528 {
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001529 using type = static_bounds<(Dimensions::value)...>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001530 };
1531
1532 template <typename T>
1533 struct is_array_view_oracle : std::false_type
1534 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001535
1536 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001537 struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type
1538 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001539
1540 template <typename ValueType, size_t Rank>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001541 struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type
1542 {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001543
1544 template <typename T>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001545 struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>>
1546 {};
1547
1548}
1549
1550
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001551template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1552class array_view : public basic_array_view <ValueType, static_bounds <FirstDimension, RestDimensions...>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001553{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001554 template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001555 friend class array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001556
1557 using Base = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001558
1559public:
1560 using typename Base::bounds_type;
1561 using typename Base::size_type;
1562 using typename Base::pointer;
1563 using typename Base::value_type;
1564 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001565 using typename Base::iterator;
1566 using typename Base::const_iterator;
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001567 using typename Base::reference;
Neil MacIntosh383dc502015-09-14 15:41:40 -07001568 using Base::rank;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001569
1570public:
1571 // basic
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001572 constexpr array_view(pointer ptr, size_type size) : Base(ptr, bounds_type{ size })
1573 {}
1574
1575 constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds))
1576 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001577
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001578 constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{})
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001579 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001580
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001581 constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001582 {
1583 fail_fast_assert(size == 0);
1584 }
1585
1586 // default
1587 template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001588 constexpr array_view() : Base(nullptr, bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001589 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001590
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001591 // 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 -07001592 template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
1593 typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>>
1594 constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size})
1595 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001596
1597 // from n-dimensions static array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001598 template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
1599 typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>
1600 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001601 constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001602 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001603
1604 // from n-dimensions static array with size
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001605 template <typename T, size_t N,
1606 typename Dummy = std::enable_if_t<std::is_convertible<T(*)[], typename Base::value_type(*)[]>::value>
1607 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001608 constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001609 {
1610 fail_fast_assert(size <= N);
1611 }
1612
1613 // from std array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001614 template <size_t N,
1615 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value>
1616 >
1617 constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1618 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001619
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001620 template <size_t N,
1621 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value
1622 && std::is_const<value_type>::value>
1623 >
1624 constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>())
1625 {}
1626
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001627 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1628 template <typename Ptr,
1629 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001630 && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value>
1631 > // remove literal 0 case
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001632 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 -07001633 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001634
1635 // from containers. It must has .size() and .data() two function signatures
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001636 template <typename Cont, typename DataType = typename Cont::value_type,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001637 typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
Anna Gringauze18cd9802015-09-14 16:34:26 -07001638 && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001639 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001640 >
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001641 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 -07001642 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001643
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001644 constexpr array_view(const array_view &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001645
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001646 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001647 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
1648 typename BaseType = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>,
1649 typename OtherBaseType = basic_array_view<OtherValueType, static_bounds<OtherDimensions...>>,
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001650 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1651 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001652 constexpr array_view(const array_view<OtherValueType, OtherDimensions...> &av)
1653 : Base(static_cast<const typename array_view<OtherValueType, OtherDimensions...>::Base&>(av))
1654 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001655
1656 // reshape
1657 template <typename... Dimensions2>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001658 constexpr array_view<ValueType, Dimensions2::value...> as_array_view(Dimensions2... dims)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001659 {
1660 static_assert(sizeof...(Dimensions2) > 0, "the target array_view must have at least one dimension.");
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001661 using BoundsType = typename array_view<ValueType, (Dimensions2::value)...>::bounds_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001662 auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
1663 details::verifyBoundsReshape(this->bounds(), tobounds);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001664 return {this->data(), tobounds};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001665 }
1666
1667 // to bytes array
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001668 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1669 constexpr auto as_bytes() const noexcept -> array_view<const byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001670 {
1671 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001672 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001673 }
1674
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001675 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
1676 constexpr auto as_writeable_bytes() const noexcept -> array_view<byte>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001677 {
1678 static_assert(Enabled, "The value_type of array_view must be standarded layout");
Anna Gringauze18cd9802015-09-14 16:34:26 -07001679 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001680 }
1681
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001682 // from bytes array
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001683 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001684 constexpr auto as_array_view() const noexcept -> array_view<const U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001685 {
1686 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1687 "Target type must be standard layout and its size must match the byte array size");
1688 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001689 return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001690 }
1691
1692 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001693 constexpr auto as_array_view() const noexcept -> array_view<U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : dynamic_range)>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001694 {
1695 static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
1696 "Target type must be standard layout and its size must match the byte array size");
1697 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
Anna Gringauze18cd9802015-09-14 16:34:26 -07001698 return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001699 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001700
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001701 // section on linear space
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001702 template<std::ptrdiff_t Count>
1703 constexpr array_view<ValueType, Count> first() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001704 {
1705 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1706 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 -07001707 return { this->data(), Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001708 }
1709
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001710 constexpr array_view<ValueType, dynamic_range> first(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001711 {
1712 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001713 return { this->data(), count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001714 }
1715
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001716 template<std::ptrdiff_t Count>
1717 constexpr array_view<ValueType, Count> last() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001718 {
1719 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1720 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001721 return { this->data() + this->size() - Count, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001722 }
1723
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001724 constexpr array_view<ValueType, dynamic_range> last(size_type count) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001725 {
1726 fail_fast_assert(count <= this->size());
Anna Gringauze18cd9802015-09-14 16:34:26 -07001727 return { this->data() + this->size() - count, count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001728 }
1729
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001730 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
1731 constexpr array_view<ValueType, Count> sub() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001732 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001733 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");
1734 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 -07001735 return { this->data() + Offset, Count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001736 }
1737
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001738 constexpr array_view<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001739 {
Neil MacIntosh05e6b6d2015-09-20 19:18:12 -07001740 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1741 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001742 }
1743
1744 // size
Neil MacIntoshd5316802015-09-30 21:54:08 -07001745 constexpr size_type length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001746 {
1747 return this->size();
1748 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001749
Neil MacIntoshd5316802015-09-30 21:54:08 -07001750 constexpr size_type used_length() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001751 {
1752 return length();
1753 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001754
Neil MacIntoshd5316802015-09-30 21:54:08 -07001755 constexpr size_type bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001756 {
1757 return sizeof(value_type) * this->size();
1758 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001759
Neil MacIntoshd5316802015-09-30 21:54:08 -07001760 constexpr size_type used_bytes() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001761 {
1762 return bytes();
1763 }
1764
1765 // section
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001766 constexpr strided_array_view<ValueType, rank> section(index_type origin, index_type extents) const
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001767 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001768 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001769 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} };
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001770 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001771
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001772 constexpr reference operator[](const index_type& idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001773 {
1774 return Base::operator[](idx);
1775 }
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001776
Anna Gringauze1a864982015-09-14 18:55:06 -07001777 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001778 constexpr array_view<ValueType, RestDimensions...> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001779 {
1780 auto ret = Base::operator[](idx);
1781 return{ ret.data(), ret.bounds() };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001782 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001783
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001784 using Base::operator==;
1785 using Base::operator!=;
1786 using Base::operator<;
1787 using Base::operator<=;
1788 using Base::operator>;
1789 using Base::operator>=;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001790};
1791
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001792template <typename T, std::ptrdiff_t... Dimensions>
1793constexpr 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 -07001794{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001795 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 -07001796}
1797
1798template <typename T>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001799constexpr auto as_array_view (T* arr, std::ptrdiff_t len) -> typename details::ArrayViewArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001800{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001801 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802}
1803
1804template <typename T, size_t N>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001805constexpr auto as_array_view (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001806{
1807 return {arr};
1808}
1809
1810template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001811constexpr array_view<const T, N> as_array_view(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001812{
1813 return {arr};
1814}
1815
1816template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001817constexpr array_view<const T, N> as_array_view(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001818
1819template <typename T, size_t N>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001820constexpr array_view<T, N> as_array_view(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001821{
1822 return {arr};
1823}
1824
1825template <typename T>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001826constexpr array_view<T, dynamic_range> as_array_view(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001827{
1828 return {begin, end};
1829}
1830
1831template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001832constexpr 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 -07001833 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
1834{
1835 return {arr.data(), arr.size()};
1836}
1837
1838template <typename Cont>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001839constexpr 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 -07001840 array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
1841
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001842template <typename ValueType, size_t Rank>
1843class strided_array_view : public basic_array_view<ValueType, strided_bounds<Rank>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001844{
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001845 using Base = basic_array_view<ValueType, strided_bounds<Rank>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001846
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001847 template<typename OtherValue, size_t OtherRank>
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001848 friend class strided_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001849
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001850public:
1851 using Base::rank;
1852 using typename Base::bounds_type;
1853 using typename Base::size_type;
1854 using typename Base::pointer;
1855 using typename Base::value_type;
1856 using typename Base::index_type;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001857 using typename Base::iterator;
1858 using typename Base::const_iterator;
Anna Gringauze9dac1782015-09-14 19:08:03 -07001859 using typename Base::reference;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001860
1861 // from static array of size N
1862 template<size_type N>
1863 strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
1864 {
1865 fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
1866 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001867
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001868 // from raw data
1869 strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001870 {
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001871 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001872 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001873
1874 // from array view
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001875 template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
1876 strided_array_view(array_view<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001877 {
1878 fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
1879 }
1880
1881 // convertible
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001882 template <typename OtherValueType,
1883 typename BaseType = basic_array_view<ValueType, strided_bounds<Rank>>,
1884 typename OtherBaseType = basic_array_view<OtherValueType, strided_bounds<Rank>>,
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001885 typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
1886 >
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001887 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
1888 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001889
1890 // convert from bytes
Anna Gringauze1a864982015-09-14 18:55:06 -07001891 template <typename OtherValueType>
1892 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 -07001893 {
1894 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1895 auto d = sizeof(OtherValueType) / sizeof(value_type);
1896
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001897 size_type size = this->bounds().total_size() / d;
1898 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 -07001899 }
1900
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001901 strided_array_view section(index_type origin, index_type extents) const
1902 {
Neil MacIntoshef6cc652015-09-14 21:26:17 +00001903 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001904 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
1905 }
1906
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001907 constexpr reference operator[](const index_type& idx) const
Anna Gringauze9dac1782015-09-14 19:08:03 -07001908 {
1909 return Base::operator[](idx);
1910 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001911
1912 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07001913 constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001914 {
1915 auto ret = Base::operator[](idx);
1916 return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
1917 }
1918
1919private:
1920 static index_type resize_extent(const index_type& extent, size_t d)
1921 {
1922 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");
1923
1924 index_type ret = extent;
1925 ret[rank - 1] /= d;
1926
1927 return ret;
1928 }
1929
1930 template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Kosov Eugene3402b922015-09-28 21:20:02 +03001931 static index_type resize_stride(const index_type& strides, size_t , void * = 0)
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001932 {
1933 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1934
1935 return strides;
1936 }
1937
1938 template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
1939 static index_type resize_stride(const index_type& strides, size_t d)
1940 {
1941 fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1942 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");
1943
Neil MacIntosh99746e22015-09-27 16:53:58 -07001944 for (size_t i = rank - 1; i > 0; --i)
1945 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 -07001946
1947 index_type ret = strides / d;
1948 ret[rank - 1] = 1;
1949
1950 return ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001951 }
1952};
1953
1954template <typename ArrayView>
1955class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
1956{
1957 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
1958public:
1959 using typename Base::reference;
1960 using typename Base::pointer;
1961 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001962
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001963private:
1964 template <typename ValueType, typename Bounds>
1965 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001966
1967 pointer m_pdata;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001968 const ArrayView * m_validator;
1969 void validateThis() const
1970 {
Neil MacIntosh383dc502015-09-14 15:41:40 -07001971 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 -07001972 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001973
1974 contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001975 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) { }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001976
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001977public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07001978 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001979 {
1980 validateThis();
1981 return *m_pdata;
1982 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001983 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001984 {
1985 validateThis();
1986 return m_pdata;
1987 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001988 contiguous_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001989 {
1990 ++m_pdata;
1991 return *this;
1992 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001993 contiguous_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001994 {
1995 auto ret = *this;
1996 ++(*this);
1997 return ret;
1998 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07001999 contiguous_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002000 {
2001 --m_pdata;
2002 return *this;
2003 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002004 contiguous_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002005 {
2006 auto ret = *this;
2007 --(*this);
2008 return ret;
2009 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002010 contiguous_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002011 {
2012 contiguous_array_view_iterator ret{ *this };
2013 return ret += n;
2014 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002015 contiguous_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002016 {
2017 m_pdata += n;
2018 return *this;
2019 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002020 contiguous_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002021 {
2022 contiguous_array_view_iterator ret{ *this };
2023 return ret -= n;
2024 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002025 contiguous_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002026 {
2027 return *this += -n;
2028 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002029 difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002030 {
2031 fail_fast_assert(m_validator == rhs.m_validator);
2032 return m_pdata - rhs.m_pdata;
2033 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002034 reference operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002035 {
2036 return *(*this + n);
2037 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002038 bool operator==(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002039 {
2040 fail_fast_assert(m_validator == rhs.m_validator);
2041 return m_pdata == rhs.m_pdata;
2042 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002043 bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002044 {
2045 return !(*this == rhs);
2046 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002047 bool operator<(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002048 {
2049 fail_fast_assert(m_validator == rhs.m_validator);
2050 return m_pdata < rhs.m_pdata;
2051 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002052 bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002053 {
2054 return !(rhs < *this);
2055 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002056 bool operator>(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002057 {
2058 return rhs < *this;
2059 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002060 bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002061 {
2062 return !(rhs > *this);
2063 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002064 void swap(contiguous_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002065 {
2066 std::swap(m_pdata, rhs.m_pdata);
2067 std::swap(m_validator, rhs.m_validator);
2068 }
2069};
2070
2071template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002072contiguous_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 -07002073{
2074 return rhs + n;
2075}
2076
2077template <typename ArrayView>
2078class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>
2079{
2080 using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>;
2081public:
2082 using typename Base::reference;
2083 using typename Base::pointer;
2084 using typename Base::difference_type;
2085 using typename Base::value_type;
2086private:
2087 template <typename ValueType, typename Bounds>
2088 friend class basic_array_view;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07002089
2090 const ArrayView * m_container;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07002091 typename ArrayView::bounds_type::iterator m_itr;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07002092
2093 general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
2094 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
2095 {}
2096
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002097public:
Neil MacIntoshd5316802015-09-30 21:54:08 -07002098 reference operator*() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002099 {
2100 return (*m_container)[*m_itr];
2101 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002102 pointer operator->() const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002103 {
2104 return &(*m_container)[*m_itr];
2105 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002106 general_array_view_iterator& operator++() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002107 {
2108 ++m_itr;
2109 return *this;
2110 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002111 general_array_view_iterator operator++(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002112 {
2113 auto ret = *this;
2114 ++(*this);
2115 return ret;
2116 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002117 general_array_view_iterator& operator--() noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002118 {
2119 --m_itr;
2120 return *this;
2121 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002122 general_array_view_iterator operator--(int)noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002123 {
2124 auto ret = *this;
2125 --(*this);
2126 return ret;
2127 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002128 general_array_view_iterator operator+(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002129 {
2130 general_array_view_iterator ret{ *this };
2131 return ret += n;
2132 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002133 general_array_view_iterator& operator+=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002134 {
2135 m_itr += n;
2136 return *this;
2137 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002138 general_array_view_iterator operator-(difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002139 {
2140 general_array_view_iterator ret{ *this };
2141 return ret -= n;
2142 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002143 general_array_view_iterator& operator-=(difference_type n) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002144 {
2145 return *this += -n;
2146 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002147 difference_type operator-(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002148 {
2149 fail_fast_assert(m_container == rhs.m_container);
2150 return m_itr - rhs.m_itr;
2151 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002152 value_type operator[](difference_type n) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002153 {
2154 return (*m_container)[m_itr[n]];;
2155 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002156 bool operator==(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002157 {
2158 fail_fast_assert(m_container == rhs.m_container);
2159 return m_itr == rhs.m_itr;
2160 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002161 bool operator !=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002162 {
2163 return !(*this == rhs);
2164 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002165 bool operator<(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002166 {
2167 fail_fast_assert(m_container == rhs.m_container);
2168 return m_itr < rhs.m_itr;
2169 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002170 bool operator<=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002171 {
2172 return !(rhs < *this);
2173 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002174 bool operator>(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002175 {
2176 return rhs < *this;
2177 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002178 bool operator>=(const general_array_view_iterator& rhs) const noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002179 {
2180 return !(rhs > *this);
2181 }
Neil MacIntoshd5316802015-09-30 21:54:08 -07002182 void swap(general_array_view_iterator& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002183 {
2184 std::swap(m_itr, rhs.m_itr);
2185 std::swap(m_container, rhs.m_container);
2186 }
2187};
2188
2189template <typename ArrayView>
Neil MacIntoshd5316802015-09-30 21:54:08 -07002190general_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 -07002191{
2192 return rhs + n;
2193}
2194
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002195} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002196
Neil MacIntoshd5316802015-09-30 21:54:08 -07002197#ifdef _MSC_VER
2198
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002199#undef constexpr
2200#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002201
Neil MacIntoshd5316802015-09-30 21:54:08 -07002202#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002203#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002204
2205#ifndef GSL_THROWS_FOR_TESTING
2206#pragma undef noexcept
2207#endif // GSL_THROWS_FOR_TESTING
2208
Neil MacIntosh9a297122015-09-14 15:11:07 -07002209#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002210
Neil MacIntoshd5316802015-09-30 21:54:08 -07002211#endif // _MSC_VER
2212
2213#if defined(GSL_THROWS_FOR_TESTING)
2214#undef noexcept
2215#endif // GSL_THROWS_FOR_TESTING
2216
Treb Connell51da1362015-09-24 18:08:34 -07002217
2218#endif // GSL_ARRAY_VIEW_H