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