span.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_DETAIL_SPAN_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_SPAN_HPP
  8. #ifdef __has_include
  9. #if __has_include(<version>)
  10. #include <version>
  11. #ifdef __cpp_lib_span
  12. #if __cpp_lib_span >= 201902
  13. #define BOOST_HISTOGRAM_DETAIL_HAS_STD_SPAN
  14. #endif
  15. #endif
  16. #endif
  17. #endif
  18. #ifdef BOOST_HISTOGRAM_DETAIL_HAS_STD_SPAN
  19. #include <span>
  20. namespace boost {
  21. namespace histogram {
  22. namespace detail {
  23. using std::span;
  24. } // namespace detail
  25. } // namespace histogram
  26. } // namespace boost
  27. #else // C++17 span not available, so we use our implementation
  28. // to be replaced by boost::span
  29. #include <array>
  30. #include <boost/histogram/detail/nonmember_container_access.hpp>
  31. #include <cassert>
  32. #include <initializer_list>
  33. #include <iterator>
  34. #include <type_traits>
  35. namespace boost {
  36. namespace histogram {
  37. namespace detail {
  38. namespace dtl = ::boost::histogram::detail;
  39. static constexpr std::size_t dynamic_extent = ~static_cast<std::size_t>(0);
  40. template <class T, std::size_t N>
  41. class span_base {
  42. public:
  43. constexpr T* data() noexcept { return begin_; }
  44. constexpr const T* data() const noexcept { return begin_; }
  45. constexpr std::size_t size() const noexcept { return N; }
  46. protected:
  47. constexpr span_base(T* b, std::size_t s) noexcept : begin_(b) {
  48. (void)s;
  49. assert(N == s);
  50. }
  51. constexpr void set(T* b, std::size_t s) noexcept {
  52. (void)s;
  53. begin_ = b;
  54. assert(N == s);
  55. }
  56. private:
  57. T* begin_;
  58. };
  59. template <class T>
  60. class span_base<T, dynamic_extent> {
  61. public:
  62. constexpr span_base() noexcept : begin_(nullptr), size_(0) {}
  63. constexpr T* data() noexcept { return begin_; }
  64. constexpr const T* data() const noexcept { return begin_; }
  65. constexpr std::size_t size() const noexcept { return size_; }
  66. protected:
  67. constexpr span_base(T* b, std::size_t s) noexcept : begin_(b), size_(s) {}
  68. constexpr void set(T* b, std::size_t s) noexcept {
  69. begin_ = b;
  70. size_ = s;
  71. }
  72. private:
  73. T* begin_;
  74. std::size_t size_;
  75. };
  76. template <class T, std::size_t Extent = dynamic_extent>
  77. class span : public span_base<T, Extent> {
  78. using base = span_base<T, Extent>;
  79. public:
  80. using element_type = T;
  81. using value_type = std::remove_cv_t<T>;
  82. using index_type = std::size_t;
  83. using difference_type = std::ptrdiff_t;
  84. using pointer = T*;
  85. using const_pointer = const T*;
  86. using reference = T&;
  87. using const_reference = const T&;
  88. using iterator = pointer;
  89. using const_iterator = const_pointer;
  90. using reverse_iterator = std::reverse_iterator<iterator>;
  91. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  92. static constexpr std::size_t extent = Extent;
  93. using base::base;
  94. constexpr span(pointer first, pointer last)
  95. : span(first, static_cast<std::size_t>(last - first)) {
  96. assert(extent == dynamic_extent ||
  97. static_cast<difference_type>(extent) == (last - first));
  98. }
  99. constexpr span(pointer ptr, index_type count) : base(ptr, count) {}
  100. template <std::size_t N>
  101. constexpr span(element_type (&arr)[N]) noexcept : span(dtl::data(arr), N) {
  102. static_assert(extent == dynamic_extent || extent == N, "static sizes do not match");
  103. }
  104. template <std::size_t N,
  105. class = std::enable_if_t<(extent == dynamic_extent || extent == N)> >
  106. constexpr span(std::array<value_type, N>& arr) noexcept : span(dtl::data(arr), N) {}
  107. template <std::size_t N,
  108. class = std::enable_if_t<(extent == dynamic_extent || extent == N)> >
  109. constexpr span(const std::array<value_type, N>& arr) noexcept
  110. : span(dtl::data(arr), N) {}
  111. template <class Container, class = std::enable_if_t<std::is_convertible<
  112. decltype(dtl::size(std::declval<const Container&>()),
  113. dtl::data(std::declval<const Container&>())),
  114. pointer>::value> >
  115. constexpr span(const Container& cont) : span(dtl::data(cont), dtl::size(cont)) {}
  116. template <class Container, class = std::enable_if_t<std::is_convertible<
  117. decltype(dtl::size(std::declval<Container&>()),
  118. dtl::data(std::declval<Container&>())),
  119. pointer>::value> >
  120. constexpr span(Container& cont) : span(dtl::data(cont), dtl::size(cont)) {}
  121. template <class U, std::size_t N,
  122. class = std::enable_if_t<((extent == dynamic_extent || extent == N) &&
  123. std::is_convertible<U, element_type>::value)> >
  124. constexpr span(const span<U, N>& s) noexcept : span(s.data(), s.size()) {}
  125. template <class U, std::size_t N,
  126. class = std::enable_if_t<((extent == dynamic_extent || extent == N) &&
  127. std::is_convertible<U, element_type>::value)> >
  128. constexpr span(span<U, N>& s) noexcept : span(s.data(), s.size()) {}
  129. constexpr span(const span& other) noexcept = default;
  130. constexpr iterator begin() { return base::data(); }
  131. constexpr const_iterator begin() const { return base::data(); }
  132. constexpr const_iterator cbegin() const { return base::data(); }
  133. constexpr iterator end() { return base::data() + base::size(); }
  134. constexpr const_iterator end() const { return base::data() + base::size(); }
  135. constexpr const_iterator cend() const { return base::data() + base::size(); }
  136. reverse_iterator rbegin() { return reverse_iterator(end()); }
  137. const_reverse_iterator rbegin() const { return reverse_iterator(end()); }
  138. const_reverse_iterator crbegin() { return reverse_iterator(end()); }
  139. reverse_iterator rend() { return reverse_iterator(begin()); }
  140. const_reverse_iterator rend() const { return reverse_iterator(begin()); }
  141. const_reverse_iterator crend() { return reverse_iterator(begin()); }
  142. constexpr reference front() { return *base::data(); }
  143. constexpr reference back() { return *(base::data() + base::size() - 1); }
  144. constexpr reference operator[](index_type idx) const { return base::data()[idx]; }
  145. constexpr std::size_t size_bytes() const noexcept {
  146. return base::size() * sizeof(element_type);
  147. }
  148. constexpr bool empty() const noexcept { return base::size() == 0; }
  149. template <std::size_t Count>
  150. constexpr span<element_type, Count> first() const {
  151. assert(Count <= base::size());
  152. return span<element_type, Count>(base::data(), Count);
  153. }
  154. constexpr span<element_type, dynamic_extent> first(std::size_t count) const {
  155. assert(count <= base::size());
  156. return span<element_type, dynamic_extent>(base::data(), count);
  157. }
  158. template <std::size_t Count>
  159. constexpr span<element_type, Count> last() const {
  160. assert(Count <= base::size());
  161. return span<element_type, Count>(base::data() + base::size() - Count, Count);
  162. }
  163. constexpr span<element_type, dynamic_extent> last(std::size_t count) const {
  164. assert(count <= base::size());
  165. return span<element_type, dynamic_extent>(base::data() + base::size() - count, count);
  166. }
  167. template <std::size_t Offset, std::size_t Count = dynamic_extent>
  168. constexpr span<element_type,
  169. (Count != dynamic_extent
  170. ? Count
  171. : (extent != dynamic_extent ? extent - Offset : dynamic_extent))>
  172. subspan() const {
  173. assert(Offset <= base::size());
  174. constexpr std::size_t E =
  175. (Count != dynamic_extent
  176. ? Count
  177. : (extent != dynamic_extent ? extent - Offset : dynamic_extent));
  178. assert(E == dynamic_extent || E <= base::size());
  179. return span<element_type, E>(base::data() + Offset,
  180. Count == dynamic_extent ? base::size() - Offset : Count);
  181. }
  182. constexpr span<element_type, dynamic_extent> subspan(
  183. std::size_t offset, std::size_t count = dynamic_extent) const {
  184. assert(offset <= base::size());
  185. const std::size_t s = count == dynamic_extent ? base::size() - offset : count;
  186. assert(s <= base::size());
  187. return span<element_type, dynamic_extent>(base::data() + offset, s);
  188. }
  189. };
  190. } // namespace detail
  191. } // namespace histogram
  192. } // namespace boost
  193. #endif
  194. #include <boost/histogram/detail/nonmember_container_access.hpp>
  195. #include <utility>
  196. namespace boost {
  197. namespace histogram {
  198. namespace detail {
  199. namespace dtl = ::boost::histogram::detail;
  200. template <class T>
  201. auto make_span(T* begin, T* end) {
  202. return dtl::span<T>{begin, end};
  203. }
  204. template <class T>
  205. auto make_span(T* begin, std::size_t size) {
  206. return dtl::span<T>{begin, size};
  207. }
  208. template <class Container, class = decltype(dtl::size(std::declval<Container>()),
  209. dtl::data(std::declval<Container>()))>
  210. auto make_span(const Container& cont) {
  211. return make_span(dtl::data(cont), dtl::size(cont));
  212. }
  213. template <class T, std::size_t N>
  214. auto make_span(T (&arr)[N]) {
  215. return dtl::span<T, N>(arr, N);
  216. }
  217. } // namespace detail
  218. } // namespace histogram
  219. } // namespace boost
  220. #endif