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