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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 76 | template <typename SizeType> |
| 77 | struct SizeTypeTraits |
| 78 | { |
| 79 | static const SizeType max_value = std::numeric_limits<SizeType>::max(); |
| 80 | }; |
Anna Gringauze | 1c208b3 | 2015-10-16 17:40:57 -0700 | [diff] [blame] | 81 | |
| 82 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 83 | template<typename... Ts> |
| 84 | class are_integral : public std::integral_constant<bool, true> {}; |
Anna Gringauze | 1c208b3 | 2015-10-16 17:40:57 -0700 | [diff] [blame] | 85 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 93 | static_assert(Rank > 0, "Rank must be greater than 0!"); |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 94 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 95 | template <size_t OtherRank> |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 99 | static const size_t rank = Rank; |
| 100 | using value_type = std::ptrdiff_t; |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 101 | using size_type = value_type; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 105 | constexpr index() noexcept |
| 106 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 107 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 108 | constexpr index(const value_type(&values)[Rank]) noexcept |
| 109 | { |
| 110 | std::copy(values, values + Rank, elems); |
| 111 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 112 | |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 113 | #ifdef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 114 | template<typename T, typename... Ts, |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 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 |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 121 | template<typename... Ts, |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 122 | typename = std::enable_if_t<(sizeof...(Ts) == Rank) && details::are_integral<Ts...>::value>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 123 | constexpr index(Ts... ds) noexcept : elems{ static_cast<value_type>(ds)... } |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 124 | {} |
| 125 | #endif |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 126 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 127 | constexpr index(const index& other) noexcept = default; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 128 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 129 | constexpr index& operator=(const index& rhs) noexcept = default; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 130 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 137 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 144 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 145 | constexpr bool operator==(const index& rhs) const noexcept |
| 146 | { |
| 147 | return std::equal(elems, elems + rank, rhs.elems); |
| 148 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 149 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 150 | constexpr bool operator!=(const index& rhs) const noexcept |
| 151 | { |
| 152 | return !(this == rhs); |
| 153 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 154 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 155 | constexpr index operator+() const noexcept |
| 156 | { |
| 157 | return *this; |
| 158 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 159 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 160 | constexpr index operator-() const noexcept |
| 161 | { |
| 162 | index ret = *this; |
| 163 | std::transform(ret, ret + rank, ret, std::negate<value_type>{}); |
| 164 | return ret; |
| 165 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 166 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 167 | constexpr index operator+(const index& rhs) const noexcept |
| 168 | { |
| 169 | index ret = *this; |
| 170 | ret += rhs; |
| 171 | return ret; |
| 172 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 173 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 174 | constexpr index operator-(const index& rhs) const noexcept |
| 175 | { |
| 176 | index ret = *this; |
| 177 | ret -= rhs; |
| 178 | return ret; |
| 179 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 180 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 181 | constexpr index& operator+=(const index& rhs) noexcept |
| 182 | { |
| 183 | std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{}); |
| 184 | return *this; |
| 185 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 186 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 187 | constexpr index& operator-=(const index& rhs) noexcept |
| 188 | { |
| 189 | std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{}); |
| 190 | return *this; |
| 191 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 192 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 193 | constexpr index operator*(value_type v) const noexcept |
| 194 | { |
| 195 | index ret = *this; |
| 196 | ret *= v; |
| 197 | return ret; |
| 198 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 199 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 200 | constexpr index operator/(value_type v) const noexcept |
| 201 | { |
| 202 | index ret = *this; |
| 203 | ret /= v; |
| 204 | return ret; |
| 205 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 206 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 207 | friend constexpr index operator*(value_type v, const index& rhs) noexcept |
| 208 | { |
| 209 | return rhs * v; |
| 210 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 211 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 212 | constexpr index& operator*=(value_type v) noexcept |
| 213 | { |
| 214 | std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); }); |
| 215 | return *this; |
| 216 | } |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 217 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 218 | constexpr index& operator/=(value_type v) noexcept |
| 219 | { |
| 220 | std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); }); |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 232 | template <typename T, typename Dummy = std::enable_if_t<std::is_integral<T>::value>> |
| 233 | constexpr operator T() const noexcept |
| 234 | { |
| 235 | return static_cast<T>(-1); |
| 236 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 237 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 |
| 240 | { |
| 241 | return static_cast<T>(-1) == other; |
| 242 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 243 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 |
| 246 | { |
| 247 | return static_cast<T>(-1) != other; |
| 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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 255 | return right == left; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 261 | return right != left; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 275 | template <std::ptrdiff_t Left, std::ptrdiff_t Right> |
| 276 | struct LessThan |
| 277 | { |
| 278 | static const bool value = Left < Right; |
| 279 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 280 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 281 | template <std::ptrdiff_t... Ranges> |
| 282 | struct BoundsRanges { |
| 283 | using size_type = std::ptrdiff_t; |
| 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> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 291 | BoundsRanges(const OtherRange&, bool /* firstLevel */) |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 292 | {} |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 293 | |
| 294 | BoundsRanges (const BoundsRanges&) = default; |
| 295 | BoundsRanges(const std::ptrdiff_t* const) { } |
| 296 | BoundsRanges() = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 297 | |
| 298 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 299 | template <typename T, size_t Dim> |
| 300 | void serialize(T&) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 301 | {} |
| 302 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 303 | template <typename T, size_t Dim> |
| 304 | size_type linearize(const T&) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 305 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 306 | return 0; |
| 307 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 308 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 309 | template <typename T, size_t Dim> |
| 310 | bool contains(const T&) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 311 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 312 | return 0; |
| 313 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 314 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 315 | size_type totalSize() const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 316 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 317 | return TotalSize; |
| 318 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 319 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 320 | bool operator==(const BoundsRanges&) const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 321 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 322 | return true; |
| 323 | } |
| 324 | }; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 325 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 326 | template <std::ptrdiff_t... RestRanges> |
| 327 | struct BoundsRanges <dynamic_range, RestRanges...> : BoundsRanges<RestRanges...>{ |
| 328 | using Base = BoundsRanges <RestRanges... >; |
| 329 | using size_type = std::ptrdiff_t; |
| 330 | static const size_t Depth = Base::Depth + 1; |
| 331 | static const size_t DynamicNum = Base::DynamicNum + 1; |
| 332 | static const size_type CurrentRange = dynamic_range; |
| 333 | static const size_type TotalSize = dynamic_range; |
| 334 | const size_type m_bound; |
| 335 | |
| 336 | BoundsRanges (const BoundsRanges&) = default; |
| 337 | |
| 338 | BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr + 1), m_bound(*arr * this->Base::totalSize()) |
| 339 | { |
| 340 | fail_fast_assert(0 <= *arr); |
| 341 | } |
| 342 | |
| 343 | BoundsRanges() : m_bound(0) {} |
| 344 | |
| 345 | template <std::ptrdiff_t OtherRange, std::ptrdiff_t... RestOtherRanges> |
| 346 | BoundsRanges(const BoundsRanges<OtherRange, RestOtherRanges...>& other, bool /* firstLevel */ = true) : |
| 347 | Base(static_cast<const BoundsRanges<RestOtherRanges...>&>(other), false), m_bound(other.totalSize()) |
| 348 | {} |
| 349 | |
| 350 | template <typename T, size_t Dim = 0> |
| 351 | void serialize(T& arr) const |
| 352 | { |
| 353 | arr[Dim] = elementNum(); |
| 354 | this->Base::template serialize<T, Dim + 1>(arr); |
| 355 | } |
| 356 | |
| 357 | template <typename T, size_t Dim = 0> |
| 358 | size_type linearize(const T& arr) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 359 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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); |
| 363 | } |
| 364 | |
| 365 | template <typename T, size_t Dim = 0> |
| 366 | size_type contains(const T & arr) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 367 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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]; |
| 372 | return cur < m_bound ? cur + last : -1; |
| 373 | } |
| 374 | |
| 375 | size_type totalSize() const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 376 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 382 | return totalSize() / this->Base::totalSize(); |
| 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 387 | if (dim > 0) |
| 388 | return this->Base::elementNum(dim - 1); |
| 389 | else |
| 390 | return elementNum(); |
| 391 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 392 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 393 | bool operator == (const BoundsRanges & rhs) const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 394 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 395 | return m_bound == rhs.m_bound && static_cast<const Base&>(*this) == static_cast<const Base&>(rhs); |
| 396 | } |
| 397 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 398 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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... >; |
| 403 | using size_type = std::ptrdiff_t; |
| 404 | static const size_t Depth = Base::Depth + 1; |
| 405 | static const size_t DynamicNum = Base::DynamicNum; |
| 406 | static const size_type CurrentRange = CurRange; |
| 407 | static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize; |
| 408 | |
| 409 | BoundsRanges (const BoundsRanges&) = default; |
| 410 | BoundsRanges(const std::ptrdiff_t* const arr) : Base(arr) { } |
| 411 | BoundsRanges() = default; |
| 412 | |
| 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) |
| 415 | { |
| 416 | fail_fast_assert((firstLevel && totalSize() <= other.totalSize()) || totalSize() == other.totalSize()); |
| 417 | } |
| 418 | |
| 419 | template <typename T, size_t Dim = 0> |
| 420 | void serialize(T& arr) const |
| 421 | { |
| 422 | arr[Dim] = elementNum(); |
| 423 | this->Base::template serialize<T, Dim + 1>(arr); |
| 424 | } |
| 425 | |
| 426 | template <typename T, size_t Dim = 0> |
| 427 | size_type linearize(const T& arr) const |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 428 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 429 | fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range"); |
| 430 | return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr); |
| 431 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 432 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 436 | if (arr[Dim] >= CurrentRange) |
| 437 | return -1; |
| 438 | const size_type last = this->Base::template contains<T, Dim + 1>(arr); |
| 439 | if (last == -1) |
| 440 | return -1; |
| 441 | return this->Base::totalSize() * arr[Dim] + last; |
| 442 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 443 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 444 | size_type totalSize() const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 445 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 446 | return CurrentRange * this->Base::totalSize(); |
| 447 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 448 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 449 | size_type elementNum() const noexcept |
| 450 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 451 | return CurrentRange; |
| 452 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 453 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 454 | size_type elementNum(size_t dim) const noexcept |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 455 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 456 | if (dim > 0) |
| 457 | return this->Base::elementNum(dim - 1); |
| 458 | else |
| 459 | return elementNum(); |
| 460 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 461 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 462 | bool operator== (const BoundsRanges& rhs) const noexcept |
| 463 | { |
| 464 | return static_cast<const Base &>(*this) == static_cast<const Base &>(rhs); |
| 465 | } |
| 466 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 467 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 468 | template <typename SourceType, typename TargetType, size_t Rank> |
| 469 | struct BoundsRangeConvertible2; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 470 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 473 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 482 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 483 | template <typename SourceType, typename TargetType> |
| 484 | struct BoundsRangeConvertible2<SourceType, TargetType, 0> : std::true_type {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 485 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 486 | template <typename SourceType, typename TargetType, std::ptrdiff_t Rank = TargetType::Depth> |
| 487 | struct BoundsRangeConvertible : decltype(helpBoundsRangeConvertible<Rank - 1>(SourceType(), TargetType(), |
| 488 | std::integral_constant<bool, SourceType::Depth == TargetType::Depth |
| 489 | && (!LessThan<SourceType::CurrentRange, TargetType::CurrentRange>::value || TargetType::CurrentRange == dynamic_range || SourceType::CurrentRange == dynamic_range)>())) |
| 490 | {}; |
| 491 | template <typename SourceType, typename TargetType> |
| 492 | struct BoundsRangeConvertible<SourceType, TargetType, 0> : std::true_type {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 493 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 494 | template <typename TypeChain> |
| 495 | struct TypeListIndexer |
| 496 | { |
| 497 | const TypeChain & obj; |
| 498 | TypeListIndexer(const TypeChain & obj) :obj(obj){} |
| 499 | template<size_t N> |
| 500 | const TypeChain & getObj(std::true_type) |
| 501 | { |
| 502 | return obj; |
| 503 | } |
| 504 | template<size_t N, typename MyChain = TypeChain, typename MyBase = typename MyChain::Base> |
| 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 | } |
| 509 | template <size_t N> |
| 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 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 515 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 |
| 524 | { |
| 525 | Ret ret{}; |
| 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 541 | static_bounds(const details::BoundsRanges<Ranges...>&) { |
| 542 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 548 | using MyRanges = details::BoundsRanges<FirstRange, RestRanges... >; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 549 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 550 | MyRanges m_ranges; |
| 551 | constexpr static_bounds(const MyRanges& range) : m_ranges(range) |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 552 | {} |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 553 | |
| 554 | template <std::ptrdiff_t... OtherRanges> |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 558 | static const size_t rank = MyRanges::Depth; |
| 559 | static const size_t dynamic_rank = MyRanges::DynamicNum; |
| 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 568 | using sliced_type = static_bounds<RestRanges...>; |
| 569 | using mapping_type = contiguous_mapping_tag; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 570 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 571 | constexpr static_bounds(const static_bounds&) = default; |
| 572 | |
| 573 | template <std::ptrdiff_t... Ranges, typename Dummy = std::enable_if_t< |
| 574 | details::BoundsRangeConvertible<details::BoundsRanges<Ranges...>, details::BoundsRanges <FirstRange, RestRanges... >>::value>> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 578 | constexpr static_bounds(std::initializer_list<size_type> il) : m_ranges((const std::ptrdiff_t*)il.begin()) |
| 579 | { |
| 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"); |
| 581 | fail_fast_assert(m_ranges.totalSize() <= PTRDIFF_MAX, "Size of the range is larger than the max element of the size type"); |
| 582 | } |
| 583 | |
| 584 | constexpr static_bounds() = default; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 585 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 586 | constexpr static_bounds& operator=(const static_bounds& otherBounds) |
| 587 | { |
| 588 | new(&m_ranges) MyRanges (otherBounds.m_ranges); |
| 589 | return *this; |
| 590 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 591 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 592 | constexpr sliced_type slice() const noexcept |
| 593 | { |
| 594 | return sliced_type{static_cast<const details::BoundsRanges<RestRanges...> &>(m_ranges)}; |
| 595 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 596 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 597 | constexpr size_type stride() const noexcept |
| 598 | { |
| 599 | return rank > 1 ? slice().size() : 1; |
| 600 | } |
| 601 | |
| 602 | constexpr size_type size() const noexcept |
| 603 | { |
| 604 | return m_ranges.totalSize(); |
| 605 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 606 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 607 | constexpr size_type total_size() const noexcept |
| 608 | { |
| 609 | return m_ranges.totalSize(); |
| 610 | } |
| 611 | |
| 612 | constexpr size_type linearize(const index_type & idx) const |
| 613 | { |
| 614 | return m_ranges.linearize(idx); |
| 615 | } |
| 616 | |
| 617 | constexpr bool contains(const index_type& idx) const noexcept |
| 618 | { |
| 619 | return m_ranges.contains(idx) != -1; |
| 620 | } |
| 621 | |
| 622 | constexpr size_type operator[](size_t index) const noexcept |
| 623 | { |
| 624 | return m_ranges.elementNum(index); |
| 625 | } |
| 626 | |
| 627 | template <size_t Dim = 0> |
| 628 | constexpr size_type extent() const noexcept |
| 629 | { |
| 630 | static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)"); |
| 631 | return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum(); |
| 632 | } |
| 633 | |
| 634 | constexpr index_type index_bounds() const noexcept |
| 635 | { |
| 636 | size_type extents[rank] = {}; |
| 637 | m_ranges.serialize(extents); |
| 638 | return{ extents }; |
| 639 | } |
| 640 | |
| 641 | template <std::ptrdiff_t... Ranges> |
| 642 | constexpr bool operator == (const static_bounds<Ranges...>& rhs) const noexcept |
| 643 | { |
| 644 | return this->size() == rhs.size(); |
| 645 | } |
| 646 | |
| 647 | template <std::ptrdiff_t... Ranges> |
| 648 | constexpr bool operator != (const static_bounds<Ranges...>& rhs) const noexcept |
| 649 | { |
| 650 | return !(*this == rhs); |
| 651 | } |
| 652 | |
| 653 | constexpr const_iterator begin() const noexcept |
| 654 | { |
| 655 | return const_iterator(*this, index_type{}); |
| 656 | } |
| 657 | |
| 658 | constexpr const_iterator end() const noexcept |
| 659 | { |
| 660 | return const_iterator(*this, this->index_bounds()); |
| 661 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 667 | template <size_t OtherRank> |
| 668 | friend class strided_bounds; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 669 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 670 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 682 | static const value_type static_size = dynamic_range; |
| 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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 696 | constexpr index_type strides() const noexcept |
| 697 | { |
| 698 | return m_strides; |
| 699 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 700 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 701 | constexpr size_type total_size() const noexcept |
| 702 | { |
| 703 | size_type ret = 0; |
| 704 | for (size_t i = 0; i < rank; ++i) |
| 705 | { |
| 706 | ret += (m_extents[i] - 1) * m_strides[i]; |
| 707 | } |
| 708 | return ret + 1; |
| 709 | } |
| 710 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 711 | constexpr size_type size() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 712 | { |
| 713 | size_type ret = 1; |
| 714 | for (size_t i = 0; i < rank; ++i) |
| 715 | { |
| 716 | ret *= m_extents[i]; |
| 717 | } |
| 718 | return ret; |
| 719 | } |
| 720 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 721 | constexpr bool contains(const index_type& idx) const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 722 | { |
| 723 | for (size_t i = 0; i < rank; ++i) |
| 724 | { |
| 725 | if (idx[i] < 0 || idx[i] >= m_extents[i]) |
| 726 | return false; |
| 727 | } |
| 728 | return true; |
| 729 | } |
Neil MacIntosh | d0f09e7 | 2015-10-15 16:38:53 -0700 | [diff] [blame] | 730 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 731 | constexpr size_type linearize(const index_type& idx) const noexcept |
| 732 | { |
| 733 | size_type ret = 0; |
| 734 | for (size_t i = 0; i < rank; i++) |
| 735 | { |
| 736 | fail_fast_assert(idx[i] < m_extents[i], "index is out of bounds of the array"); |
| 737 | ret += idx[i] * m_strides[i]; |
| 738 | } |
| 739 | return ret; |
| 740 | } |
| 741 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 742 | constexpr size_type stride() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 743 | { |
| 744 | return m_strides[0]; |
| 745 | } |
| 746 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 747 | template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 748 | constexpr sliced_type slice() const |
| 749 | { |
| 750 | return{ details::shift_left(m_extents), details::shift_left(m_strides) }; |
| 751 | } |
| 752 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 753 | template <size_t Dim = 0> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 754 | constexpr size_type extent() const noexcept |
| 755 | { |
| 756 | static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)"); |
| 757 | return m_extents[Dim]; |
| 758 | } |
| 759 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 760 | constexpr index_type index_bounds() const noexcept |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 761 | { |
| 762 | return m_extents; |
| 763 | } |
| 764 | constexpr const_iterator begin() const noexcept |
| 765 | { |
| 766 | return const_iterator{ *this, index_type{} }; |
| 767 | } |
| 768 | |
| 769 | constexpr const_iterator end() const noexcept |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 775 | index_type m_extents; |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 790 | using Base = std::iterator <std::random_access_iterator_tag, IndexType>; |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 791 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 792 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 793 | static const size_t rank = IndexType::rank; |
| 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; |
| 799 | using index_size_type = typename IndexType::value_type; |
| 800 | template <typename Bounds> |
| 801 | explicit bounds_iterator(const Bounds& bnd, value_type curr) noexcept |
| 802 | : boundary(bnd.index_bounds()), curr(std::move(curr)) |
| 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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 807 | constexpr reference operator*() const noexcept |
| 808 | { |
| 809 | return curr; |
| 810 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 811 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 812 | constexpr pointer operator->() const noexcept |
| 813 | { |
| 814 | return &curr; |
| 815 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 816 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 817 | constexpr bounds_iterator& operator++() noexcept |
| 818 | { |
| 819 | for (size_t i = rank; i-- > 0;) |
| 820 | { |
| 821 | if (curr[i] < boundary[i] - 1) |
| 822 | { |
| 823 | curr[i]++; |
| 824 | return *this; |
| 825 | } |
| 826 | curr[i] = 0; |
| 827 | } |
| 828 | // If we're here we've wrapped over - set to past-the-end. |
| 829 | curr = boundary; |
| 830 | return *this; |
| 831 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 832 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 833 | constexpr bounds_iterator operator++(int) noexcept |
| 834 | { |
| 835 | auto ret = *this; |
| 836 | ++(*this); |
| 837 | return ret; |
| 838 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 839 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 840 | constexpr bounds_iterator& operator--() noexcept |
| 841 | { |
| 842 | if (!less(curr, boundary)) |
| 843 | { |
| 844 | // if at the past-the-end, set to last element |
| 845 | for (size_t i = 0; i < rank; ++i) |
| 846 | { |
| 847 | curr[i] = boundary[i] - 1; |
| 848 | } |
| 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; |
| 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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 866 | constexpr bounds_iterator operator--(int) noexcept |
| 867 | { |
| 868 | auto ret = *this; |
| 869 | --(*this); |
| 870 | return ret; |
| 871 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 872 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 873 | constexpr bounds_iterator operator+(difference_type n) const noexcept |
| 874 | { |
| 875 | bounds_iterator ret{ *this }; |
| 876 | return ret += n; |
| 877 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 878 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 879 | constexpr bounds_iterator& operator+=(difference_type n) noexcept |
| 880 | { |
| 881 | auto linear_idx = linearize(curr) + n; |
| 882 | std::remove_const_t<value_type> stride = 0; |
| 883 | stride[rank - 1] = 1; |
| 884 | for (size_t i = rank - 1; i-- > 0;) |
| 885 | { |
| 886 | stride[i] = stride[i + 1] * boundary[i + 1]; |
| 887 | } |
| 888 | for (size_t i = 0; i < rank; ++i) |
| 889 | { |
| 890 | curr[i] = linear_idx / stride[i]; |
| 891 | linear_idx = linear_idx % stride[i]; |
| 892 | } |
| 893 | fail_fast_assert(!less(curr, index_type{}) && !less(boundary, curr), "index is out of bounds of the array"); |
| 894 | return *this; |
| 895 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 896 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 897 | constexpr bounds_iterator operator-(difference_type n) const noexcept |
| 898 | { |
| 899 | bounds_iterator ret{ *this }; |
| 900 | return ret -= n; |
| 901 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 902 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 903 | constexpr bounds_iterator& operator-=(difference_type n) noexcept |
| 904 | { |
| 905 | return *this += -n; |
| 906 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 907 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 908 | constexpr difference_type operator-(const bounds_iterator& rhs) const noexcept |
| 909 | { |
| 910 | return linearize(curr) - linearize(rhs.curr); |
| 911 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 912 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 913 | constexpr value_type operator[](difference_type n) const noexcept |
| 914 | { |
| 915 | return *(*this + n); |
| 916 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 917 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 918 | constexpr bool operator==(const bounds_iterator& rhs) const noexcept |
| 919 | { |
| 920 | return curr == rhs.curr; |
| 921 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 922 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 923 | constexpr bool operator!=(const bounds_iterator& rhs) const noexcept |
| 924 | { |
| 925 | return !(*this == rhs); |
| 926 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 927 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 928 | constexpr bool operator<(const bounds_iterator& rhs) const noexcept |
| 929 | { |
| 930 | return less(curr, rhs.curr); |
| 931 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 932 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 933 | constexpr bool operator<=(const bounds_iterator& rhs) const noexcept |
| 934 | { |
| 935 | return !(rhs < *this); |
| 936 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 937 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 938 | constexpr bool operator>(const bounds_iterator& rhs) const noexcept |
| 939 | { |
| 940 | return rhs < *this; |
| 941 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 942 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 943 | constexpr bool operator>=(const bounds_iterator& rhs) const noexcept |
| 944 | { |
| 945 | return !(rhs > *this); |
| 946 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 947 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 948 | void swap(bounds_iterator& rhs) noexcept |
| 949 | { |
| 950 | std::swap(boundary, rhs.boundary); |
| 951 | std::swap(curr, rhs.curr); |
| 952 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 953 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | } |
Anna Gringauze | a4654a4 | 2015-10-16 12:15:22 -0700 | [diff] [blame] | 963 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 964 | constexpr index_size_type linearize(const value_type& idx) const noexcept |
| 965 | { |
| 966 | // TODO: Smarter impl. |
| 967 | // Check if past-the-end |
| 968 | index_size_type multiplier = 1; |
| 969 | index_size_type res = 0; |
| 970 | if (!less(idx, boundary)) |
| 971 | { |
| 972 | res = 1; |
| 973 | for (size_t i = rank; i-- > 0;) |
| 974 | { |
| 975 | res += (idx[i] - 1) * multiplier; |
| 976 | multiplier *= boundary[i]; |
| 977 | } |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | for (size_t i = rank; i-- > 0;) |
| 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 990 | value_type boundary; |
| 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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 997 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1005 | template <typename Bounds> |
| 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 |
| 1007 | { |
| 1008 | return bnd.strides(); |
| 1009 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1010 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1011 | // Make a stride vector from bounds, assuming contiguous memory. |
| 1012 | template <typename Bounds> |
| 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 |
| 1014 | { |
| 1015 | auto extents = bnd.index_bounds(); |
| 1016 | typename Bounds::size_type stride[Bounds::rank] = {}; |
Anna Gringauze | db38497 | 2015-10-05 12:34:23 -0700 | [diff] [blame] | 1017 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1018 | stride[Bounds::rank - 1] = 1; |
| 1019 | for (size_t i = 1; i < Bounds::rank; ++i) |
| 1020 | { |
| 1021 | stride[Bounds::rank - i - 1] = stride[Bounds::rank - i] * extents[Bounds::rank - i]; |
| 1022 | } |
| 1023 | return{ stride }; |
| 1024 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1025 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1048 | static const size_t rank = BoundsType::rank; |
| 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; |
| 1053 | using const_value_type = std::add_const_t<value_type>; |
| 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>>; |
| 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>>>; |
| 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>>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1061 | |
| 1062 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1063 | pointer m_pdata; |
| 1064 | bounds_type m_bounds; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1065 | |
| 1066 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1067 | constexpr bounds_type bounds() const noexcept |
| 1068 | { |
| 1069 | return m_bounds; |
| 1070 | } |
| 1071 | template <size_t Dim = 0> |
| 1072 | constexpr size_type extent() const noexcept |
| 1073 | { |
| 1074 | static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)"); |
| 1075 | return m_bounds.template extent<Dim>(); |
| 1076 | } |
| 1077 | constexpr size_type size() const noexcept |
| 1078 | { |
| 1079 | return m_bounds.size(); |
| 1080 | } |
| 1081 | constexpr reference operator[](const index_type& idx) const |
| 1082 | { |
| 1083 | return m_pdata[m_bounds.linearize(idx)]; |
| 1084 | } |
| 1085 | constexpr pointer data() const noexcept |
| 1086 | { |
| 1087 | return m_pdata; |
| 1088 | } |
| 1089 | template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> |
| 1090 | constexpr Ret operator[](size_type idx) const |
| 1091 | { |
| 1092 | fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array"); |
| 1093 | const size_type ridx = idx * m_bounds.stride(); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1094 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1095 | fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data"); |
| 1096 | return Ret {m_pdata + ridx, m_bounds.slice()}; |
| 1097 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1098 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1099 | constexpr operator bool () const noexcept |
| 1100 | { |
| 1101 | return m_pdata != nullptr; |
| 1102 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1103 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1104 | constexpr iterator begin() const |
| 1105 | { |
| 1106 | return iterator {this, true}; |
| 1107 | } |
| 1108 | constexpr iterator end() const |
| 1109 | { |
| 1110 | return iterator {this, false}; |
| 1111 | } |
| 1112 | constexpr const_iterator cbegin() const |
| 1113 | { |
| 1114 | return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), true}; |
| 1115 | } |
| 1116 | constexpr const_iterator cend() const |
| 1117 | { |
| 1118 | return const_iterator {reinterpret_cast<const basic_array_view<const value_type, bounds_type> *>(this), false}; |
| 1119 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1120 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1121 | constexpr reverse_iterator rbegin() const |
| 1122 | { |
| 1123 | return reverse_iterator {end()}; |
| 1124 | } |
| 1125 | constexpr reverse_iterator rend() const |
| 1126 | { |
| 1127 | return reverse_iterator {begin()}; |
| 1128 | } |
| 1129 | constexpr const_reverse_iterator crbegin() const |
| 1130 | { |
| 1131 | return const_reverse_iterator {cend()}; |
| 1132 | } |
| 1133 | constexpr const_reverse_iterator crend() const |
| 1134 | { |
| 1135 | return const_reverse_iterator {cbegin()}; |
| 1136 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1137 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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>> |
| 1139 | constexpr bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 1140 | { |
| 1141 | return m_bounds.size() == other.m_bounds.size() && |
| 1142 | (m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin())); |
| 1143 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1144 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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>> |
| 1146 | constexpr bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 1147 | { |
| 1148 | return !(*this == other); |
| 1149 | } |
Neil MacIntosh | 9f9fad9 | 2015-08-27 18:13:49 -0700 | [diff] [blame] | 1150 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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>> |
| 1152 | constexpr bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 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 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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>> |
| 1158 | constexpr bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 1159 | { |
| 1160 | return !(other < *this); |
| 1161 | } |
Neil MacIntosh | 9f9fad9 | 2015-08-27 18:13:49 -0700 | [diff] [blame] | 1162 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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>> |
| 1164 | constexpr bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 1165 | { |
| 1166 | return (other < *this); |
| 1167 | } |
Neil MacIntosh | 9f9fad9 | 2015-08-27 18:13:49 -0700 | [diff] [blame] | 1168 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [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>> |
| 1170 | constexpr bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const noexcept |
| 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: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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>> |
| 1179 | constexpr basic_array_view(const basic_array_view<OtherValueType, OtherBounds> & other ) noexcept |
| 1180 | : m_pdata(other.m_pdata), m_bounds(other.m_bounds) |
| 1181 | { |
| 1182 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1183 | protected: |
| 1184 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1185 | constexpr basic_array_view(pointer data, bounds_type bound) noexcept |
| 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> |
| 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 |
| 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> |
| 1199 | constexpr basic_array_view<value_type, DestBounds> as_array_view(const DestBounds &bounds) |
| 1200 | { |
| 1201 | details::verifyBoundsReshape(m_bounds, bounds); |
| 1202 | return {m_pdata, bounds}; |
| 1203 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1204 | private: |
| 1205 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1206 | friend iterator; |
| 1207 | friend const_iterator; |
| 1208 | template <typename ValueType2, typename BoundsType2> |
| 1209 | friend class basic_array_view; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [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 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1233 | template <typename T, typename = std::true_type> |
| 1234 | struct ArrayViewTypeTraits |
| 1235 | { |
| 1236 | using value_type = T; |
| 1237 | using size_type = size_t; |
| 1238 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1239 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 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 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1246 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1247 | template <typename T, std::ptrdiff_t... Ranks> |
| 1248 | struct ArrayViewArrayTraits { |
| 1249 | using type = array_view<T, Ranks...>; |
| 1250 | using value_type = T; |
| 1251 | using bounds_type = static_bounds<Ranks...>; |
| 1252 | using pointer = T*; |
| 1253 | using reference = T&; |
| 1254 | }; |
| 1255 | template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks> |
| 1256 | struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1257 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1258 | template <typename BoundsType> |
| 1259 | BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size |
| 1260 | { |
| 1261 | fail_fast_assert(totalSize <= PTRDIFF_MAX); |
| 1262 | return BoundsType{totalSize}; |
| 1263 | } |
| 1264 | template <typename BoundsType> |
| 1265 | BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::false_type) // static size |
| 1266 | { |
| 1267 | fail_fast_assert(BoundsType::static_size == totalSize); |
| 1268 | return {}; |
| 1269 | } |
| 1270 | template <typename BoundsType> |
| 1271 | BoundsType newBoundsHelper(std::ptrdiff_t totalSize) |
| 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 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1294 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1295 | template <typename ...Dimensions> |
| 1296 | struct static_as_array_view_static_bounds_helper |
| 1297 | { |
| 1298 | using type = static_bounds<(Dimensions::value)...>; |
| 1299 | }; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1300 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1301 | template <typename T> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1302 | struct is_array_view_oracle : std::false_type |
| 1303 | {}; |
| 1304 | |
| 1305 | template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions> |
| 1306 | struct is_array_view_oracle<array_view<ValueType, FirstDimension, RestDimensions...>> : std::true_type |
| 1307 | {}; |
| 1308 | |
| 1309 | template <typename ValueType, std::ptrdiff_t Rank> |
| 1310 | struct is_array_view_oracle<strided_array_view<ValueType, Rank>> : std::true_type |
| 1311 | {}; |
| 1312 | |
| 1313 | template <typename T> |
| 1314 | struct is_array_view : is_array_view_oracle<std::remove_cv_t<T>> |
| 1315 | {}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 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 | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1323 | template <typename ValueType2, std::ptrdiff_t FirstDimension2, |
| 1324 | std::ptrdiff_t... RestDimensions2> |
| 1325 | friend class array_view; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1326 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1327 | using Base = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1328 | |
| 1329 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1330 | using typename Base::bounds_type; |
| 1331 | using typename Base::size_type; |
| 1332 | using typename Base::pointer; |
| 1333 | using typename Base::value_type; |
| 1334 | using typename Base::index_type; |
| 1335 | using typename Base::iterator; |
| 1336 | using typename Base::const_iterator; |
| 1337 | using typename Base::reference; |
| 1338 | using Base::rank; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1339 | |
| 1340 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1341 | // basic |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1342 | constexpr array_view(pointer ptr, size_type size) : Base(ptr, bounds_type{ size }) |
| 1343 | {} |
| 1344 | |
| 1345 | constexpr array_view(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds)) |
| 1346 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1347 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1348 | constexpr array_view(std::nullptr_t) : Base(nullptr, bounds_type{}) |
| 1349 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1350 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1351 | constexpr array_view(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{}) |
| 1352 | { |
| 1353 | fail_fast_assert(size == 0); |
| 1354 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1355 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1356 | // default |
| 1357 | template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>> |
| 1358 | constexpr array_view() : Base(nullptr, bounds_type()) |
| 1359 | {} |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1360 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1361 | // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer) |
| 1362 | template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range> |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 1363 | /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1364 | constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size}) |
| 1365 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1366 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1367 | // from n-dimensions static array |
| 1368 | template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>, |
Neil MacIntosh | 41517ff | 2015-11-04 02:11:49 +0000 | [diff] [blame] | 1369 | typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>> |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1370 | constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type()) |
| 1371 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1372 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1373 | // from n-dimensions static array with size |
| 1374 | template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>, |
| 1375 | 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] | 1376 | > |
Neil MacIntosh | 41517ff | 2015-11-04 02:11:49 +0000 | [diff] [blame] | 1377 | constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{size}) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1378 | { |
| 1379 | fail_fast_assert(size <= N); |
| 1380 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1381 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1382 | // from std array |
| 1383 | template <size_t N, |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1384 | typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value> |
| 1385 | > |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1386 | constexpr array_view (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>()) |
| 1387 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1388 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1389 | template <size_t N, |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1390 | typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value |
| 1391 | && std::is_const<value_type>::value> |
| 1392 | > |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1393 | constexpr array_view (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>()) |
| 1394 | {} |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1395 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1396 | // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity |
| 1397 | template <typename Ptr, |
| 1398 | typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value |
| 1399 | && details::LessThan<Base::bounds_type::dynamic_rank, 2>::value> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1400 | > // remove literal 0 case |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1401 | constexpr array_view (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin)) |
| 1402 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1403 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1404 | // from containers. It must has .size() and .data() two function signatures |
| 1405 | template <typename Cont, typename DataType = typename Cont::value_type, |
| 1406 | typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value |
| 1407 | && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value |
| 1408 | && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value> |
| 1409 | > |
| 1410 | constexpr array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size())) |
| 1411 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1412 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1413 | constexpr array_view(const array_view &) = default; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1414 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1415 | // convertible |
| 1416 | template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, |
| 1417 | typename BaseType = basic_array_view<ValueType, static_bounds<FirstDimension, RestDimensions...>>, |
| 1418 | typename OtherBaseType = basic_array_view<OtherValueType, static_bounds<OtherDimensions...>>, |
| 1419 | typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value> |
| 1420 | > |
| 1421 | constexpr array_view(const array_view<OtherValueType, OtherDimensions...> &av) |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1422 | : Base(static_cast<const typename array_view<OtherValueType, OtherDimensions...>::Base&>(av)) |
| 1423 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1424 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1425 | // reshape |
Neil MacIntosh | 14d50a6 | 2015-11-03 12:44:09 -0800 | [diff] [blame] | 1426 | // DimCount here is a workaround for a bug in MSVC 2015 |
| 1427 | template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), typename = std::enable_if_t<(DimCount > 0)>> |
| 1428 | constexpr array_view<ValueType, Dimensions2::value...> as_array_view(Dimensions2... dims) |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1429 | { |
| 1430 | using BoundsType = typename array_view<ValueType, (Dimensions2::value)...>::bounds_type; |
| 1431 | auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{}); |
| 1432 | details::verifyBoundsReshape(this->bounds(), tobounds); |
| 1433 | return {this->data(), tobounds}; |
| 1434 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1435 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1436 | // to bytes array |
| 1437 | template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value> |
| 1438 | auto as_bytes() const noexcept -> array_view<const byte> |
| 1439 | { |
| 1440 | static_assert(Enabled, "The value_type of array_view must be standarded layout"); |
| 1441 | return { reinterpret_cast<const byte*>(this->data()), this->bytes() }; |
| 1442 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1443 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1444 | template <bool Enabled = std::is_standard_layout<std::decay_t<ValueType>>::value> |
| 1445 | auto as_writeable_bytes() const noexcept -> array_view<byte> |
| 1446 | { |
| 1447 | static_assert(Enabled, "The value_type of array_view must be standarded layout"); |
| 1448 | return { reinterpret_cast<byte*>(this->data()), this->bytes() }; |
| 1449 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1450 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1451 | // from bytes array |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1452 | template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> |
| 1453 | 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)> |
| 1454 | { |
| 1455 | 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), |
| 1456 | "Target type must be standard layout and its size must match the byte array size"); |
| 1457 | fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX); |
| 1458 | return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) }; |
| 1459 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1460 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1461 | template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> |
| 1462 | 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)> |
| 1463 | { |
| 1464 | 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), |
| 1465 | "Target type must be standard layout and its size must match the byte array size"); |
| 1466 | fail_fast_assert((this->bytes() % sizeof(U)) == 0); |
| 1467 | return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) }; |
| 1468 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1469 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1470 | // section on linear space |
| 1471 | template<std::ptrdiff_t Count> |
| 1472 | constexpr array_view<ValueType, Count> first() const noexcept |
| 1473 | { |
| 1474 | static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound"); |
| 1475 | fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed |
| 1476 | return { this->data(), Count }; |
| 1477 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1478 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1479 | constexpr array_view<ValueType, dynamic_range> first(size_type count) const noexcept |
| 1480 | { |
| 1481 | fail_fast_assert(count <= this->size()); |
| 1482 | return { this->data(), count }; |
| 1483 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1484 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1485 | template<std::ptrdiff_t Count> |
| 1486 | constexpr array_view<ValueType, Count> last() const noexcept |
| 1487 | { |
| 1488 | static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound"); |
| 1489 | fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); |
| 1490 | return { this->data() + this->size() - Count, Count }; |
| 1491 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1492 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1493 | constexpr array_view<ValueType, dynamic_range> last(size_type count) const noexcept |
| 1494 | { |
| 1495 | fail_fast_assert(count <= this->size()); |
| 1496 | return { this->data() + this->size() - count, count }; |
| 1497 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1498 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1499 | template<std::ptrdiff_t Offset, std::ptrdiff_t Count> |
| 1500 | constexpr array_view<ValueType, Count> sub() const noexcept |
| 1501 | { |
| 1502 | 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"); |
| 1503 | fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset <= this->size()) && Offset + Count <= this->size())); |
| 1504 | return { this->data() + Offset, Count }; |
| 1505 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1506 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1507 | constexpr array_view<ValueType, dynamic_range> sub(size_type offset, size_type count = dynamic_range) const noexcept |
| 1508 | { |
| 1509 | fail_fast_assert((offset == 0 || offset <= this->size()) && (count == dynamic_range || (offset + count) <= this->size())); |
| 1510 | return { this->data() + offset, count == dynamic_range ? this->length() - offset : count }; |
| 1511 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1512 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1513 | // size |
| 1514 | constexpr size_type length() const noexcept |
| 1515 | { |
| 1516 | return this->size(); |
| 1517 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1518 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1519 | constexpr size_type used_length() const noexcept |
| 1520 | { |
| 1521 | return length(); |
| 1522 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1523 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1524 | constexpr size_type bytes() const noexcept |
| 1525 | { |
| 1526 | return sizeof(value_type) * this->size(); |
| 1527 | } |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1528 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1529 | constexpr size_type used_bytes() const noexcept |
| 1530 | { |
| 1531 | return bytes(); |
| 1532 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1533 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1534 | // section |
| 1535 | constexpr strided_array_view<ValueType, rank> section(index_type origin, index_type extents) const |
| 1536 | { |
| 1537 | size_type size = this->bounds().total_size() - this->bounds().linearize(origin); |
| 1538 | return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} }; |
| 1539 | } |
| 1540 | |
| 1541 | constexpr reference operator[](const index_type& idx) const |
| 1542 | { |
| 1543 | return Base::operator[](idx); |
| 1544 | } |
| 1545 | |
| 1546 | template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> |
| 1547 | constexpr array_view<ValueType, RestDimensions...> operator[](size_type idx) const |
| 1548 | { |
| 1549 | auto ret = Base::operator[](idx); |
| 1550 | return{ ret.data(), ret.bounds() }; |
| 1551 | } |
Neil MacIntosh | 9f9fad9 | 2015-08-27 18:13:49 -0700 | [diff] [blame] | 1552 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1553 | using Base::operator==; |
| 1554 | using Base::operator!=; |
| 1555 | using Base::operator<; |
| 1556 | using Base::operator<=; |
| 1557 | using Base::operator>; |
| 1558 | using Base::operator>=; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1559 | }; |
| 1560 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1561 | template <typename T, std::ptrdiff_t... Dimensions> |
| 1562 | 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] | 1563 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1564 | 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] | 1565 | } |
| 1566 | |
| 1567 | template <typename T> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1568 | 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] | 1569 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1570 | return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | template <typename T, size_t N> |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1574 | 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] | 1575 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1576 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | template <typename T, size_t N> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1580 | 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] | 1581 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1582 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | template <typename T, size_t N> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1586 | 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] | 1587 | |
| 1588 | template <typename T, size_t N> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1589 | 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] | 1590 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1591 | return {arr}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | template <typename T> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1595 | 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] | 1596 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1597 | return {begin, end}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | template <typename Cont> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1601 | constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value, |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1602 | array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1603 | { |
Neil MacIntosh | ace9ab9 | 2015-10-23 19:49:17 -0700 | [diff] [blame] | 1604 | fail_fast_assert(arr.size() < PTRDIFF_MAX); |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1605 | return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())}; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | template <typename Cont> |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1609 | constexpr auto as_array_view(Cont &&arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value, |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1610 | array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1611 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1612 | template <typename ValueType, size_t Rank> |
| 1613 | class strided_array_view : public basic_array_view<ValueType, strided_bounds<Rank>> |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1614 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1615 | using Base = basic_array_view<ValueType, strided_bounds<Rank>>; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1616 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1617 | template<typename OtherValue, size_t OtherRank> |
| 1618 | friend class strided_array_view; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1619 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1620 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1621 | using Base::rank; |
| 1622 | using typename Base::bounds_type; |
| 1623 | using typename Base::size_type; |
| 1624 | using typename Base::pointer; |
| 1625 | using typename Base::value_type; |
| 1626 | using typename Base::index_type; |
| 1627 | using typename Base::iterator; |
| 1628 | using typename Base::const_iterator; |
| 1629 | using typename Base::reference; |
| 1630 | |
| 1631 | // from static array of size N |
| 1632 | template<size_type N> |
| 1633 | strided_array_view(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds)) |
| 1634 | { |
| 1635 | fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries"); |
| 1636 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1637 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1638 | // from raw data |
| 1639 | strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds)) |
| 1640 | { |
| 1641 | fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries"); |
| 1642 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1643 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1644 | // from array view |
| 1645 | template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>> |
| 1646 | strided_array_view(array_view<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds)) |
| 1647 | { |
| 1648 | fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries"); |
| 1649 | } |
| 1650 | |
| 1651 | // convertible |
| 1652 | template <typename OtherValueType, |
| 1653 | typename BaseType = basic_array_view<ValueType, strided_bounds<Rank>>, |
| 1654 | typename OtherBaseType = basic_array_view<OtherValueType, strided_bounds<Rank>>, |
| 1655 | typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value> |
| 1656 | > |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1657 | 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 |
| 1658 | {} |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1659 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1660 | // convert from bytes |
| 1661 | template <typename OtherValueType> |
| 1662 | strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const |
| 1663 | { |
| 1664 | static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes"); |
| 1665 | auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type)); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1666 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1667 | size_type size = this->bounds().total_size() / d; |
| 1668 | return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} }; |
| 1669 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1670 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1671 | strided_array_view section(index_type origin, index_type extents) const |
| 1672 | { |
| 1673 | size_type size = this->bounds().total_size() - this->bounds().linearize(origin); |
| 1674 | return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}}; |
| 1675 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1676 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1677 | constexpr reference operator[](const index_type& idx) const |
| 1678 | { |
| 1679 | return Base::operator[](idx); |
| 1680 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1681 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1682 | template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> |
| 1683 | constexpr strided_array_view<value_type, rank-1> operator[](size_type idx) const |
| 1684 | { |
| 1685 | auto ret = Base::operator[](idx); |
| 1686 | return{ ret.data(), ret.bounds().total_size(), ret.bounds() }; |
| 1687 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1688 | |
| 1689 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1690 | static index_type resize_extent(const index_type& extent, std::ptrdiff_t d) |
| 1691 | { |
| 1692 | 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"); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1693 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1694 | index_type ret = extent; |
| 1695 | ret[rank - 1] /= d; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1696 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1697 | return ret; |
| 1698 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1699 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1700 | template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>> |
| 1701 | static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0) |
| 1702 | { |
| 1703 | fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1704 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1705 | return strides; |
| 1706 | } |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1707 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1708 | template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> |
| 1709 | static index_type resize_stride(const index_type& strides, std::ptrdiff_t d) |
| 1710 | { |
| 1711 | fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); |
| 1712 | 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"); |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1713 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1714 | for (size_t i = rank - 1; i > 0; --i) |
| 1715 | 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] | 1716 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1717 | index_type ret = strides / d; |
| 1718 | ret[rank - 1] = 1; |
Anna Gringauze | 17ed5c3 | 2015-08-30 23:30:15 -0700 | [diff] [blame] | 1719 | |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1720 | return ret; |
| 1721 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1722 | }; |
| 1723 | |
| 1724 | template <typename ArrayView> |
| 1725 | class contiguous_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type> |
| 1726 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1727 | using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1728 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1729 | using typename Base::reference; |
| 1730 | using typename Base::pointer; |
| 1731 | using typename Base::difference_type; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1732 | |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1733 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1734 | template <typename ValueType, typename Bounds> |
| 1735 | friend class basic_array_view; |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1736 | |
| 1737 | pointer m_pdata; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1738 | const ArrayView * m_validator; |
| 1739 | void validateThis() const |
| 1740 | { |
| 1741 | 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"); |
| 1742 | } |
| 1743 | contiguous_array_view_iterator (const ArrayView *container, bool isbegin) : |
| 1744 | 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] | 1745 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1746 | reference operator*() const noexcept |
| 1747 | { |
| 1748 | validateThis(); |
| 1749 | return *m_pdata; |
| 1750 | } |
| 1751 | pointer operator->() const noexcept |
| 1752 | { |
| 1753 | validateThis(); |
| 1754 | return m_pdata; |
| 1755 | } |
| 1756 | contiguous_array_view_iterator& operator++() noexcept |
| 1757 | { |
| 1758 | ++m_pdata; |
| 1759 | return *this; |
| 1760 | } |
| 1761 | contiguous_array_view_iterator operator++(int)noexcept |
| 1762 | { |
| 1763 | auto ret = *this; |
| 1764 | ++(*this); |
| 1765 | return ret; |
| 1766 | } |
| 1767 | contiguous_array_view_iterator& operator--() noexcept |
| 1768 | { |
| 1769 | --m_pdata; |
| 1770 | return *this; |
| 1771 | } |
| 1772 | contiguous_array_view_iterator operator--(int)noexcept |
| 1773 | { |
| 1774 | auto ret = *this; |
| 1775 | --(*this); |
| 1776 | return ret; |
| 1777 | } |
| 1778 | contiguous_array_view_iterator operator+(difference_type n) const noexcept |
| 1779 | { |
| 1780 | contiguous_array_view_iterator ret{ *this }; |
| 1781 | return ret += n; |
| 1782 | } |
| 1783 | contiguous_array_view_iterator& operator+=(difference_type n) noexcept |
| 1784 | { |
| 1785 | m_pdata += n; |
| 1786 | return *this; |
| 1787 | } |
| 1788 | contiguous_array_view_iterator operator-(difference_type n) const noexcept |
| 1789 | { |
| 1790 | contiguous_array_view_iterator ret{ *this }; |
| 1791 | return ret -= n; |
| 1792 | } |
| 1793 | contiguous_array_view_iterator& operator-=(difference_type n) noexcept |
| 1794 | { |
| 1795 | return *this += -n; |
| 1796 | } |
| 1797 | difference_type operator-(const contiguous_array_view_iterator& rhs) const noexcept |
| 1798 | { |
| 1799 | fail_fast_assert(m_validator == rhs.m_validator); |
| 1800 | return m_pdata - rhs.m_pdata; |
| 1801 | } |
| 1802 | reference operator[](difference_type n) const noexcept |
| 1803 | { |
| 1804 | return *(*this + n); |
| 1805 | } |
| 1806 | bool operator==(const contiguous_array_view_iterator& rhs) const noexcept |
| 1807 | { |
| 1808 | fail_fast_assert(m_validator == rhs.m_validator); |
| 1809 | return m_pdata == rhs.m_pdata; |
| 1810 | } |
| 1811 | bool operator!=(const contiguous_array_view_iterator& rhs) const noexcept |
| 1812 | { |
| 1813 | return !(*this == rhs); |
| 1814 | } |
| 1815 | bool operator<(const contiguous_array_view_iterator& rhs) const noexcept |
| 1816 | { |
| 1817 | fail_fast_assert(m_validator == rhs.m_validator); |
| 1818 | return m_pdata < rhs.m_pdata; |
| 1819 | } |
| 1820 | bool operator<=(const contiguous_array_view_iterator& rhs) const noexcept |
| 1821 | { |
| 1822 | return !(rhs < *this); |
| 1823 | } |
| 1824 | bool operator>(const contiguous_array_view_iterator& rhs) const noexcept |
| 1825 | { |
| 1826 | return rhs < *this; |
| 1827 | } |
| 1828 | bool operator>=(const contiguous_array_view_iterator& rhs) const noexcept |
| 1829 | { |
| 1830 | return !(rhs > *this); |
| 1831 | } |
| 1832 | void swap(contiguous_array_view_iterator& rhs) noexcept |
| 1833 | { |
| 1834 | std::swap(m_pdata, rhs.m_pdata); |
| 1835 | std::swap(m_validator, rhs.m_validator); |
| 1836 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1837 | }; |
| 1838 | |
| 1839 | template <typename ArrayView> |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1840 | 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] | 1841 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1842 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | template <typename ArrayView> |
| 1846 | class general_array_view_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type> |
| 1847 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1848 | using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1849 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1850 | using typename Base::reference; |
| 1851 | using typename Base::pointer; |
| 1852 | using typename Base::difference_type; |
| 1853 | using typename Base::value_type; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1854 | private: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1855 | template <typename ValueType, typename Bounds> |
| 1856 | friend class basic_array_view; |
| 1857 | |
Neil MacIntosh | f45fedb | 2015-10-15 14:29:35 -0700 | [diff] [blame] | 1858 | const ArrayView * m_container; |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1859 | typename ArrayView::bounds_type::iterator m_itr; |
| 1860 | general_array_view_iterator(const ArrayView *container, bool isbegin) : |
| 1861 | m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end()) |
| 1862 | {} |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1863 | public: |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1864 | reference operator*() noexcept |
| 1865 | { |
| 1866 | return (*m_container)[*m_itr]; |
| 1867 | } |
| 1868 | pointer operator->() noexcept |
| 1869 | { |
| 1870 | return &(*m_container)[*m_itr]; |
| 1871 | } |
| 1872 | general_array_view_iterator& operator++() noexcept |
| 1873 | { |
| 1874 | ++m_itr; |
| 1875 | return *this; |
| 1876 | } |
| 1877 | general_array_view_iterator operator++(int)noexcept |
| 1878 | { |
| 1879 | auto ret = *this; |
| 1880 | ++(*this); |
| 1881 | return ret; |
| 1882 | } |
| 1883 | general_array_view_iterator& operator--() noexcept |
| 1884 | { |
| 1885 | --m_itr; |
| 1886 | return *this; |
| 1887 | } |
| 1888 | general_array_view_iterator operator--(int)noexcept |
| 1889 | { |
| 1890 | auto ret = *this; |
| 1891 | --(*this); |
| 1892 | return ret; |
| 1893 | } |
| 1894 | general_array_view_iterator operator+(difference_type n) const noexcept |
| 1895 | { |
| 1896 | general_array_view_iterator ret{ *this }; |
| 1897 | return ret += n; |
| 1898 | } |
| 1899 | general_array_view_iterator& operator+=(difference_type n) noexcept |
| 1900 | { |
| 1901 | m_itr += n; |
| 1902 | return *this; |
| 1903 | } |
| 1904 | general_array_view_iterator operator-(difference_type n) const noexcept |
| 1905 | { |
| 1906 | general_array_view_iterator ret{ *this }; |
| 1907 | return ret -= n; |
| 1908 | } |
| 1909 | general_array_view_iterator& operator-=(difference_type n) noexcept |
| 1910 | { |
| 1911 | return *this += -n; |
| 1912 | } |
| 1913 | difference_type operator-(const general_array_view_iterator& rhs) const noexcept |
| 1914 | { |
| 1915 | fail_fast_assert(m_container == rhs.m_container); |
| 1916 | return m_itr - rhs.m_itr; |
| 1917 | } |
| 1918 | value_type operator[](difference_type n) const noexcept |
| 1919 | { |
| 1920 | return (*m_container)[m_itr[n]];; |
| 1921 | } |
| 1922 | bool operator==(const general_array_view_iterator& rhs) const noexcept |
| 1923 | { |
| 1924 | fail_fast_assert(m_container == rhs.m_container); |
| 1925 | return m_itr == rhs.m_itr; |
| 1926 | } |
| 1927 | bool operator !=(const general_array_view_iterator& rhs) const noexcept |
| 1928 | { |
| 1929 | return !(*this == rhs); |
| 1930 | } |
| 1931 | bool operator<(const general_array_view_iterator& rhs) const noexcept |
| 1932 | { |
| 1933 | fail_fast_assert(m_container == rhs.m_container); |
| 1934 | return m_itr < rhs.m_itr; |
| 1935 | } |
| 1936 | bool operator<=(const general_array_view_iterator& rhs) const noexcept |
| 1937 | { |
| 1938 | return !(rhs < *this); |
| 1939 | } |
| 1940 | bool operator>(const general_array_view_iterator& rhs) const noexcept |
| 1941 | { |
| 1942 | return rhs < *this; |
| 1943 | } |
| 1944 | bool operator>=(const general_array_view_iterator& rhs) const noexcept |
| 1945 | { |
| 1946 | return !(rhs > *this); |
| 1947 | } |
| 1948 | void swap(general_array_view_iterator& rhs) noexcept |
| 1949 | { |
| 1950 | std::swap(m_itr, rhs.m_itr); |
| 1951 | std::swap(m_container, rhs.m_container); |
| 1952 | } |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1953 | }; |
| 1954 | |
| 1955 | template <typename ArrayView> |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1956 | 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] | 1957 | { |
Neil MacIntosh | 68064d6 | 2015-11-03 19:17:11 -0800 | [diff] [blame^] | 1958 | return rhs + n; |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Neil MacIntosh | ef626fd | 2015-09-29 16:41:37 -0700 | [diff] [blame] | 1961 | } // namespace gsl |
Neil MacIntosh | a9dcbe0 | 2015-08-20 18:09:14 -0700 | [diff] [blame] | 1962 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1963 | #ifdef _MSC_VER |
| 1964 | |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1965 | #undef constexpr |
| 1966 | #pragma pop_macro("constexpr") |
Gabriel Dos Reis | 6554e83 | 2015-09-28 05:10:44 -0700 | [diff] [blame] | 1967 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1968 | #if _MSC_VER <= 1800 |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 1969 | #pragma warning(pop) |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1970 | |
| 1971 | #ifndef GSL_THROWS_FOR_TESTING |
| 1972 | #pragma undef noexcept |
| 1973 | #endif // GSL_THROWS_FOR_TESTING |
| 1974 | |
Neil MacIntosh | e9a9602 | 2015-11-03 18:56:55 -0800 | [diff] [blame] | 1975 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 1976 | #pragma pop_macro("GSL_MSVC_HAS_VARIADIC_CTOR_BUG") |
| 1977 | |
| 1978 | |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 1979 | #endif // _MSC_VER <= 1800 |
Neil MacIntosh | 9a29712 | 2015-09-14 15:11:07 -0700 | [diff] [blame] | 1980 | |
Neil MacIntosh | d531680 | 2015-09-30 21:54:08 -0700 | [diff] [blame] | 1981 | #endif // _MSC_VER |
| 1982 | |
| 1983 | #if defined(GSL_THROWS_FOR_TESTING) |
| 1984 | #undef noexcept |
| 1985 | #endif // GSL_THROWS_FOR_TESTING |
| 1986 | |
Treb Connell | 51da136 | 2015-09-24 18:08:34 -0700 | [diff] [blame] | 1987 | |
| 1988 | #endif // GSL_ARRAY_VIEW_H |