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