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