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