blob: be7e74fd615980c960557c874b3e540252d4fbb9 [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
Neil MacIntoshb63ec942015-11-04 12:42:27 -080019#ifndef GSL_SPAN_H
20#define GSL_SPAN_H
Treb Connell51da1362015-09-24 18:08:34 -070021
Olaf van der Spek550361c2015-11-12 10:23:56 +010022#include <algorithm>
23#include <array>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070024#include <cstddef>
25#include <cstdint>
Olaf van der Spek550361c2015-11-12 10:23:56 +010026#include <functional>
27#include <iterator>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070028#include <limits>
Olaf van der Spek550361c2015-11-12 10:23:56 +010029#include <new>
Anna Gringauze2cdedda2015-10-15 13:19:24 -070030#include <numeric>
Olaf van der Spek550361c2015-11-12 10:23:56 +010031#include <stdexcept>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070032#include <type_traits>
33#include <utility>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070034#include "fail_fast.h"
35
Neil MacIntoshd5316802015-09-30 21:54:08 -070036#ifdef _MSC_VER
37
38// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070039#pragma push_macro("constexpr")
40#define constexpr /* nothing */
Neil MacIntoshd5316802015-09-30 21:54:08 -070041
42
43// VS 2013 workarounds
44#if _MSC_VER <= 1800
45
Neil MacIntoshe9a96022015-11-03 18:56:55 -080046#pragma push_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG")
47#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
48
49
Neil MacIntoshd5316802015-09-30 21:54:08 -070050// noexcept is not understood
51#ifndef GSL_THROWS_FOR_TESTING
52#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070053#endif
54
Neil MacIntoshd5316802015-09-30 21:54:08 -070055// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070056#pragma warning(push)
57#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntosha998a9b2015-11-12 18:57:23 -080058#pragma warning(disable: 4512) // warns that assignment op could not be generated
Neil MacIntoshd5316802015-09-30 21:54:08 -070059
Neil MacIntosh9a297122015-09-14 15:11:07 -070060#endif // _MSC_VER <= 1800
61
Neil MacIntoshd5316802015-09-30 21:54:08 -070062#endif // _MSC_VER
63
64// In order to test the library, we need it to throw exceptions that we can catch
65#ifdef GSL_THROWS_FOR_TESTING
66#define noexcept /* nothing */
67#endif // GSL_THROWS_FOR_TESTING
68
69
Neil MacIntoshef626fd2015-09-29 16:41:37 -070070namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070071
72/*
73** begin definitions of index and bounds
74*/
75namespace details
76{
Neil MacIntosh68064d62015-11-03 19:17:11 -080077 template <typename SizeType>
78 struct SizeTypeTraits
79 {
80 static const SizeType max_value = std::numeric_limits<SizeType>::max();
81 };
Anna Gringauze1c208b32015-10-16 17:40:57 -070082
83
Neil MacIntosh68064d62015-11-03 19:17:11 -080084 template<typename... Ts>
85 class are_integral : public std::integral_constant<bool, true> {};
Anna Gringauze1c208b32015-10-16 17:40:57 -070086
Neil MacIntosh68064d62015-11-03 19:17:11 -080087 template<typename T, typename... Ts>
88 class are_integral<T, Ts...> : public std::integral_constant<bool, std::is_integral<T>::value && are_integral<Ts...>::value> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070089}
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{
Neil MacIntosh68064d62015-11-03 19:17:11 -080094 static_assert(Rank > 0, "Rank must be greater than 0!");
Anna Gringauzedb384972015-10-05 12:34:23 -070095
Neil MacIntosh68064d62015-11-03 19:17:11 -080096 template <size_t OtherRank>
97 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -070098
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070099public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800100 static const size_t rank = Rank;
101 using value_type = std::ptrdiff_t;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700102 using size_type = value_type;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800103 using reference = std::add_lvalue_reference_t<value_type>;
104 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700105
Neil MacIntosh68064d62015-11-03 19:17:11 -0800106 constexpr index() noexcept
107 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700108
Neil MacIntosh68064d62015-11-03 19:17:11 -0800109 constexpr index(const value_type(&values)[Rank]) noexcept
110 {
111 std::copy(values, values + Rank, elems);
112 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700113
Anna Gringauze8aa42482015-11-11 12:41:11 -0800114#ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
115 template<typename T, typename... Ts,
116 typename = std::enable_if_t<((sizeof...(Ts) + 1) == Rank) &&
117 std::is_integral<T>::value &&
118 details::are_integral<Ts...>::value>>
119 constexpr index(T t, Ts... ds) : index({ static_cast<value_type>(t), static_cast<value_type>(ds)... })
120 {}
121#else
122 template<typename... Ts,
123 typename = std::enable_if_t<(sizeof...(Ts) == Rank) && details::are_integral<Ts...>::value>>
124 constexpr index(Ts... ds) noexcept : elems{ static_cast<value_type>(ds)... }
125 {}
126#endif
Anna Gringauzedb384972015-10-05 12:34:23 -0700127
Neil MacIntosh68064d62015-11-03 19:17:11 -0800128 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700129
Neil MacIntosh68064d62015-11-03 19:17:11 -0800130 constexpr index& operator=(const index& rhs) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700131
Neil MacIntosh68064d62015-11-03 19:17:11 -0800132 // Preconditions: component_idx < rank
133 constexpr reference operator[](size_t component_idx)
134 {
135 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
136 return elems[component_idx];
137 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700138
Neil MacIntosh68064d62015-11-03 19:17:11 -0800139 // Preconditions: component_idx < rank
140 constexpr const_reference operator[](size_t component_idx) const noexcept
141 {
142 fail_fast_assert(component_idx < Rank, "Component index must be less than rank");
143 return elems[component_idx];
144 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700145
Neil MacIntosh68064d62015-11-03 19:17:11 -0800146 constexpr bool operator==(const index& rhs) const noexcept
147 {
148 return std::equal(elems, elems + rank, rhs.elems);
149 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700150
Neil MacIntosh68064d62015-11-03 19:17:11 -0800151 constexpr bool operator!=(const index& rhs) const noexcept
152 {
153 return !(this == rhs);
154 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700155
Neil MacIntosh68064d62015-11-03 19:17:11 -0800156 constexpr index operator+() const noexcept
157 {
158 return *this;
159 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700160
Neil MacIntosh68064d62015-11-03 19:17:11 -0800161 constexpr index operator-() const noexcept
162 {
163 index ret = *this;
164 std::transform(ret, ret + rank, ret, std::negate<value_type>{});
165 return ret;
166 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700167
Neil MacIntosh68064d62015-11-03 19:17:11 -0800168 constexpr index operator+(const index& rhs) const noexcept
169 {
170 index ret = *this;
171 ret += rhs;
172 return ret;
173 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700174
Neil MacIntosh68064d62015-11-03 19:17:11 -0800175 constexpr index operator-(const index& rhs) const noexcept
176 {
177 index ret = *this;
178 ret -= rhs;
179 return ret;
180 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700181
Neil MacIntosh68064d62015-11-03 19:17:11 -0800182 constexpr index& operator+=(const index& rhs) noexcept
183 {
184 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
185 return *this;
186 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700187
Neil MacIntosh68064d62015-11-03 19:17:11 -0800188 constexpr index& operator-=(const index& rhs) noexcept
189 {
190 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
191 return *this;
192 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700193
Neil MacIntosh68064d62015-11-03 19:17:11 -0800194 constexpr index operator*(value_type v) const noexcept
195 {
196 index ret = *this;
197 ret *= v;
198 return ret;
199 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700200
Neil MacIntosh68064d62015-11-03 19:17:11 -0800201 constexpr index operator/(value_type v) const noexcept
202 {
203 index ret = *this;
204 ret /= v;
205 return ret;
206 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700207
Neil MacIntosh68064d62015-11-03 19:17:11 -0800208 friend constexpr index operator*(value_type v, const index& rhs) noexcept
209 {
210 return rhs * v;
211 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700212
Neil MacIntosh68064d62015-11-03 19:17:11 -0800213 constexpr index& operator*=(value_type v) noexcept
214 {
215 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
216 return *this;
217 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700218
Neil MacIntosh68064d62015-11-03 19:17:11 -0800219 constexpr index& operator/=(value_type v) noexcept
220 {
221 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
222 return *this;
223 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700224
Anna Gringauzedb384972015-10-05 12:34:23 -0700225private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800226 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700227};
228
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700229#ifndef _MSC_VER
230
231struct static_bounds_dynamic_range_t
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700232{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800233 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
234 constexpr operator T() const noexcept
235 {
236 return static_cast<T>(-1);
237 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700238
Neil MacIntosh68064d62015-11-03 19:17:11 -0800239 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
240 constexpr bool operator ==(T other) const noexcept
241 {
242 return static_cast<T>(-1) == other;
243 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700244
Neil MacIntosh68064d62015-11-03 19:17:11 -0800245 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
246 constexpr bool operator !=(T other) const noexcept
247 {
248 return static_cast<T>(-1) != other;
249 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700250
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700251};
252
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700253template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
254constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
255{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800256 return right == left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700257}
258
259template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
260constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
261{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800262 return right != left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700263}
264
265constexpr static_bounds_dynamic_range_t dynamic_range{};
266#else
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700267const std::ptrdiff_t dynamic_range = -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700268#endif
269
270struct generalized_mapping_tag {};
271struct contiguous_mapping_tag : generalized_mapping_tag {};
272
273namespace details
274{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700275
Neil MacIntosh68064d62015-11-03 19:17:11 -0800276 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
277 struct LessThan
278 {
279 static const bool value = Left < Right;
280 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700281
Neil MacIntosh68064d62015-11-03 19:17:11 -0800282 template <std::ptrdiff_t... Ranges>
283 struct BoundsRanges {
284 using size_type = std::ptrdiff_t;
285 static const size_type Depth = 0;
286 static const size_type DynamicNum = 0;
287 static const size_type CurrentRange = 1;
288 static const size_type TotalSize = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700289
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700290 // TODO : following signature is for work around VS bug
291 template <typename OtherRange>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800292 BoundsRanges(const OtherRange&, bool /* firstLevel */)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700293 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800294
295 BoundsRanges (const BoundsRanges&) = default;
Vladislav Yaroslavlev995cfdf2015-11-12 10:46:21 +0300296 BoundsRanges& operator=(const BoundsRanges&) = default;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800297 BoundsRanges(const std::ptrdiff_t* const) { }
298 BoundsRanges() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700299
300
Neil MacIntosh68064d62015-11-03 19:17:11 -0800301 template <typename T, size_t Dim>
302 void serialize(T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700303 {}
304
Neil MacIntosh68064d62015-11-03 19:17:11 -0800305 template <typename T, size_t Dim>
306 size_type linearize(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700307 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800308 return 0;
309 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700310
Neil MacIntosh68064d62015-11-03 19:17:11 -0800311 template <typename T, size_t Dim>
312 bool contains(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700313 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800314 return 0;
315 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700316
Neil MacIntosh68064d62015-11-03 19:17:11 -0800317 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700318 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800319 return TotalSize;
320 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700321
Neil MacIntosh68064d62015-11-03 19:17:11 -0800322 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700323 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800324 return true;
325 }
326 };
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700327
Neil MacIntosh68064d62015-11-03 19:17:11 -0800328 template <std::ptrdiff_t... RestRanges>
329 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
330 using Base = BoundsRanges <RestRanges... >;
331 using size_type = std::ptrdiff_t;
332 static const size_t Depth = Base::Depth + 1;
333 static const size_t DynamicNum = Base::DynamicNum + 1;
334 static const size_type CurrentRange = dynamic_range;
335 static const size_type TotalSize = dynamic_range;
336 const size_type m_bound;
337
338 BoundsRanges (const BoundsRanges&) = default;
339
340 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
341 {
342 fail_fast_assert(0 <= *arr);
343 }
344
345 BoundsRanges() : m_bound(0) {}
346
347 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
348 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
349 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
350 {}
351
352 template <typename T, size_t Dim = 0>
353 void serialize(T& arr) const
354 {
355 arr[Dim] = elementNum();
356 this->Base::template serialize<T, Dim + 1>(arr);
357 }
358
359 template <typename T, size_t Dim = 0>
360 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700361 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800362 const size_type index = this->Base::totalSize() * arr[Dim];
363 fail_fast_assert(index < m_bound);
364 return index + this->Base::template linearize<T, Dim + 1>(arr);
365 }
366
367 template <typename T, size_t Dim = 0>
368 size_type contains(const T & arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700369 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800370 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
371 if (last == -1)
372 return -1;
373 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
374 return cur < m_bound ? cur + last : -1;
375 }
376
377 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700378 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800379 return m_bound;
380 }
381
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700382 size_type elementNum() const noexcept
383 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800384 return totalSize() / this->Base::totalSize();
385 }
386
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700387 size_type elementNum(size_t dim) const noexcept
388 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800389 if (dim > 0)
390 return this->Base::elementNum(dim - 1);
391 else
392 return elementNum();
393 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700394
Neil MacIntosh68064d62015-11-03 19:17:11 -0800395 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700396 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800397 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
398 }
399 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700400
Neil MacIntosh68064d62015-11-03 19:17:11 -0800401 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
402 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
403 {
404 using Base = BoundsRanges <RestRanges... >;
405 using size_type = std::ptrdiff_t;
406 static const size_t Depth = Base::Depth + 1;
407 static const size_t DynamicNum = Base::DynamicNum;
408 static const size_type CurrentRange = CurRange;
409 static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
410
411 BoundsRanges (const BoundsRanges&) = default;
412 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) { }
413 BoundsRanges() = default;
414
415 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
416 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>&other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
417 {
418 fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
419 }
420
421 template <typename T, size_t Dim = 0>
422 void serialize(T& arr) const
423 {
424 arr[Dim] = elementNum();
425 this->Base::template serialize<T, Dim + 1>(arr);
426 }
427
428 template <typename T, size_t Dim = 0>
429 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700430 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800431 fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
432 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
433 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700434
Neil MacIntosh68064d62015-11-03 19:17:11 -0800435 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700436 size_type contains(const T& arr) const
437 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800438 if (arr[Dim] >= CurrentRange)
439 return -1;
440 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
441 if (last == -1)
442 return -1;
443 return this->Base::totalSize() * arr[Dim] + last;
444 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700445
Neil MacIntosh68064d62015-11-03 19:17:11 -0800446 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700447 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800448 return CurrentRange * this->Base::totalSize();
449 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700450
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700451 size_type elementNum() const noexcept
452 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800453 return CurrentRange;
454 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700455
Neil MacIntosh68064d62015-11-03 19:17:11 -0800456 size_type elementNum(size_t dim) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700457 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800458 if (dim > 0)
459 return this->Base::elementNum(dim - 1);
460 else
461 return elementNum();
462 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700463
Neil MacIntosh68064d62015-11-03 19:17:11 -0800464 bool operator== (const BoundsRanges& rhs) const noexcept
465 {
466 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
467 }
468 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700469
Neil MacIntosh68064d62015-11-03 19:17:11 -0800470 template <typename SourceType, typename TargetType, size_t Rank>
471 struct BoundsRangeConvertible2;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700472
Neil MacIntosh68064d62015-11-03 19:17:11 -0800473 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
474 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700475
Neil MacIntosh68064d62015-11-03 19:17:11 -0800476 template <size_t Rank, typename SourceType, typename TargetType>
477 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
478
479 template <typename SourceType, typename TargetType, size_t Rank>
480 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
481 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
482 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
483 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700484
Neil MacIntosh68064d62015-11-03 19:17:11 -0800485 template <typename SourceType, typename TargetType>
486 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700487
Neil MacIntosh68064d62015-11-03 19:17:11 -0800488 template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth>
489 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
490 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
491 && (!LessThan<SourceType::CurrentRange, TargetType::CurrentRange>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
492 {};
493 template <typename SourceType, typename TargetType>
494 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700495
Neil MacIntosh68064d62015-11-03 19:17:11 -0800496 template <typename TypeChain>
497 struct TypeListIndexer
498 {
499 const TypeChain & obj;
500 TypeListIndexer(const TypeChain & obj) :obj(obj){}
501 template<size_t N>
502 const TypeChain & getObj(std::true_type)
503 {
504 return obj;
505 }
506 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
507 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
508 {
509 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
510 }
511 template <size_t N>
512 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
513 {
514 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
515 }
516 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700517
Neil MacIntosh68064d62015-11-03 19:17:11 -0800518 template <typename TypeChain>
519 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
520 {
521 return TypeListIndexer<TypeChain>(obj);
522 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700523
Neil MacIntosh68064d62015-11-03 19:17:11 -0800524 template <size_t Rank, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
525 constexpr Ret shift_left(const index<Rank>& other) noexcept
526 {
527 Ret ret{};
528 for (size_t i = 0; i < Rank - 1; ++i)
529 {
530 ret[i] = other[i + 1];
531 }
532 return ret;
533 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700534}
535
536template <typename IndexType>
537class bounds_iterator;
538
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700539template <std::ptrdiff_t... Ranges>
540class static_bounds
541{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700542public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800543 static_bounds(const details::BoundsRanges<Ranges...>&) {
544 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700545};
546
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700547template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
548class static_bounds<FirstRange, RestRanges...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700549{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800550 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700551
Neil MacIntosh68064d62015-11-03 19:17:11 -0800552 MyRanges m_ranges;
553 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700554 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800555
556 template <std::ptrdiff_t... OtherRanges>
557 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700558
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700559public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800560 static const size_t rank = MyRanges::Depth;
561 static const size_t dynamic_rank = MyRanges::DynamicNum;
562 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700563
Neil MacIntosh68064d62015-11-03 19:17:11 -0800564 using size_type = std::ptrdiff_t;
565 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700566 using const_index_type = std::add_const_t<index_type>;
567 using iterator = bounds_iterator<const_index_type>;
568 using const_iterator = bounds_iterator<const_index_type>;
569 using difference_type = std::ptrdiff_t;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800570 using sliced_type = static_bounds<RestRanges...>;
571 using mapping_type = contiguous_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700572
Neil MacIntosh68064d62015-11-03 19:17:11 -0800573 constexpr static_bounds(const static_bounds&) = default;
574
575 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
576 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700577 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
578 {}
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700579
Neil MacIntosh68064d62015-11-03 19:17:11 -0800580 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges((const std::ptrdiff_t*)il.begin())
581 {
582 fail_fast_assert((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) || MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
583 fail_fast_assert(m_ranges.totalSize() <= PTRDIFF_MAX, "Size of the range is larger than the max element of the size type");
584 }
585
586 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700587
Neil MacIntosh68064d62015-11-03 19:17:11 -0800588 constexpr static_bounds& operator=(const static_bounds& otherBounds)
589 {
590 new(&m_ranges) MyRanges (otherBounds.m_ranges);
591 return *this;
592 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700593
Neil MacIntosh68064d62015-11-03 19:17:11 -0800594 constexpr sliced_type slice() const noexcept
595 {
596 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
597 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700598
Neil MacIntosh68064d62015-11-03 19:17:11 -0800599 constexpr size_type stride() const noexcept
600 {
601 return rank > 1 ? slice().size() : 1;
602 }
603
604 constexpr size_type size() const noexcept
605 {
606 return m_ranges.totalSize();
607 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700608
Neil MacIntosh68064d62015-11-03 19:17:11 -0800609 constexpr size_type total_size() const noexcept
610 {
611 return m_ranges.totalSize();
612 }
613
614 constexpr size_type linearize(const index_type & idx) const
615 {
616 return m_ranges.linearize(idx);
617 }
618
619 constexpr bool contains(const index_type& idx) const noexcept
620 {
621 return m_ranges.contains(idx) != -1;
622 }
623
624 constexpr size_type operator[](size_t index) const noexcept
625 {
626 return m_ranges.elementNum(index);
627 }
628
629 template <size_t Dim = 0>
630 constexpr size_type extent() const noexcept
631 {
632 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
633 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
634 }
635
636 constexpr index_type index_bounds() const noexcept
637 {
638 size_type extents[rank] = {};
639 m_ranges.serialize(extents);
640 return{ extents };
641 }
642
643 template <std::ptrdiff_t... Ranges>
644 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
645 {
646 return this->size() == rhs.size();
647 }
648
649 template <std::ptrdiff_t... Ranges>
650 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
651 {
652 return !(*this == rhs);
653 }
654
655 constexpr const_iterator begin() const noexcept
656 {
657 return const_iterator(*this, index_type{});
658 }
659
660 constexpr const_iterator end() const noexcept
661 {
662 return const_iterator(*this, this->index_bounds());
663 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700664};
665
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700666template <size_t Rank>
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700667class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700668{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800669 template <size_t OtherRank>
670 friend class strided_bounds;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700671
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700672public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800673 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700674 using value_type = std::ptrdiff_t;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800675 using reference = std::add_lvalue_reference_t<value_type>;
676 using const_reference = std::add_const_t<reference>;
677 using size_type = value_type;
678 using difference_type = value_type;
679 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700680 using const_index_type = std::add_const_t<index_type>;
681 using iterator = bounds_iterator<const_index_type>;
682 using const_iterator = bounds_iterator<const_index_type>;
683 static const value_type dynamic_rank = rank;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800684 static const value_type static_size = dynamic_range;
685 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
686 using mapping_type = generalized_mapping_tag;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700687
Neil MacIntosh68064d62015-11-03 19:17:11 -0800688 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700689
Vladislav Yaroslavlev557e6692015-11-12 10:44:41 +0300690 constexpr strided_bounds & operator=(const strided_bounds &) noexcept = default;
691
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700692 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700693 : m_extents(values), m_strides(std::move(strides))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700694 {}
695
Neil MacIntosh68064d62015-11-03 19:17:11 -0800696 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
697 : m_extents(extents), m_strides(strides)
698 {}
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700699
Neil MacIntosh68064d62015-11-03 19:17:11 -0800700 constexpr index_type strides() const noexcept
701 {
702 return m_strides;
703 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700704
Neil MacIntosh68064d62015-11-03 19:17:11 -0800705 constexpr size_type total_size() const noexcept
706 {
707 size_type ret = 0;
708 for (size_t i = 0; i < rank; ++i)
709 {
710 ret += (m_extents[i] - 1) * m_strides[i];
711 }
712 return ret + 1;
713 }
714
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700715 constexpr size_type size() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800716 {
717 size_type ret = 1;
718 for (size_t i = 0; i < rank; ++i)
719 {
720 ret *= m_extents[i];
721 }
722 return ret;
723 }
724
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700725 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800726 {
727 for (size_t i = 0; i < rank; ++i)
728 {
729 if (idx[i] < 0 || idx[i] >= m_extents[i])
730 return false;
731 }
732 return true;
733 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700734
Neil MacIntosh68064d62015-11-03 19:17:11 -0800735 constexpr size_type linearize(const index_type& idx) const noexcept
736 {
737 size_type ret = 0;
738 for (size_t i = 0; i < rank; i++)
739 {
740 fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array");
741 ret += idx[i] * m_strides[i];
742 }
743 return ret;
744 }
745
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700746 constexpr size_type stride() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800747 {
748 return m_strides[0];
749 }
750
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700751 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800752 constexpr sliced_type slice() const
753 {
754 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
755 }
756
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700757 template <size_t Dim = 0>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800758 constexpr size_type extent() const noexcept
759 {
760 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
761 return m_extents[Dim];
762 }
763
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700764 constexpr index_type index_bounds() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800765 {
766 return m_extents;
767 }
768 constexpr const_iterator begin() const noexcept
769 {
770 return const_iterator{ *this, index_type{} };
771 }
772
773 constexpr const_iterator end() const noexcept
774 {
775 return const_iterator{ *this, index_bounds() };
776 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700777
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700778private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800779 index_type m_extents;
780 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700781};
782
783template <typename T>
784struct is_bounds : std::integral_constant<bool, false> {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700785template <std::ptrdiff_t... Ranges>
786struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> {};
787template <size_t Rank>
788struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700789
790template <typename IndexType>
Anna Gringauzea4654a42015-10-16 12:15:22 -0700791class bounds_iterator: public std::iterator<std::random_access_iterator_tag, IndexType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700792{
793private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800794 using Base = std::iterator <std::random_access_iterator_tag, IndexType>;
Anna Gringauzea4654a42015-10-16 12:15:22 -0700795
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700796public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800797 static const size_t rank = IndexType::rank;
798 using typename Base::reference;
799 using typename Base::pointer;
800 using typename Base::difference_type;
801 using typename Base::value_type;
802 using index_type = value_type;
803 using index_size_type = typename IndexType::value_type;
804 template <typename Bounds>
805 explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
806 : boundary(bnd.index_bounds()), curr(std::move(curr))
807 {
808 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
809 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700810
Neil MacIntosh68064d62015-11-03 19:17:11 -0800811 constexpr reference operator*() const noexcept
812 {
813 return curr;
814 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700815
Neil MacIntosh68064d62015-11-03 19:17:11 -0800816 constexpr pointer operator->() const noexcept
817 {
818 return &curr;
819 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700820
Neil MacIntosh68064d62015-11-03 19:17:11 -0800821 constexpr bounds_iterator& operator++() noexcept
822 {
823 for (size_t i = rank; i-- > 0;)
824 {
825 if (curr[i] < boundary[i] - 1)
826 {
827 curr[i]++;
828 return *this;
829 }
830 curr[i] = 0;
831 }
832 // If we're here we've wrapped over - set to past-the-end.
833 curr = boundary;
834 return *this;
835 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700836
Neil MacIntosh68064d62015-11-03 19:17:11 -0800837 constexpr bounds_iterator operator++(int) noexcept
838 {
839 auto ret = *this;
840 ++(*this);
841 return ret;
842 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700843
Neil MacIntosh68064d62015-11-03 19:17:11 -0800844 constexpr bounds_iterator& operator--() noexcept
845 {
846 if (!less(curr, boundary))
847 {
848 // if at the past-the-end, set to last element
849 for (size_t i = 0; i < rank; ++i)
850 {
851 curr[i] = boundary[i] - 1;
852 }
853 return *this;
854 }
855 for (size_t i = rank; i-- > 0;)
856 {
857 if (curr[i] >= 1)
858 {
859 curr[i]--;
860 return *this;
861 }
862 curr[i] = boundary[i] - 1;
863 }
864 // If we're here the preconditions were violated
865 // "pre: there exists s such that r == ++s"
866 fail_fast_assert(false);
867 return *this;
868 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700869
Neil MacIntosh68064d62015-11-03 19:17:11 -0800870 constexpr bounds_iterator operator--(int) noexcept
871 {
872 auto ret = *this;
873 --(*this);
874 return ret;
875 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700876
Neil MacIntosh68064d62015-11-03 19:17:11 -0800877 constexpr bounds_iterator operator+(difference_type n) const noexcept
878 {
879 bounds_iterator ret{ *this };
880 return ret += n;
881 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700882
Neil MacIntosh68064d62015-11-03 19:17:11 -0800883 constexpr bounds_iterator& operator+=(difference_type n) noexcept
884 {
885 auto linear_idx = linearize(curr) + n;
886 std::remove_const_t<value_type> stride = 0;
887 stride[rank - 1] = 1;
888 for (size_t i = rank - 1; i-- > 0;)
889 {
890 stride[i] = stride[i + 1] * boundary[i + 1];
891 }
892 for (size_t i = 0; i < rank; ++i)
893 {
894 curr[i] = linear_idx / stride[i];
895 linear_idx = linear_idx % stride[i];
896 }
897 fail_fast_assert(!less(curr, index_type{}) && !less(boundary, curr), "index is out of bounds of the array");
898 return *this;
899 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700900
Neil MacIntosh68064d62015-11-03 19:17:11 -0800901 constexpr bounds_iterator operator-(difference_type n) const noexcept
902 {
903 bounds_iterator ret{ *this };
904 return ret -= n;
905 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700906
Neil MacIntosh68064d62015-11-03 19:17:11 -0800907 constexpr bounds_iterator& operator-=(difference_type n) noexcept
908 {
909 return *this += -n;
910 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700911
Neil MacIntosh68064d62015-11-03 19:17:11 -0800912 constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
913 {
914 return linearize(curr) - linearize(rhs.curr);
915 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700916
Neil MacIntosh68064d62015-11-03 19:17:11 -0800917 constexpr value_type operator[](difference_type n) const noexcept
918 {
919 return *(*this + n);
920 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700921
Neil MacIntosh68064d62015-11-03 19:17:11 -0800922 constexpr bool operator==(const bounds_iterator& rhs) const noexcept
923 {
924 return curr == rhs.curr;
925 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700926
Neil MacIntosh68064d62015-11-03 19:17:11 -0800927 constexpr bool operator!=(const bounds_iterator& rhs) const noexcept
928 {
929 return !(*this == rhs);
930 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700931
Neil MacIntosh68064d62015-11-03 19:17:11 -0800932 constexpr bool operator<(const bounds_iterator& rhs) const noexcept
933 {
934 return less(curr, rhs.curr);
935 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700936
Neil MacIntosh68064d62015-11-03 19:17:11 -0800937 constexpr bool operator<=(const bounds_iterator& rhs) const noexcept
938 {
939 return !(rhs < *this);
940 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700941
Neil MacIntosh68064d62015-11-03 19:17:11 -0800942 constexpr bool operator>(const bounds_iterator& rhs) const noexcept
943 {
944 return rhs < *this;
945 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700946
Neil MacIntosh68064d62015-11-03 19:17:11 -0800947 constexpr bool operator>=(const bounds_iterator& rhs) const noexcept
948 {
949 return !(rhs > *this);
950 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700951
Neil MacIntosh68064d62015-11-03 19:17:11 -0800952 void swap(bounds_iterator& rhs) noexcept
953 {
954 std::swap(boundary, rhs.boundary);
955 std::swap(curr, rhs.curr);
956 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700957private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800958 constexpr bool less(index_type& one, index_type& other) const noexcept
959 {
960 for (size_t i = 0; i < rank; ++i)
961 {
962 if (one[i] < other[i])
963 return true;
964 }
965 return false;
966 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700967
Neil MacIntosh68064d62015-11-03 19:17:11 -0800968 constexpr index_size_type linearize(const value_type& idx) const noexcept
969 {
970 // TODO: Smarter impl.
971 // Check if past-the-end
972 index_size_type multiplier = 1;
973 index_size_type res = 0;
974 if (!less(idx, boundary))
975 {
976 res = 1;
977 for (size_t i = rank; i-- > 0;)
978 {
979 res += (idx[i] - 1) * multiplier;
980 multiplier *= boundary[i];
981 }
982 }
983 else
984 {
985 for (size_t i = rank; i-- > 0;)
986 {
987 res += idx[i] * multiplier;
988 multiplier *= boundary[i];
989 }
990 }
991 return res;
992 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700993
Neil MacIntosh68064d62015-11-03 19:17:11 -0800994 value_type boundary;
995 std::remove_const_t<value_type> curr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700996};
997
998template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -0700999bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001000{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001001 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001002}
1003
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001004//
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001005// begin definitions of basic_span
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001006//
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001007namespace details
1008{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001009 template <typename Bounds>
1010 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
1011 {
1012 return bnd.strides();
1013 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001014
Neil MacIntosh68064d62015-11-03 19:17:11 -08001015 // Make a stride vector from bounds, assuming contiguous memory.
1016 template <typename Bounds>
1017 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
1018 {
1019 auto extents = bnd.index_bounds();
1020 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001021
Neil MacIntosh68064d62015-11-03 19:17:11 -08001022 stride[Bounds::rank - 1] = 1;
1023 for (size_t i = 1; i < Bounds::rank; ++i)
1024 {
1025 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
1026 }
1027 return{ stride };
1028 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001029
Neil MacIntosh68064d62015-11-03 19:17:11 -08001030 template <typename BoundsSrc, typename BoundsDest>
1031 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1032 {
1033 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1034 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1035 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");
1036 fail_fast_assert(src.size() == dest.size());
1037 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001038
1039
1040} // namespace details
1041
Anna Gringauze8aa42482015-11-11 12:41:11 -08001042template <typename Span>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001043class contiguous_span_iterator;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001044template <typename Span>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001045class general_span_iterator;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001046enum class byte : std::uint8_t {};
1047
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001048template <std::ptrdiff_t DimSize = dynamic_range>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001049struct dim
1050{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001051 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001052};
1053template <>
1054struct dim<dynamic_range>
1055{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001056 static const std::ptrdiff_t value = dynamic_range;
1057 const std::ptrdiff_t dvalue;
1058 dim(std::ptrdiff_t size) : dvalue(size) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001059};
1060
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001061template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, std::ptrdiff_t... RestDimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001062class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001063
1064template <typename ValueType, size_t Rank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001065class strided_span;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001066
1067namespace details
1068{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001069 template <typename T, typename = std::true_type>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001070 struct SpanTypeTraits
Neil MacIntosh68064d62015-11-03 19:17:11 -08001071 {
1072 using value_type = T;
1073 using size_type = size_t;
1074 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001075
Neil MacIntosh68064d62015-11-03 19:17:11 -08001076 template <typename Traits>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001077 struct SpanTypeTraits<Traits, typename std::is_reference<typename Traits::span_traits &>::type>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001078 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001079 using value_type = typename Traits::span_traits::value_type;
1080 using size_type = typename Traits::span_traits::size_type;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001081 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001082
Neil MacIntosh68064d62015-11-03 19:17:11 -08001083 template <typename T, std::ptrdiff_t... Ranks>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001084 struct SpanArrayTraits {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001085 using type = span<T, Ranks...>;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001086 using value_type = T;
1087 using bounds_type = static_bounds<Ranks...>;
1088 using pointer = T*;
1089 using reference = T&;
1090 };
1091 template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001092 struct SpanArrayTraits<T[N], Ranks...> : SpanArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001093
Neil MacIntosh68064d62015-11-03 19:17:11 -08001094 template <typename BoundsType>
1095 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
1096 {
1097 fail_fast_assert(totalSize <= PTRDIFF_MAX);
1098 return BoundsType{totalSize};
1099 }
1100 template <typename BoundsType>
1101 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
1102 {
1103 fail_fast_assert(BoundsType::static_size == totalSize);
1104 return {};
1105 }
1106 template <typename BoundsType>
1107 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
1108 {
1109 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1110 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1111 }
1112
1113 struct Sep{};
1114
1115 template <typename T, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001116 T static_as_span_helper(Sep, Args... args)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001117 {
1118 return T{static_cast<typename T::size_type>(args)...};
1119 }
1120 template <typename T, typename Arg, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001121 std::enable_if_t<!std::is_same<Arg, dim<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T> static_as_span_helper(Arg, Args... args)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001122 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001123 return static_as_span_helper<T>(args...);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001124 }
1125 template <typename T, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001126 T static_as_span_helper(dim<dynamic_range> val, Args ... args)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001127 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001128 return static_as_span_helper<T>(args..., val.dvalue);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001129 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001130
Neil MacIntosh68064d62015-11-03 19:17:11 -08001131 template <typename ...Dimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001132 struct static_as_span_static_bounds_helper
Neil MacIntosh68064d62015-11-03 19:17:11 -08001133 {
1134 using type = static_bounds<(Dimensions::value)...>;
1135 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001136
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001137 template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001138 struct is_span_oracle : std::false_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001139 {};
1140
1141 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001142 struct is_span_oracle<span<ValueType, FirstDimension, RestDimensions...>> : std::true_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001143 {};
1144
1145 template <typename ValueType, std::ptrdiff_t Rank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001146 struct is_span_oracle<strided_span<ValueType, Rank>> : std::true_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001147 {};
1148
1149 template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001150 struct is_span : is_span_oracle<std::remove_cv_t<T>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001151 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001152
1153}
1154
1155
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001156template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001157class span
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001158{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001159 template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001160 friend class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001161
Anna Gringauze8aa42482015-11-11 12:41:11 -08001162public:
Anna Gringauzef5100252015-11-12 12:48:49 -08001163 using bounds_type = static_bounds<FirstDimension, RestDimensions...>;
1164 static const size_t rank = bounds_type::rank;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001165 using size_type = typename bounds_type::size_type;
1166 using index_type = typename bounds_type::index_type;
1167 using value_type = ValueType;
1168 using const_value_type = std::add_const_t<value_type>;
Anna Gringauzef5100252015-11-12 12:48:49 -08001169 using pointer = std::add_pointer_t<value_type>;
1170 using reference = std::add_lvalue_reference_t<value_type>;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001171 using iterator = contiguous_span_iterator<span>;
1172 using const_span = span<const_value_type, FirstDimension, RestDimensions...>;
1173 using const_iterator = contiguous_span_iterator<const_span>;
1174 using reverse_iterator = std::reverse_iterator<iterator>;
1175 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1176 using sliced_type = std::conditional_t<rank == 1, value_type, span<value_type, RestDimensions...>>;
1177
1178private:
1179 pointer m_pdata;
1180 bounds_type m_bounds;
1181
1182 friend iterator;
1183 friend const_iterator;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001184
1185public:
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001186
Anna Gringauzef5100252015-11-12 12:48:49 -08001187 constexpr span(pointer data, bounds_type bounds) noexcept
1188 : m_pdata(data), m_bounds(std::move(bounds))
Anna Gringauze8aa42482015-11-11 12:41:11 -08001189 {
1190 fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
1191 }
1192
Anna Gringauzef5100252015-11-12 12:48:49 -08001193 constexpr span(pointer ptr, size_type size) noexcept
1194 : span(ptr, bounds_type{ size })
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001195 {}
1196
Anna Gringauzef5100252015-11-12 12:48:49 -08001197 constexpr span(std::nullptr_t) noexcept
1198 : span(nullptr, bounds_type{})
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001199 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001200
Anna Gringauzef5100252015-11-12 12:48:49 -08001201 constexpr span(std::nullptr_t, size_type size) noexcept
1202 : span(nullptr, bounds_type{})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001203 {
1204 fail_fast_assert(size == 0);
1205 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001206
Neil MacIntosh68064d62015-11-03 19:17:11 -08001207 // default
1208 template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
Anna Gringauzef5100252015-11-12 12:48:49 -08001209 constexpr span() noexcept
1210 : span(nullptr, bounds_type())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001211 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001212
Neil MacIntosh68064d62015-11-03 19:17:11 -08001213 // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
Anna Gringauzef5100252015-11-12 12:48:49 -08001214 template <typename T, typename Helper = details::SpanArrayTraits<T, dynamic_range>,
1215 typename Dummy = std::enable_if_t<std::is_same<value_type, std::remove_all_extents_t<T>>::value>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001216 /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], value_type (*)[]>::value>*/
1217 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001218 constexpr span(T* const& data, size_type size) : span(reinterpret_cast<pointer>(data), typename Helper::bounds_type{size})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001219 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001220
Neil MacIntosh68064d62015-11-03 19:17:11 -08001221 // from n-dimensions static array
Anna Gringauze8aa42482015-11-11 12:41:11 -08001222 template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
1223 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
1224 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001225 constexpr span (T (&arr)[N]) : span(reinterpret_cast<pointer>(arr), typename Helper::bounds_type())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001226 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001227
Neil MacIntosh68064d62015-11-03 19:17:11 -08001228 // from n-dimensions static array with size
Anna Gringauze8aa42482015-11-11 12:41:11 -08001229 template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
1230 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001231 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001232 constexpr span(T(&arr)[N], size_type size) : span(arr, typename Helper::bounds_type{size})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001233 {
1234 fail_fast_assert(size <= N);
1235 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001236
Neil MacIntosh68064d62015-11-03 19:17:11 -08001237 // from std array
1238 template <size_t N,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001239 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001240 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001241 constexpr span (std::array<std::remove_const_t<value_type>, N> & arr) : span(arr.data(), static_bounds<N>())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001242 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001243
Neil MacIntosh68064d62015-11-03 19:17:11 -08001244 template <size_t N,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001245 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001246 && std::is_const<value_type>::value>
1247 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001248 constexpr span (const std::array<std::remove_const_t<value_type>, N> & arr) : span(arr.data(), static_bounds<N>())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001249 {}
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001250
Neil MacIntosh68064d62015-11-03 19:17:11 -08001251 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1252 template <typename Ptr,
1253 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
Anna Gringauze8aa42482015-11-11 12:41:11 -08001254 && details::LessThan<bounds_type::dynamic_rank, 2>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001255 > // remove literal 0 case
Anna Gringauze8aa42482015-11-11 12:41:11 -08001256 constexpr span (pointer begin, Ptr end) : span(begin, details::newBoundsHelper<bounds_type>(static_cast<pointer>(end) - begin))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001257 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001258
Neil MacIntosh68064d62015-11-03 19:17:11 -08001259 // from containers. It must has .size() and .data() two function signatures
1260 template <typename Cont, typename DataType = typename Cont::value_type,
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001261 typename Dummy = std::enable_if_t<!details::is_span<Cont>::value
Anna Gringauze8aa42482015-11-11 12:41:11 -08001262 && std::is_convertible<DataType (*)[], value_type (*)[]>::value
Neil MacIntosh68064d62015-11-03 19:17:11 -08001263 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
1264 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001265 constexpr span (Cont& cont) : span(static_cast<pointer>(cont.data()), details::newBoundsHelper<bounds_type>(cont.size()))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001266 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001267
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001268 constexpr span(const span &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001269
Neil MacIntosh68064d62015-11-03 19:17:11 -08001270 // convertible
1271 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001272 typename OtherBounds = static_bounds<OtherDimensions...>,
1273 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType, ValueType>::value && std::is_convertible<OtherBounds, bounds_type>::value>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001274 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001275 constexpr span(const span<OtherValueType, OtherDimensions...>& other) noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001276 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001277 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001278
Neil MacIntosh68064d62015-11-03 19:17:11 -08001279 // reshape
Neil MacIntosh14d50a62015-11-03 12:44:09 -08001280 // DimCount here is a workaround for a bug in MSVC 2015
Anna Gringauzef5100252015-11-12 12:48:49 -08001281 template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), bool Enabled = (DimCount > 0), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001282 constexpr span<ValueType, Dimensions2::value...> as_span(Dimensions2... dims)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001283 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001284 using BoundsType = typename span<ValueType, (Dimensions2::value)...>::bounds_type;
1285 auto tobounds = details::static_as_span_helper<BoundsType>(dims..., details::Sep{});
Neil MacIntosh68064d62015-11-03 19:17:11 -08001286 details::verifyBoundsReshape(this->bounds(), tobounds);
1287 return {this->data(), tobounds};
1288 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001289
Neil MacIntosh68064d62015-11-03 19:17:11 -08001290 // to bytes array
1291 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001292 auto as_bytes() const noexcept -> span<const byte>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001293 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001294 static_assert(Enabled, "The value_type of span must be standarded layout");
Neil MacIntosh68064d62015-11-03 19:17:11 -08001295 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
1296 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001297
Neil MacIntosh68064d62015-11-03 19:17:11 -08001298 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001299 auto as_writeable_bytes() const noexcept -> span<byte>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001300 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001301 static_assert(Enabled, "The value_type of span must be standarded layout");
Neil MacIntosh68064d62015-11-03 19:17:11 -08001302 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
1303 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001304
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001305 // from bytes array
Neil MacIntosh68064d62015-11-03 19:17:11 -08001306 template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001307 constexpr auto as_span() const noexcept -> span<const U, (bounds_type::static_size != dynamic_range ? static_cast<std::ptrdiff_t>(static_cast<size_t>(bounds_type::static_size) / sizeof(U)) : dynamic_range)>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001308 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001309 static_assert(std::is_standard_layout<U>::value && (bounds_type::static_size == dynamic_range || bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0),
Neil MacIntosh68064d62015-11-03 19:17:11 -08001310 "Target type must be standard layout and its size must match the byte array size");
1311 fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
1312 return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1313 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001314
Neil MacIntosh68064d62015-11-03 19:17:11 -08001315 template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001316 constexpr auto as_span() const noexcept -> span<U, (bounds_type::static_size != dynamic_range ? static_cast<ptrdiff_t>(static_cast<size_t>(bounds_type::static_size) / sizeof(U)) : dynamic_range)>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001317 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001318 static_assert(std::is_standard_layout<U>::value && (bounds_type::static_size == dynamic_range || bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0),
Neil MacIntosh68064d62015-11-03 19:17:11 -08001319 "Target type must be standard layout and its size must match the byte array size");
1320 fail_fast_assert((this->bytes() % sizeof(U)) == 0);
1321 return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1322 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001323
Neil MacIntosh68064d62015-11-03 19:17:11 -08001324 // section on linear space
1325 template<std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001326 constexpr span<ValueType, Count> first() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001327 {
1328 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1329 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
1330 return { this->data(), Count };
1331 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001332
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001333 constexpr span<ValueType, dynamic_range> first(size_type count) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001334 {
1335 fail_fast_assert(count <= this->size());
1336 return { this->data(), count };
1337 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001338
Neil MacIntosh68064d62015-11-03 19:17:11 -08001339 template<std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001340 constexpr span<ValueType, Count> last() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001341 {
1342 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
1343 fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
1344 return { this->data() + this->size() - Count, Count };
1345 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001346
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001347 constexpr span<ValueType, dynamic_range> last(size_type count) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001348 {
1349 fail_fast_assert(count <= this->size());
1350 return { this->data() + this->size() - count, count };
1351 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001352
Neil MacIntosh68064d62015-11-03 19:17:11 -08001353 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001354 constexpr span<ValueType, Count> sub() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001355 {
1356 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");
1357 fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size()));
1358 return { this->data() + Offset, Count };
1359 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001360
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001361 constexpr span<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001362 {
1363 fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
1364 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
1365 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001366
Neil MacIntosh68064d62015-11-03 19:17:11 -08001367 // size
1368 constexpr size_type length() const noexcept
1369 {
1370 return this->size();
1371 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001372
Neil MacIntosh68064d62015-11-03 19:17:11 -08001373 constexpr size_type used_length() const noexcept
1374 {
1375 return length();
1376 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001377
Neil MacIntosh68064d62015-11-03 19:17:11 -08001378 constexpr size_type bytes() const noexcept
1379 {
1380 return sizeof(value_type) * this->size();
1381 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001382
Neil MacIntosh68064d62015-11-03 19:17:11 -08001383 constexpr size_type used_bytes() const noexcept
1384 {
1385 return bytes();
1386 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001387
Neil MacIntosh68064d62015-11-03 19:17:11 -08001388 // section
Anna Gringauzef5100252015-11-12 12:48:49 -08001389 constexpr strided_span<ValueType, rank> section(index_type origin, index_type extents) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001390 {
1391 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze8aa42482015-11-11 12:41:11 -08001392 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(bounds())} };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001393 }
1394
Anna Gringauzef5100252015-11-12 12:48:49 -08001395 constexpr reference operator[](const index_type& idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001396 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001397 return m_pdata[m_bounds.linearize(idx)];
Neil MacIntosh68064d62015-11-03 19:17:11 -08001398 }
1399
Anna Gringauze8aa42482015-11-11 12:41:11 -08001400 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Anna Gringauzef5100252015-11-12 12:48:49 -08001401 constexpr Ret operator[](size_type idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001402 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001403 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
1404 const size_type ridx = idx * m_bounds.stride();
1405
1406 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
1407 return Ret{ m_pdata + ridx, m_bounds.slice() };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001408 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001409
Anna Gringauze8aa42482015-11-11 12:41:11 -08001410 constexpr bounds_type bounds() const noexcept
1411 {
1412 return m_bounds;
1413 }
1414
1415 template <size_t Dim = 0>
1416 constexpr size_type extent() const noexcept
1417 {
1418 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
1419 return m_bounds.template extent<Dim>();
1420 }
1421
1422 constexpr size_type size() const noexcept
1423 {
1424 return m_bounds.size();
1425 }
1426
1427 constexpr pointer data() const noexcept
1428 {
1429 return m_pdata;
1430 }
1431
1432 constexpr operator bool() const noexcept
1433 {
1434 return m_pdata != nullptr;
1435 }
1436
Anna Gringauzef5100252015-11-12 12:48:49 -08001437 constexpr iterator begin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001438 {
1439 return iterator{ this, true };
1440 }
1441
Anna Gringauzef5100252015-11-12 12:48:49 -08001442 constexpr iterator end() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001443 {
1444 return iterator{ this, false };
1445 }
1446
Anna Gringauzef5100252015-11-12 12:48:49 -08001447 constexpr const_iterator cbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001448 {
1449 return const_iterator{ reinterpret_cast<const const_span*>(this), true };
1450 }
1451
Anna Gringauzef5100252015-11-12 12:48:49 -08001452 constexpr const_iterator cend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001453 {
1454 return const_iterator{ reinterpret_cast<const const_span*>(this), false };
1455 }
1456
Anna Gringauzef5100252015-11-12 12:48:49 -08001457 constexpr reverse_iterator rbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001458 {
1459 return reverse_iterator{ end() };
1460 }
1461
Anna Gringauzef5100252015-11-12 12:48:49 -08001462 constexpr reverse_iterator rend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001463 {
1464 return reverse_iterator{ begin() };
1465 }
1466
Anna Gringauzef5100252015-11-12 12:48:49 -08001467 constexpr const_reverse_iterator crbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001468 {
1469 return const_reverse_iterator{ cend() };
1470 }
1471
Anna Gringauzef5100252015-11-12 12:48:49 -08001472 constexpr const_reverse_iterator crend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001473 {
1474 return const_reverse_iterator{ cbegin() };
1475 }
1476
1477 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1478 constexpr bool operator== (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1479 {
1480 return m_bounds.size() == other.m_bounds.size() &&
1481 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
1482 }
1483
1484 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1485 constexpr bool operator!= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1486 {
1487 return !(*this == other);
1488 }
1489
1490 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1491 constexpr bool operator< (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1492 {
1493 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1494 }
1495
1496 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1497 constexpr bool operator<= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1498 {
1499 return !(other < *this);
1500 }
1501
1502 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1503 constexpr bool operator> (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1504 {
1505 return (other < *this);
1506 }
1507
1508 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1509 constexpr bool operator>= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1510 {
1511 return !(*this < other);
1512 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001513};
1514
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001515template <typename T, std::ptrdiff_t... Dimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001516constexpr auto as_span(T* const& ptr, dim<Dimensions>... args) -> span<std::remove_all_extents_t<T>, Dimensions...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001517{
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001518 return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), details::static_as_span_helper<static_bounds<Dimensions...>>(args..., details::Sep{})};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001519}
1520
1521template <typename T>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001522constexpr auto as_span (T* arr, std::ptrdiff_t len) -> typename details::SpanArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001523{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001524 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001525}
1526
1527template <typename T, size_t N>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001528constexpr auto as_span (T (&arr)[N]) -> typename details::SpanArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001529{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001530 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001531}
1532
1533template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001534constexpr span<const T, N> as_span(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001535{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001536 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001537}
1538
1539template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001540constexpr span<const T, N> as_span(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001541
1542template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001543constexpr span<T, N> as_span(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001544{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001545 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001546}
1547
1548template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001549constexpr span<T, dynamic_range> as_span(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001550{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001551 return {begin, end};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001552}
1553
1554template <typename Cont>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001555constexpr auto as_span(Cont &arr) -> std::enable_if_t<!details::is_span<std::decay_t<Cont>>::value,
1556 span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001557{
Neil MacIntoshace9ab92015-10-23 19:49:17 -07001558 fail_fast_assert(arr.size() < PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001559 return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001560}
1561
1562template <typename Cont>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001563constexpr auto as_span(Cont &&arr) -> std::enable_if_t<!details::is_span<std::decay_t<Cont>>::value,
1564 span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001565
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001566template <typename ValueType, size_t Rank>
Anna Gringauzef5100252015-11-12 12:48:49 -08001567class strided_span
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001568{
Anna Gringauzef5100252015-11-12 12:48:49 -08001569public:
1570 using bounds_type = strided_bounds<Rank>;
1571 using size_type = typename bounds_type::size_type;
1572 using index_type = typename bounds_type::index_type;
1573 using value_type = ValueType;
1574 using const_value_type = std::add_const_t<value_type>;
1575 using pointer = std::add_pointer_t<value_type>;
1576 using reference = std::add_lvalue_reference_t<value_type>;
1577 using iterator = general_span_iterator<strided_span>;
1578 using const_strided_span = strided_span<const_value_type, Rank>;
1579 using const_iterator = general_span_iterator<const_strided_span>;
1580 using reverse_iterator = std::reverse_iterator<iterator>;
1581 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1582 using sliced_type = std::conditional_t<Rank == 1, value_type, strided_span<value_type, Rank-1>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001583
Anna Gringauzef5100252015-11-12 12:48:49 -08001584private:
1585 pointer m_pdata;
1586 bounds_type m_bounds;
1587
1588 friend iterator;
1589 friend const_iterator;
1590 template <typename OtherValueType, size_t OtherRank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001591 friend class strided_span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001592
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001593public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001594 // from raw data
Anna Gringauzef5100252015-11-12 12:48:49 -08001595 constexpr strided_span(pointer ptr, size_type size, bounds_type bounds)
1596 : m_pdata(ptr), m_bounds(std::move(bounds))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001597 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001598 fail_fast_assert((m_bounds.size() > 0 && ptr != nullptr) || m_bounds.size() == 0);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001599 fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
1600 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001601
Anna Gringauzef5100252015-11-12 12:48:49 -08001602 // from static array of size N
1603 template<size_type N>
1604 constexpr strided_span(value_type(&values)[N], bounds_type bounds) : strided_span(values, N, std::move(bounds))
1605 {}
1606
Neil MacIntosh68064d62015-11-03 19:17:11 -08001607 // from array view
1608 template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
Anna Gringauzef5100252015-11-12 12:48:49 -08001609 constexpr strided_span(span<ValueType, Dimensions...> av, bounds_type bounds) : strided_span(av.data(), av.bounds().total_size(), std::move(bounds))
1610 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -08001611
1612 // convertible
1613 template <typename OtherValueType,
Anna Gringauzef5100252015-11-12 12:48:49 -08001614 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001615 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001616 constexpr strided_span(const strided_span<OtherValueType, Rank>& other)
1617 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001618 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001619
Neil MacIntosh68064d62015-11-03 19:17:11 -08001620 // convert from bytes
Anna Gringauzef5100252015-11-12 12:48:49 -08001621 template <typename OtherValueType>
1622 constexpr strided_span<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, Rank> as_strided_span() const
Neil MacIntosh68064d62015-11-03 19:17:11 -08001623 {
1624 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1625 auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001626
Neil MacIntosh68064d62015-11-03 19:17:11 -08001627 size_type size = this->bounds().total_size() / d;
1628 return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
1629 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001630
Anna Gringauzef5100252015-11-12 12:48:49 -08001631 constexpr strided_span section(index_type origin, index_type extents) const
Neil MacIntosh68064d62015-11-03 19:17:11 -08001632 {
1633 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauzef5100252015-11-12 12:48:49 -08001634 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(bounds())}};
Neil MacIntosh68064d62015-11-03 19:17:11 -08001635 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001636
Neil MacIntosh68064d62015-11-03 19:17:11 -08001637 constexpr reference operator[](const index_type& idx) const
1638 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001639 return m_pdata[m_bounds.linearize(idx)];
Neil MacIntosh68064d62015-11-03 19:17:11 -08001640 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001641
Anna Gringauzef5100252015-11-12 12:48:49 -08001642 template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
1643 constexpr Ret operator[](size_type idx) const
Neil MacIntosh68064d62015-11-03 19:17:11 -08001644 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001645 fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
1646 const size_type ridx = idx * m_bounds.stride();
1647
1648 fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
1649 return{ m_pdata + ridx, m_bounds.slice().total_size(), m_bounds.slice() };
1650 }
1651
1652 constexpr bounds_type bounds() const noexcept
1653 {
1654 return m_bounds;
1655 }
1656
1657 template <size_t Dim = 0>
1658 constexpr size_type extent() const noexcept
1659 {
1660 static_assert(Dim < Rank, "dimension should be less than Rank (dimension count starts from 0)");
1661 return m_bounds.template extent<Dim>();
1662 }
1663
1664 constexpr size_type size() const noexcept
1665 {
1666 return m_bounds.size();
1667 }
1668
1669 constexpr pointer data() const noexcept
1670 {
1671 return m_pdata;
1672 }
1673
1674 constexpr operator bool() const noexcept
1675 {
1676 return m_pdata != nullptr;
1677 }
1678
1679 constexpr iterator begin() const
1680 {
1681 return iterator{ this, true };
1682 }
1683
1684 constexpr iterator end() const
1685 {
1686 return iterator{ this, false };
1687 }
1688
1689 constexpr const_iterator cbegin() const
1690 {
1691 return const_iterator{ reinterpret_cast<const const_strided_span*>(this), true };
1692 }
1693
1694 constexpr const_iterator cend() const
1695 {
1696 return const_iterator{ reinterpret_cast<const const_strided_span*>(this), false };
1697 }
1698
1699 constexpr reverse_iterator rbegin() const
1700 {
1701 return reverse_iterator{ end() };
1702 }
1703
1704 constexpr reverse_iterator rend() const
1705 {
1706 return reverse_iterator{ begin() };
1707 }
1708
1709 constexpr const_reverse_iterator crbegin() const
1710 {
1711 return const_reverse_iterator{ cend() };
1712 }
1713
1714 constexpr const_reverse_iterator crend() const
1715 {
1716 return const_reverse_iterator{ cbegin() };
1717 }
1718
1719 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1720 constexpr bool operator== (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1721 {
1722 return m_bounds.size() == other.m_bounds.size() &&
1723 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
1724 }
1725
1726 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1727 constexpr bool operator!= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1728 {
1729 return !(*this == other);
1730 }
1731
1732 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1733 constexpr bool operator< (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1734 {
1735 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1736 }
1737
1738 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1739 constexpr bool operator<= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1740 {
1741 return !(other < *this);
1742 }
1743
1744 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1745 constexpr bool operator> (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1746 {
1747 return (other < *this);
1748 }
1749
1750 template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
1751 constexpr bool operator>= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1752 {
1753 return !(*this < other);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001754 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001755
1756private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001757 static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
1758 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001759 fail_fast_assert(extent[Rank - 1] >= d && (extent[Rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001760
Neil MacIntosh68064d62015-11-03 19:17:11 -08001761 index_type ret = extent;
Anna Gringauzef5100252015-11-12 12:48:49 -08001762 ret[Rank - 1] /= d;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001763
Neil MacIntosh68064d62015-11-03 19:17:11 -08001764 return ret;
1765 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001766
Anna Gringauzef5100252015-11-12 12:48:49 -08001767 template <bool Enabled = (Rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001768 static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
1769 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001770 fail_fast_assert(strides[Rank - 1] == 1, "Only strided arrays with regular strides can be resized");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001771
Neil MacIntosh68064d62015-11-03 19:17:11 -08001772 return strides;
1773 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001774
Anna Gringauzef5100252015-11-12 12:48:49 -08001775 template <bool Enabled = (Rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001776 static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
1777 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001778 fail_fast_assert(strides[Rank - 1] == 1, "Only strided arrays with regular strides can be resized");
1779 fail_fast_assert(strides[Rank - 2] >= d && (strides[Rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements");
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001780
Anna Gringauzef5100252015-11-12 12:48:49 -08001781 for (size_t i = Rank - 1; i > 0; --i)
1782 {
1783 fail_fast_assert((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0), "Only strided arrays with regular strides can be resized");
1784 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001785
Neil MacIntosh68064d62015-11-03 19:17:11 -08001786 index_type ret = strides / d;
Anna Gringauzef5100252015-11-12 12:48:49 -08001787 ret[Rank - 1] = 1;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001788
Neil MacIntosh68064d62015-11-03 19:17:11 -08001789 return ret;
1790 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001791};
1792
Anna Gringauze8aa42482015-11-11 12:41:11 -08001793template <class Span>
1794class contiguous_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001795{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001796 using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001797public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001798 using typename Base::reference;
1799 using typename Base::pointer;
1800 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001801
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001802private:
Anna Gringauze8aa42482015-11-11 12:41:11 -08001803 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1804 friend class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001805
1806 pointer m_pdata;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001807 const Span* m_validator;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001808 void validateThis() const
1809 {
1810 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");
1811 }
Anna Gringauze8aa42482015-11-11 12:41:11 -08001812 contiguous_span_iterator (const Span* container, bool isbegin) :
Neil MacIntosh68064d62015-11-03 19:17:11 -08001813 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001814public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001815 reference operator*() const noexcept
1816 {
1817 validateThis();
1818 return *m_pdata;
1819 }
1820 pointer operator->() const noexcept
1821 {
1822 validateThis();
1823 return m_pdata;
1824 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001825 contiguous_span_iterator& operator++() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001826 {
1827 ++m_pdata;
1828 return *this;
1829 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001830 contiguous_span_iterator operator++(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001831 {
1832 auto ret = *this;
1833 ++(*this);
1834 return ret;
1835 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001836 contiguous_span_iterator& operator--() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001837 {
1838 --m_pdata;
1839 return *this;
1840 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001841 contiguous_span_iterator operator--(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001842 {
1843 auto ret = *this;
1844 --(*this);
1845 return ret;
1846 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001847 contiguous_span_iterator operator+(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001848 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001849 contiguous_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001850 return ret += n;
1851 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001852 contiguous_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001853 {
1854 m_pdata += n;
1855 return *this;
1856 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001857 contiguous_span_iterator operator-(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001858 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001859 contiguous_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001860 return ret -= n;
1861 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001862 contiguous_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001863 {
1864 return *this += -n;
1865 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001866 difference_type operator-(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001867 {
1868 fail_fast_assert(m_validator == rhs.m_validator);
1869 return m_pdata - rhs.m_pdata;
1870 }
1871 reference operator[](difference_type n) const noexcept
1872 {
1873 return *(*this + n);
1874 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001875 bool operator==(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001876 {
1877 fail_fast_assert(m_validator == rhs.m_validator);
1878 return m_pdata == rhs.m_pdata;
1879 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001880 bool operator!=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001881 {
1882 return !(*this == rhs);
1883 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001884 bool operator<(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001885 {
1886 fail_fast_assert(m_validator == rhs.m_validator);
1887 return m_pdata < rhs.m_pdata;
1888 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001889 bool operator<=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001890 {
1891 return !(rhs < *this);
1892 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001893 bool operator>(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001894 {
1895 return rhs < *this;
1896 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001897 bool operator>=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001898 {
1899 return !(rhs > *this);
1900 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001901 void swap(contiguous_span_iterator& rhs) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001902 {
1903 std::swap(m_pdata, rhs.m_pdata);
1904 std::swap(m_validator, rhs.m_validator);
1905 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001906};
1907
Anna Gringauze8aa42482015-11-11 12:41:11 -08001908template <typename Span>
1909contiguous_span_iterator<Span> operator+(typename contiguous_span_iterator<Span>::difference_type n, const contiguous_span_iterator<Span>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001910{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001911 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001912}
1913
Anna Gringauze8aa42482015-11-11 12:41:11 -08001914template <typename Span>
1915class general_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001916{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001917 using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001918public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001919 using typename Base::reference;
1920 using typename Base::pointer;
1921 using typename Base::difference_type;
1922 using typename Base::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001923private:
Anna Gringauzef5100252015-11-12 12:48:49 -08001924 template <typename ValueType, size_t Rank>
1925 friend class strided_span;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001926
Anna Gringauzef5100252015-11-12 12:48:49 -08001927 const Span* m_container;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001928 typename Span::bounds_type::iterator m_itr;
Anna Gringauzef5100252015-11-12 12:48:49 -08001929 general_span_iterator(const Span* container, bool isbegin) :
Neil MacIntosh68064d62015-11-03 19:17:11 -08001930 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
1931 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001932public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001933 reference operator*() noexcept
1934 {
1935 return (*m_container)[*m_itr];
1936 }
1937 pointer operator->() noexcept
1938 {
1939 return &(*m_container)[*m_itr];
1940 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001941 general_span_iterator& operator++() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001942 {
1943 ++m_itr;
1944 return *this;
1945 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001946 general_span_iterator operator++(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001947 {
1948 auto ret = *this;
1949 ++(*this);
1950 return ret;
1951 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001952 general_span_iterator& operator--() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001953 {
1954 --m_itr;
1955 return *this;
1956 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001957 general_span_iterator operator--(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001958 {
1959 auto ret = *this;
1960 --(*this);
1961 return ret;
1962 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001963 general_span_iterator operator+(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001964 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001965 general_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001966 return ret += n;
1967 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001968 general_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001969 {
1970 m_itr += n;
1971 return *this;
1972 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001973 general_span_iterator operator-(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001974 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001975 general_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001976 return ret -= n;
1977 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001978 general_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001979 {
1980 return *this += -n;
1981 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001982 difference_type operator-(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001983 {
1984 fail_fast_assert(m_container == rhs.m_container);
1985 return m_itr - rhs.m_itr;
1986 }
1987 value_type operator[](difference_type n) const noexcept
1988 {
1989 return (*m_container)[m_itr[n]];;
1990 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001991 bool operator==(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001992 {
1993 fail_fast_assert(m_container == rhs.m_container);
1994 return m_itr == rhs.m_itr;
1995 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001996 bool operator !=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001997 {
1998 return !(*this == rhs);
1999 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002000 bool operator<(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002001 {
2002 fail_fast_assert(m_container == rhs.m_container);
2003 return m_itr < rhs.m_itr;
2004 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002005 bool operator<=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002006 {
2007 return !(rhs < *this);
2008 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002009 bool operator>(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002010 {
2011 return rhs < *this;
2012 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002013 bool operator>=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002014 {
2015 return !(rhs > *this);
2016 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002017 void swap(general_span_iterator& rhs) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002018 {
2019 std::swap(m_itr, rhs.m_itr);
2020 std::swap(m_container, rhs.m_container);
2021 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002022};
2023
Anna Gringauze8aa42482015-11-11 12:41:11 -08002024template <typename Span>
2025general_span_iterator<Span> operator+(typename general_span_iterator<Span>::difference_type n, const general_span_iterator<Span>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002026{
Neil MacIntosh68064d62015-11-03 19:17:11 -08002027 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002028}
2029
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002030} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002031
Neil MacIntoshd5316802015-09-30 21:54:08 -07002032#ifdef _MSC_VER
2033
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002034#undef constexpr
2035#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002036
Neil MacIntoshd5316802015-09-30 21:54:08 -07002037#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002038#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002039
2040#ifndef GSL_THROWS_FOR_TESTING
2041#pragma undef noexcept
2042#endif // GSL_THROWS_FOR_TESTING
2043
Neil MacIntoshe9a96022015-11-03 18:56:55 -08002044#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
2045#pragma pop_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG")
2046
2047
Neil MacIntosh9a297122015-09-14 15:11:07 -07002048#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002049
Neil MacIntoshd5316802015-09-30 21:54:08 -07002050#endif // _MSC_VER
2051
2052#if defined(GSL_THROWS_FOR_TESTING)
Anna Gringauzef5100252015-11-12 12:48:49 -08002053#undef noexcept
Neil MacIntoshd5316802015-09-30 21:54:08 -07002054#endif // GSL_THROWS_FOR_TESTING
2055
Treb Connell51da1362015-09-24 18:08:34 -07002056
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002057#endif // GSL_SPAN_H