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
7 #ifndef BOOST_HISTOGRAM_DETAIL_SPAN_HPP
8 #define BOOST_HISTOGRAM_DETAIL_SPAN_HPP
9
10 #ifdef __has_include
11 #if __has_include(<version>)
12 #include <version>
13 #ifdef __cpp_lib_span
14 #if __cpp_lib_span >= 201902
15 #define BOOST_HISTOGRAM_DETAIL_HAS_STD_SPAN
16 #endif
17 #endif
18 #endif
19 #endif
20
21 #ifdef BOOST_HISTOGRAM_DETAIL_HAS_STD_SPAN
22
23 #include <span>
24
25 namespace boost {
26 namespace histogram {
27 namespace detail {
28 using std::span;
29 } // namespace detail
30 } // namespace histogram
31 } // namespace boost
32
33 #else // C++17 span not available, so we use our implementation
34
35 // to be replaced by boost::span
36
37 #include <array>
38 #include <boost/histogram/detail/nonmember_container_access.hpp>
39 #include <cassert>
40 #include <initializer_list>
41 #include <iterator>
42 #include <type_traits>
43
44 namespace boost {
45 namespace histogram {
46 namespace detail {
47
48 namespace dtl = ::boost::histogram::detail;
49
50 static constexpr std::size_t dynamic_extent = ~static_cast<std::size_t>(0);
51
52 template <class T, std::size_t N>
53 class span_base {
54 public:
data()55 constexpr T* data() noexcept { return begin_; }
data() const56 constexpr const T* data() const noexcept { return begin_; }
size() const57 constexpr std::size_t size() const noexcept { return N; }
58
59 protected:
span_base(T * b,std::size_t s)60 constexpr span_base(T* b, std::size_t s) noexcept : begin_(b) {
61 (void)s;
62 assert(N == s);
63 }
set(T * b,std::size_t s)64 constexpr void set(T* b, std::size_t s) noexcept {
65 (void)s;
66 begin_ = b;
67 assert(N == s);
68 }
69
70 private:
71 T* begin_;
72 };
73
74 template <class T>
75 class span_base<T, dynamic_extent> {
76 public:
span_base()77 constexpr span_base() noexcept : begin_(nullptr), size_(0) {}
78
data()79 constexpr T* data() noexcept { return begin_; }
data() const80 constexpr const T* data() const noexcept { return begin_; }
size() const81 constexpr std::size_t size() const noexcept { return size_; }
82
83 protected:
span_base(T * b,std::size_t s)84 constexpr span_base(T* b, std::size_t s) noexcept : begin_(b), size_(s) {}
set(T * b,std::size_t s)85 constexpr void set(T* b, std::size_t s) noexcept {
86 begin_ = b;
87 size_ = s;
88 }
89
90 private:
91 T* begin_;
92 std::size_t size_;
93 };
94
95 template <class T, std::size_t Extent = dynamic_extent>
96 class span : public span_base<T, Extent> {
97 using base = span_base<T, Extent>;
98
99 public:
100 using element_type = T;
101 using value_type = std::remove_cv_t<T>;
102 using index_type = std::size_t;
103 using difference_type = std::ptrdiff_t;
104 using pointer = T*;
105 using const_pointer = const T*;
106 using reference = T&;
107 using const_reference = const T&;
108 using iterator = pointer;
109 using const_iterator = const_pointer;
110 using reverse_iterator = std::reverse_iterator<iterator>;
111 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
112
113 static constexpr std::size_t extent = Extent;
114
115 using base::base;
116
span(pointer first,pointer last)117 constexpr span(pointer first, pointer last)
118 : span(first, static_cast<std::size_t>(last - first)) {
119 assert(extent == dynamic_extent ||
120 static_cast<difference_type>(extent) == (last - first));
121 }
122
span(pointer ptr,index_type count)123 constexpr span(pointer ptr, index_type count) : base(ptr, count) {}
124
125 template <std::size_t N>
span(element_type (& arr)[N])126 constexpr span(element_type (&arr)[N]) noexcept : span(dtl::data(arr), N) {
127 static_assert(extent == dynamic_extent || extent == N, "static sizes do not match");
128 }
129
130 template <std::size_t N,
131 class = std::enable_if_t<(extent == dynamic_extent || extent == N)> >
span(std::array<value_type,N> & arr)132 constexpr span(std::array<value_type, N>& arr) noexcept : span(dtl::data(arr), N) {}
133
134 template <std::size_t N,
135 class = std::enable_if_t<(extent == dynamic_extent || extent == N)> >
span(const std::array<value_type,N> & arr)136 constexpr span(const std::array<value_type, N>& arr) noexcept
137 : span(dtl::data(arr), N) {}
138
139 template <class Container, class = std::enable_if_t<std::is_convertible<
140 decltype(dtl::size(std::declval<const Container&>()),
141 dtl::data(std::declval<const Container&>())),
142 pointer>::value> >
span(const Container & cont)143 constexpr span(const Container& cont) : span(dtl::data(cont), dtl::size(cont)) {}
144
145 template <class Container, class = std::enable_if_t<std::is_convertible<
146 decltype(dtl::size(std::declval<Container&>()),
147 dtl::data(std::declval<Container&>())),
148 pointer>::value> >
span(Container & cont)149 constexpr span(Container& cont) : span(dtl::data(cont), dtl::size(cont)) {}
150
151 template <class U, std::size_t N,
152 class = std::enable_if_t<((extent == dynamic_extent || extent == N) &&
153 std::is_convertible<U, element_type>::value)> >
span(const span<U,N> & s)154 constexpr span(const span<U, N>& s) noexcept : span(s.data(), s.size()) {}
155
156 template <class U, std::size_t N,
157 class = std::enable_if_t<((extent == dynamic_extent || extent == N) &&
158 std::is_convertible<U, element_type>::value)> >
span(span<U,N> & s)159 constexpr span(span<U, N>& s) noexcept : span(s.data(), s.size()) {}
160
161 constexpr span(const span& other) noexcept = default;
162
begin()163 constexpr iterator begin() { return base::data(); }
begin() const164 constexpr const_iterator begin() const { return base::data(); }
cbegin() const165 constexpr const_iterator cbegin() const { return base::data(); }
166
end()167 constexpr iterator end() { return base::data() + base::size(); }
end() const168 constexpr const_iterator end() const { return base::data() + base::size(); }
cend() const169 constexpr const_iterator cend() const { return base::data() + base::size(); }
170
rbegin()171 reverse_iterator rbegin() { return reverse_iterator(end()); }
rbegin() const172 const_reverse_iterator rbegin() const { return reverse_iterator(end()); }
crbegin()173 const_reverse_iterator crbegin() { return reverse_iterator(end()); }
174
rend()175 reverse_iterator rend() { return reverse_iterator(begin()); }
rend() const176 const_reverse_iterator rend() const { return reverse_iterator(begin()); }
crend()177 const_reverse_iterator crend() { return reverse_iterator(begin()); }
178
front()179 constexpr reference front() { return *base::data(); }
back()180 constexpr reference back() { return *(base::data() + base::size() - 1); }
181
operator [](index_type idx) const182 constexpr reference operator[](index_type idx) const { return base::data()[idx]; }
183
size_bytes() const184 constexpr std::size_t size_bytes() const noexcept {
185 return base::size() * sizeof(element_type);
186 }
187
empty() const188 constexpr bool empty() const noexcept { return base::size() == 0; }
189
190 template <std::size_t Count>
first() const191 constexpr span<element_type, Count> first() const {
192 assert(Count <= base::size());
193 return span<element_type, Count>(base::data(), Count);
194 }
195
first(std::size_t count) const196 constexpr span<element_type, dynamic_extent> first(std::size_t count) const {
197 assert(count <= base::size());
198 return span<element_type, dynamic_extent>(base::data(), count);
199 }
200
201 template <std::size_t Count>
last() const202 constexpr span<element_type, Count> last() const {
203 assert(Count <= base::size());
204 return span<element_type, Count>(base::data() + base::size() - Count, Count);
205 }
206
last(std::size_t count) const207 constexpr span<element_type, dynamic_extent> last(std::size_t count) const {
208 assert(count <= base::size());
209 return span<element_type, dynamic_extent>(base::data() + base::size() - count, count);
210 }
211
212 template <std::size_t Offset, std::size_t Count = dynamic_extent>
213 constexpr span<element_type,
214 (Count != dynamic_extent
215 ? Count
216 : (extent != dynamic_extent ? extent - Offset : dynamic_extent))>
subspan() const217 subspan() const {
218 assert(Offset <= base::size());
219 constexpr std::size_t E =
220 (Count != dynamic_extent
221 ? Count
222 : (extent != dynamic_extent ? extent - Offset : dynamic_extent));
223 assert(E == dynamic_extent || E <= base::size());
224 return span<element_type, E>(base::data() + Offset,
225 Count == dynamic_extent ? base::size() - Offset : Count);
226 }
227
subspan(std::size_t offset,std::size_t count=dynamic_extent) const228 constexpr span<element_type, dynamic_extent> subspan(
229 std::size_t offset, std::size_t count = dynamic_extent) const {
230 assert(offset <= base::size());
231 const std::size_t s = count == dynamic_extent ? base::size() - offset : count;
232 assert(s <= base::size());
233 return span<element_type, dynamic_extent>(base::data() + offset, s);
234 }
235 };
236
237 } // namespace detail
238 } // namespace histogram
239 } // namespace boost
240
241 #endif
242
243 #include <boost/histogram/detail/nonmember_container_access.hpp>
244 #include <utility>
245
246 namespace boost {
247 namespace histogram {
248 namespace detail {
249
250 namespace dtl = ::boost::histogram::detail;
251
252 template <class T>
make_span(T * begin,T * end)253 auto make_span(T* begin, T* end) {
254 return dtl::span<T>{begin, end};
255 }
256
257 template <class T>
make_span(T * begin,std::size_t size)258 auto make_span(T* begin, std::size_t size) {
259 return dtl::span<T>{begin, size};
260 }
261
262 template <class Container, class = decltype(dtl::size(std::declval<Container>()),
263 dtl::data(std::declval<Container>()))>
make_span(const Container & cont)264 auto make_span(const Container& cont) {
265 return make_span(dtl::data(cont), dtl::size(cont));
266 }
267
268 template <class T, std::size_t N>
make_span(T (& arr)[N])269 auto make_span(T (&arr)[N]) {
270 return dtl::span<T, N>(arr, N);
271 }
272
273 } // namespace detail
274 } // namespace histogram
275 } // namespace boost
276
277 #endif
278