Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 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 | // |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #pragma once |
| 18 | |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 19 | #ifndef GSL_SPAN_H |
| 20 | #define GSL_SPAN_H |
Treb Connell | 51da136 | 2015-09-24 18:08:34 -0700 | [diff] [blame] | 21 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 22 | #include "gsl_assert.h" |
| 23 | #include "gsl_util.h" |
Olaf van der Spek | 550361c | 2015-11-12 10:23:56 +0100 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <array> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 26 | #include <cassert> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <cstdint> |
Olaf van der Spek | 550361c | 2015-11-12 10:23:56 +0100 | [diff] [blame] | 29 | #include <functional> |
| 30 | #include <iterator> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 31 | #include <limits> |
Olaf van der Spek | 550361c | 2015-11-12 10:23:56 +0100 | [diff] [blame] | 32 | #include <new> |
Anna Gringauze | 2cdedda | 2015-10-15 13:19:24 -0700 | [diff] [blame] | 33 | #include <numeric> |
Olaf van der Spek | 550361c | 2015-11-12 10:23:56 +0100 | [diff] [blame] | 34 | #include <stdexcept> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 35 | #include <type_traits> |
| 36 | #include <utility> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 37 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 38 | #ifdef _MSC_VER |
| 39 | |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 40 | // turn off some warnings that are noisy about our Expects statements |
| 41 | #pragma warning(push) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 42 | #pragma warning(disable : 4127) // conditional expression is constant |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 43 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 44 | // No MSVC does constexpr fully yet |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 45 | #pragma push_macro("constexpr") |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 46 | #define constexpr |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 47 | |
| 48 | // VS 2013 workarounds |
| 49 | #if _MSC_VER <= 1800 |
| 50 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 51 | #define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 52 | #define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 53 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 54 | // noexcept is not understood |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 55 | #ifndef GSL_THROWS_ON_CONTRACT_VIOLATION |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 56 | #pragma push_macro("noexcept") |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 57 | #define noexcept /* nothing */ |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 58 | #endif |
| 59 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 60 | // turn off some misguided warnings |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 61 | #pragma warning(push) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 62 | #pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
| 63 | #pragma warning(disable : 4512) // warns that assignment op could not be generated |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 64 | |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 65 | #endif // _MSC_VER <= 1800 |
| 66 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 67 | #endif // _MSC_VER |
| 68 | |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 69 | #ifdef GSL_THROW_ON_CONTRACT_VIOLATION |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 70 | |
| 71 | #ifdef _MSC_VER |
| 72 | #pragma push_macro("noexcept") |
| 73 | #endif |
| 74 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 75 | #define noexcept /* nothing */ |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 76 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 77 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 78 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 79 | namespace gsl |
| 80 | { |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | ** begin definitions of index and bounds |
| 84 | */ |
| 85 | namespace details |
| 86 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 87 | template <typename SizeType> |
| 88 | struct SizeTypeTraits |
| 89 | { |
| 90 | static const SizeType max_value = std::numeric_limits<SizeType>::max(); |
| 91 | }; |
Anna Gringauze | 1c208b3 | 2015-10-16 17:40:57 -0700 | [diff] [blame] | 92 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 93 | template <typename... Ts> |
| 94 | class are_integral : public std::integral_constant<bool, true> |
| 95 | { |
| 96 | }; |
Anna Gringauze | 1c208b3 | 2015-10-16 17:40:57 -0700 | [diff] [blame] | 97 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 98 | template <typename T, typename... Ts> |
| 99 | class are_integral<T, Ts...> |
| 100 | : public std::integral_constant<bool, |
| 101 | std::is_integral<T>::value && are_integral<Ts...>::value> |
| 102 | { |
| 103 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 106 | template <size_t Rank> |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 107 | class index final |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 108 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 109 | static_assert(Rank > 0, "Rank must be greater than 0!"); |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 110 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 111 | template <size_t OtherRank> |
| 112 | friend class index; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 113 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 114 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 115 | static const size_t rank = Rank; |
| 116 | using value_type = std::ptrdiff_t; |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 117 | using size_type = value_type; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 118 | using reference = std::add_lvalue_reference_t<value_type>; |
| 119 | using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 120 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 121 | constexpr index() noexcept {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 122 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 123 | constexpr index(const value_type (&values)[Rank]) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 124 | { |
| 125 | std::copy(values, values + Rank, elems); |
| 126 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 127 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 128 | #ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 129 | template < |
| 130 | typename T, typename... Ts, |
| 131 | typename = std::enable_if_t<((sizeof...(Ts) + 1) == Rank) && std::is_integral<T>::value && |
| 132 | details::are_integral<Ts...>::value>> |
| 133 | constexpr index(T t, Ts... ds) |
| 134 | : index({narrow_cast<value_type>(t), narrow_cast<value_type>(ds)...}) |
| 135 | { |
| 136 | } |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 137 | #else |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 138 | template <typename... Ts, typename = std::enable_if_t<(sizeof...(Ts) == Rank) && |
| 139 | details::are_integral<Ts...>::value>> |
| 140 | constexpr index(Ts... ds) noexcept : elems{narrow_cast<value_type>(ds)...} |
| 141 | { |
| 142 | } |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 143 | #endif |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 144 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 145 | constexpr index(const index& other) noexcept = default; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 146 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 147 | constexpr index& operator=(const index& rhs) noexcept = default; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 148 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 149 | // Preconditions: component_idx < rank |
| 150 | constexpr reference operator[](size_t component_idx) |
| 151 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 152 | Expects(component_idx < Rank); // Component index must be less than rank |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 153 | return elems[component_idx]; |
| 154 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 155 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 156 | // Preconditions: component_idx < rank |
| 157 | constexpr const_reference operator[](size_t component_idx) const noexcept |
| 158 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 159 | Expects(component_idx < Rank); // Component index must be less than rank |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 160 | return elems[component_idx]; |
| 161 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 162 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 163 | constexpr bool operator==(const index& rhs) const noexcept |
| 164 | { |
| 165 | return std::equal(elems, elems + rank, rhs.elems); |
| 166 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 167 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 168 | constexpr bool operator!=(const index& rhs) const noexcept { return !(this == rhs); } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 169 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 170 | constexpr index operator+() const noexcept { return *this; } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 171 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 172 | constexpr index operator-() const noexcept |
| 173 | { |
| 174 | index ret = *this; |
| 175 | std::transform(ret, ret + rank, ret, std::negate<value_type>{}); |
| 176 | return ret; |
| 177 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 178 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 179 | constexpr index operator+(const index& rhs) const noexcept |
| 180 | { |
| 181 | index ret = *this; |
| 182 | ret += rhs; |
| 183 | return ret; |
| 184 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 185 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 186 | constexpr index operator-(const index& rhs) const noexcept |
| 187 | { |
| 188 | index ret = *this; |
| 189 | ret -= rhs; |
| 190 | return ret; |
| 191 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 192 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 193 | constexpr index& operator+=(const index& rhs) noexcept |
| 194 | { |
| 195 | std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{}); |
| 196 | return *this; |
| 197 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 198 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 199 | constexpr index& operator-=(const index& rhs) noexcept |
| 200 | { |
| 201 | std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{}); |
| 202 | return *this; |
| 203 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 204 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 205 | constexpr index operator*(value_type v) const noexcept |
| 206 | { |
| 207 | index ret = *this; |
| 208 | ret *= v; |
| 209 | return ret; |
| 210 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 211 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 212 | constexpr index operator/(value_type v) const noexcept |
| 213 | { |
| 214 | index ret = *this; |
| 215 | ret /= v; |
| 216 | return ret; |
| 217 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 218 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 219 | friend constexpr index operator*(value_type v, const index& rhs) noexcept |
| 220 | { |
| 221 | return rhs * v; |
| 222 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 223 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 224 | constexpr index& operator*=(value_type v) noexcept |
| 225 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 226 | std::transform(elems, elems + rank, elems, |
| 227 | [v](value_type x) { return std::multiplies<value_type>{}(x, v); }); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 228 | return *this; |
| 229 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 230 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 231 | constexpr index& operator/=(value_type v) noexcept |
| 232 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 233 | std::transform(elems, elems + rank, elems, |
| 234 | [v](value_type x) { return std::divides<value_type>{}(x, v); }); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 235 | return *this; |
| 236 | } |
Anna Gringauze | 546f8cc | 2015-10-05 21:04:56 -0700 | [diff] [blame] | 237 | |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 238 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 239 | value_type elems[Rank] = {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 242 | #ifndef _MSC_VER |
| 243 | |
| 244 | struct static_bounds_dynamic_range_t |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 245 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 246 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
| 247 | constexpr operator T() const noexcept |
| 248 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 249 | return narrow_cast<T>(-1); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 250 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 251 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 252 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 253 | constexpr bool operator==(T other) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 254 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 255 | return narrow_cast<T>(-1) == other; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 256 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 257 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 258 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 259 | constexpr bool operator!=(T other) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 260 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 261 | return narrow_cast<T>(-1) != other; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 262 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 265 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 266 | constexpr bool operator==(T left, static_bounds_dynamic_range_t right) noexcept |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 267 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 268 | return right == left; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 272 | constexpr bool operator!=(T left, static_bounds_dynamic_range_t right) noexcept |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 273 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 274 | return right != left; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | constexpr static_bounds_dynamic_range_t dynamic_range{}; |
| 278 | #else |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 279 | const std::ptrdiff_t dynamic_range = -1; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 280 | #endif |
| 281 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 282 | struct generalized_mapping_tag |
| 283 | { |
| 284 | }; |
| 285 | struct contiguous_mapping_tag : generalized_mapping_tag |
| 286 | { |
| 287 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 288 | |
| 289 | namespace details |
| 290 | { |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 291 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 292 | template <std::ptrdiff_t Left, std::ptrdiff_t Right> |
| 293 | struct LessThan |
| 294 | { |
| 295 | static const bool value = Left < Right; |
| 296 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 297 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 298 | template <std::ptrdiff_t... Ranges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 299 | struct BoundsRanges |
| 300 | { |
| 301 | using size_type = std::ptrdiff_t; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 302 | static const size_type Depth = 0; |
| 303 | static const size_type DynamicNum = 0; |
| 304 | static const size_type CurrentRange = 1; |
| 305 | static const size_type TotalSize = 1; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 306 | |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 307 | // TODO : following signature is for work around VS bug |
| 308 | template <typename OtherRange> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 309 | BoundsRanges(const OtherRange&, bool /* firstLevel */) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 310 | { |
| 311 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 312 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 313 | BoundsRanges(const BoundsRanges&) = default; |
| 314 | BoundsRanges& operator=(const BoundsRanges&) = default; |
| 315 | BoundsRanges(const std::ptrdiff_t* const) {} |
| 316 | BoundsRanges() = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 317 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 318 | template <typename T, size_t Dim> |
| 319 | void serialize(T&) const |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 320 | { |
| 321 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 322 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 323 | template <typename T, size_t Dim> |
| 324 | size_type linearize(const T&) const |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 325 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 326 | return 0; |
| 327 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 328 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 329 | template <typename T, size_t Dim> |
Neil MacIntosh | f76f739 | 2015-11-30 18:20:14 -0800 | [diff] [blame] | 330 | size_type contains(const T&) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 331 | { |
Neil MacIntosh | f76f739 | 2015-11-30 18:20:14 -0800 | [diff] [blame] | 332 | return -1; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 333 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 334 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 335 | size_type elementNum(size_t) const noexcept { return 0; } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 336 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 337 | size_type totalSize() const noexcept { return TotalSize; } |
| 338 | |
| 339 | bool operator==(const BoundsRanges&) const noexcept { return true; } |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 340 | }; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 341 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 342 | template <std::ptrdiff_t... RestRanges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 343 | struct BoundsRanges<dynamic_range, RestRanges...> : BoundsRanges<RestRanges...> |
| 344 | { |
| 345 | using Base = BoundsRanges<RestRanges...>; |
| 346 | using size_type = std::ptrdiff_t; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 347 | static const size_t Depth = Base::Depth + 1; |
| 348 | static const size_t DynamicNum = Base::DynamicNum + 1; |
| 349 | static const size_type CurrentRange = dynamic_range; |
| 350 | static const size_type TotalSize = dynamic_range; |
| 351 | const size_type m_bound; |
| 352 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 353 | BoundsRanges(const BoundsRanges&) = default; |
| 354 | |
| 355 | BoundsRanges(const std::ptrdiff_t* const arr) |
| 356 | : Base(arr + 1), m_bound(*arr * this->Base::totalSize()) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 357 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 358 | Expects(0 <= *arr); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | BoundsRanges() : m_bound(0) {} |
| 362 | |
| 363 | template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 364 | BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, |
| 365 | bool /* firstLevel */ = true) |
| 366 | : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false) |
| 367 | , m_bound(other.totalSize()) |
| 368 | { |
| 369 | } |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 370 | |
| 371 | template <typename T, size_t Dim = 0> |
| 372 | void serialize(T& arr) const |
| 373 | { |
| 374 | arr[Dim] = elementNum(); |
| 375 | this->Base::template serialize<T, Dim + 1>(arr); |
| 376 | } |
| 377 | |
| 378 | template <typename T, size_t Dim = 0> |
| 379 | size_type linearize(const T& arr) const |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 380 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 381 | const size_type index = this->Base::totalSize() * arr[Dim]; |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 382 | Expects(index < m_bound); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 383 | return index + this->Base::template linearize<T, Dim + 1>(arr); |
| 384 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 385 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 386 | template <typename T, size_t Dim = 0> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 387 | size_type contains(const T& arr) const |
| 388 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 389 | const ptrdiff_t last = this->Base::template contains<T, Dim + 1>(arr); |
| 390 | if (last == -1) return -1; |
| 391 | const ptrdiff_t cur = this->Base::totalSize() * arr[Dim]; |
| 392 | return cur < m_bound ? cur + last : -1; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 393 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 394 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 395 | size_type totalSize() const noexcept { return m_bound; } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 396 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 397 | size_type elementNum() const noexcept { return totalSize() / this->Base::totalSize(); } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 398 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 399 | size_type elementNum(size_t dim) const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 400 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 401 | if (dim > 0) |
| 402 | return this->Base::elementNum(dim - 1); |
| 403 | else |
| 404 | return elementNum(); |
| 405 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 406 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 407 | bool operator==(const BoundsRanges& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 408 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 409 | return m_bound == rhs.m_bound && |
| 410 | static_cast<const Base&>(*this) == static_cast<const Base&>(rhs); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 411 | } |
| 412 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 413 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 414 | template <std::ptrdiff_t CurRange, std::ptrdiff_t... RestRanges> |
| 415 | struct BoundsRanges<CurRange, RestRanges...> : BoundsRanges<RestRanges...> |
| 416 | { |
| 417 | using Base = BoundsRanges<RestRanges...>; |
| 418 | using size_type = std::ptrdiff_t; |
| 419 | static const size_t Depth = Base::Depth + 1; |
| 420 | static const size_t DynamicNum = Base::DynamicNum; |
| 421 | static const size_type CurrentRange = CurRange; |
| 422 | static const size_type TotalSize = |
| 423 | Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 424 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 425 | BoundsRanges(const BoundsRanges&) = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 426 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 427 | BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) {} |
| 428 | BoundsRanges() = default; |
| 429 | |
| 430 | template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges> |
| 431 | BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, |
| 432 | bool firstLevel = true) |
| 433 | : Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false) |
| 434 | { |
| 435 | (void) firstLevel; |
| 436 | } |
| 437 | |
| 438 | template <typename T, size_t Dim = 0> |
| 439 | void serialize(T& arr) const |
| 440 | { |
| 441 | arr[Dim] = elementNum(); |
| 442 | this->Base::template serialize<T, Dim + 1>(arr); |
| 443 | } |
| 444 | |
| 445 | template <typename T, size_t Dim = 0> |
| 446 | size_type linearize(const T& arr) const |
| 447 | { |
| 448 | Expects(arr[Dim] < CurrentRange); // Index is out of range |
| 449 | return this->Base::totalSize() * arr[Dim] + |
| 450 | this->Base::template linearize<T, Dim + 1>(arr); |
| 451 | } |
| 452 | |
| 453 | template <typename T, size_t Dim = 0> |
| 454 | size_type contains(const T& arr) const |
| 455 | { |
| 456 | if (arr[Dim] >= CurrentRange) return -1; |
| 457 | const size_type last = this->Base::template contains<T, Dim + 1>(arr); |
| 458 | if (last == -1) return -1; |
| 459 | return this->Base::totalSize() * arr[Dim] + last; |
| 460 | } |
| 461 | |
| 462 | size_type totalSize() const noexcept { return CurrentRange * this->Base::totalSize(); } |
| 463 | |
| 464 | size_type elementNum() const noexcept { return CurrentRange; } |
| 465 | |
| 466 | size_type elementNum(size_t dim) const noexcept |
| 467 | { |
| 468 | if (dim > 0) |
| 469 | return this->Base::elementNum(dim - 1); |
| 470 | else |
| 471 | return elementNum(); |
| 472 | } |
| 473 | |
| 474 | bool operator==(const BoundsRanges& rhs) const noexcept |
| 475 | { |
| 476 | return static_cast<const Base&>(*this) == static_cast<const Base&>(rhs); |
| 477 | } |
| 478 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 479 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 480 | template <typename SourceType, typename TargetType> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 481 | struct BoundsRangeConvertible |
| 482 | : public std::integral_constant<bool, (SourceType::TotalSize >= TargetType::TotalSize || |
| 483 | TargetType::TotalSize == dynamic_range || |
| 484 | SourceType::TotalSize == dynamic_range || |
| 485 | TargetType::TotalSize == 0)> |
| 486 | { |
| 487 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 488 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 489 | template <typename TypeChain> |
| 490 | struct TypeListIndexer |
| 491 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 492 | const TypeChain& obj_; |
| 493 | TypeListIndexer(const TypeChain& obj) : obj_(obj) {} |
| 494 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 495 | template <size_t N> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 496 | const TypeChain& getObj(std::true_type) |
| 497 | { |
| 498 | return obj_; |
| 499 | } |
| 500 | |
| 501 | template <size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base> |
| 502 | auto getObj(std::false_type) |
| 503 | -> decltype(TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>()) |
| 504 | { |
| 505 | return TypeListIndexer<MyBase>(static_cast<const MyBase&>(obj_)).template get<N>(); |
| 506 | } |
| 507 | |
| 508 | template <size_t N> |
| 509 | auto get() -> decltype(getObj<N - 1>(std::integral_constant<bool, N == 0>())) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 510 | { |
| 511 | return getObj<N - 1>(std::integral_constant<bool, N == 0>()); |
| 512 | } |
| 513 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 514 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 515 | template <typename TypeChain> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 516 | TypeListIndexer<TypeChain> createTypeListIndexer(const TypeChain& obj) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 517 | { |
| 518 | return TypeListIndexer<TypeChain>(obj); |
| 519 | } |
Anna Gringauze | fdf8643 | 2015-10-14 10:46:22 -0700 | [diff] [blame] | 520 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 521 | template <size_t Rank, bool Enabled = (Rank > 1), |
| 522 | typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 523 | constexpr Ret shift_left(const index<Rank>& other) noexcept |
| 524 | { |
| 525 | Ret ret{}; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 526 | for (size_t i = 0; i < Rank - 1; ++i) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 527 | ret[i] = other[i + 1]; |
| 528 | } |
| 529 | return ret; |
| 530 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | template <typename IndexType> |
| 534 | class bounds_iterator; |
| 535 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 536 | template <std::ptrdiff_t... Ranges> |
| 537 | class static_bounds |
| 538 | { |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 539 | public: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 540 | static_bounds(const details::BoundsRanges<Ranges...>&) {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 541 | }; |
| 542 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 543 | template <std::ptrdiff_t FirstRange, std::ptrdiff_t... RestRanges> |
| 544 | class static_bounds<FirstRange, RestRanges...> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 545 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 546 | using MyRanges = details::BoundsRanges<FirstRange, RestRanges...>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 547 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 548 | MyRanges m_ranges; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 549 | constexpr static_bounds(const MyRanges& range) : m_ranges(range) {} |
| 550 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 551 | template <std::ptrdiff_t... OtherRanges> |
| 552 | friend class static_bounds; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 553 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 554 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 555 | static const size_t rank = MyRanges::Depth; |
| 556 | static const size_t dynamic_rank = MyRanges::DynamicNum; |
| 557 | static const std::ptrdiff_t static_size = MyRanges::TotalSize; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 558 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 559 | using size_type = std::ptrdiff_t; |
| 560 | using index_type = index<rank>; |
Neil MacIntosh | a4fa2b3 | 2015-10-28 16:53:53 -0700 | [diff] [blame] | 561 | using const_index_type = std::add_const_t<index_type>; |
| 562 | using iterator = bounds_iterator<const_index_type>; |
| 563 | using const_iterator = bounds_iterator<const_index_type>; |
| 564 | using difference_type = std::ptrdiff_t; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 565 | using sliced_type = static_bounds<RestRanges...>; |
| 566 | using mapping_type = contiguous_mapping_tag; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 567 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 568 | constexpr static_bounds(const static_bounds&) = default; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 569 | |
| 570 | template <typename SourceType, typename TargetType, size_t Rank> |
| 571 | struct BoundsRangeConvertible2; |
| 572 | |
| 573 | template <size_t Rank, typename SourceType, typename TargetType, |
| 574 | typename Ret = BoundsRangeConvertible2<typename SourceType::Base, |
| 575 | typename TargetType::Base, Rank>> |
| 576 | static auto helpBoundsRangeConvertible(SourceType, TargetType, std::true_type) -> Ret; |
| 577 | |
| 578 | template <size_t Rank, typename SourceType, typename TargetType> |
| 579 | static auto helpBoundsRangeConvertible(SourceType, TargetType, ...) -> std::false_type; |
| 580 | |
| 581 | template <typename SourceType, typename TargetType, size_t Rank> |
| 582 | struct BoundsRangeConvertible2 |
| 583 | : decltype(helpBoundsRangeConvertible<Rank - 1>( |
| 584 | SourceType(), TargetType(), |
| 585 | std::integral_constant<bool, |
| 586 | SourceType::Depth == TargetType::Depth && |
| 587 | (SourceType::CurrentRange == TargetType::CurrentRange || |
| 588 | TargetType::CurrentRange == dynamic_range || |
| 589 | SourceType::CurrentRange == dynamic_range)>())) |
| 590 | { |
| 591 | }; |
| 592 | |
| 593 | template <typename SourceType, typename TargetType> |
| 594 | struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type |
| 595 | { |
| 596 | }; |
| 597 | |
| 598 | template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth> |
| 599 | struct BoundsRangeConvertible |
| 600 | : decltype(helpBoundsRangeConvertible<Rank - 1>( |
| 601 | SourceType(), TargetType(), |
| 602 | std::integral_constant<bool, |
| 603 | SourceType::Depth == TargetType::Depth && |
| 604 | (!details::LessThan<SourceType::CurrentRange, |
| 605 | TargetType::CurrentRange>::value || |
| 606 | TargetType::CurrentRange == dynamic_range || |
| 607 | SourceType::CurrentRange == dynamic_range)>())) |
| 608 | { |
| 609 | }; |
| 610 | |
| 611 | template <typename SourceType, typename TargetType> |
| 612 | struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type |
| 613 | { |
| 614 | }; |
| 615 | |
| 616 | template <std::ptrdiff_t... Ranges, |
| 617 | typename = std::enable_if_t<details::BoundsRangeConvertible< |
| 618 | details::BoundsRanges<Ranges...>, |
| 619 | details::BoundsRanges<FirstRange, RestRanges...>>::value>> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 620 | constexpr static_bounds(const static_bounds<Ranges...>& other) : m_ranges(other.m_ranges) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 621 | { |
| 622 | Expects((MyRanges::DynamicNum == 0 && details::BoundsRanges<Ranges...>::DynamicNum == 0) || |
| 623 | MyRanges::DynamicNum > 0 || other.m_ranges.totalSize() >= m_ranges.totalSize()); |
| 624 | } |
| 625 | |
| 626 | constexpr static_bounds(std::initializer_list<size_type> il) |
| 627 | : m_ranges(static_cast<const std::ptrdiff_t*>(il.begin())) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 628 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 629 | // Size of the initializer list must match the rank of the array |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 630 | Expects((MyRanges::DynamicNum == 0 && il.size() == 1 && *il.begin() == static_size) || |
| 631 | MyRanges::DynamicNum == il.size()); |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 632 | // Size of the range must be less than the max element of the size type |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 633 | Expects(m_ranges.totalSize() <= PTRDIFF_MAX); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 634 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 635 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 636 | constexpr static_bounds() = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 637 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 638 | constexpr static_bounds& operator=(const static_bounds& otherBounds) |
| 639 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 640 | new (&m_ranges) MyRanges(otherBounds.m_ranges); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 641 | return *this; |
| 642 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 643 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 644 | constexpr sliced_type slice() const noexcept |
| 645 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 646 | return sliced_type{static_cast<const details::BoundsRanges<RestRanges...>&>(m_ranges)}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 647 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 648 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 649 | constexpr size_type stride() const noexcept { return rank > 1 ? slice().size() : 1; } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 650 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 651 | constexpr size_type size() const noexcept { return m_ranges.totalSize(); } |
| 652 | |
| 653 | constexpr size_type total_size() const noexcept { return m_ranges.totalSize(); } |
| 654 | |
| 655 | constexpr size_type linearize(const index_type& idx) const { return m_ranges.linearize(idx); } |
| 656 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 657 | constexpr bool contains(const index_type& idx) const noexcept |
| 658 | { |
| 659 | return m_ranges.contains(idx) != -1; |
| 660 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 661 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 662 | constexpr size_type operator[](size_t index) const noexcept |
| 663 | { |
| 664 | return m_ranges.elementNum(index); |
| 665 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 666 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 667 | template <size_t Dim = 0> |
| 668 | constexpr size_type extent() const noexcept |
| 669 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 670 | static_assert(Dim < rank, |
| 671 | "dimension should be less than rank (dimension count starts from 0)"); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 672 | return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum(); |
| 673 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 674 | |
| 675 | template <typename IntType> |
| 676 | constexpr size_type extent(IntType dim) const noexcept |
| 677 | { |
| 678 | static_assert(std::is_integral<IntType>::value, |
| 679 | "Dimension parameter must be supplied as an integral type."); |
| 680 | auto real_dim = narrow_cast<size_t>(dim); |
| 681 | Expects(real_dim < rank); |
| 682 | |
| 683 | return m_ranges.elementNum(real_dim); |
| 684 | } |
| 685 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 686 | constexpr index_type index_bounds() const noexcept |
| 687 | { |
| 688 | size_type extents[rank] = {}; |
| 689 | m_ranges.serialize(extents); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 690 | return {extents}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 691 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 692 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 693 | template <std::ptrdiff_t... Ranges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 694 | constexpr bool operator==(const static_bounds<Ranges...>& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 695 | { |
| 696 | return this->size() == rhs.size(); |
| 697 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 698 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 699 | template <std::ptrdiff_t... Ranges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 700 | constexpr bool operator!=(const static_bounds<Ranges...>& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 701 | { |
| 702 | return !(*this == rhs); |
| 703 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 704 | |
| 705 | constexpr const_iterator begin() const noexcept { return const_iterator(*this, index_type{}); } |
| 706 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 707 | constexpr const_iterator end() const noexcept |
| 708 | { |
| 709 | return const_iterator(*this, this->index_bounds()); |
| 710 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 711 | }; |
| 712 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 713 | template <size_t Rank> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 714 | class strided_bounds |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 715 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 716 | template <size_t OtherRank> |
| 717 | friend class strided_bounds; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 718 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 719 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 720 | static const size_t rank = Rank; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 721 | using value_type = std::ptrdiff_t; |
| 722 | using reference = std::add_lvalue_reference_t<value_type>; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 723 | using const_reference = std::add_const_t<reference>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 724 | using size_type = value_type; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 725 | using difference_type = value_type; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 726 | using index_type = index<rank>; |
Neil MacIntosh | a4fa2b3 | 2015-10-28 16:53:53 -0700 | [diff] [blame] | 727 | using const_index_type = std::add_const_t<index_type>; |
| 728 | using iterator = bounds_iterator<const_index_type>; |
| 729 | using const_iterator = bounds_iterator<const_index_type>; |
| 730 | static const value_type dynamic_rank = rank; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 731 | static const value_type static_size = dynamic_range; |
| 732 | using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>; |
| 733 | using mapping_type = generalized_mapping_tag; |
Neil MacIntosh | d0f09e7 | 2015-10-15 16:38:53 -0700 | [diff] [blame] | 734 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 735 | constexpr strided_bounds(const strided_bounds&) noexcept = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 736 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 737 | constexpr strided_bounds& operator=(const strided_bounds&) noexcept = default; |
Vladislav Yaroslavlev | 557e669 | 2015-11-12 10:44:41 +0300 | [diff] [blame] | 738 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 739 | constexpr strided_bounds(const value_type (&values)[rank], index_type strides) |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 740 | : m_extents(values), m_strides(std::move(strides)) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 741 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 742 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 743 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 744 | constexpr strided_bounds(const index_type& extents, const index_type& strides) noexcept |
| 745 | : m_extents(extents), |
| 746 | m_strides(strides) |
| 747 | { |
| 748 | } |
| 749 | |
| 750 | constexpr index_type strides() const noexcept { return m_strides; } |
| 751 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 752 | constexpr size_type total_size() const noexcept |
| 753 | { |
| 754 | size_type ret = 0; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 755 | for (size_t i = 0; i < rank; ++i) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 756 | ret += (m_extents[i] - 1) * m_strides[i]; |
| 757 | } |
| 758 | return ret + 1; |
| 759 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 760 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 761 | constexpr size_type size() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 762 | { |
| 763 | size_type ret = 1; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 764 | for (size_t i = 0; i < rank; ++i) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 765 | ret *= m_extents[i]; |
| 766 | } |
| 767 | return ret; |
| 768 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 769 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 770 | constexpr bool contains(const index_type& idx) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 771 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 772 | for (size_t i = 0; i < rank; ++i) { |
| 773 | if (idx[i] < 0 || idx[i] >= m_extents[i]) return false; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 774 | } |
| 775 | return true; |
| 776 | } |
Neil MacIntosh | d0f09e7 | 2015-10-15 16:38:53 -0700 | [diff] [blame] | 777 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 778 | constexpr size_type linearize(const index_type& idx) const noexcept |
| 779 | { |
| 780 | size_type ret = 0; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 781 | for (size_t i = 0; i < rank; i++) { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 782 | Expects(idx[i] < m_extents[i]); // index is out of bounds of the array |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 783 | ret += idx[i] * m_strides[i]; |
| 784 | } |
| 785 | return ret; |
| 786 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 787 | |
| 788 | constexpr size_type stride() const noexcept { return m_strides[0]; } |
| 789 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 790 | template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 791 | constexpr sliced_type slice() const |
| 792 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 793 | return {details::shift_left(m_extents), details::shift_left(m_strides)}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 794 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 795 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 796 | template <size_t Dim = 0> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 797 | constexpr size_type extent() const noexcept |
| 798 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 799 | static_assert(Dim < Rank, |
| 800 | "dimension should be less than rank (dimension count starts from 0)"); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 801 | return m_extents[Dim]; |
| 802 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 803 | |
| 804 | constexpr index_type index_bounds() const noexcept { return m_extents; } |
| 805 | constexpr const_iterator begin() const noexcept { return const_iterator{*this, index_type{}}; } |
| 806 | |
| 807 | constexpr const_iterator end() const noexcept { return const_iterator{*this, index_bounds()}; } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 808 | |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 809 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 810 | index_type m_extents; |
| 811 | index_type m_strides; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 812 | }; |
| 813 | |
| 814 | template <typename T> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 815 | struct is_bounds : std::integral_constant<bool, false> |
| 816 | { |
| 817 | }; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 818 | template <std::ptrdiff_t... Ranges> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 819 | struct is_bounds<static_bounds<Ranges...>> : std::integral_constant<bool, true> |
| 820 | { |
| 821 | }; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 822 | template <size_t Rank> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 823 | struct is_bounds<strided_bounds<Rank>> : std::integral_constant<bool, true> |
| 824 | { |
| 825 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 826 | |
| 827 | template <typename IndexType> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 828 | class bounds_iterator : public std::iterator<std::random_access_iterator_tag, IndexType> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 829 | { |
| 830 | private: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 831 | using Base = std::iterator<std::random_access_iterator_tag, IndexType>; |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 832 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 833 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 834 | static const size_t rank = IndexType::rank; |
| 835 | using typename Base::reference; |
| 836 | using typename Base::pointer; |
| 837 | using typename Base::difference_type; |
| 838 | using typename Base::value_type; |
| 839 | using index_type = value_type; |
| 840 | using index_size_type = typename IndexType::value_type; |
| 841 | template <typename Bounds> |
| 842 | explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 843 | : boundary_(bnd.index_bounds()), |
| 844 | curr_(std::move(curr)) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 845 | { |
| 846 | static_assert(is_bounds<Bounds>::value, "Bounds type must be provided"); |
| 847 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 848 | |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 849 | constexpr reference operator*() const noexcept { return curr_; } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 850 | |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 851 | constexpr pointer operator->() const noexcept { return &curr_; } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 852 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 853 | constexpr bounds_iterator& operator++() noexcept |
| 854 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 855 | for (size_t i = rank; i-- > 0;) { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 856 | if (curr_[i] < boundary_[i] - 1) { |
| 857 | curr_[i]++; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 858 | return *this; |
| 859 | } |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 860 | curr_[i] = 0; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 861 | } |
| 862 | // If we're here we've wrapped over - set to past-the-end. |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 863 | curr_ = boundary_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 864 | return *this; |
| 865 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 866 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 867 | constexpr bounds_iterator operator++(int) noexcept |
| 868 | { |
| 869 | auto ret = *this; |
| 870 | ++(*this); |
| 871 | return ret; |
| 872 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 873 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 874 | constexpr bounds_iterator& operator--() noexcept |
| 875 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 876 | if (!less(curr_, boundary_)) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 877 | // if at the past-the-end, set to last element |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 878 | for (size_t i = 0; i < rank; ++i) { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 879 | curr_[i] = boundary_[i] - 1; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 880 | } |
| 881 | return *this; |
| 882 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 883 | for (size_t i = rank; i-- > 0;) { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 884 | if (curr_[i] >= 1) { |
| 885 | curr_[i]--; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 886 | return *this; |
| 887 | } |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 888 | curr_[i] = boundary_[i] - 1; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 889 | } |
| 890 | // If we're here the preconditions were violated |
| 891 | // "pre: there exists s such that r == ++s" |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 892 | Expects(false); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 893 | return *this; |
| 894 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 895 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 896 | constexpr bounds_iterator operator--(int) noexcept |
| 897 | { |
| 898 | auto ret = *this; |
| 899 | --(*this); |
| 900 | return ret; |
| 901 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 902 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 903 | constexpr bounds_iterator operator+(difference_type n) const noexcept |
| 904 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 905 | bounds_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 906 | return ret += n; |
| 907 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 908 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 909 | constexpr bounds_iterator& operator+=(difference_type n) noexcept |
| 910 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 911 | auto linear_idx = linearize(curr_) + n; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 912 | std::remove_const_t<value_type> stride = 0; |
| 913 | stride[rank - 1] = 1; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 914 | for (size_t i = rank - 1; i-- > 0;) { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 915 | stride[i] = stride[i + 1] * boundary_[i + 1]; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 916 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 917 | for (size_t i = 0; i < rank; ++i) { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 918 | curr_[i] = linear_idx / stride[i]; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 919 | linear_idx = linear_idx % stride[i]; |
| 920 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 921 | // index is out of bounds of the array |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 922 | Expects(!less(curr_, index_type{}) && !less(boundary_, curr_)); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 923 | return *this; |
| 924 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 925 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 926 | constexpr bounds_iterator operator-(difference_type n) const noexcept |
| 927 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 928 | bounds_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 929 | return ret -= n; |
| 930 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 931 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 932 | constexpr bounds_iterator& operator-=(difference_type n) noexcept { return * this += -n; } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 933 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 934 | constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept |
| 935 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 936 | return linearize(curr_) - linearize(rhs.curr_); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 937 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 938 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 939 | constexpr value_type operator[](difference_type n) const noexcept { return *(*this + n); } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 940 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 941 | constexpr bool operator==(const bounds_iterator& rhs) const noexcept |
| 942 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 943 | return curr_ == rhs.curr_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 944 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 945 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 946 | constexpr bool operator!=(const bounds_iterator& rhs) const noexcept { return !(*this == rhs); } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 947 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 948 | constexpr bool operator<(const bounds_iterator& rhs) const noexcept |
| 949 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 950 | return less(curr_, rhs.curr_); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 951 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 952 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 953 | constexpr bool operator<=(const bounds_iterator& rhs) const noexcept { return !(rhs < *this); } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 954 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 955 | constexpr bool operator>(const bounds_iterator& rhs) const noexcept { return rhs < *this; } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 956 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 957 | constexpr bool operator>=(const bounds_iterator& rhs) const noexcept { return !(rhs > *this); } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 958 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 959 | void swap(bounds_iterator& rhs) noexcept |
| 960 | { |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 961 | std::swap(boundary_, rhs.boundary_); |
| 962 | std::swap(curr_, rhs.curr_); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 963 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 964 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 965 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 966 | constexpr bool less(index_type& one, index_type& other) const noexcept |
| 967 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 968 | for (size_t i = 0; i < rank; ++i) { |
| 969 | if (one[i] < other[i]) return true; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 970 | } |
| 971 | return false; |
| 972 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 973 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 974 | constexpr index_size_type linearize(const value_type& idx) const noexcept |
| 975 | { |
| 976 | // TODO: Smarter impl. |
| 977 | // Check if past-the-end |
| 978 | index_size_type multiplier = 1; |
| 979 | index_size_type res = 0; |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 980 | if (!less(idx, boundary_)) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 981 | res = 1; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 982 | for (size_t i = rank; i-- > 0;) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 983 | res += (idx[i] - 1) * multiplier; |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 984 | multiplier *= boundary_[i]; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | else |
| 988 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 989 | for (size_t i = rank; i-- > 0;) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 990 | res += idx[i] * multiplier; |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 991 | multiplier *= boundary_[i]; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | return res; |
| 995 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 996 | |
Neil MacIntosh | c9959b1 | 2015-11-30 05:34:38 +0000 | [diff] [blame] | 997 | value_type boundary_; |
| 998 | std::remove_const_t<value_type> curr_; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 999 | }; |
| 1000 | |
| 1001 | template <typename IndexType> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1002 | bounds_iterator<IndexType> operator+(typename bounds_iterator<IndexType>::difference_type n, |
| 1003 | const bounds_iterator<IndexType>& rhs) noexcept |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1004 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1005 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1008 | namespace details |
| 1009 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1010 | template <typename Bounds> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1011 | constexpr std::enable_if_t< |
| 1012 | std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value, |
| 1013 | typename Bounds::index_type> |
| 1014 | make_stride(const Bounds& bnd) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1015 | { |
| 1016 | return bnd.strides(); |
| 1017 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1018 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1019 | // Make a stride vector from bounds, assuming contiguous memory. |
| 1020 | template <typename Bounds> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1021 | constexpr std::enable_if_t< |
| 1022 | std::is_same<typename Bounds::mapping_type, contiguous_mapping_tag>::value, |
| 1023 | typename Bounds::index_type> |
| 1024 | make_stride(const Bounds& bnd) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1025 | { |
| 1026 | auto extents = bnd.index_bounds(); |
| 1027 | typename Bounds::size_type stride[Bounds::rank] = {}; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 1028 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1029 | stride[Bounds::rank - 1] = 1; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1030 | for (size_t i = 1; i < Bounds::rank; ++i) { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1031 | stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i]; |
| 1032 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1033 | return {stride}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1034 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1035 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1036 | template <typename BoundsSrc, typename BoundsDest> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1037 | void verifyBoundsReshape(const BoundsSrc& src, const BoundsDest& dest) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1038 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1039 | static_assert(is_bounds<BoundsSrc>::value && is_bounds<BoundsDest>::value, |
| 1040 | "The src type and dest type must be bounds"); |
| 1041 | static_assert(std::is_same<typename BoundsSrc::mapping_type, contiguous_mapping_tag>::value, |
| 1042 | "The source type must be a contiguous bounds"); |
| 1043 | static_assert(BoundsDest::static_size == dynamic_range || |
| 1044 | BoundsSrc::static_size == dynamic_range || |
| 1045 | BoundsDest::static_size == BoundsSrc::static_size, |
| 1046 | "The source bounds must have same size as dest bounds"); |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1047 | Expects(src.size() == dest.size()); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1048 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1049 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1050 | } // namespace details |
| 1051 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1052 | template <typename Span> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1053 | class contiguous_span_iterator; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1054 | template <typename Span> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1055 | class general_span_iterator; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1056 | enum class byte : std::uint8_t |
| 1057 | { |
| 1058 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1059 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1060 | template <std::ptrdiff_t DimSize = dynamic_range> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1061 | struct dim |
| 1062 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1063 | static const std::ptrdiff_t value = DimSize; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1064 | }; |
| 1065 | template <> |
| 1066 | struct dim<dynamic_range> |
| 1067 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1068 | static const std::ptrdiff_t value = dynamic_range; |
| 1069 | const std::ptrdiff_t dvalue; |
| 1070 | dim(std::ptrdiff_t size) : dvalue(size) {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1071 | }; |
| 1072 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1073 | template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range, |
| 1074 | std::ptrdiff_t... RestDimensions> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1075 | class span; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1076 | |
| 1077 | template <typename ValueType, size_t Rank> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1078 | class strided_span; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1079 | |
| 1080 | namespace details |
| 1081 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1082 | template <typename T, typename = std::true_type> |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1083 | struct SpanTypeTraits |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1084 | { |
| 1085 | using value_type = T; |
| 1086 | using size_type = size_t; |
| 1087 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1088 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1089 | template <typename Traits> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1090 | struct SpanTypeTraits<Traits, typename std::is_reference<typename Traits::span_traits&>::type> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1091 | { |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1092 | using value_type = typename Traits::span_traits::value_type; |
| 1093 | using size_type = typename Traits::span_traits::size_type; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1094 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1095 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1096 | template <typename T, std::ptrdiff_t... Ranks> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1097 | struct SpanArrayTraits |
| 1098 | { |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1099 | using type = span<T, Ranks...>; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1100 | using value_type = T; |
| 1101 | using bounds_type = static_bounds<Ranks...>; |
| 1102 | using pointer = T*; |
| 1103 | using reference = T&; |
| 1104 | }; |
| 1105 | template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1106 | struct SpanArrayTraits<T[N], Ranks...> : SpanArrayTraits<T, Ranks..., N> |
| 1107 | { |
| 1108 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1109 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1110 | template <typename BoundsType> |
| 1111 | BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size |
| 1112 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1113 | Expects(totalSize >= 0 && totalSize <= PTRDIFF_MAX); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1114 | return BoundsType{totalSize}; |
| 1115 | } |
| 1116 | template <typename BoundsType> |
| 1117 | BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size |
| 1118 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1119 | Expects(BoundsType::static_size <= totalSize); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1120 | return {}; |
| 1121 | } |
| 1122 | template <typename BoundsType> |
| 1123 | BoundsType newBoundsHelper(std::ptrdiff_t totalSize) |
| 1124 | { |
| 1125 | static_assert(BoundsType::dynamic_rank <= 1, "dynamic rank must less or equal to 1"); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1126 | return newBoundsHelperImpl<BoundsType>( |
| 1127 | totalSize, std::integral_constant<bool, BoundsType::dynamic_rank == 1>()); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1128 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1129 | |
| 1130 | struct Sep |
| 1131 | { |
| 1132 | }; |
| 1133 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1134 | template <typename T, typename... Args> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1135 | T static_as_span_helper(Sep, Args... args) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1136 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1137 | return T{narrow_cast<typename T::size_type>(args)...}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1138 | } |
| 1139 | template <typename T, typename Arg, typename... Args> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1140 | std::enable_if_t< |
| 1141 | !std::is_same<Arg, dim<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T> |
| 1142 | static_as_span_helper(Arg, Args... args) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1143 | { |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1144 | return static_as_span_helper<T>(args...); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1145 | } |
| 1146 | template <typename T, typename... Args> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1147 | T static_as_span_helper(dim<dynamic_range> val, Args... args) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1148 | { |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1149 | return static_as_span_helper<T>(args..., val.dvalue); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1150 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1151 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1152 | template <typename... Dimensions> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1153 | struct static_as_span_static_bounds_helper |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1154 | { |
| 1155 | using type = static_bounds<(Dimensions::value)...>; |
| 1156 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1157 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1158 | template <typename T> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1159 | struct is_span_oracle : std::false_type |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1160 | { |
| 1161 | }; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1162 | |
| 1163 | template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1164 | struct is_span_oracle<span<ValueType, FirstDimension, RestDimensions...>> : std::true_type |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1165 | { |
| 1166 | }; |
| 1167 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1168 | template <typename ValueType, std::ptrdiff_t Rank> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1169 | struct is_span_oracle<strided_span<ValueType, Rank>> : std::true_type |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1170 | { |
| 1171 | }; |
| 1172 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1173 | template <typename T> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1174 | struct is_span : is_span_oracle<std::remove_cv_t<T>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1175 | { |
| 1176 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1179 | template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions> |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1180 | class span |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1181 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1182 | // TODO do we still need this? |
| 1183 | template <typename ValueType2, std::ptrdiff_t FirstDimension2, |
| 1184 | std::ptrdiff_t... RestDimensions2> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1185 | friend class span; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1186 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1187 | public: |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1188 | using bounds_type = static_bounds<FirstDimension, RestDimensions...>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1189 | static const size_t Rank = bounds_type::rank; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1190 | using size_type = typename bounds_type::size_type; |
| 1191 | using index_type = typename bounds_type::index_type; |
| 1192 | using value_type = ValueType; |
| 1193 | using const_value_type = std::add_const_t<value_type>; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1194 | using pointer = std::add_pointer_t<value_type>; |
| 1195 | using reference = std::add_lvalue_reference_t<value_type>; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1196 | using iterator = contiguous_span_iterator<span>; |
| 1197 | using const_span = span<const_value_type, FirstDimension, RestDimensions...>; |
| 1198 | using const_iterator = contiguous_span_iterator<const_span>; |
| 1199 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 1200 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1201 | using sliced_type = |
| 1202 | std::conditional_t<Rank == 1, value_type, span<value_type, RestDimensions...>>; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1203 | |
| 1204 | private: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1205 | pointer data_; |
| 1206 | bounds_type bounds_; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1207 | |
| 1208 | friend iterator; |
| 1209 | friend const_iterator; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1210 | |
| 1211 | public: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1212 | // default constructor - same as constructing from nullptr_t |
| 1213 | constexpr span() noexcept : span(nullptr, bounds_type{}) |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1214 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1215 | static_assert(bounds_type::dynamic_rank != 0 || |
| 1216 | (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0), |
| 1217 | "Default construction of span<T> only possible " |
| 1218 | "for dynamic or fixed, zero-length spans."); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1221 | // construct from nullptr - get an empty span |
| 1222 | constexpr span(std::nullptr_t) noexcept : span(nullptr, bounds_type{}) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1223 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1224 | static_assert(bounds_type::dynamic_rank != 0 || |
| 1225 | (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0), |
| 1226 | "nullptr_t construction of span<T> only possible " |
| 1227 | "for dynamic or fixed, zero-length spans."); |
| 1228 | } |
| 1229 | |
| 1230 | // construct from nullptr with size of 0 (helps with template function calls) |
| 1231 | template <class IntType, typename = std::enable_if_t<std::is_integral<IntType>::value>> |
| 1232 | constexpr span(std::nullptr_t, IntType size) noexcept : span(nullptr, bounds_type{}) |
| 1233 | { |
| 1234 | static_assert(bounds_type::dynamic_rank != 0 || |
| 1235 | (bounds_type::dynamic_rank == 0 && bounds_type::static_size == 0), |
| 1236 | "nullptr_t construction of span<T> only possible " |
| 1237 | "for dynamic or fixed, zero-length spans."); |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1238 | Expects(size == 0); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1239 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1240 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1241 | // construct from a single element |
| 1242 | constexpr span(reference data) noexcept : span(&data, bounds_type{1}) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1243 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1244 | static_assert(bounds_type::dynamic_rank > 0 || bounds_type::static_size == 0 || |
| 1245 | bounds_type::static_size == 1, |
| 1246 | "Construction from a single element only possible " |
| 1247 | "for dynamic or fixed spans of length 0 or 1."); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1248 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1249 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1250 | // prevent constructing from temporaries for single-elements |
| 1251 | constexpr span(value_type&&) = delete; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1252 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1253 | // construct from pointer + length |
| 1254 | constexpr span(pointer ptr, size_type size) noexcept : span(ptr, bounds_type{size}) {} |
| 1255 | |
| 1256 | // construct from pointer + length - multidimensional |
| 1257 | constexpr span(pointer data, bounds_type bounds) noexcept : data_(data), |
| 1258 | bounds_(std::move(bounds)) |
| 1259 | { |
| 1260 | Expects((bounds_.size() > 0 && data != nullptr) || bounds_.size() == 0); |
| 1261 | } |
| 1262 | |
| 1263 | // construct from begin,end pointer pair |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1264 | template <typename Ptr, |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1265 | typename = std::enable_if_t<std::is_convertible<Ptr, pointer>::value && |
| 1266 | details::LessThan<bounds_type::dynamic_rank, 2>::value>> |
| 1267 | constexpr span(pointer begin, Ptr end) |
| 1268 | : span(begin, details::newBoundsHelper<bounds_type>(static_cast<pointer>(end) - begin)) |
| 1269 | { |
| 1270 | Expects(begin != nullptr && end != nullptr && begin <= static_cast<pointer>(end)); |
| 1271 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1272 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1273 | // construct from n-dimensions static array |
| 1274 | template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>> |
| 1275 | constexpr span(T (&arr)[N]) |
| 1276 | : span(reinterpret_cast<pointer>(arr), bounds_type{typename Helper::bounds_type{}}) |
| 1277 | { |
| 1278 | static_assert( |
| 1279 | std::is_convertible<typename Helper::value_type(*) [], value_type(*) []>::value, |
| 1280 | "Cannot convert from source type to target span type."); |
| 1281 | static_assert(std::is_convertible<typename Helper::bounds_type, bounds_type>::value, |
| 1282 | "Cannot construct a span from an array with fewer elements."); |
| 1283 | } |
| 1284 | |
| 1285 | // construct from n-dimensions dynamic array (e.g. new int[m][4]) |
| 1286 | // (precedence will be lower than the 1-dimension pointer) |
| 1287 | template <typename T, typename Helper = details::SpanArrayTraits<T, dynamic_range>> |
| 1288 | constexpr span(T* const& data, size_type size) |
| 1289 | : span(reinterpret_cast<pointer>(data), typename Helper::bounds_type{size}) |
| 1290 | { |
| 1291 | static_assert( |
| 1292 | std::is_convertible<typename Helper::value_type(*) [], value_type(*) []>::value, |
| 1293 | "Cannot convert from source type to target span type."); |
| 1294 | } |
| 1295 | |
| 1296 | // construct from std::array |
| 1297 | template <typename T, size_t N> |
| 1298 | constexpr span(std::array<T, N>& arr) : span(arr.data(), bounds_type{static_bounds<N>{}}) |
| 1299 | { |
| 1300 | static_assert( |
| 1301 | std::is_convertible<T(*) [], typename std::remove_const_t<value_type>(*) []>::value, |
| 1302 | "Cannot convert from source type to target span type."); |
| 1303 | static_assert(std::is_convertible<static_bounds<N>, bounds_type>::value, |
| 1304 | "You cannot construct a span from a std::array of smaller size."); |
| 1305 | } |
| 1306 | |
| 1307 | // construct from const std::array |
| 1308 | template <typename T, size_t N> |
| 1309 | constexpr span(const std::array<std::remove_const_t<value_type>, N>& arr) |
| 1310 | : span(arr.data(), static_bounds<N>()) |
| 1311 | { |
| 1312 | static_assert(std::is_convertible<T(*) [], std::remove_const_t<value_type>>::value, |
| 1313 | "Cannot convert from source type to target span type."); |
| 1314 | static_assert(std::is_convertible<static_bounds<N>, bounds_type>::value, |
| 1315 | "You cannot construct a span from a std::array of smaller size."); |
| 1316 | } |
| 1317 | |
| 1318 | // prevent constructing from temporary std::array |
| 1319 | template <typename T, size_t N> |
| 1320 | constexpr span(std::array<T, N>&& arr) = delete; |
| 1321 | |
| 1322 | // construct from containers |
| 1323 | // future: could use contiguous_iterator_traits to identify only contiguous containers |
| 1324 | // type-requirements: container must have .size(), operator[] which are value_type compatible |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1325 | template <typename Cont, typename DataType = typename Cont::value_type, |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1326 | typename = std::enable_if_t< |
| 1327 | !details::is_span<Cont>::value && |
| 1328 | std::is_convertible<DataType (*)[], value_type (*)[]>::value && |
| 1329 | std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), |
| 1330 | *std::declval<Cont>().data())>, |
| 1331 | DataType>::value>> |
| 1332 | constexpr span(Cont& cont) |
| 1333 | : span(static_cast<pointer>(cont.data()), |
| 1334 | details::newBoundsHelper<bounds_type>(narrow_cast<size_type>(cont.size()))) |
| 1335 | { |
| 1336 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1337 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1338 | // prevent constructing from temporary containers |
| 1339 | template <typename Cont, typename DataType = typename Cont::value_type, |
| 1340 | typename = std::enable_if_t< |
| 1341 | !details::is_span<Cont>::value && |
| 1342 | std::is_convertible<DataType (*)[], value_type (*)[]>::value && |
| 1343 | std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), |
| 1344 | *std::declval<Cont>().data())>, |
| 1345 | DataType>::value>> |
| 1346 | explicit constexpr span(Cont&& cont) = delete; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1347 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1348 | // construct from a convertible span |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1349 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1350 | typename OtherBounds = static_bounds<OtherDimensions...>, |
| 1351 | typename = std::enable_if_t<std::is_convertible<OtherValueType, ValueType>::value && |
| 1352 | std::is_convertible<OtherBounds, bounds_type>::value>> |
| 1353 | constexpr span(span<OtherValueType, OtherDimensions...> other) noexcept : data_(other.data_), |
| 1354 | bounds_(other.bounds_) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1355 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1356 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1357 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1358 | // trivial copy and move |
| 1359 | #ifndef GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 1360 | constexpr span(span&&) = default; |
| 1361 | #endif |
| 1362 | constexpr span(const span&) = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1363 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1364 | // trivial assignment |
| 1365 | #ifndef GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 1366 | constexpr span& operator=(span&&) = default; |
| 1367 | #endif |
| 1368 | constexpr span& operator=(const span&) = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1369 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1370 | // first() - extract the first Count elements into a new span |
| 1371 | template <std::ptrdiff_t Count> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1372 | constexpr span<ValueType, Count> first() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1373 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1374 | static_assert(Count >= 0, "Count must be >= 0."); |
| 1375 | static_assert(bounds_type::static_size == dynamic_range || |
| 1376 | Count <= bounds_type::static_size, |
| 1377 | "Count is out of bounds."); |
| 1378 | |
| 1379 | Expects(bounds_type::static_size != dynamic_range || Count <= this->size()); |
| 1380 | return {this->data(), Count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1381 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1382 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1383 | // first() - extract the first count elements into a new span |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1384 | constexpr span<ValueType, dynamic_range> first(size_type count) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1385 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1386 | Expects(count >= 0 && count <= this->size()); |
| 1387 | return {this->data(), count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1388 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1389 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1390 | // last() - extract the last Count elements into a new span |
| 1391 | template <std::ptrdiff_t Count> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1392 | constexpr span<ValueType, Count> last() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1393 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1394 | static_assert(Count >= 0, "Count must be >= 0."); |
| 1395 | static_assert(bounds_type::static_size == dynamic_range || |
| 1396 | Count <= bounds_type::static_size, |
| 1397 | "Count is out of bounds."); |
| 1398 | |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1399 | Expects(bounds_type::static_size != dynamic_range || Count <= this->size()); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1400 | return {this->data() + this->size() - Count, Count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1401 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1402 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1403 | // last() - extract the last count elements into a new span |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1404 | constexpr span<ValueType, dynamic_range> last(size_type count) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1405 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1406 | Expects(count >= 0 && count <= this->size()); |
| 1407 | return {this->data() + this->size() - count, count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1408 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1409 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1410 | // subspan() - create a subview of Count elements starting at Offset |
| 1411 | template <std::ptrdiff_t Offset, std::ptrdiff_t Count> |
| 1412 | constexpr span<ValueType, Count> subspan() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1413 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1414 | static_assert(Count >= 0, "Count must be >= 0."); |
| 1415 | static_assert(Offset >= 0, "Offset must be >= 0."); |
| 1416 | static_assert(bounds_type::static_size == dynamic_range || |
| 1417 | ((Offset <= bounds_type::static_size) && |
| 1418 | Count <= bounds_type::static_size - Offset), |
| 1419 | "You must describe a sub-range within bounds of the span."); |
| 1420 | |
| 1421 | Expects(bounds_type::static_size != dynamic_range || |
| 1422 | (Offset <= this->size() && Count <= this->size() - Offset)); |
| 1423 | return {this->data() + Offset, Count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1424 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1425 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1426 | // subspan() - create a subview of count elements starting at offset |
| 1427 | // supplying dynamic_range for count will consume all available elements from offset |
| 1428 | constexpr span<ValueType, dynamic_range> subspan(size_type offset, |
| 1429 | size_type count = dynamic_range) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1430 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1431 | Expects((offset >= 0 && offset <= this->size()) && |
| 1432 | (count == dynamic_range || (count <= this->size() - offset))); |
| 1433 | return {this->data() + offset, count == dynamic_range ? this->length() - offset : count}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1434 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1435 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1436 | // section - creates a non-contiguous, strided span from a contiguous one |
| 1437 | constexpr strided_span<ValueType, Rank> section(index_type origin, index_type extents) const |
| 1438 | noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1439 | { |
| 1440 | size_type size = this->bounds().total_size() - this->bounds().linearize(origin); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1441 | return {&this->operator[](origin), size, |
| 1442 | strided_bounds<Rank>{extents, details::make_stride(bounds())}}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1443 | } |
Neil MacIntosh | 9f9fad9 | 2015-08-27 18:13:49 -0700 | [diff] [blame] | 1444 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1445 | // length of the span in elements |
| 1446 | constexpr size_type size() const noexcept { return bounds_.size(); } |
| 1447 | |
| 1448 | // length of the span in elements |
| 1449 | constexpr size_type length() const noexcept { return this->size(); } |
| 1450 | |
| 1451 | // length of the span in bytes |
| 1452 | constexpr size_type size_bytes() const noexcept { return sizeof(value_type) * this->size(); } |
| 1453 | |
| 1454 | // length of the span in bytes |
| 1455 | constexpr size_type length_bytes() const noexcept { return this->size_bytes(); } |
| 1456 | |
| 1457 | constexpr bool empty() const noexcept { return this->size() == 0; } |
| 1458 | |
| 1459 | static constexpr std::size_t rank() { return Rank; } |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1460 | |
| 1461 | template <size_t Dim = 0> |
| 1462 | constexpr size_type extent() const noexcept |
| 1463 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1464 | static_assert(Dim < Rank, |
| 1465 | "Dimension should be less than rank (dimension count starts from 0)."); |
| 1466 | return bounds_.template extent<Dim>(); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1469 | template <typename IntType> |
| 1470 | constexpr size_type extent(IntType dim) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1471 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1472 | return bounds_.extent(dim); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1475 | constexpr bounds_type bounds() const noexcept { return bounds_; } |
| 1476 | |
| 1477 | constexpr pointer data() const noexcept { return data_; } |
| 1478 | |
| 1479 | template <typename FirstIndex> |
| 1480 | constexpr reference operator()(FirstIndex index) |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1481 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1482 | return this->operator[](narrow_cast<std::ptrdiff_t>(index)); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1485 | template <typename FirstIndex, typename... OtherIndices> |
| 1486 | constexpr reference operator()(FirstIndex index, OtherIndices... indices) |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1487 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1488 | index_type idx = {narrow_cast<std::ptrdiff_t>(index), |
| 1489 | narrow_cast<std::ptrdiff_t>(indices...)}; |
| 1490 | return this->operator[](idx); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1493 | constexpr reference operator[](const index_type& idx) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1494 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1495 | return data_[bounds_.linearize(idx)]; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1498 | template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> |
| 1499 | constexpr Ret operator[](size_type idx) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1500 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1501 | Expects(idx < bounds_.size()); // index is out of bounds of the array |
| 1502 | const size_type ridx = idx * bounds_.stride(); |
| 1503 | |
| 1504 | // index is out of bounds of the underlying data |
| 1505 | Expects(ridx < bounds_.total_size()); |
| 1506 | return Ret{data_ + ridx, bounds_.slice()}; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1509 | constexpr iterator begin() const noexcept { return iterator{this, true}; } |
| 1510 | |
| 1511 | constexpr iterator end() const noexcept { return iterator{this, false}; } |
| 1512 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1513 | constexpr const_iterator cbegin() const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1514 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1515 | return const_iterator{reinterpret_cast<const const_span*>(this), true}; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1518 | constexpr const_iterator cend() const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1519 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1520 | return const_iterator{reinterpret_cast<const const_span*>(this), false}; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1523 | constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1524 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1525 | constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1526 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1527 | constexpr const_reverse_iterator crbegin() const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1528 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1529 | return const_reverse_iterator{cend()}; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1532 | constexpr const_reverse_iterator crend() const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1533 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1534 | return const_reverse_iterator{cbegin()}; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1537 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1538 | typename Dummy = std::enable_if_t<std::is_same< |
| 1539 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1540 | constexpr bool operator==(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1541 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1542 | return bounds_.size() == other.bounds_.size() && |
| 1543 | (data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin())); |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1546 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1547 | typename Dummy = std::enable_if_t<std::is_same< |
| 1548 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1549 | constexpr bool operator!=(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1550 | { |
| 1551 | return !(*this == other); |
| 1552 | } |
| 1553 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1554 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1555 | typename Dummy = std::enable_if_t<std::is_same< |
| 1556 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1557 | constexpr bool operator<(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1558 | { |
| 1559 | return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end()); |
| 1560 | } |
| 1561 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1562 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1563 | typename Dummy = std::enable_if_t<std::is_same< |
| 1564 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1565 | constexpr bool operator<=(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1566 | { |
| 1567 | return !(other < *this); |
| 1568 | } |
| 1569 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1570 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1571 | typename Dummy = std::enable_if_t<std::is_same< |
| 1572 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1573 | constexpr bool operator>(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1574 | { |
| 1575 | return (other < *this); |
| 1576 | } |
| 1577 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1578 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1579 | typename Dummy = std::enable_if_t<std::is_same< |
| 1580 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1581 | constexpr bool operator>=(const span<OtherValueType, OtherDimensions...>& other) const noexcept |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1582 | { |
| 1583 | return !(*this < other); |
| 1584 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1585 | }; |
| 1586 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1587 | // |
| 1588 | // Free functions for manipulating spans |
| 1589 | // |
| 1590 | |
| 1591 | // reshape a span into a different dimensionality |
| 1592 | // DimCount and Enabled here are workarounds for a bug in MSVC 2015 |
| 1593 | template <typename SpanType, typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), |
| 1594 | bool Enabled = (DimCount > 0), typename = std::enable_if_t<Enabled>> |
| 1595 | constexpr span<typename SpanType::value_type, Dimensions2::value...> as_span(SpanType s, |
| 1596 | Dimensions2... dims) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1597 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1598 | static_assert(details::is_span<SpanType>::value, |
| 1599 | "Variadic as_span() is for reshaping existing spans."); |
| 1600 | using BoundsType = |
| 1601 | typename span<typename SpanType::value_type, (Dimensions2::value)...>::bounds_type; |
| 1602 | auto tobounds = details::static_as_span_helper<BoundsType>(dims..., details::Sep{}); |
| 1603 | details::verifyBoundsReshape(s.bounds(), tobounds); |
| 1604 | return {s.data(), tobounds}; |
| 1605 | } |
| 1606 | |
| 1607 | // convert a span<T> to a span<const byte> |
| 1608 | template <typename U, std::ptrdiff_t... Dimensions> |
| 1609 | span<const byte, dynamic_range> as_bytes(span<U, Dimensions...> s) noexcept |
| 1610 | { |
| 1611 | static_assert(std::is_trivial<std::decay_t<U>>::value, |
| 1612 | "The value_type of span must be a trivial type."); |
| 1613 | return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; |
| 1614 | } |
| 1615 | |
| 1616 | // convert a span<T> to a span<byte> (a writeable byte span) |
| 1617 | // this is not currently a portable function that can be relied upon to work |
| 1618 | // on all implementations. It should be considered an experimental extension |
| 1619 | // to the standard GSL interface. |
| 1620 | template <typename U, std::ptrdiff_t... Dimensions> |
| 1621 | span<byte> as_writeable_bytes(span<U, Dimensions...> s) noexcept |
| 1622 | { |
| 1623 | static_assert(std::is_trivial<std::decay_t<U>>::value, |
| 1624 | "The value_type of span must be a trivial type."); |
| 1625 | return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; |
| 1626 | } |
| 1627 | |
| 1628 | // convert a span<const byte> to a span<const T> |
| 1629 | // this is not currently a portable function that can be relied upon to work |
| 1630 | // on all implementations. It should be considered an experimental extension |
| 1631 | // to the standard GSL interface. |
| 1632 | template <typename U, std::ptrdiff_t... Dimensions> |
| 1633 | constexpr auto as_span(span<const byte, Dimensions...> s) noexcept |
| 1634 | -> span<const U, static_cast<std::ptrdiff_t>( |
| 1635 | span<const byte, Dimensions...>::bounds_type::static_size != dynamic_range |
| 1636 | ? (static_cast<size_t>( |
| 1637 | span<const byte, Dimensions...>::bounds_type::static_size) / |
| 1638 | sizeof(U)) |
| 1639 | : dynamic_range)> |
| 1640 | { |
| 1641 | using ConstByteSpan = span<const byte, Dimensions...>; |
| 1642 | static_assert( |
| 1643 | std::is_trivial<std::decay_t<U>>::value && |
| 1644 | (ConstByteSpan::bounds_type::static_size == dynamic_range || |
| 1645 | ConstByteSpan::bounds_type::static_size % narrow_cast<std::ptrdiff_t>(sizeof(U)) == 0), |
| 1646 | "Target type must be a trivial type and its size must match the byte array size"); |
| 1647 | |
| 1648 | Expects((s.size_bytes() % sizeof(U)) == 0 && (s.size_bytes() / sizeof(U)) < PTRDIFF_MAX); |
| 1649 | return {reinterpret_cast<const U*>(s.data()), |
| 1650 | s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))}; |
| 1651 | } |
| 1652 | |
| 1653 | // convert a span<byte> to a span<T> |
| 1654 | // this is not currently a portable function that can be relied upon to work |
| 1655 | // on all implementations. It should be considered an experimental extension |
| 1656 | // to the standard GSL interface. |
| 1657 | template <typename U, std::ptrdiff_t... Dimensions> |
| 1658 | constexpr auto as_span(span<byte, Dimensions...> s) noexcept -> span< |
| 1659 | U, narrow_cast<std::ptrdiff_t>( |
| 1660 | span<byte, Dimensions...>::bounds_type::static_size != dynamic_range |
| 1661 | ? static_cast<std::size_t>(span<byte, Dimensions...>::bounds_type::static_size) / |
| 1662 | sizeof(U) |
| 1663 | : dynamic_range)> |
| 1664 | { |
| 1665 | using ByteSpan = span<byte, Dimensions...>; |
| 1666 | static_assert( |
| 1667 | std::is_trivial<std::decay_t<U>>::value && |
| 1668 | (ByteSpan::bounds_type::static_size == dynamic_range || |
| 1669 | ByteSpan::bounds_type::static_size % static_cast<std::size_t>(sizeof(U)) == 0), |
| 1670 | "Target type must be a trivial type and its size must match the byte array size"); |
| 1671 | |
| 1672 | Expects((s.bytes() % sizeof(U)) == 0); |
| 1673 | return {reinterpret_cast<U*>(s.data()), |
| 1674 | s.size_bytes() / narrow_cast<std::ptrdiff_t>(sizeof(U))}; |
| 1675 | } |
| 1676 | |
| 1677 | template <typename T, std::ptrdiff_t... Dimensions> |
| 1678 | constexpr auto as_span(T* const& ptr, dim<Dimensions>... args) |
| 1679 | -> span<std::remove_all_extents_t<T>, Dimensions...> |
| 1680 | { |
| 1681 | return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr), |
| 1682 | details::static_as_span_helper<static_bounds<Dimensions...>>(args..., details::Sep{})}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | template <typename T> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1686 | constexpr auto as_span(T* arr, std::ptrdiff_t len) -> |
| 1687 | typename details::SpanArrayTraits<T, dynamic_range>::type |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1688 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1689 | return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | template <typename T, size_t N> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1693 | constexpr auto as_span(T (&arr)[N]) -> typename details::SpanArrayTraits<T, N>::type |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1694 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1695 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | template <typename T, size_t N> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1699 | constexpr span<const T, N> as_span(const std::array<T, N>& arr) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1700 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1701 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | template <typename T, size_t N> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1705 | constexpr span<const T, N> as_span(const std::array<T, N>&&) = delete; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1706 | |
| 1707 | template <typename T, size_t N> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1708 | constexpr span<T, N> as_span(std::array<T, N>& arr) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1709 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1710 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | template <typename T> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1714 | constexpr span<T, dynamic_range> as_span(T* begin, T* end) |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1715 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1716 | return {begin, end}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | template <typename Cont> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1720 | constexpr auto as_span(Cont& arr) -> std::enable_if_t< |
| 1721 | !details::is_span<std::decay_t<Cont>>::value, |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1722 | span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1723 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1724 | Expects(arr.size() < PTRDIFF_MAX); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1725 | return {arr.data(), narrow_cast<std::ptrdiff_t>(arr.size())}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | template <typename Cont> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1729 | constexpr auto as_span(Cont&& arr) -> std::enable_if_t< |
| 1730 | !details::is_span<std::decay_t<Cont>>::value, |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1731 | span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1732 | |
Elron A. Yellin | e4d8d35 | 2015-11-20 17:50:02 -0500 | [diff] [blame] | 1733 | // from basic_string which doesn't have nonconst .data() member like other contiguous containers |
| 1734 | template <typename CharT, typename Traits, typename Allocator> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1735 | constexpr auto as_span(std::basic_string<CharT, Traits, Allocator>& str) |
| 1736 | -> span<CharT, dynamic_range> |
Elron A. Yellin | e4d8d35 | 2015-11-20 17:50:02 -0500 | [diff] [blame] | 1737 | { |
Neil MacIntosh | d505737 | 2015-11-20 17:14:21 -0800 | [diff] [blame] | 1738 | Expects(str.size() < PTRDIFF_MAX); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1739 | return {&str[0], narrow_cast<std::ptrdiff_t>(str.size())}; |
Elron A. Yellin | e4d8d35 | 2015-11-20 17:50:02 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1742 | // strided_span is an extension that is not strictly part of the GSL at this time. |
| 1743 | // It is kept here while the multidimensional interface is still being defined. |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1744 | template <typename ValueType, size_t Rank> |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1745 | class strided_span |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1746 | { |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1747 | public: |
| 1748 | using bounds_type = strided_bounds<Rank>; |
| 1749 | using size_type = typename bounds_type::size_type; |
| 1750 | using index_type = typename bounds_type::index_type; |
| 1751 | using value_type = ValueType; |
| 1752 | using const_value_type = std::add_const_t<value_type>; |
| 1753 | using pointer = std::add_pointer_t<value_type>; |
| 1754 | using reference = std::add_lvalue_reference_t<value_type>; |
| 1755 | using iterator = general_span_iterator<strided_span>; |
| 1756 | using const_strided_span = strided_span<const_value_type, Rank>; |
| 1757 | using const_iterator = general_span_iterator<const_strided_span>; |
| 1758 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 1759 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1760 | using sliced_type = |
| 1761 | std::conditional_t<Rank == 1, value_type, strided_span<value_type, Rank - 1>>; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1762 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1763 | private: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1764 | pointer data_; |
| 1765 | bounds_type bounds_; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1766 | |
| 1767 | friend iterator; |
| 1768 | friend const_iterator; |
| 1769 | template <typename OtherValueType, size_t OtherRank> |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 1770 | friend class strided_span; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1771 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1772 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1773 | // from raw data |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1774 | constexpr strided_span(pointer ptr, size_type size, bounds_type bounds) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1775 | : data_(ptr), bounds_(std::move(bounds)) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1776 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1777 | Expects((bounds_.size() > 0 && ptr != nullptr) || bounds_.size() == 0); |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1778 | // Bounds cross data boundaries |
| 1779 | Expects(this->bounds().total_size() <= size); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1780 | (void) size; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1781 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1782 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1783 | // from static array of size N |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1784 | template <size_type N> |
| 1785 | constexpr strided_span(value_type (&values)[N], bounds_type bounds) |
| 1786 | : strided_span(values, N, std::move(bounds)) |
| 1787 | { |
| 1788 | } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1789 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1790 | // from array view |
Anna Gringauze | c95eb57 | 2015-11-19 12:02:06 -0800 | [diff] [blame] | 1791 | template <typename OtherValueType, std::ptrdiff_t... Dimensions, |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1792 | bool Enabled1 = (sizeof...(Dimensions) == Rank), |
| 1793 | bool Enabled2 = std::is_convertible<OtherValueType*, ValueType*>::value, |
| 1794 | typename Dummy = std::enable_if_t<Enabled1 && Enabled2>> |
| 1795 | constexpr strided_span(span<OtherValueType, Dimensions...> av, bounds_type bounds) |
| 1796 | : strided_span(av.data(), av.bounds().total_size(), std::move(bounds)) |
| 1797 | { |
| 1798 | } |
| 1799 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1800 | // convertible |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1801 | template <typename OtherValueType, typename Dummy = std::enable_if_t<std::is_convertible< |
| 1802 | OtherValueType (*)[], value_type (*)[]>::value>> |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1803 | constexpr strided_span(const strided_span<OtherValueType, Rank>& other) |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1804 | : data_(other.data_), bounds_(other.bounds_) |
| 1805 | { |
| 1806 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1807 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1808 | // convert from bytes |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1809 | template <typename OtherValueType> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1810 | constexpr strided_span< |
| 1811 | typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, |
| 1812 | Rank> |
| 1813 | as_strided_span() const |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1814 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1815 | static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && |
| 1816 | (sizeof(OtherValueType) % sizeof(value_type) == 0), |
| 1817 | "OtherValueType should have a size to contain a multiple of ValueTypes"); |
| 1818 | auto d = narrow_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type)); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1819 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1820 | size_type size = this->bounds().total_size() / d; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1821 | return {(OtherValueType*) this->data(), size, |
| 1822 | bounds_type{resize_extent(this->bounds().index_bounds(), d), |
| 1823 | resize_stride(this->bounds().strides(), d)}}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1824 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1825 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1826 | constexpr strided_span section(index_type origin, index_type extents) const |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1827 | { |
| 1828 | size_type size = this->bounds().total_size() - this->bounds().linearize(origin); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1829 | return {&this->operator[](origin), size, |
| 1830 | bounds_type{extents, details::make_stride(bounds())}}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1831 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1832 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1833 | constexpr reference operator[](const index_type& idx) const |
| 1834 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1835 | return data_[bounds_.linearize(idx)]; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1836 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1837 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1838 | template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> |
| 1839 | constexpr Ret operator[](size_type idx) const |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1840 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1841 | Expects(idx < bounds_.size()); // index is out of bounds of the array |
| 1842 | const size_type ridx = idx * bounds_.stride(); |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1843 | |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1844 | // index is out of bounds of the underlying data |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1845 | Expects(ridx < bounds_.total_size()); |
| 1846 | return {data_ + ridx, bounds_.slice().total_size(), bounds_.slice()}; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1847 | } |
| 1848 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1849 | constexpr bounds_type bounds() const noexcept { return bounds_; } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1850 | |
| 1851 | template <size_t Dim = 0> |
| 1852 | constexpr size_type extent() const noexcept |
| 1853 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1854 | static_assert(Dim < Rank, |
| 1855 | "dimension should be less than Rank (dimension count starts from 0)"); |
| 1856 | return bounds_.template extent<Dim>(); |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1859 | constexpr size_type size() const noexcept { return bounds_.size(); } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1860 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1861 | constexpr pointer data() const noexcept { return data_; } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1862 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1863 | constexpr explicit operator bool() const noexcept { return data_ != nullptr; } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1864 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1865 | constexpr iterator begin() const { return iterator{this, true}; } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1866 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1867 | constexpr iterator end() const { return iterator{this, false}; } |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1868 | |
| 1869 | constexpr const_iterator cbegin() const |
| 1870 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1871 | return const_iterator{reinterpret_cast<const const_strided_span*>(this), true}; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | constexpr const_iterator cend() const |
| 1875 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1876 | return const_iterator{reinterpret_cast<const const_strided_span*>(this), false}; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1879 | constexpr reverse_iterator rbegin() const { return reverse_iterator{end()}; } |
| 1880 | |
| 1881 | constexpr reverse_iterator rend() const { return reverse_iterator{begin()}; } |
| 1882 | |
| 1883 | constexpr const_reverse_iterator crbegin() const { return const_reverse_iterator{cend()}; } |
| 1884 | |
| 1885 | constexpr const_reverse_iterator crend() const { return const_reverse_iterator{cbegin()}; } |
| 1886 | |
| 1887 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1888 | typename Dummy = std::enable_if_t<std::is_same< |
| 1889 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1890 | constexpr bool operator==(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1891 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1892 | return bounds_.size() == other.bounds_.size() && |
| 1893 | (data_ == other.data_ || std::equal(this->begin(), this->end(), other.begin())); |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1894 | } |
| 1895 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1896 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1897 | typename Dummy = std::enable_if_t<std::is_same< |
| 1898 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1899 | constexpr bool operator!=(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1900 | { |
| 1901 | return !(*this == other); |
| 1902 | } |
| 1903 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1904 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1905 | typename Dummy = std::enable_if_t<std::is_same< |
| 1906 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1907 | constexpr bool operator<(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1908 | { |
| 1909 | return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end()); |
| 1910 | } |
| 1911 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1912 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1913 | typename Dummy = std::enable_if_t<std::is_same< |
| 1914 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1915 | constexpr bool operator<=(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1916 | { |
| 1917 | return !(other < *this); |
| 1918 | } |
| 1919 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1920 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1921 | typename Dummy = std::enable_if_t<std::is_same< |
| 1922 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1923 | constexpr bool operator>(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1924 | { |
| 1925 | return (other < *this); |
| 1926 | } |
| 1927 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1928 | template <typename OtherValueType, std::ptrdiff_t OtherRank, |
| 1929 | typename Dummy = std::enable_if_t<std::is_same< |
| 1930 | std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>> |
| 1931 | constexpr bool operator>=(const strided_span<OtherValueType, OtherRank>& other) const noexcept |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1932 | { |
| 1933 | return !(*this < other); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1934 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1935 | |
| 1936 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1937 | static index_type resize_extent(const index_type& extent, std::ptrdiff_t d) |
| 1938 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1939 | // The last dimension of the array needs to contain a multiple of new type elements |
| 1940 | Expects(extent[Rank - 1] >= d && (extent[Rank - 1] % d == 0)); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1941 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1942 | index_type ret = extent; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1943 | ret[Rank - 1] /= d; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1944 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1945 | return ret; |
| 1946 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1947 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1948 | template <bool Enabled = (Rank == 1), typename Dummy = std::enable_if_t<Enabled>> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1949 | static index_type resize_stride(const index_type& strides, std::ptrdiff_t, void* = 0) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1950 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1951 | // Only strided arrays with regular strides can be resized |
| 1952 | Expects(strides[Rank - 1] == 1); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1953 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1954 | return strides; |
| 1955 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1956 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1957 | template <bool Enabled = (Rank > 1), typename Dummy = std::enable_if_t<Enabled>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1958 | static index_type resize_stride(const index_type& strides, std::ptrdiff_t d) |
| 1959 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1960 | // Only strided arrays with regular strides can be resized |
| 1961 | Expects(strides[Rank - 1] == 1); |
| 1962 | // The strides must have contiguous chunks of |
| 1963 | // memory that can contain a multiple of new type elements |
| 1964 | Expects(strides[Rank - 2] >= d && (strides[Rank - 2] % d == 0)); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1965 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1966 | for (size_t i = Rank - 1; i > 0; --i) { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1967 | // Only strided arrays with regular strides can be resized |
| 1968 | Expects((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0)); |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1969 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1970 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1971 | index_type ret = strides / d; |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 1972 | ret[Rank - 1] = 1; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1973 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1974 | return ret; |
| 1975 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1976 | }; |
| 1977 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1978 | template <class Span> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1979 | class contiguous_span_iterator |
| 1980 | : public std::iterator<std::random_access_iterator_tag, typename Span::value_type> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1981 | { |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1982 | using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1983 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1984 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1985 | using typename Base::reference; |
| 1986 | using typename Base::pointer; |
| 1987 | using typename Base::difference_type; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1988 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1989 | private: |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1990 | template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions> |
| 1991 | friend class span; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1992 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1993 | pointer data_; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 1994 | const Span* m_validator; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1995 | void validateThis() const |
| 1996 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 1997 | // iterator is out of range of the array |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 1998 | Expects(data_ >= m_validator->data_ && data_ < m_validator->data_ + m_validator->size()); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 1999 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2000 | contiguous_span_iterator(const Span* container, bool isbegin) |
| 2001 | : data_(isbegin ? container->data_ : container->data_ + container->size()) |
| 2002 | , m_validator(container) |
| 2003 | { |
| 2004 | } |
| 2005 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2006 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2007 | reference operator*() const noexcept |
| 2008 | { |
| 2009 | validateThis(); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2010 | return *data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2011 | } |
| 2012 | pointer operator->() const noexcept |
| 2013 | { |
| 2014 | validateThis(); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2015 | return data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2016 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2017 | contiguous_span_iterator& operator++() noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2018 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2019 | ++data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2020 | return *this; |
| 2021 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2022 | contiguous_span_iterator operator++(int) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2023 | { |
| 2024 | auto ret = *this; |
| 2025 | ++(*this); |
| 2026 | return ret; |
| 2027 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2028 | contiguous_span_iterator& operator--() noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2029 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2030 | --data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2031 | return *this; |
| 2032 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2033 | contiguous_span_iterator operator--(int) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2034 | { |
| 2035 | auto ret = *this; |
| 2036 | --(*this); |
| 2037 | return ret; |
| 2038 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2039 | contiguous_span_iterator operator+(difference_type n) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2040 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2041 | contiguous_span_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2042 | return ret += n; |
| 2043 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2044 | contiguous_span_iterator& operator+=(difference_type n) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2045 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2046 | data_ += n; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2047 | return *this; |
| 2048 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2049 | contiguous_span_iterator operator-(difference_type n) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2050 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2051 | contiguous_span_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2052 | return ret -= n; |
| 2053 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2054 | contiguous_span_iterator& operator-=(difference_type n) noexcept { return * this += -n; } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2055 | difference_type operator-(const contiguous_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2056 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2057 | Expects(m_validator == rhs.m_validator); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2058 | return data_ - rhs.data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2059 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2060 | reference operator[](difference_type n) const noexcept { return *(*this + n); } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2061 | bool operator==(const contiguous_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2062 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2063 | Expects(m_validator == rhs.m_validator); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2064 | return data_ == rhs.data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2065 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2066 | bool operator!=(const contiguous_span_iterator& rhs) const noexcept { return !(*this == rhs); } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2067 | bool operator<(const contiguous_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2068 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2069 | Expects(m_validator == rhs.m_validator); |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2070 | return data_ < rhs.data_; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2071 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2072 | bool operator<=(const contiguous_span_iterator& rhs) const noexcept { return !(rhs < *this); } |
| 2073 | bool operator>(const contiguous_span_iterator& rhs) const noexcept { return rhs < *this; } |
| 2074 | bool operator>=(const contiguous_span_iterator& rhs) const noexcept { return !(rhs > *this); } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2075 | void swap(contiguous_span_iterator& rhs) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2076 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2077 | std::swap(data_, rhs.data_); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2078 | std::swap(m_validator, rhs.m_validator); |
| 2079 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2080 | }; |
| 2081 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 2082 | template <typename Span> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2083 | contiguous_span_iterator<Span> operator+(typename contiguous_span_iterator<Span>::difference_type n, |
| 2084 | const contiguous_span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2085 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2086 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 2089 | template <typename Span> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2090 | class general_span_iterator |
| 2091 | : public std::iterator<std::random_access_iterator_tag, typename Span::value_type> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2092 | { |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 2093 | using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2094 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2095 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2096 | using typename Base::reference; |
| 2097 | using typename Base::pointer; |
| 2098 | using typename Base::difference_type; |
| 2099 | using typename Base::value_type; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2100 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2101 | private: |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 2102 | template <typename ValueType, size_t Rank> |
| 2103 | friend class strided_span; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2104 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 2105 | const Span* m_container; |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 2106 | typename Span::bounds_type::iterator m_itr; |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2107 | general_span_iterator(const Span* container, bool isbegin) |
| 2108 | : m_container(container) |
| 2109 | , m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end()) |
| 2110 | { |
| 2111 | } |
| 2112 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2113 | public: |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2114 | reference operator*() noexcept { return (*m_container)[*m_itr]; } |
| 2115 | pointer operator->() noexcept { return &(*m_container)[*m_itr]; } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2116 | general_span_iterator& operator++() noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2117 | { |
| 2118 | ++m_itr; |
| 2119 | return *this; |
| 2120 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2121 | general_span_iterator operator++(int) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2122 | { |
| 2123 | auto ret = *this; |
| 2124 | ++(*this); |
| 2125 | return ret; |
| 2126 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2127 | general_span_iterator& operator--() noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2128 | { |
| 2129 | --m_itr; |
| 2130 | return *this; |
| 2131 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2132 | general_span_iterator operator--(int) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2133 | { |
| 2134 | auto ret = *this; |
| 2135 | --(*this); |
| 2136 | return ret; |
| 2137 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2138 | general_span_iterator operator+(difference_type n) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2139 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2140 | general_span_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2141 | return ret += n; |
| 2142 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2143 | general_span_iterator& operator+=(difference_type n) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2144 | { |
| 2145 | m_itr += n; |
| 2146 | return *this; |
| 2147 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2148 | general_span_iterator operator-(difference_type n) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2149 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2150 | general_span_iterator ret{*this}; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2151 | return ret -= n; |
| 2152 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2153 | general_span_iterator& operator-=(difference_type n) noexcept { return * this += -n; } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2154 | difference_type operator-(const general_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2155 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2156 | Expects(m_container == rhs.m_container); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2157 | return m_itr - rhs.m_itr; |
| 2158 | } |
| 2159 | value_type operator[](difference_type n) const noexcept |
| 2160 | { |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2161 | return (*m_container)[m_itr[n]]; |
| 2162 | ; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2163 | } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2164 | bool operator==(const general_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2165 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2166 | Expects(m_container == rhs.m_container); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2167 | return m_itr == rhs.m_itr; |
| 2168 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2169 | bool operator!=(const general_span_iterator& rhs) const noexcept { return !(*this == rhs); } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2170 | bool operator<(const general_span_iterator& rhs) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2171 | { |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2172 | Expects(m_container == rhs.m_container); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2173 | return m_itr < rhs.m_itr; |
| 2174 | } |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2175 | bool operator<=(const general_span_iterator& rhs) const noexcept { return !(rhs < *this); } |
| 2176 | bool operator>(const general_span_iterator& rhs) const noexcept { return rhs < *this; } |
| 2177 | bool operator>=(const general_span_iterator& rhs) const noexcept { return !(rhs > *this); } |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2178 | void swap(general_span_iterator& rhs) noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2179 | { |
| 2180 | std::swap(m_itr, rhs.m_itr); |
| 2181 | std::swap(m_container, rhs.m_container); |
| 2182 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2183 | }; |
| 2184 | |
Anna Gringauze | 8aa4248 | 2015-11-11 12:41:11 -0800 | [diff] [blame] | 2185 | template <typename Span> |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2186 | general_span_iterator<Span> operator+(typename general_span_iterator<Span>::difference_type n, |
| 2187 | const general_span_iterator<Span>& rhs) noexcept |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2188 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame] | 2189 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Neil MacIntosh | ef626fd | 2015-09-29 16:41:37 -0700 | [diff] [blame] | 2192 | } // namespace gsl |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 2193 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 2194 | #ifdef _MSC_VER |
| 2195 | |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 2196 | #undef constexpr |
| 2197 | #pragma pop_macro("constexpr") |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 2198 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 2199 | #if _MSC_VER <= 1800 |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 2200 | #pragma warning(pop) |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 2201 | |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2202 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
Lukas Haselsteiner | e51eb22 | 2015-11-15 23:08:35 +0100 | [diff] [blame] | 2203 | #undef noexcept |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 2204 | #pragma pop_macro("noexcept") |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2205 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 2206 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2207 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 2208 | |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 2209 | #endif // _MSC_VER <= 1800 |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 2210 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 2211 | #endif // _MSC_VER |
| 2212 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2213 | #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 2214 | |
Anna Gringauze | f510025 | 2015-11-12 12:48:49 -0800 | [diff] [blame] | 2215 | #undef noexcept |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 2216 | |
| 2217 | #ifdef _MSC_VER |
Neil MacIntosh | d13f6da | 2015-11-20 16:03:00 -0800 | [diff] [blame] | 2218 | #pragma warning(pop) |
Neil MacIntosh | 292f81e | 2015-11-17 15:07:51 -0800 | [diff] [blame] | 2219 | #pragma pop_macro("noexcept") |
| 2220 | #endif |
| 2221 | |
Neil MacIntosh | 0cf947d | 2015-11-24 12:49:03 -0800 | [diff] [blame] | 2222 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
Treb Connell | 51da136 | 2015-09-24 18:08:34 -0700 | [diff] [blame] | 2223 | |
Neil MacIntosh | b63ec94 | 2015-11-04 12:42:27 -0800 | [diff] [blame] | 2224 | #endif // GSL_SPAN_H |