span.hpp 11 KB

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