blob: 0c67d22628a99783d45ef4be7fd085ca9d41ec17 [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 MacIntoshd13f6da2015-11-20 16:03:00 -080034#include <cassert>
35#include "gsl_assert.h"
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070036
Neil MacIntoshd5316802015-09-30 21:54:08 -070037#ifdef _MSC_VER
38
Neil MacIntoshd13f6da2015-11-20 16:03:00 -080039// turn off some warnings that are noisy about our Expects statements
40#pragma warning(push)
41#pragma warning(disable: 4127) // conditional expression is constant
42
Neil MacIntoshd5316802015-09-30 21:54:08 -070043// No MSVC does constexpr fully yet
Gabriel Dos Reis6554e832015-09-28 05:10:44 -070044#pragma push_macro("constexpr")
Neil MacIntoshd13f6da2015-11-20 16:03:00 -080045#define constexpr
Neil MacIntoshd5316802015-09-30 21:54:08 -070046
47// VS 2013 workarounds
48#if _MSC_VER <= 1800
49
Neil MacIntosh292f81e2015-11-17 15:07:51 -080050// needed in span.h
Neil MacIntoshe9a96022015-11-03 18:56:55 -080051#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG
52
Neil MacIntoshd5316802015-09-30 21:54:08 -070053// noexcept is not understood
Neil MacIntoshd13f6da2015-11-20 16:03:00 -080054#ifndef GSL_THROWS_ON_CONTRACT_VIOLATION
Neil MacIntosh292f81e2015-11-17 15:07:51 -080055#pragma push_macro("noexcept")
Neil MacIntoshd5316802015-09-30 21:54:08 -070056#define noexcept /* nothing */
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070057#endif
58
Neil MacIntoshd5316802015-09-30 21:54:08 -070059// turn off some misguided warnings
Neil MacIntosh9a297122015-09-14 15:11:07 -070060#pragma warning(push)
61#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
Neil MacIntosha998a9b2015-11-12 18:57:23 -080062#pragma warning(disable: 4512) // warns that assignment op could not be generated
Neil MacIntoshd5316802015-09-30 21:54:08 -070063
Neil MacIntosh9a297122015-09-14 15:11:07 -070064#endif // _MSC_VER <= 1800
65
Neil MacIntoshd5316802015-09-30 21:54:08 -070066#endif // _MSC_VER
67
Neil MacIntoshd13f6da2015-11-20 16:03:00 -080068#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
Neil MacIntosh292f81e2015-11-17 15:07:51 -080069
70#ifdef _MSC_VER
71#pragma push_macro("noexcept")
72#endif
73
Neil MacIntoshd5316802015-09-30 21:54:08 -070074#define noexcept /* nothing */
Neil MacIntosh292f81e2015-11-17 15:07:51 -080075
Neil MacIntoshd13f6da2015-11-20 16:03:00 -080076#endif // GSL_THROW_ON_CONTRACT_VIOLATION
Neil MacIntoshd5316802015-09-30 21:54:08 -070077
78
Neil MacIntoshef626fd2015-09-29 16:41:37 -070079namespace gsl {
Neil MacIntosha9dcbe02015-08-20 18:09:14 -070080
81/*
82** begin definitions of index and bounds
83*/
84namespace details
85{
Neil MacIntosh68064d62015-11-03 19:17:11 -080086 template <typename SizeType>
87 struct SizeTypeTraits
88 {
89 static const SizeType max_value = std::numeric_limits<SizeType>::max();
90 };
Anna Gringauze1c208b32015-10-16 17:40:57 -070091
92
Neil MacIntosh68064d62015-11-03 19:17:11 -080093 template<typename... Ts>
94 class are_integral : public std::integral_constant<bool, true> {};
Anna Gringauze1c208b32015-10-16 17:40:57 -070095
Neil MacIntosh68064d62015-11-03 19:17:11 -080096 template<typename T, typename... Ts>
97 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 -070098}
99
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700100template <size_t Rank>
Anna Gringauzedb384972015-10-05 12:34:23 -0700101class index final
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700102{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800103 static_assert(Rank > 0, "Rank must be greater than 0!");
Anna Gringauzedb384972015-10-05 12:34:23 -0700104
Neil MacIntosh68064d62015-11-03 19:17:11 -0800105 template <size_t OtherRank>
106 friend class index;
Anna Gringauzedb384972015-10-05 12:34:23 -0700107
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700108public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800109 static const size_t rank = Rank;
110 using value_type = std::ptrdiff_t;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700111 using size_type = value_type;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800112 using reference = std::add_lvalue_reference_t<value_type>;
113 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700114
Neil MacIntosh68064d62015-11-03 19:17:11 -0800115 constexpr index() noexcept
116 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700117
Neil MacIntosh68064d62015-11-03 19:17:11 -0800118 constexpr index(const value_type(&values)[Rank]) noexcept
119 {
120 std::copy(values, values + Rank, elems);
121 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700122
Anna Gringauze8aa42482015-11-11 12:41:11 -0800123#ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
124 template<typename T, typename... Ts,
125 typename = std::enable_if_t<((sizeof...(Ts) + 1) == Rank) &&
126 std::is_integral<T>::value &&
127 details::are_integral<Ts...>::value>>
128 constexpr index(T t, Ts... ds) : index({ static_cast<value_type>(t), static_cast<value_type>(ds)... })
129 {}
130#else
131 template<typename... Ts,
132 typename = std::enable_if_t<(sizeof...(Ts) == Rank) && details::are_integral<Ts...>::value>>
133 constexpr index(Ts... ds) noexcept : elems{ static_cast<value_type>(ds)... }
134 {}
135#endif
Anna Gringauzedb384972015-10-05 12:34:23 -0700136
Neil MacIntosh68064d62015-11-03 19:17:11 -0800137 constexpr index(const index& other) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700138
Neil MacIntosh68064d62015-11-03 19:17:11 -0800139 constexpr index& operator=(const index& rhs) noexcept = default;
Anna Gringauzedb384972015-10-05 12:34:23 -0700140
Neil MacIntosh68064d62015-11-03 19:17:11 -0800141 // Preconditions: component_idx < rank
142 constexpr reference operator[](size_t component_idx)
143 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800144 Expects(component_idx < Rank); // Component index must be less than rank
Neil MacIntosh68064d62015-11-03 19:17:11 -0800145 return elems[component_idx];
146 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700147
Neil MacIntosh68064d62015-11-03 19:17:11 -0800148 // Preconditions: component_idx < rank
149 constexpr const_reference operator[](size_t component_idx) const noexcept
150 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800151 Expects(component_idx < Rank); // Component index must be less than rank
Neil MacIntosh68064d62015-11-03 19:17:11 -0800152 return elems[component_idx];
153 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700154
Neil MacIntosh68064d62015-11-03 19:17:11 -0800155 constexpr bool operator==(const index& rhs) const noexcept
156 {
157 return std::equal(elems, elems + rank, rhs.elems);
158 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700159
Neil MacIntosh68064d62015-11-03 19:17:11 -0800160 constexpr bool operator!=(const index& rhs) const noexcept
161 {
162 return !(this == rhs);
163 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700164
Neil MacIntosh68064d62015-11-03 19:17:11 -0800165 constexpr index operator+() const noexcept
166 {
167 return *this;
168 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700169
Neil MacIntosh68064d62015-11-03 19:17:11 -0800170 constexpr index operator-() const noexcept
171 {
172 index ret = *this;
173 std::transform(ret, ret + rank, ret, std::negate<value_type>{});
174 return ret;
175 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700176
Neil MacIntosh68064d62015-11-03 19:17:11 -0800177 constexpr index operator+(const index& rhs) const noexcept
178 {
179 index ret = *this;
180 ret += rhs;
181 return ret;
182 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700183
Neil MacIntosh68064d62015-11-03 19:17:11 -0800184 constexpr index operator-(const index& rhs) const noexcept
185 {
186 index ret = *this;
187 ret -= rhs;
188 return ret;
189 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700190
Neil MacIntosh68064d62015-11-03 19:17:11 -0800191 constexpr index& operator+=(const index& rhs) noexcept
192 {
193 std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
194 return *this;
195 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700196
Neil MacIntosh68064d62015-11-03 19:17:11 -0800197 constexpr index& operator-=(const index& rhs) noexcept
198 {
199 std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
200 return *this;
201 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700202
Neil MacIntosh68064d62015-11-03 19:17:11 -0800203 constexpr index operator*(value_type v) const noexcept
204 {
205 index ret = *this;
206 ret *= v;
207 return ret;
208 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700209
Neil MacIntosh68064d62015-11-03 19:17:11 -0800210 constexpr index operator/(value_type v) const noexcept
211 {
212 index ret = *this;
213 ret /= v;
214 return ret;
215 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700216
Neil MacIntosh68064d62015-11-03 19:17:11 -0800217 friend constexpr index operator*(value_type v, const index& rhs) noexcept
218 {
219 return rhs * v;
220 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700221
Neil MacIntosh68064d62015-11-03 19:17:11 -0800222 constexpr index& operator*=(value_type v) noexcept
223 {
224 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
225 return *this;
226 }
Anna Gringauzedb384972015-10-05 12:34:23 -0700227
Neil MacIntosh68064d62015-11-03 19:17:11 -0800228 constexpr index& operator/=(value_type v) noexcept
229 {
230 std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
231 return *this;
232 }
Anna Gringauze546f8cc2015-10-05 21:04:56 -0700233
Anna Gringauzedb384972015-10-05 12:34:23 -0700234private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800235 value_type elems[Rank] = {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700236};
237
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700238#ifndef _MSC_VER
239
240struct static_bounds_dynamic_range_t
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700241{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800242 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
243 constexpr operator T() const noexcept
244 {
245 return static_cast<T>(-1);
246 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700247
Neil MacIntosh68064d62015-11-03 19:17:11 -0800248 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
249 constexpr bool operator ==(T other) const noexcept
250 {
251 return static_cast<T>(-1) == other;
252 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700253
Neil MacIntosh68064d62015-11-03 19:17:11 -0800254 template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
255 constexpr bool operator !=(T other) const noexcept
256 {
257 return static_cast<T>(-1) != other;
258 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700259
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700260};
261
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700262template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
263constexpr bool operator ==(T left, static_bounds_dynamic_range_t right) noexcept
264{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800265 return right == left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700266}
267
268template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>>
269constexpr bool operator !=(T left, static_bounds_dynamic_range_t right) noexcept
270{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800271 return right != left;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700272}
273
274constexpr static_bounds_dynamic_range_t dynamic_range{};
275#else
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700276const std::ptrdiff_t dynamic_range = -1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700277#endif
278
279struct generalized_mapping_tag {};
280struct contiguous_mapping_tag : generalized_mapping_tag {};
281
282namespace details
283{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700284
Neil MacIntosh68064d62015-11-03 19:17:11 -0800285 template <std::ptrdiff_t Left, std::ptrdiff_t Right>
286 struct LessThan
287 {
288 static const bool value = Left < Right;
289 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700290
Neil MacIntosh68064d62015-11-03 19:17:11 -0800291 template <std::ptrdiff_t... Ranges>
292 struct BoundsRanges {
293 using size_type = std::ptrdiff_t;
294 static const size_type Depth = 0;
295 static const size_type DynamicNum = 0;
296 static const size_type CurrentRange = 1;
297 static const size_type TotalSize = 1;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700298
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700299 // TODO : following signature is for work around VS bug
300 template <typename OtherRange>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800301 BoundsRanges(const OtherRange&, bool /* firstLevel */)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700302 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800303
304 BoundsRanges (const BoundsRanges&) = default;
Vladislav Yaroslavlev995cfdf2015-11-12 10:46:21 +0300305 BoundsRanges& operator=(const BoundsRanges&) = default;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800306 BoundsRanges(const std::ptrdiff_t* const) { }
307 BoundsRanges() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700308
309
Neil MacIntosh68064d62015-11-03 19:17:11 -0800310 template <typename T, size_t Dim>
311 void serialize(T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700312 {}
313
Neil MacIntosh68064d62015-11-03 19:17:11 -0800314 template <typename T, size_t Dim>
315 size_type linearize(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700316 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800317 return 0;
318 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700319
Neil MacIntosh68064d62015-11-03 19:17:11 -0800320 template <typename T, size_t Dim>
321 bool contains(const T&) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700322 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800323 return 0;
324 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700325
Neil MacIntosh68064d62015-11-03 19:17:11 -0800326 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700327 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800328 return TotalSize;
329 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700330
Neil MacIntosh68064d62015-11-03 19:17:11 -0800331 bool operator==(const BoundsRanges&) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700332 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800333 return true;
334 }
335 };
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700336
Neil MacIntosh68064d62015-11-03 19:17:11 -0800337 template <std::ptrdiff_t... RestRanges>
338 struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{
339 using Base = BoundsRanges <RestRanges... >;
340 using size_type = std::ptrdiff_t;
341 static const size_t Depth = Base::Depth + 1;
342 static const size_t DynamicNum = Base::DynamicNum + 1;
343 static const size_type CurrentRange = dynamic_range;
344 static const size_type TotalSize = dynamic_range;
345 const size_type m_bound;
346
347 BoundsRanges (const BoundsRanges&) = default;
348
349 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize())
350 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800351 Expects(0 <= *arr);
Neil MacIntosh68064d62015-11-03 19:17:11 -0800352 }
353
354 BoundsRanges() : m_bound(0) {}
355
356 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
357 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) :
358 Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize())
359 {}
360
361 template <typename T, size_t Dim = 0>
362 void serialize(T& arr) const
363 {
364 arr[Dim] = elementNum();
365 this->Base::template serialize<T, Dim + 1>(arr);
366 }
367
368 template <typename T, size_t Dim = 0>
369 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700370 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800371 const size_type index = this->Base::totalSize() * arr[Dim];
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800372 Expects(index < m_bound);
Neil MacIntosh68064d62015-11-03 19:17:11 -0800373 return index + this->Base::template linearize<T, Dim + 1>(arr);
374 }
375
376 template <typename T, size_t Dim = 0>
377 size_type contains(const T & arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700378 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800379 const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr);
380 if (last == -1)
381 return -1;
382 const ptrdiff_t cur = this->Base::totalSize() * arr[Dim];
383 return cur < m_bound ? cur + last : -1;
384 }
385
386 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700387 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800388 return m_bound;
389 }
390
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700391 size_type elementNum() const noexcept
392 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800393 return totalSize() / this->Base::totalSize();
394 }
395
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700396 size_type elementNum(size_t dim) const noexcept
397 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800398 if (dim > 0)
399 return this->Base::elementNum(dim - 1);
400 else
401 return elementNum();
402 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700403
Neil MacIntosh68064d62015-11-03 19:17:11 -0800404 bool operator == (const BoundsRanges & rhs) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700405 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800406 return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs);
407 }
408 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700409
Neil MacIntosh68064d62015-11-03 19:17:11 -0800410 template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges>
411 struct BoundsRanges <CurRange, RestRanges...> : BoundsRanges<RestRanges...>
412 {
413 using Base = BoundsRanges <RestRanges... >;
414 using size_type = std::ptrdiff_t;
415 static const size_t Depth = Base::Depth + 1;
416 static const size_t DynamicNum = Base::DynamicNum;
417 static const size_type CurrentRange = CurRange;
418 static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
419
420 BoundsRanges (const BoundsRanges&) = default;
421 BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) { }
422 BoundsRanges() = default;
423
424 template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges>
425 BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>&other, bool firstLevel = true) : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false)
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800426 {
427 Expects((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize());
428 (void)firstLevel;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800429 }
430
431 template <typename T, size_t Dim = 0>
432 void serialize(T& arr) const
433 {
434 arr[Dim] = elementNum();
435 this->Base::template serialize<T, Dim + 1>(arr);
436 }
437
438 template <typename T, size_t Dim = 0>
439 size_type linearize(const T& arr) const
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700440 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800441 Expects(arr[Dim] < CurrentRange); // Index is out of range
Neil MacIntosh68064d62015-11-03 19:17:11 -0800442 return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
443 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700444
Neil MacIntosh68064d62015-11-03 19:17:11 -0800445 template <typename T, size_t Dim = 0>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700446 size_type contains(const T& arr) const
447 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800448 if (arr[Dim] >= CurrentRange)
449 return -1;
450 const size_type last = this->Base::template contains<T, Dim + 1>(arr);
451 if (last == -1)
452 return -1;
453 return this->Base::totalSize() * arr[Dim] + last;
454 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700455
Neil MacIntosh68064d62015-11-03 19:17:11 -0800456 size_type totalSize() const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700457 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800458 return CurrentRange * this->Base::totalSize();
459 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700460
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700461 size_type elementNum() const noexcept
462 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800463 return CurrentRange;
464 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700465
Neil MacIntosh68064d62015-11-03 19:17:11 -0800466 size_type elementNum(size_t dim) const noexcept
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700467 {
Neil MacIntosh68064d62015-11-03 19:17:11 -0800468 if (dim > 0)
469 return this->Base::elementNum(dim - 1);
470 else
471 return elementNum();
472 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700473
Neil MacIntosh68064d62015-11-03 19:17:11 -0800474 bool operator== (const BoundsRanges& rhs) const noexcept
475 {
476 return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs);
477 }
478 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700479
Neil MacIntosh68064d62015-11-03 19:17:11 -0800480 template <typename SourceType, typename TargetType, size_t Rank>
481 struct BoundsRangeConvertible2;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700482
Neil MacIntosh68064d62015-11-03 19:17:11 -0800483 template <size_t Rank, typename SourceType, typename TargetType, typename Ret = BoundsRangeConvertible2<typename SourceType::Base, typename TargetType::Base, Rank>>
484 auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700485
Neil MacIntosh68064d62015-11-03 19:17:11 -0800486 template <size_t Rank, typename SourceType, typename TargetType>
487 auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type;
488
489 template <typename SourceType, typename TargetType, size_t Rank>
490 struct BoundsRangeConvertible2 : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
491 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
492 && (SourceType::CurrentRange == TargetType::CurrentRange || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
493 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700494
Neil MacIntosh68064d62015-11-03 19:17:11 -0800495 template <typename SourceType, typename TargetType>
496 struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700497
Neil MacIntosh68064d62015-11-03 19:17:11 -0800498 template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth>
499 struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(),
500 std::integral_constant<bool, SourceType::Depth == TargetType::Depth
501 && (!LessThan<SourceType::CurrentRange, TargetType::CurrentRange>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>()))
502 {};
503 template <typename SourceType, typename TargetType>
504 struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700505
Neil MacIntosh68064d62015-11-03 19:17:11 -0800506 template <typename TypeChain>
507 struct TypeListIndexer
508 {
509 const TypeChain & obj;
510 TypeListIndexer(const TypeChain & obj) :obj(obj){}
511 template<size_t N>
512 const TypeChain & getObj(std::true_type)
513 {
514 return obj;
515 }
516 template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base>
517 auto getObj(std::false_type) -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>())
518 {
519 return TypeListIndexer<MyBase>(static_cast<const MyBase &>(obj)).template get<N>();
520 }
521 template <size_t N>
522 auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, true>()))
523 {
524 return getObj<N - 1>(std::integral_constant<bool, N == 0>());
525 }
526 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700527
Neil MacIntosh68064d62015-11-03 19:17:11 -0800528 template <typename TypeChain>
529 TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain &obj)
530 {
531 return TypeListIndexer<TypeChain>(obj);
532 }
Anna Gringauzefdf86432015-10-14 10:46:22 -0700533
Neil MacIntosh68064d62015-11-03 19:17:11 -0800534 template <size_t Rank, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
535 constexpr Ret shift_left(const index<Rank>& other) noexcept
536 {
537 Ret ret{};
538 for (size_t i = 0; i < Rank - 1; ++i)
539 {
540 ret[i] = other[i + 1];
541 }
542 return ret;
543 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700544}
545
546template <typename IndexType>
547class bounds_iterator;
548
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700549template <std::ptrdiff_t... Ranges>
550class static_bounds
551{
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700552public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800553 static_bounds(const details::BoundsRanges<Ranges...>&) {
554 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700555};
556
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700557template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges>
558class static_bounds<FirstRange, RestRanges...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700559{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800560 using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700561
Neil MacIntosh68064d62015-11-03 19:17:11 -0800562 MyRanges m_ranges;
563 constexpr static_bounds(const MyRanges& range) : m_ranges(range)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700564 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -0800565
566 template <std::ptrdiff_t... OtherRanges>
567 friend class static_bounds;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700568
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700569public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800570 static const size_t rank = MyRanges::Depth;
571 static const size_t dynamic_rank = MyRanges::DynamicNum;
572 static const std::ptrdiff_t static_size = MyRanges::TotalSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700573
Neil MacIntosh68064d62015-11-03 19:17:11 -0800574 using size_type = std::ptrdiff_t;
575 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700576 using const_index_type = std::add_const_t<index_type>;
577 using iterator = bounds_iterator<const_index_type>;
578 using const_iterator = bounds_iterator<const_index_type>;
579 using difference_type = std::ptrdiff_t;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800580 using sliced_type = static_bounds<RestRanges...>;
581 using mapping_type = contiguous_mapping_tag;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700582
Neil MacIntosh68064d62015-11-03 19:17:11 -0800583 constexpr static_bounds(const static_bounds&) = default;
584
585 template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t<
586 details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700587 constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges)
588 {}
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700589
Neil MacIntosh68064d62015-11-03 19:17:11 -0800590 constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges((const std::ptrdiff_t*)il.begin())
591 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800592 // Size of the initializer list must match the rank of the array
593 Expects((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) || MyRanges::DynamicNum == il.size());
594 // Size of the range must be less than the max element of the size type
595 Expects(m_ranges.totalSize() <= PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -0800596 }
597
598 constexpr static_bounds() = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700599
Neil MacIntosh68064d62015-11-03 19:17:11 -0800600 constexpr static_bounds& operator=(const static_bounds& otherBounds)
601 {
602 new(&m_ranges) MyRanges (otherBounds.m_ranges);
603 return *this;
604 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700605
Neil MacIntosh68064d62015-11-03 19:17:11 -0800606 constexpr sliced_type slice() const noexcept
607 {
608 return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)};
609 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700610
Neil MacIntosh68064d62015-11-03 19:17:11 -0800611 constexpr size_type stride() const noexcept
612 {
613 return rank > 1 ? slice().size() : 1;
614 }
615
616 constexpr size_type size() const noexcept
617 {
618 return m_ranges.totalSize();
619 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700620
Neil MacIntosh68064d62015-11-03 19:17:11 -0800621 constexpr size_type total_size() const noexcept
622 {
623 return m_ranges.totalSize();
624 }
625
626 constexpr size_type linearize(const index_type & idx) const
627 {
628 return m_ranges.linearize(idx);
629 }
630
631 constexpr bool contains(const index_type& idx) const noexcept
632 {
633 return m_ranges.contains(idx) != -1;
634 }
635
636 constexpr size_type operator[](size_t index) const noexcept
637 {
638 return m_ranges.elementNum(index);
639 }
640
641 template <size_t Dim = 0>
642 constexpr size_type extent() const noexcept
643 {
644 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
645 return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
646 }
647
648 constexpr index_type index_bounds() const noexcept
649 {
650 size_type extents[rank] = {};
651 m_ranges.serialize(extents);
652 return{ extents };
653 }
654
655 template <std::ptrdiff_t... Ranges>
656 constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept
657 {
658 return this->size() == rhs.size();
659 }
660
661 template <std::ptrdiff_t... Ranges>
662 constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept
663 {
664 return !(*this == rhs);
665 }
666
667 constexpr const_iterator begin() const noexcept
668 {
669 return const_iterator(*this, index_type{});
670 }
671
672 constexpr const_iterator end() const noexcept
673 {
674 return const_iterator(*this, this->index_bounds());
675 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700676};
677
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700678template <size_t Rank>
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700679class strided_bounds
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700680{
Neil MacIntosh68064d62015-11-03 19:17:11 -0800681 template <size_t OtherRank>
682 friend class strided_bounds;
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700683
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700684public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800685 static const size_t rank = Rank;
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700686 using value_type = std::ptrdiff_t;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800687 using reference = std::add_lvalue_reference_t<value_type>;
688 using const_reference = std::add_const_t<reference>;
689 using size_type = value_type;
690 using difference_type = value_type;
691 using index_type = index<rank>;
Neil MacIntosha4fa2b32015-10-28 16:53:53 -0700692 using const_index_type = std::add_const_t<index_type>;
693 using iterator = bounds_iterator<const_index_type>;
694 using const_iterator = bounds_iterator<const_index_type>;
695 static const value_type dynamic_rank = rank;
Neil MacIntosh68064d62015-11-03 19:17:11 -0800696 static const value_type static_size = dynamic_range;
697 using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
698 using mapping_type = generalized_mapping_tag;
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700699
Neil MacIntosh68064d62015-11-03 19:17:11 -0800700 constexpr strided_bounds(const strided_bounds &) noexcept = default;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700701
Vladislav Yaroslavlev557e6692015-11-12 10:44:41 +0300702 constexpr strided_bounds & operator=(const strided_bounds &) noexcept = default;
703
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700704 constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
Neil MacIntoshace9ab92015-10-23 19:49:17 -0700705 : m_extents(values), m_strides(std::move(strides))
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700706 {}
707
Neil MacIntosh68064d62015-11-03 19:17:11 -0800708 constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
709 : m_extents(extents), m_strides(strides)
710 {}
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700711
Neil MacIntosh68064d62015-11-03 19:17:11 -0800712 constexpr index_type strides() const noexcept
713 {
714 return m_strides;
715 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700716
Neil MacIntosh68064d62015-11-03 19:17:11 -0800717 constexpr size_type total_size() const noexcept
718 {
719 size_type ret = 0;
720 for (size_t i = 0; i < rank; ++i)
721 {
722 ret += (m_extents[i] - 1) * m_strides[i];
723 }
724 return ret + 1;
725 }
726
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700727 constexpr size_type size() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800728 {
729 size_type ret = 1;
730 for (size_t i = 0; i < rank; ++i)
731 {
732 ret *= m_extents[i];
733 }
734 return ret;
735 }
736
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700737 constexpr bool contains(const index_type& idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800738 {
739 for (size_t i = 0; i < rank; ++i)
740 {
741 if (idx[i] < 0 || idx[i] >= m_extents[i])
742 return false;
743 }
744 return true;
745 }
Neil MacIntoshd0f09e72015-10-15 16:38:53 -0700746
Neil MacIntosh68064d62015-11-03 19:17:11 -0800747 constexpr size_type linearize(const index_type& idx) const noexcept
748 {
749 size_type ret = 0;
750 for (size_t i = 0; i < rank; i++)
751 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800752 Expects(idx[i] < m_extents[i]); // index is out of bounds of the array
Neil MacIntosh68064d62015-11-03 19:17:11 -0800753 ret += idx[i] * m_strides[i];
754 }
755 return ret;
756 }
757
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700758 constexpr size_type stride() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800759 {
760 return m_strides[0];
761 }
762
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700763 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800764 constexpr sliced_type slice() const
765 {
766 return{ details::shift_left(m_extents), details::shift_left(m_strides) };
767 }
768
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700769 template <size_t Dim = 0>
Neil MacIntosh68064d62015-11-03 19:17:11 -0800770 constexpr size_type extent() const noexcept
771 {
772 static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
773 return m_extents[Dim];
774 }
775
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700776 constexpr index_type index_bounds() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -0800777 {
778 return m_extents;
779 }
780 constexpr const_iterator begin() const noexcept
781 {
782 return const_iterator{ *this, index_type{} };
783 }
784
785 constexpr const_iterator end() const noexcept
786 {
787 return const_iterator{ *this, index_bounds() };
788 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700789
Anna Gringauze17ed5c32015-08-30 23:30:15 -0700790private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800791 index_type m_extents;
792 index_type m_strides;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700793};
794
795template <typename T>
796struct is_bounds : std::integral_constant<bool, false> {};
Neil MacIntoshf45fedb2015-10-15 14:29:35 -0700797template <std::ptrdiff_t... Ranges>
798struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> {};
799template <size_t Rank>
800struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700801
802template <typename IndexType>
Anna Gringauzea4654a42015-10-16 12:15:22 -0700803class bounds_iterator: public std::iterator<std::random_access_iterator_tag, IndexType>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700804{
805private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800806 using Base = std::iterator <std::random_access_iterator_tag, IndexType>;
Anna Gringauzea4654a42015-10-16 12:15:22 -0700807
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700808public:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800809 static const size_t rank = IndexType::rank;
810 using typename Base::reference;
811 using typename Base::pointer;
812 using typename Base::difference_type;
813 using typename Base::value_type;
814 using index_type = value_type;
815 using index_size_type = typename IndexType::value_type;
816 template <typename Bounds>
817 explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept
818 : boundary(bnd.index_bounds()), curr(std::move(curr))
819 {
820 static_assert(is_bounds<Bounds>::value, "Bounds type must be provided");
821 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700822
Neil MacIntosh68064d62015-11-03 19:17:11 -0800823 constexpr reference operator*() const noexcept
824 {
825 return curr;
826 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700827
Neil MacIntosh68064d62015-11-03 19:17:11 -0800828 constexpr pointer operator->() const noexcept
829 {
830 return &curr;
831 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700832
Neil MacIntosh68064d62015-11-03 19:17:11 -0800833 constexpr bounds_iterator& operator++() noexcept
834 {
835 for (size_t i = rank; i-- > 0;)
836 {
837 if (curr[i] < boundary[i] - 1)
838 {
839 curr[i]++;
840 return *this;
841 }
842 curr[i] = 0;
843 }
844 // If we're here we've wrapped over - set to past-the-end.
845 curr = boundary;
846 return *this;
847 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700848
Neil MacIntosh68064d62015-11-03 19:17:11 -0800849 constexpr bounds_iterator operator++(int) noexcept
850 {
851 auto ret = *this;
852 ++(*this);
853 return ret;
854 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700855
Neil MacIntosh68064d62015-11-03 19:17:11 -0800856 constexpr bounds_iterator& operator--() noexcept
857 {
858 if (!less(curr, boundary))
859 {
860 // if at the past-the-end, set to last element
861 for (size_t i = 0; i < rank; ++i)
862 {
863 curr[i] = boundary[i] - 1;
864 }
865 return *this;
866 }
867 for (size_t i = rank; i-- > 0;)
868 {
869 if (curr[i] >= 1)
870 {
871 curr[i]--;
872 return *this;
873 }
874 curr[i] = boundary[i] - 1;
875 }
876 // If we're here the preconditions were violated
877 // "pre: there exists s such that r == ++s"
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800878 Expects(false);
Neil MacIntosh68064d62015-11-03 19:17:11 -0800879 return *this;
880 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700881
Neil MacIntosh68064d62015-11-03 19:17:11 -0800882 constexpr bounds_iterator operator--(int) noexcept
883 {
884 auto ret = *this;
885 --(*this);
886 return ret;
887 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700888
Neil MacIntosh68064d62015-11-03 19:17:11 -0800889 constexpr bounds_iterator operator+(difference_type n) const noexcept
890 {
891 bounds_iterator ret{ *this };
892 return ret += n;
893 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700894
Neil MacIntosh68064d62015-11-03 19:17:11 -0800895 constexpr bounds_iterator& operator+=(difference_type n) noexcept
896 {
897 auto linear_idx = linearize(curr) + n;
898 std::remove_const_t<value_type> stride = 0;
899 stride[rank - 1] = 1;
900 for (size_t i = rank - 1; i-- > 0;)
901 {
902 stride[i] = stride[i + 1] * boundary[i + 1];
903 }
904 for (size_t i = 0; i < rank; ++i)
905 {
906 curr[i] = linear_idx / stride[i];
907 linear_idx = linear_idx % stride[i];
908 }
Neil MacIntoshd13f6da2015-11-20 16:03:00 -0800909 //index is out of bounds of the array
910 Expects(!less(curr, index_type{}) && !less(boundary, curr));
Neil MacIntosh68064d62015-11-03 19:17:11 -0800911 return *this;
912 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700913
Neil MacIntosh68064d62015-11-03 19:17:11 -0800914 constexpr bounds_iterator operator-(difference_type n) const noexcept
915 {
916 bounds_iterator ret{ *this };
917 return ret -= n;
918 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700919
Neil MacIntosh68064d62015-11-03 19:17:11 -0800920 constexpr bounds_iterator& operator-=(difference_type n) noexcept
921 {
922 return *this += -n;
923 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700924
Neil MacIntosh68064d62015-11-03 19:17:11 -0800925 constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept
926 {
927 return linearize(curr) - linearize(rhs.curr);
928 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700929
Neil MacIntosh68064d62015-11-03 19:17:11 -0800930 constexpr value_type operator[](difference_type n) const noexcept
931 {
932 return *(*this + n);
933 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700934
Neil MacIntosh68064d62015-11-03 19:17:11 -0800935 constexpr bool operator==(const bounds_iterator& rhs) const noexcept
936 {
937 return curr == rhs.curr;
938 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700939
Neil MacIntosh68064d62015-11-03 19:17:11 -0800940 constexpr bool operator!=(const bounds_iterator& rhs) const noexcept
941 {
942 return !(*this == rhs);
943 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700944
Neil MacIntosh68064d62015-11-03 19:17:11 -0800945 constexpr bool operator<(const bounds_iterator& rhs) const noexcept
946 {
947 return less(curr, rhs.curr);
948 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700949
Neil MacIntosh68064d62015-11-03 19:17:11 -0800950 constexpr bool operator<=(const bounds_iterator& rhs) const noexcept
951 {
952 return !(rhs < *this);
953 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700954
Neil MacIntosh68064d62015-11-03 19:17:11 -0800955 constexpr bool operator>(const bounds_iterator& rhs) const noexcept
956 {
957 return rhs < *this;
958 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700959
Neil MacIntosh68064d62015-11-03 19:17:11 -0800960 constexpr bool operator>=(const bounds_iterator& rhs) const noexcept
961 {
962 return !(rhs > *this);
963 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700964
Neil MacIntosh68064d62015-11-03 19:17:11 -0800965 void swap(bounds_iterator& rhs) noexcept
966 {
967 std::swap(boundary, rhs.boundary);
968 std::swap(curr, rhs.curr);
969 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -0700970private:
Neil MacIntosh68064d62015-11-03 19:17:11 -0800971 constexpr bool less(index_type& one, index_type& other) const noexcept
972 {
973 for (size_t i = 0; i < rank; ++i)
974 {
975 if (one[i] < other[i])
976 return true;
977 }
978 return false;
979 }
Anna Gringauzea4654a42015-10-16 12:15:22 -0700980
Neil MacIntosh68064d62015-11-03 19:17:11 -0800981 constexpr index_size_type linearize(const value_type& idx) const noexcept
982 {
983 // TODO: Smarter impl.
984 // Check if past-the-end
985 index_size_type multiplier = 1;
986 index_size_type res = 0;
987 if (!less(idx, boundary))
988 {
989 res = 1;
990 for (size_t i = rank; i-- > 0;)
991 {
992 res += (idx[i] - 1) * multiplier;
993 multiplier *= boundary[i];
994 }
995 }
996 else
997 {
998 for (size_t i = rank; i-- > 0;)
999 {
1000 res += idx[i] * multiplier;
1001 multiplier *= boundary[i];
1002 }
1003 }
1004 return res;
1005 }
Anna Gringauzea4654a42015-10-16 12:15:22 -07001006
Neil MacIntosh68064d62015-11-03 19:17:11 -08001007 value_type boundary;
1008 std::remove_const_t<value_type> curr;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001009};
1010
1011template <typename IndexType>
Neil MacIntoshd5316802015-09-30 21:54:08 -07001012bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, const bounds_iterator<IndexType>& rhs) noexcept
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001013{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001014 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001015}
1016
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001017//
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001018// begin definitions of basic_span
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001019//
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001020namespace details
1021{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001022 template <typename Bounds>
1023 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
1024 {
1025 return bnd.strides();
1026 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001027
Neil MacIntosh68064d62015-11-03 19:17:11 -08001028 // Make a stride vector from bounds, assuming contiguous memory.
1029 template <typename Bounds>
1030 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
1031 {
1032 auto extents = bnd.index_bounds();
1033 typename Bounds::size_type stride[Bounds::rank] = {};
Anna Gringauzedb384972015-10-05 12:34:23 -07001034
Neil MacIntosh68064d62015-11-03 19:17:11 -08001035 stride[Bounds::rank - 1] = 1;
1036 for (size_t i = 1; i < Bounds::rank; ++i)
1037 {
1038 stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i];
1039 }
1040 return{ stride };
1041 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001042
Neil MacIntosh68064d62015-11-03 19:17:11 -08001043 template <typename BoundsSrc, typename BoundsDest>
1044 void verifyBoundsReshape(const BoundsSrc &src, const BoundsDest &dest)
1045 {
1046 static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, "The src type and dest type must be bounds");
1047 static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, "The source type must be a contiguous bounds");
1048 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");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001049 Expects(src.size() == dest.size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001050 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001051
1052
1053} // namespace details
1054
Anna Gringauze8aa42482015-11-11 12:41:11 -08001055template <typename Span>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001056class contiguous_span_iterator;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001057template <typename Span>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001058class general_span_iterator;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001059enum class byte : std::uint8_t {};
1060
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001061template <std::ptrdiff_t DimSize = dynamic_range>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001062struct dim
1063{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001064 static const std::ptrdiff_t value = DimSize;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001065};
1066template <>
1067struct dim<dynamic_range>
1068{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001069 static const std::ptrdiff_t value = dynamic_range;
1070 const std::ptrdiff_t dvalue;
1071 dim(std::ptrdiff_t size) : dvalue(size) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001072};
1073
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001074template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, std::ptrdiff_t... RestDimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001075class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001076
1077template <typename ValueType, size_t Rank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001078class strided_span;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001079
1080namespace details
1081{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001082 template <typename T, typename = std::true_type>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001083 struct SpanTypeTraits
Neil MacIntosh68064d62015-11-03 19:17:11 -08001084 {
1085 using value_type = T;
1086 using size_type = size_t;
1087 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001088
Neil MacIntosh68064d62015-11-03 19:17:11 -08001089 template <typename Traits>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001090 struct SpanTypeTraits<Traits, typename std::is_reference<typename Traits::span_traits &>::type>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001091 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001092 using value_type = typename Traits::span_traits::value_type;
1093 using size_type = typename Traits::span_traits::size_type;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001094 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001095
Neil MacIntosh68064d62015-11-03 19:17:11 -08001096 template <typename T, std::ptrdiff_t... Ranks>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001097 struct SpanArrayTraits {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001098 using type = span<T, Ranks...>;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001099 using value_type = T;
1100 using bounds_type = static_bounds<Ranks...>;
1101 using pointer = T*;
1102 using reference = T&;
1103 };
1104 template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001105 struct SpanArrayTraits<T[N], Ranks...> : SpanArrayTraits<T, Ranks..., N> {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001106
Neil MacIntosh68064d62015-11-03 19:17:11 -08001107 template <typename BoundsType>
1108 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
1109 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001110 Expects(totalSize <= PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001111 return BoundsType{totalSize};
1112 }
1113 template <typename BoundsType>
1114 BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size
1115 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001116 Expects(BoundsType::static_size == totalSize);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001117 return {};
1118 }
1119 template <typename BoundsType>
1120 BoundsType newBoundsHelper(std::ptrdiff_t totalSize)
1121 {
1122 static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1");
1123 return newBoundsHelperImpl<BoundsType>(totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>());
1124 }
1125
1126 struct Sep{};
1127
1128 template <typename T, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001129 T static_as_span_helper(Sep, Args... args)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001130 {
1131 return T{static_cast<typename T::size_type>(args)...};
1132 }
1133 template <typename T, typename Arg, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001134 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 -08001135 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001136 return static_as_span_helper<T>(args...);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001137 }
1138 template <typename T, typename... Args>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001139 T static_as_span_helper(dim<dynamic_range> val, Args ... args)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001140 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001141 return static_as_span_helper<T>(args..., val.dvalue);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001142 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001143
Neil MacIntosh68064d62015-11-03 19:17:11 -08001144 template <typename ...Dimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001145 struct static_as_span_static_bounds_helper
Neil MacIntosh68064d62015-11-03 19:17:11 -08001146 {
1147 using type = static_bounds<(Dimensions::value)...>;
1148 };
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001149
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001150 template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001151 struct is_span_oracle : std::false_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001152 {};
1153
1154 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001155 struct is_span_oracle<span<ValueType, FirstDimension, RestDimensions...>> : std::true_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001156 {};
1157
1158 template <typename ValueType, std::ptrdiff_t Rank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001159 struct is_span_oracle<strided_span<ValueType, Rank>> : std::true_type
Neil MacIntosh68064d62015-11-03 19:17:11 -08001160 {};
1161
1162 template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001163 struct is_span : is_span_oracle<std::remove_cv_t<T>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001164 {};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001165
1166}
1167
1168
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001169template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001170class span
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001171{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001172 template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001173 friend class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001174
Anna Gringauze8aa42482015-11-11 12:41:11 -08001175public:
Anna Gringauzef5100252015-11-12 12:48:49 -08001176 using bounds_type = static_bounds<FirstDimension, RestDimensions...>;
1177 static const size_t rank = bounds_type::rank;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001178 using size_type = typename bounds_type::size_type;
1179 using index_type = typename bounds_type::index_type;
1180 using value_type = ValueType;
1181 using const_value_type = std::add_const_t<value_type>;
Anna Gringauzef5100252015-11-12 12:48:49 -08001182 using pointer = std::add_pointer_t<value_type>;
1183 using reference = std::add_lvalue_reference_t<value_type>;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001184 using iterator = contiguous_span_iterator<span>;
1185 using const_span = span<const_value_type, FirstDimension, RestDimensions...>;
1186 using const_iterator = contiguous_span_iterator<const_span>;
1187 using reverse_iterator = std::reverse_iterator<iterator>;
1188 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1189 using sliced_type = std::conditional_t<rank == 1, value_type, span<value_type, RestDimensions...>>;
1190
1191private:
1192 pointer m_pdata;
1193 bounds_type m_bounds;
1194
1195 friend iterator;
1196 friend const_iterator;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001197
1198public:
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001199
Anna Gringauzef5100252015-11-12 12:48:49 -08001200 constexpr span(pointer data, bounds_type bounds) noexcept
1201 : m_pdata(data), m_bounds(std::move(bounds))
Anna Gringauze8aa42482015-11-11 12:41:11 -08001202 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001203 Expects((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
Anna Gringauze8aa42482015-11-11 12:41:11 -08001204 }
1205
Anna Gringauzef5100252015-11-12 12:48:49 -08001206 constexpr span(pointer ptr, size_type size) noexcept
1207 : span(ptr, bounds_type{ size })
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001208 {}
1209
Anna Gringauzef5100252015-11-12 12:48:49 -08001210 constexpr span(std::nullptr_t) noexcept
1211 : span(nullptr, bounds_type{})
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001212 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001213
Anna Gringauzef5100252015-11-12 12:48:49 -08001214 constexpr span(std::nullptr_t, size_type size) noexcept
1215 : span(nullptr, bounds_type{})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001216 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001217 Expects(size == 0);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001218 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001219
Neil MacIntosh68064d62015-11-03 19:17:11 -08001220 // default
1221 template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
Anna Gringauzef5100252015-11-12 12:48:49 -08001222 constexpr span() noexcept
1223 : span(nullptr, bounds_type())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001224 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001225
Neil MacIntosh68064d62015-11-03 19:17:11 -08001226 // 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 -08001227 template <typename T, typename Helper = details::SpanArrayTraits<T, dynamic_range>,
1228 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 -08001229 /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], value_type (*)[]>::value>*/
1230 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001231 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 -08001232 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001233
Neil MacIntosh68064d62015-11-03 19:17:11 -08001234 // from n-dimensions static array
Anna Gringauze8aa42482015-11-11 12:41:11 -08001235 template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
1236 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
1237 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001238 constexpr span (T (&arr)[N]) : span(reinterpret_cast<pointer>(arr), typename Helper::bounds_type())
Neil MacIntosh68064d62015-11-03 19:17:11 -08001239 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001240
Neil MacIntosh68064d62015-11-03 19:17:11 -08001241 // from n-dimensions static array with size
Anna Gringauze8aa42482015-11-11 12:41:11 -08001242 template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
1243 typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001244 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001245 constexpr span(T(&arr)[N], size_type size) : span(arr, typename Helper::bounds_type{size})
Neil MacIntosh68064d62015-11-03 19:17:11 -08001246 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001247 Expects(size <= N);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001248 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001249
Neil MacIntosh68064d62015-11-03 19:17:11 -08001250 // from std array
1251 template <size_t N,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001252 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001253 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001254 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 -08001255 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001256
Neil MacIntosh68064d62015-11-03 19:17:11 -08001257 template <size_t N,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001258 typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001259 && std::is_const<value_type>::value>
1260 >
Anna Gringauze8aa42482015-11-11 12:41:11 -08001261 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 -08001262 {}
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001263
Neil MacIntosh68064d62015-11-03 19:17:11 -08001264 // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
1265 template <typename Ptr,
1266 typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
Anna Gringauze8aa42482015-11-11 12:41:11 -08001267 && details::LessThan<bounds_type::dynamic_rank, 2>::value>
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001268 > // remove literal 0 case
Anna Gringauze8aa42482015-11-11 12:41:11 -08001269 constexpr span (pointer begin, Ptr end) : span(begin, details::newBoundsHelper<bounds_type>(static_cast<pointer>(end) - begin))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001270 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001271
Neil MacIntosh68064d62015-11-03 19:17:11 -08001272 // from containers. It must has .size() and .data() two function signatures
1273 template <typename Cont, typename DataType = typename Cont::value_type,
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001274 typename Dummy = std::enable_if_t<!details::is_span<Cont>::value
Anna Gringauze8aa42482015-11-11 12:41:11 -08001275 && std::is_convertible<DataType (*)[], value_type (*)[]>::value
Neil MacIntosh68064d62015-11-03 19:17:11 -08001276 && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
1277 >
Matus Chochlikda75d0e2015-11-18 17:43:59 +01001278 constexpr span (Cont& cont) : span(static_cast<pointer>(cont.data()), details::newBoundsHelper<bounds_type>(static_cast<size_type>(cont.size())))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001279 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001280
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001281 constexpr span(const span &) = default;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001282
Neil MacIntosh68064d62015-11-03 19:17:11 -08001283 // convertible
1284 template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
Anna Gringauze8aa42482015-11-11 12:41:11 -08001285 typename OtherBounds = static_bounds<OtherDimensions...>,
1286 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 -08001287 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001288 constexpr span(const span<OtherValueType, OtherDimensions...>& other) noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001289 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001290 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001291
Neil MacIntosh68064d62015-11-03 19:17:11 -08001292 // reshape
Neil MacIntosh14d50a62015-11-03 12:44:09 -08001293 // DimCount here is a workaround for a bug in MSVC 2015
Anna Gringauzef5100252015-11-12 12:48:49 -08001294 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 -08001295 constexpr span<ValueType, Dimensions2::value...> as_span(Dimensions2... dims)
Neil MacIntosh68064d62015-11-03 19:17:11 -08001296 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001297 using BoundsType = typename span<ValueType, (Dimensions2::value)...>::bounds_type;
1298 auto tobounds = details::static_as_span_helper<BoundsType>(dims..., details::Sep{});
Neil MacIntosh68064d62015-11-03 19:17:11 -08001299 details::verifyBoundsReshape(this->bounds(), tobounds);
1300 return {this->data(), tobounds};
1301 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001302
Neil MacIntosh68064d62015-11-03 19:17:11 -08001303 // to bytes array
1304 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001305 auto as_bytes() const noexcept -> span<const byte>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001306 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001307 static_assert(Enabled, "The value_type of span must be standarded layout");
Neil MacIntosh68064d62015-11-03 19:17:11 -08001308 return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
1309 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001310
Neil MacIntosh68064d62015-11-03 19:17:11 -08001311 template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001312 auto as_writeable_bytes() const noexcept -> span<byte>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001313 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001314 static_assert(Enabled, "The value_type of span must be standarded layout");
Neil MacIntosh68064d62015-11-03 19:17:11 -08001315 return { reinterpret_cast<byte*>(this->data()), this->bytes() };
1316 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001317
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001318 // from bytes array
Neil MacIntosh68064d62015-11-03 19:17:11 -08001319 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 -08001320 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 -08001321 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001322 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 -08001323 "Target type must be standard layout and its size must match the byte array size");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001324 Expects((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001325 return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1326 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001327
Neil MacIntosh68064d62015-11-03 19:17:11 -08001328 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 -08001329 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 -08001330 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001331 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 -08001332 "Target type must be standard layout and its size must match the byte array size");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001333 Expects((this->bytes() % sizeof(U)) == 0);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001334 return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
1335 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001336
Neil MacIntosh68064d62015-11-03 19:17:11 -08001337 // section on linear space
1338 template<std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001339 constexpr span<ValueType, Count> first() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001340 {
1341 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001342 Expects(bounds_type::static_size != dynamic_range || Count <= this->size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001343 return { this->data(), Count };
1344 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001345
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001346 constexpr span<ValueType, dynamic_range> first(size_type count) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001347 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001348 Expects(count <= this->size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001349 return { this->data(), count };
1350 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001351
Neil MacIntosh68064d62015-11-03 19:17:11 -08001352 template<std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001353 constexpr span<ValueType, Count> last() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001354 {
1355 static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001356 Expects(bounds_type::static_size != dynamic_range || Count <= this->size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001357 return { this->data() + this->size() - Count, Count };
1358 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001359
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001360 constexpr span<ValueType, dynamic_range> last(size_type count) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001361 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001362 Expects(count <= this->size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001363 return { this->data() + this->size() - count, count };
1364 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001365
Neil MacIntosh68064d62015-11-03 19:17:11 -08001366 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001367 constexpr span<ValueType, Count> sub() const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001368 {
1369 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");
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001370 Expects(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size()));
Neil MacIntosh68064d62015-11-03 19:17:11 -08001371 return { this->data() + Offset, Count };
1372 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001373
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001374 constexpr span<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001375 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001376 Expects((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size()));
Neil MacIntosh68064d62015-11-03 19:17:11 -08001377 return { this->data() + offset, count == dynamic_range ? this->length() - offset : count };
1378 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001379
Neil MacIntosh68064d62015-11-03 19:17:11 -08001380 // size
1381 constexpr size_type length() const noexcept
1382 {
1383 return this->size();
1384 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001385
Neil MacIntosh68064d62015-11-03 19:17:11 -08001386 constexpr size_type used_length() const noexcept
1387 {
1388 return length();
1389 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001390
Neil MacIntosh68064d62015-11-03 19:17:11 -08001391 constexpr size_type bytes() const noexcept
1392 {
1393 return sizeof(value_type) * this->size();
1394 }
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001395
Neil MacIntosh68064d62015-11-03 19:17:11 -08001396 constexpr size_type used_bytes() const noexcept
1397 {
1398 return bytes();
1399 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001400
Neil MacIntosh68064d62015-11-03 19:17:11 -08001401 // section
Anna Gringauzef5100252015-11-12 12:48:49 -08001402 constexpr strided_span<ValueType, rank> section(index_type origin, index_type extents) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001403 {
1404 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauze8aa42482015-11-11 12:41:11 -08001405 return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(bounds())} };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001406 }
1407
Anna Gringauzef5100252015-11-12 12:48:49 -08001408 constexpr reference operator[](const index_type& idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001409 {
Anna Gringauze8aa42482015-11-11 12:41:11 -08001410 return m_pdata[m_bounds.linearize(idx)];
Neil MacIntosh68064d62015-11-03 19:17:11 -08001411 }
1412
Anna Gringauze8aa42482015-11-11 12:41:11 -08001413 template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
Anna Gringauzef5100252015-11-12 12:48:49 -08001414 constexpr Ret operator[](size_type idx) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001415 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001416 Expects(idx < m_bounds.size()); // index is out of bounds of the array
Anna Gringauze8aa42482015-11-11 12:41:11 -08001417 const size_type ridx = idx * m_bounds.stride();
1418
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001419 // index is out of bounds of the underlying data
1420 Expects(ridx < m_bounds.total_size());
Anna Gringauze8aa42482015-11-11 12:41:11 -08001421 return Ret{ m_pdata + ridx, m_bounds.slice() };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001422 }
Neil MacIntosh9f9fad92015-08-27 18:13:49 -07001423
Anna Gringauze8aa42482015-11-11 12:41:11 -08001424 constexpr bounds_type bounds() const noexcept
1425 {
1426 return m_bounds;
1427 }
1428
1429 template <size_t Dim = 0>
1430 constexpr size_type extent() const noexcept
1431 {
1432 static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
1433 return m_bounds.template extent<Dim>();
1434 }
1435
1436 constexpr size_type size() const noexcept
1437 {
1438 return m_bounds.size();
1439 }
1440
1441 constexpr pointer data() const noexcept
1442 {
1443 return m_pdata;
1444 }
1445
Matus Chochlik73ec6882015-11-19 10:27:08 +01001446 constexpr explicit operator bool() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001447 {
1448 return m_pdata != nullptr;
1449 }
1450
Anna Gringauzef5100252015-11-12 12:48:49 -08001451 constexpr iterator begin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001452 {
1453 return iterator{ this, true };
1454 }
1455
Anna Gringauzef5100252015-11-12 12:48:49 -08001456 constexpr iterator end() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001457 {
1458 return iterator{ this, false };
1459 }
1460
Anna Gringauzef5100252015-11-12 12:48:49 -08001461 constexpr const_iterator cbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001462 {
1463 return const_iterator{ reinterpret_cast<const const_span*>(this), true };
1464 }
1465
Anna Gringauzef5100252015-11-12 12:48:49 -08001466 constexpr const_iterator cend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001467 {
1468 return const_iterator{ reinterpret_cast<const const_span*>(this), false };
1469 }
1470
Anna Gringauzef5100252015-11-12 12:48:49 -08001471 constexpr reverse_iterator rbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001472 {
1473 return reverse_iterator{ end() };
1474 }
1475
Anna Gringauzef5100252015-11-12 12:48:49 -08001476 constexpr reverse_iterator rend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001477 {
1478 return reverse_iterator{ begin() };
1479 }
1480
Anna Gringauzef5100252015-11-12 12:48:49 -08001481 constexpr const_reverse_iterator crbegin() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001482 {
1483 return const_reverse_iterator{ cend() };
1484 }
1485
Anna Gringauzef5100252015-11-12 12:48:49 -08001486 constexpr const_reverse_iterator crend() const noexcept
Anna Gringauze8aa42482015-11-11 12:41:11 -08001487 {
1488 return const_reverse_iterator{ cbegin() };
1489 }
1490
1491 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>>
1492 constexpr bool operator== (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1493 {
1494 return m_bounds.size() == other.m_bounds.size() &&
1495 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
1496 }
1497
1498 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>>
1499 constexpr bool operator!= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1500 {
1501 return !(*this == other);
1502 }
1503
1504 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>>
1505 constexpr bool operator< (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1506 {
1507 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1508 }
1509
1510 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>>
1511 constexpr bool operator<= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1512 {
1513 return !(other < *this);
1514 }
1515
1516 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>>
1517 constexpr bool operator> (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1518 {
1519 return (other < *this);
1520 }
1521
1522 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>>
1523 constexpr bool operator>= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
1524 {
1525 return !(*this < other);
1526 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001527};
1528
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001529template <typename T, std::ptrdiff_t... Dimensions>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001530constexpr auto as_span(T* const& ptr, dim<Dimensions>... args) -> span<std::remove_all_extents_t<T>, Dimensions...>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001531{
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001532 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 -07001533}
1534
1535template <typename T>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001536constexpr auto as_span (T* arr, std::ptrdiff_t len) -> typename details::SpanArrayTraits<T, dynamic_range>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001537{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001538 return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001539}
1540
1541template <typename T, size_t N>
Anna Gringauze8aa42482015-11-11 12:41:11 -08001542constexpr auto as_span (T (&arr)[N]) -> typename details::SpanArrayTraits<T, N>::type
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001543{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001544 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001545}
1546
1547template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001548constexpr span<const T, N> as_span(const std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001549{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001550 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001551}
1552
1553template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001554constexpr span<const T, N> as_span(const std::array<T, N> &&) = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001555
1556template <typename T, size_t N>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001557constexpr span<T, N> as_span(std::array<T, N> &arr)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001558{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001559 return {arr};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001560}
1561
1562template <typename T>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001563constexpr span<T, dynamic_range> as_span(T *begin, T *end)
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001564{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001565 return {begin, end};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001566}
1567
1568template <typename Cont>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001569constexpr auto as_span(Cont &arr) -> std::enable_if_t<!details::is_span<std::decay_t<Cont>>::value,
1570 span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001571{
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001572 Expects(arr.size() < PTRDIFF_MAX);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001573 return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001574}
1575
1576template <typename Cont>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001577constexpr auto as_span(Cont &&arr) -> std::enable_if_t<!details::is_span<std::decay_t<Cont>>::value,
1578 span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001579
Elron A. Yelline4d8d352015-11-20 17:50:02 -05001580// from basic_string which doesn't have nonconst .data() member like other contiguous containers
1581template <typename CharT, typename Traits, typename Allocator>
1582constexpr auto as_span(std::basic_string<CharT, Traits, Allocator> &str) -> span<CharT, dynamic_range>
1583{
Neil MacIntoshd5057372015-11-20 17:14:21 -08001584 Expects(str.size() < PTRDIFF_MAX);
Elron A. Yelline4d8d352015-11-20 17:50:02 -05001585 return {&str[0], static_cast<std::ptrdiff_t>(str.size())};
1586}
1587
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001588template <typename ValueType, size_t Rank>
Anna Gringauzef5100252015-11-12 12:48:49 -08001589class strided_span
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001590{
Anna Gringauzef5100252015-11-12 12:48:49 -08001591public:
1592 using bounds_type = strided_bounds<Rank>;
1593 using size_type = typename bounds_type::size_type;
1594 using index_type = typename bounds_type::index_type;
1595 using value_type = ValueType;
1596 using const_value_type = std::add_const_t<value_type>;
1597 using pointer = std::add_pointer_t<value_type>;
1598 using reference = std::add_lvalue_reference_t<value_type>;
1599 using iterator = general_span_iterator<strided_span>;
1600 using const_strided_span = strided_span<const_value_type, Rank>;
1601 using const_iterator = general_span_iterator<const_strided_span>;
1602 using reverse_iterator = std::reverse_iterator<iterator>;
1603 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1604 using sliced_type = std::conditional_t<Rank == 1, value_type, strided_span<value_type, Rank-1>>;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001605
Anna Gringauzef5100252015-11-12 12:48:49 -08001606private:
1607 pointer m_pdata;
1608 bounds_type m_bounds;
1609
1610 friend iterator;
1611 friend const_iterator;
1612 template <typename OtherValueType, size_t OtherRank>
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001613 friend class strided_span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001614
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001615public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001616 // from raw data
Anna Gringauzef5100252015-11-12 12:48:49 -08001617 constexpr strided_span(pointer ptr, size_type size, bounds_type bounds)
1618 : m_pdata(ptr), m_bounds(std::move(bounds))
Neil MacIntosh68064d62015-11-03 19:17:11 -08001619 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001620 Expects((m_bounds.size() > 0 && ptr != nullptr) || m_bounds.size() == 0);
1621 // Bounds cross data boundaries
1622 Expects(this->bounds().total_size() <= size);
1623 (void)size;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001624 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001625
Anna Gringauzef5100252015-11-12 12:48:49 -08001626 // from static array of size N
1627 template<size_type N>
1628 constexpr strided_span(value_type(&values)[N], bounds_type bounds) : strided_span(values, N, std::move(bounds))
1629 {}
1630
Neil MacIntosh68064d62015-11-03 19:17:11 -08001631 // from array view
Anna Gringauzec95eb572015-11-19 12:02:06 -08001632 template <typename OtherValueType, std::ptrdiff_t... Dimensions,
1633 bool Enabled1 = (sizeof...(Dimensions) == Rank),
1634 bool Enabled2 = std::is_convertible<OtherValueType*, ValueType*>::value,
1635 typename Dummy = std::enable_if_t<Enabled1 && Enabled2>
1636 >
1637 constexpr strided_span(span<OtherValueType, Dimensions...> av, bounds_type bounds) : strided_span(av.data(), av.bounds().total_size(), std::move(bounds))
Anna Gringauzef5100252015-11-12 12:48:49 -08001638 {}
Neil MacIntosh68064d62015-11-03 19:17:11 -08001639
1640 // convertible
1641 template <typename OtherValueType,
Anna Gringauzef5100252015-11-12 12:48:49 -08001642 typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001643 >
Anna Gringauzef5100252015-11-12 12:48:49 -08001644 constexpr strided_span(const strided_span<OtherValueType, Rank>& other)
1645 : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001646 {}
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001647
Neil MacIntosh68064d62015-11-03 19:17:11 -08001648 // convert from bytes
Anna Gringauzef5100252015-11-12 12:48:49 -08001649 template <typename OtherValueType>
1650 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 -08001651 {
1652 static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
1653 auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001654
Neil MacIntosh68064d62015-11-03 19:17:11 -08001655 size_type size = this->bounds().total_size() / d;
1656 return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
1657 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001658
Anna Gringauzef5100252015-11-12 12:48:49 -08001659 constexpr strided_span section(index_type origin, index_type extents) const
Neil MacIntosh68064d62015-11-03 19:17:11 -08001660 {
1661 size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
Anna Gringauzef5100252015-11-12 12:48:49 -08001662 return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(bounds())}};
Neil MacIntosh68064d62015-11-03 19:17:11 -08001663 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001664
Neil MacIntosh68064d62015-11-03 19:17:11 -08001665 constexpr reference operator[](const index_type& idx) const
1666 {
Anna Gringauzef5100252015-11-12 12:48:49 -08001667 return m_pdata[m_bounds.linearize(idx)];
Neil MacIntosh68064d62015-11-03 19:17:11 -08001668 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001669
Anna Gringauzef5100252015-11-12 12:48:49 -08001670 template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
1671 constexpr Ret operator[](size_type idx) const
Neil MacIntosh68064d62015-11-03 19:17:11 -08001672 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001673 Expects(idx < m_bounds.size()); // index is out of bounds of the array
Anna Gringauzef5100252015-11-12 12:48:49 -08001674 const size_type ridx = idx * m_bounds.stride();
1675
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001676 // index is out of bounds of the underlying data
1677 Expects(ridx < m_bounds.total_size());
Anna Gringauzef5100252015-11-12 12:48:49 -08001678 return{ m_pdata + ridx, m_bounds.slice().total_size(), m_bounds.slice() };
1679 }
1680
1681 constexpr bounds_type bounds() const noexcept
1682 {
1683 return m_bounds;
1684 }
1685
1686 template <size_t Dim = 0>
1687 constexpr size_type extent() const noexcept
1688 {
1689 static_assert(Dim < Rank, "dimension should be less than Rank (dimension count starts from 0)");
1690 return m_bounds.template extent<Dim>();
1691 }
1692
1693 constexpr size_type size() const noexcept
1694 {
1695 return m_bounds.size();
1696 }
1697
1698 constexpr pointer data() const noexcept
1699 {
1700 return m_pdata;
1701 }
1702
Matus Chochlik73ec6882015-11-19 10:27:08 +01001703 constexpr explicit operator bool() const noexcept
Anna Gringauzef5100252015-11-12 12:48:49 -08001704 {
1705 return m_pdata != nullptr;
1706 }
1707
1708 constexpr iterator begin() const
1709 {
1710 return iterator{ this, true };
1711 }
1712
1713 constexpr iterator end() const
1714 {
1715 return iterator{ this, false };
1716 }
1717
1718 constexpr const_iterator cbegin() const
1719 {
1720 return const_iterator{ reinterpret_cast<const const_strided_span*>(this), true };
1721 }
1722
1723 constexpr const_iterator cend() const
1724 {
1725 return const_iterator{ reinterpret_cast<const const_strided_span*>(this), false };
1726 }
1727
1728 constexpr reverse_iterator rbegin() const
1729 {
1730 return reverse_iterator{ end() };
1731 }
1732
1733 constexpr reverse_iterator rend() const
1734 {
1735 return reverse_iterator{ begin() };
1736 }
1737
1738 constexpr const_reverse_iterator crbegin() const
1739 {
1740 return const_reverse_iterator{ cend() };
1741 }
1742
1743 constexpr const_reverse_iterator crend() const
1744 {
1745 return const_reverse_iterator{ cbegin() };
1746 }
1747
1748 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>>
1749 constexpr bool operator== (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1750 {
1751 return m_bounds.size() == other.m_bounds.size() &&
1752 (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
1753 }
1754
1755 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>>
1756 constexpr bool operator!= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1757 {
1758 return !(*this == other);
1759 }
1760
1761 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>>
1762 constexpr bool operator< (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1763 {
1764 return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
1765 }
1766
1767 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>>
1768 constexpr bool operator<= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1769 {
1770 return !(other < *this);
1771 }
1772
1773 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>>
1774 constexpr bool operator> (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1775 {
1776 return (other < *this);
1777 }
1778
1779 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>>
1780 constexpr bool operator>= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
1781 {
1782 return !(*this < other);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001783 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001784
1785private:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001786 static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
1787 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001788 // The last dimension of the array needs to contain a multiple of new type elements
1789 Expects(extent[Rank - 1] >= d && (extent[Rank - 1] % d == 0));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001790
Neil MacIntosh68064d62015-11-03 19:17:11 -08001791 index_type ret = extent;
Anna Gringauzef5100252015-11-12 12:48:49 -08001792 ret[Rank - 1] /= d;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001793
Neil MacIntosh68064d62015-11-03 19:17:11 -08001794 return ret;
1795 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001796
Anna Gringauzef5100252015-11-12 12:48:49 -08001797 template <bool Enabled = (Rank == 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001798 static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
1799 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001800 // Only strided arrays with regular strides can be resized
1801 Expects(strides[Rank - 1] == 1);
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001802
Neil MacIntosh68064d62015-11-03 19:17:11 -08001803 return strides;
1804 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001805
Anna Gringauzef5100252015-11-12 12:48:49 -08001806 template <bool Enabled = (Rank > 1), typename Dummy = std::enable_if_t<Enabled>>
Neil MacIntosh68064d62015-11-03 19:17:11 -08001807 static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
1808 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001809 // Only strided arrays with regular strides can be resized
1810 Expects(strides[Rank - 1] == 1);
1811 // The strides must have contiguous chunks of
1812 // memory that can contain a multiple of new type elements
1813 Expects(strides[Rank - 2] >= d && (strides[Rank - 2] % d == 0));
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001814
Anna Gringauzef5100252015-11-12 12:48:49 -08001815 for (size_t i = Rank - 1; i > 0; --i)
1816 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001817 // Only strided arrays with regular strides can be resized
1818 Expects((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0));
Anna Gringauzef5100252015-11-12 12:48:49 -08001819 }
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001820
Neil MacIntosh68064d62015-11-03 19:17:11 -08001821 index_type ret = strides / d;
Anna Gringauzef5100252015-11-12 12:48:49 -08001822 ret[Rank - 1] = 1;
Anna Gringauze17ed5c32015-08-30 23:30:15 -07001823
Neil MacIntosh68064d62015-11-03 19:17:11 -08001824 return ret;
1825 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001826};
1827
Anna Gringauze8aa42482015-11-11 12:41:11 -08001828template <class Span>
1829class contiguous_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001830{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001831 using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001832public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001833 using typename Base::reference;
1834 using typename Base::pointer;
1835 using typename Base::difference_type;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001836
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001837private:
Anna Gringauze8aa42482015-11-11 12:41:11 -08001838 template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
1839 friend class span;
Neil MacIntoshf45fedb2015-10-15 14:29:35 -07001840
1841 pointer m_pdata;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001842 const Span* m_validator;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001843 void validateThis() const
1844 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001845 // iterator is out of range of the array
1846 Expects(m_pdata >= m_validator->m_pdata &&
1847 m_pdata < m_validator->m_pdata + m_validator->size());
Neil MacIntosh68064d62015-11-03 19:17:11 -08001848 }
Anna Gringauze8aa42482015-11-11 12:41:11 -08001849 contiguous_span_iterator (const Span* container, bool isbegin) :
Neil MacIntosh68064d62015-11-03 19:17:11 -08001850 m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001851public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001852 reference operator*() const noexcept
1853 {
1854 validateThis();
1855 return *m_pdata;
1856 }
1857 pointer operator->() const noexcept
1858 {
1859 validateThis();
1860 return m_pdata;
1861 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001862 contiguous_span_iterator& operator++() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001863 {
1864 ++m_pdata;
1865 return *this;
1866 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001867 contiguous_span_iterator operator++(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001868 {
1869 auto ret = *this;
1870 ++(*this);
1871 return ret;
1872 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001873 contiguous_span_iterator& operator--() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001874 {
1875 --m_pdata;
1876 return *this;
1877 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001878 contiguous_span_iterator operator--(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001879 {
1880 auto ret = *this;
1881 --(*this);
1882 return ret;
1883 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001884 contiguous_span_iterator operator+(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001885 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001886 contiguous_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001887 return ret += n;
1888 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001889 contiguous_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001890 {
1891 m_pdata += n;
1892 return *this;
1893 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001894 contiguous_span_iterator operator-(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001895 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001896 contiguous_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08001897 return ret -= n;
1898 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001899 contiguous_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001900 {
1901 return *this += -n;
1902 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001903 difference_type operator-(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001904 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001905 Expects(m_validator == rhs.m_validator);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001906 return m_pdata - rhs.m_pdata;
1907 }
1908 reference operator[](difference_type n) const noexcept
1909 {
1910 return *(*this + n);
1911 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001912 bool operator==(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001913 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001914 Expects(m_validator == rhs.m_validator);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001915 return m_pdata == rhs.m_pdata;
1916 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001917 bool operator!=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001918 {
1919 return !(*this == rhs);
1920 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001921 bool operator<(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001922 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08001923 Expects(m_validator == rhs.m_validator);
Neil MacIntosh68064d62015-11-03 19:17:11 -08001924 return m_pdata < rhs.m_pdata;
1925 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001926 bool operator<=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001927 {
1928 return !(rhs < *this);
1929 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001930 bool operator>(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001931 {
1932 return rhs < *this;
1933 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001934 bool operator>=(const contiguous_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001935 {
1936 return !(rhs > *this);
1937 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001938 void swap(contiguous_span_iterator& rhs) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001939 {
1940 std::swap(m_pdata, rhs.m_pdata);
1941 std::swap(m_validator, rhs.m_validator);
1942 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001943};
1944
Anna Gringauze8aa42482015-11-11 12:41:11 -08001945template <typename Span>
1946contiguous_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 -07001947{
Neil MacIntosh68064d62015-11-03 19:17:11 -08001948 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001949}
1950
Anna Gringauze8aa42482015-11-11 12:41:11 -08001951template <typename Span>
1952class general_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001953{
Anna Gringauze8aa42482015-11-11 12:41:11 -08001954 using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001955public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001956 using typename Base::reference;
1957 using typename Base::pointer;
1958 using typename Base::difference_type;
1959 using typename Base::value_type;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001960private:
Anna Gringauzef5100252015-11-12 12:48:49 -08001961 template <typename ValueType, size_t Rank>
1962 friend class strided_span;
Neil MacIntosh68064d62015-11-03 19:17:11 -08001963
Anna Gringauzef5100252015-11-12 12:48:49 -08001964 const Span* m_container;
Anna Gringauze8aa42482015-11-11 12:41:11 -08001965 typename Span::bounds_type::iterator m_itr;
Anna Gringauzef5100252015-11-12 12:48:49 -08001966 general_span_iterator(const Span* container, bool isbegin) :
Neil MacIntosh68064d62015-11-03 19:17:11 -08001967 m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
1968 {}
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07001969public:
Neil MacIntosh68064d62015-11-03 19:17:11 -08001970 reference operator*() noexcept
1971 {
1972 return (*m_container)[*m_itr];
1973 }
1974 pointer operator->() noexcept
1975 {
1976 return &(*m_container)[*m_itr];
1977 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001978 general_span_iterator& operator++() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001979 {
1980 ++m_itr;
1981 return *this;
1982 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001983 general_span_iterator operator++(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001984 {
1985 auto ret = *this;
1986 ++(*this);
1987 return ret;
1988 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001989 general_span_iterator& operator--() noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001990 {
1991 --m_itr;
1992 return *this;
1993 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08001994 general_span_iterator operator--(int)noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08001995 {
1996 auto ret = *this;
1997 --(*this);
1998 return ret;
1999 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002000 general_span_iterator operator+(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002001 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002002 general_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08002003 return ret += n;
2004 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002005 general_span_iterator& operator+=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002006 {
2007 m_itr += n;
2008 return *this;
2009 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002010 general_span_iterator operator-(difference_type n) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002011 {
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002012 general_span_iterator ret{ *this };
Neil MacIntosh68064d62015-11-03 19:17:11 -08002013 return ret -= n;
2014 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002015 general_span_iterator& operator-=(difference_type n) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002016 {
2017 return *this += -n;
2018 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002019 difference_type operator-(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002020 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002021 Expects(m_container == rhs.m_container);
Neil MacIntosh68064d62015-11-03 19:17:11 -08002022 return m_itr - rhs.m_itr;
2023 }
2024 value_type operator[](difference_type n) const noexcept
2025 {
2026 return (*m_container)[m_itr[n]];;
2027 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002028 bool operator==(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002029 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002030 Expects(m_container == rhs.m_container);
Neil MacIntosh68064d62015-11-03 19:17:11 -08002031 return m_itr == rhs.m_itr;
2032 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002033 bool operator !=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002034 {
2035 return !(*this == rhs);
2036 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002037 bool operator<(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002038 {
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002039 Expects(m_container == rhs.m_container);
Neil MacIntosh68064d62015-11-03 19:17:11 -08002040 return m_itr < rhs.m_itr;
2041 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002042 bool operator<=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002043 {
2044 return !(rhs < *this);
2045 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002046 bool operator>(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002047 {
2048 return rhs < *this;
2049 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002050 bool operator>=(const general_span_iterator& rhs) const noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002051 {
2052 return !(rhs > *this);
2053 }
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002054 void swap(general_span_iterator& rhs) noexcept
Neil MacIntosh68064d62015-11-03 19:17:11 -08002055 {
2056 std::swap(m_itr, rhs.m_itr);
2057 std::swap(m_container, rhs.m_container);
2058 }
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002059};
2060
Anna Gringauze8aa42482015-11-11 12:41:11 -08002061template <typename Span>
2062general_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 -07002063{
Neil MacIntosh68064d62015-11-03 19:17:11 -08002064 return rhs + n;
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002065}
2066
Neil MacIntoshef626fd2015-09-29 16:41:37 -07002067} // namespace gsl
Neil MacIntosha9dcbe02015-08-20 18:09:14 -07002068
Neil MacIntosh292f81e2015-11-17 15:07:51 -08002069
Neil MacIntoshd5316802015-09-30 21:54:08 -07002070#ifdef _MSC_VER
2071
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002072#undef constexpr
2073#pragma pop_macro("constexpr")
Gabriel Dos Reis6554e832015-09-28 05:10:44 -07002074
Neil MacIntoshd5316802015-09-30 21:54:08 -07002075#if _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002076#pragma warning(pop)
Neil MacIntoshd5316802015-09-30 21:54:08 -07002077
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002078#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
Lukas Haselsteinere51eb222015-11-15 23:08:35 +01002079#undef noexcept
Neil MacIntosh292f81e2015-11-17 15:07:51 -08002080#pragma pop_macro("noexcept")
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002081#endif // GSL_THROW_ON_CONTRACT_VIOLATION
Neil MacIntoshd5316802015-09-30 21:54:08 -07002082
Neil MacIntoshe9a96022015-11-03 18:56:55 -08002083#undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG
Neil MacIntoshe9a96022015-11-03 18:56:55 -08002084
Neil MacIntosh9a297122015-09-14 15:11:07 -07002085#endif // _MSC_VER <= 1800
Neil MacIntosh9a297122015-09-14 15:11:07 -07002086
Neil MacIntoshd5316802015-09-30 21:54:08 -07002087#endif // _MSC_VER
2088
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002089#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
Neil MacIntosh292f81e2015-11-17 15:07:51 -08002090
Anna Gringauzef5100252015-11-12 12:48:49 -08002091#undef noexcept
Neil MacIntosh292f81e2015-11-17 15:07:51 -08002092
2093#ifdef _MSC_VER
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002094#pragma warning(pop)
Neil MacIntosh292f81e2015-11-17 15:07:51 -08002095#pragma pop_macro("noexcept")
2096#endif
2097
Neil MacIntoshd13f6da2015-11-20 16:03:00 -08002098#endif // GSL_THROW_ON_CONTRACT_VIOLATION
Neil MacIntoshd5316802015-09-30 21:54:08 -07002099
Treb Connell51da1362015-09-24 18:08:34 -07002100
Neil MacIntoshb63ec942015-11-04 12:42:27 -08002101#endif // GSL_SPAN_H