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