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