| 1 | /* |
| 2 | Copyright 2019-2023 Glen Joseph Fernandes |
| 3 | (glenjofe@gmail.com) |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. |
| 6 | (http://www.boost.org/LICENSE_1_0.txt) |
| 7 | */ |
| 8 | #ifndef BOOST_CORE_SPAN_HPP |
| 9 | #define BOOST_CORE_SPAN_HPP |
| 10 | |
| 11 | #include <boost/core/data.hpp> |
| 12 | #include <array> |
| 13 | #include <iterator> |
| 14 | #include <type_traits> |
| 15 | |
| 16 | namespace boost { |
| 17 | |
| 18 | constexpr std::size_t dynamic_extent = static_cast<std::size_t>(-1); |
| 19 | |
| 20 | template<class T, std::size_t E = dynamic_extent> |
| 21 | class span; |
| 22 | |
| 23 | namespace detail { |
| 24 | |
| 25 | template<class U, class T> |
| 26 | struct span_convertible { |
| 27 | static constexpr bool value = std::is_convertible<U(*)[], T(*)[]>::value; |
| 28 | }; |
| 29 | |
| 30 | template<std::size_t E, std::size_t N> |
| 31 | struct span_capacity { |
| 32 | static constexpr bool value = E == boost::dynamic_extent || E == N; |
| 33 | }; |
| 34 | |
| 35 | template<class T, std::size_t E, class U, std::size_t N> |
| 36 | struct span_compatible { |
| 37 | static constexpr bool value = span_capacity<E, N>::value && |
| 38 | span_convertible<U, T>::value; |
| 39 | }; |
| 40 | |
| 41 | template<class T> |
| 42 | using span_uncvref = typename std::remove_cv<typename |
| 43 | std::remove_reference<T>::type>::type; |
| 44 | |
| 45 | template<class> |
| 46 | struct span_is_span { |
| 47 | static constexpr bool value = false; |
| 48 | }; |
| 49 | |
| 50 | template<class T, std::size_t E> |
| 51 | struct span_is_span<boost::span<T, E> > { |
| 52 | static constexpr bool value = true; |
| 53 | }; |
| 54 | |
| 55 | template<class T> |
| 56 | struct span_is_array { |
| 57 | static constexpr bool value = false; |
| 58 | }; |
| 59 | |
| 60 | template<class T, std::size_t N> |
| 61 | struct span_is_array<std::array<T, N> > { |
| 62 | static constexpr bool value = true; |
| 63 | }; |
| 64 | |
| 65 | template<class T> |
| 66 | using span_ptr = decltype(boost::data(std::declval<T&>())); |
| 67 | |
| 68 | template<class, class = void> |
| 69 | struct span_data { }; |
| 70 | |
| 71 | template<class T> |
| 72 | struct span_data<T, |
| 73 | typename std::enable_if<std::is_pointer<span_ptr<T> >::value>::type> { |
| 74 | typedef typename std::remove_pointer<span_ptr<T> >::type type; |
| 75 | }; |
| 76 | |
| 77 | template<class, class, class = void> |
| 78 | struct span_has_data { |
| 79 | static constexpr bool value = false; |
| 80 | }; |
| 81 | |
| 82 | template<class R, class T> |
| 83 | struct span_has_data<R, T, typename std::enable_if<span_convertible<typename |
| 84 | span_data<R>::type, T>::value>::type> { |
| 85 | static constexpr bool value = true; |
| 86 | }; |
| 87 | |
| 88 | template<class, class = void> |
| 89 | struct span_has_size { |
| 90 | static constexpr bool value = false; |
| 91 | }; |
| 92 | |
| 93 | template<class R> |
| 94 | struct span_has_size<R, typename |
| 95 | std::enable_if<std::is_convertible<decltype(std::declval<R&>().size()), |
| 96 | std::size_t>::value>::type> { |
| 97 | static constexpr bool value = true; |
| 98 | }; |
| 99 | |
| 100 | template<class R, class T> |
| 101 | struct span_is_range { |
| 102 | static constexpr bool value = (std::is_const<T>::value || |
| 103 | std::is_lvalue_reference<R>::value) && |
| 104 | !span_is_span<span_uncvref<R> >::value && |
| 105 | !span_is_array<span_uncvref<R> >::value && |
| 106 | !std::is_array<span_uncvref<R> >::value && |
| 107 | span_has_data<R, T>::value && |
| 108 | span_has_size<R>::value; |
| 109 | }; |
| 110 | |
| 111 | template<std::size_t E, std::size_t N> |
| 112 | struct span_implicit { |
| 113 | static constexpr bool value = E == boost::dynamic_extent || |
| 114 | N != boost::dynamic_extent; |
| 115 | }; |
| 116 | |
| 117 | template<class T, std::size_t E, class U, std::size_t N> |
| 118 | struct span_copyable { |
| 119 | static constexpr bool value = (N == boost::dynamic_extent || |
| 120 | span_capacity<E, N>::value) && span_convertible<U, T>::value; |
| 121 | }; |
| 122 | |
| 123 | template<std::size_t E, std::size_t O> |
| 124 | struct span_sub { |
| 125 | static constexpr std::size_t value = E == boost::dynamic_extent ? |
| 126 | boost::dynamic_extent : E - O; |
| 127 | }; |
| 128 | |
| 129 | template<class T, std::size_t E> |
| 130 | struct span_store { |
| 131 | constexpr span_store(T* p_, std::size_t) noexcept |
| 132 | : p(p_) { } |
| 133 | static constexpr std::size_t n = E; |
| 134 | T* p; |
| 135 | }; |
| 136 | |
| 137 | template<class T> |
| 138 | struct span_store<T, boost::dynamic_extent> { |
| 139 | constexpr span_store(T* p_, std::size_t n_) noexcept |
| 140 | : p(p_) |
| 141 | , n(n_) { } |
| 142 | T* p; |
| 143 | std::size_t n; |
| 144 | }; |
| 145 | |
| 146 | template<class T, std::size_t E> |
| 147 | struct span_bytes { |
| 148 | static constexpr std::size_t value = sizeof(T) * E; |
| 149 | }; |
| 150 | |
| 151 | template<class T> |
| 152 | struct span_bytes<T, boost::dynamic_extent> { |
| 153 | static constexpr std::size_t value = boost::dynamic_extent; |
| 154 | }; |
| 155 | |
| 156 | } /* detail */ |
| 157 | |
| 158 | template<class T, std::size_t E> |
| 159 | class span { |
| 160 | public: |
| 161 | typedef T element_type; |
| 162 | typedef typename std::remove_cv<T>::type value_type; |
| 163 | typedef std::size_t size_type; |
| 164 | typedef std::ptrdiff_t difference_type; |
| 165 | typedef T* pointer; |
| 166 | typedef const T* const_pointer; |
| 167 | typedef T& reference; |
| 168 | typedef const T& const_reference; |
| 169 | typedef T* iterator; |
| 170 | typedef const T* const_iterator; |
| 171 | typedef std::reverse_iterator<T*> reverse_iterator; |
| 172 | typedef std::reverse_iterator<const T*> const_reverse_iterator; |
| 173 | |
| 174 | static constexpr std::size_t extent = E; |
| 175 | |
| 176 | template<std::size_t N = E, |
| 177 | typename std::enable_if<N == dynamic_extent || N == 0, int>::type = 0> |
| 178 | constexpr span() noexcept |
| 179 | : s_(0, 0) { } |
| 180 | |
| 181 | template<class I, |
| 182 | typename std::enable_if<E == dynamic_extent && |
| 183 | detail::span_convertible<I, T>::value, int>::type = 0> |
| 184 | constexpr span(I* f, size_type c) |
| 185 | : s_(f, c) { } |
| 186 | |
| 187 | template<class I, |
| 188 | typename std::enable_if<E != dynamic_extent && |
| 189 | detail::span_convertible<I, T>::value, int>::type = 0> |
| 190 | explicit constexpr span(I* f, size_type c) |
| 191 | : s_(f, c) { } |
| 192 | |
| 193 | template<class I, class L, |
| 194 | typename std::enable_if<E == dynamic_extent && |
| 195 | detail::span_convertible<I, T>::value, int>::type = 0> |
| 196 | constexpr span(I* f, L* l) |
| 197 | : s_(f, l - f) { } |
| 198 | |
| 199 | template<class I, class L, |
| 200 | typename std::enable_if<E != dynamic_extent && |
| 201 | detail::span_convertible<I, T>::value, int>::type = 0> |
| 202 | explicit constexpr span(I* f, L* l) |
| 203 | : s_(f, l - f) { } |
| 204 | |
| 205 | template<std::size_t N, |
| 206 | typename std::enable_if<detail::span_capacity<E, N>::value, |
| 207 | int>::type = 0> |
| 208 | constexpr span(typename std::enable_if<true, T>::type (&a)[N]) noexcept |
| 209 | : s_(a, N) { } |
| 210 | |
| 211 | template<class U, std::size_t N, |
| 212 | typename std::enable_if<detail::span_compatible<T, E, U, N>::value, |
| 213 | int>::type = 0> |
| 214 | constexpr span(std::array<U, N>& a) noexcept |
| 215 | : s_(a.data(), N) { } |
| 216 | |
| 217 | template<class U, std::size_t N, |
| 218 | typename std::enable_if<detail::span_compatible<T, E, const U, |
| 219 | N>::value, int>::type = 0> |
| 220 | constexpr span(const std::array<U, N>& a) noexcept |
| 221 | : s_(a.data(), N) { } |
| 222 | |
| 223 | template<class R, |
| 224 | typename std::enable_if<E == dynamic_extent && |
| 225 | detail::span_is_range<R, T>::value, int>::type = 0> |
| 226 | constexpr span(R&& r) noexcept(noexcept(boost::data(r)) && |
| 227 | noexcept(r.size())) |
| 228 | : s_(boost::data(r), r.size()) { } |
| 229 | |
| 230 | template<class R, |
| 231 | typename std::enable_if<E != dynamic_extent && |
| 232 | detail::span_is_range<R, T>::value, int>::type = 0> |
| 233 | explicit constexpr span(R&& r) noexcept(noexcept(boost::data(r)) && |
| 234 | noexcept(r.size())) |
| 235 | : s_(boost::data(r), r.size()) { } |
| 236 | |
| 237 | template<class U, std::size_t N, |
| 238 | typename std::enable_if<detail::span_implicit<E, N>::value && |
| 239 | detail::span_copyable<T, E, U, N>::value, int>::type = 0> |
| 240 | constexpr span(const span<U, N>& s) noexcept |
| 241 | : s_(s.data(), s.size()) { } |
| 242 | |
| 243 | template<class U, std::size_t N, |
| 244 | typename std::enable_if<!detail::span_implicit<E, N>::value && |
| 245 | detail::span_copyable<T, E, U, N>::value, int>::type = 0> |
| 246 | explicit constexpr span(const span<U, N>& s) noexcept |
| 247 | : s_(s.data(), s.size()) { } |
| 248 | |
| 249 | template<std::size_t C> |
| 250 | constexpr span<T, C> first() const { |
| 251 | static_assert(C <= E, "Count <= Extent" ); |
| 252 | return span<T, C>(s_.p, C); |
| 253 | } |
| 254 | |
| 255 | template<std::size_t C> |
| 256 | constexpr span<T, C> last() const { |
| 257 | static_assert(C <= E, "Count <= Extent" ); |
| 258 | return span<T, C>(s_.p + (s_.n - C), C); |
| 259 | } |
| 260 | |
| 261 | template<std::size_t O, std::size_t C = dynamic_extent> |
| 262 | constexpr typename std::enable_if<C == dynamic_extent, |
| 263 | span<T, detail::span_sub<E, O>::value> >::type subspan() const { |
| 264 | static_assert(O <= E, "Offset <= Extent" ); |
| 265 | return span<T, detail::span_sub<E, O>::value>(s_.p + O, s_.n - O); |
| 266 | } |
| 267 | |
| 268 | template<std::size_t O, std::size_t C = dynamic_extent> |
| 269 | constexpr typename std::enable_if<C != dynamic_extent, |
| 270 | span<T, C> >::type subspan() const { |
| 271 | static_assert(O <= E && C <= E - O, |
| 272 | "Offset <= Extent && Count <= Extent - Offset" ); |
| 273 | return span<T, C>(s_.p + O, C); |
| 274 | } |
| 275 | |
| 276 | constexpr span<T, dynamic_extent> first(size_type c) const { |
| 277 | return span<T, dynamic_extent>(s_.p, c); |
| 278 | } |
| 279 | |
| 280 | constexpr span<T, dynamic_extent> last(size_type c) const { |
| 281 | return span<T, dynamic_extent>(s_.p + (s_.n - c), c); |
| 282 | } |
| 283 | |
| 284 | constexpr span<T, dynamic_extent> subspan(size_type o, |
| 285 | size_type c = dynamic_extent) const { |
| 286 | return span<T, dynamic_extent>(s_.p + o, |
| 287 | c == dynamic_extent ? s_.n - o : c); |
| 288 | } |
| 289 | |
| 290 | constexpr size_type size() const noexcept { |
| 291 | return s_.n; |
| 292 | } |
| 293 | |
| 294 | constexpr size_type size_bytes() const noexcept { |
| 295 | return s_.n * sizeof(T); |
| 296 | } |
| 297 | |
| 298 | constexpr bool empty() const noexcept { |
| 299 | return s_.n == 0; |
| 300 | } |
| 301 | |
| 302 | constexpr reference operator[](size_type i) const { |
| 303 | return s_.p[i]; |
| 304 | } |
| 305 | |
| 306 | constexpr reference front() const { |
| 307 | return *s_.p; |
| 308 | } |
| 309 | |
| 310 | constexpr reference back() const { |
| 311 | return s_.p[s_.n - 1]; |
| 312 | } |
| 313 | |
| 314 | constexpr pointer data() const noexcept { |
| 315 | return s_.p; |
| 316 | } |
| 317 | |
| 318 | constexpr iterator begin() const noexcept { |
| 319 | return s_.p; |
| 320 | } |
| 321 | |
| 322 | constexpr iterator end() const noexcept { |
| 323 | return s_.p + s_.n; |
| 324 | } |
| 325 | |
| 326 | constexpr reverse_iterator rbegin() const noexcept { |
| 327 | return reverse_iterator(s_.p + s_.n); |
| 328 | } |
| 329 | |
| 330 | constexpr reverse_iterator rend() const noexcept { |
| 331 | return reverse_iterator(s_.p); |
| 332 | } |
| 333 | |
| 334 | constexpr const_iterator cbegin() const noexcept { |
| 335 | return s_.p; |
| 336 | } |
| 337 | |
| 338 | constexpr const_iterator cend() const noexcept { |
| 339 | return s_.p + s_.n; |
| 340 | } |
| 341 | |
| 342 | constexpr const_reverse_iterator crbegin() const noexcept { |
| 343 | return const_reverse_iterator(s_.p + s_.n); |
| 344 | } |
| 345 | |
| 346 | constexpr const_reverse_iterator crend() const noexcept { |
| 347 | return const_reverse_iterator(s_.p); |
| 348 | } |
| 349 | |
| 350 | private: |
| 351 | detail::span_store<T, E> s_; |
| 352 | }; |
| 353 | |
| 354 | template<class T, std::size_t E> |
| 355 | constexpr std::size_t span<T, E>::extent; |
| 356 | |
| 357 | #ifdef __cpp_deduction_guides |
| 358 | template<class I, class L> |
| 359 | span(I*, L) -> span<I>; |
| 360 | |
| 361 | template<class T, std::size_t N> |
| 362 | span(T(&)[N]) -> span<T, N>; |
| 363 | |
| 364 | template<class T, std::size_t N> |
| 365 | span(std::array<T, N>&) -> span<T, N>; |
| 366 | |
| 367 | template<class T, std::size_t N> |
| 368 | span(const std::array<T, N>&) -> span<const T, N>; |
| 369 | |
| 370 | template<class R> |
| 371 | span(R&&) -> span<typename detail::span_data<R>::type>; |
| 372 | |
| 373 | template<class T, std::size_t E> |
| 374 | span(span<T, E>) -> span<T, E>; |
| 375 | #endif |
| 376 | |
| 377 | #ifdef __cpp_lib_byte |
| 378 | template<class T, std::size_t E> |
| 379 | inline span<const std::byte, detail::span_bytes<T, E>::value> |
| 380 | as_bytes(span<T, E> s) noexcept |
| 381 | { |
| 382 | return span<const std::byte, detail::span_bytes<T, |
| 383 | E>::value>(reinterpret_cast<const std::byte*>(s.data()), |
| 384 | s.size_bytes()); |
| 385 | } |
| 386 | |
| 387 | template<class T, std::size_t E> |
| 388 | inline typename std::enable_if<!std::is_const<T>::value, |
| 389 | span<std::byte, detail::span_bytes<T, E>::value> >::type |
| 390 | as_writable_bytes(span<T, E> s) noexcept |
| 391 | { |
| 392 | return span<std::byte, detail::span_bytes<T, |
| 393 | E>::value>(reinterpret_cast<std::byte*>(s.data()), s.size_bytes()); |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | } /* boost */ |
| 398 | |
| 399 | #endif |
| 400 | |