Bitcoin ABC 0.30.7
P2P Digital Currency
span.h
Go to the documentation of this file.
1// Copyright (c) 2018 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_SPAN_H
6#define BITCOIN_SPAN_H
7
8#include <algorithm>
9#include <cassert>
10#include <cstddef>
11#include <cstdint>
12#include <type_traits>
13
14#ifdef DEBUG
15#define CONSTEXPR_IF_NOT_DEBUG
16#define ASSERT_IF_DEBUG(x) assert((x))
17#else
18#define CONSTEXPR_IF_NOT_DEBUG constexpr
19#define ASSERT_IF_DEBUG(x)
20#endif
21
22#if defined(__clang__)
23#if __has_attribute(lifetimebound)
24#define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]]
25#else
26#define SPAN_ATTR_LIFETIMEBOUND
27#endif
28#else
29#define SPAN_ATTR_LIFETIMEBOUND
30#endif
31
93template <typename C> class Span {
95 std::size_t m_size;
96
97 template <class T> struct is_Span_int : public std::false_type {};
98 template <class T> struct is_Span_int<Span<T>> : public std::true_type {};
99 template <class T>
100 struct is_Span : public is_Span_int<typename std::remove_cv<T>::type> {};
101
102public:
103 constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
104
111 template <typename T,
112 typename std::enable_if<
113 std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
114 constexpr Span(T *begin, std::size_t size) noexcept
115 : m_data(begin), m_size(size) {}
116
123 template <typename T,
124 typename std::enable_if<
125 std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
127 : m_data(begin), m_size(end - begin) {
129 }
130
142 template <typename O,
143 typename std::enable_if<
144 std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0>
145 constexpr Span(const Span<O> &other) noexcept
146 : m_data(other.m_data), m_size(other.m_size) {}
147
149 constexpr Span(const Span &) noexcept = default;
150
152 Span &operator=(const Span &other) noexcept = default;
153
156 template <int N>
157 constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {}
158
170 template <typename V>
171 constexpr Span(
173 typename std::enable_if<
175 std::is_convertible<
176 typename std::remove_pointer<
177 decltype(std::declval<V &>().data())>::type (*)[],
178 C (*)[]>::value &&
179 std::is_convertible<decltype(std::declval<V &>().size()),
180 std::size_t>::value,
181 std::nullptr_t>::type = nullptr)
182 : m_data(other.data()), m_size(other.size()) {}
183
184 template <typename V>
185 constexpr Span(
186 const V &other SPAN_ATTR_LIFETIMEBOUND,
187 typename std::enable_if<
189 std::is_convertible<
190 typename std::remove_pointer<
191 decltype(std::declval<const V &>().data())>::type (*)[],
192 C (*)[]>::value &&
193 std::is_convertible<decltype(std::declval<const V &>().size()),
194 std::size_t>::value,
195 std::nullptr_t>::type = nullptr)
196 : m_data(other.data()), m_size(other.size()) {}
197
198 constexpr C *data() const noexcept { return m_data; }
199 constexpr C *begin() const noexcept { return m_data; }
200 constexpr C *end() const noexcept { return m_data + m_size; }
201 CONSTEXPR_IF_NOT_DEBUG C &front() const noexcept {
202 ASSERT_IF_DEBUG(size() > 0);
203 return m_data[0];
204 }
205 CONSTEXPR_IF_NOT_DEBUG C &back() const noexcept {
206 ASSERT_IF_DEBUG(size() > 0);
207 return m_data[m_size - 1];
208 }
209 constexpr std::size_t size() const noexcept { return m_size; }
210 constexpr std::size_t size_bytes() const noexcept {
211 return sizeof(C) * m_size;
212 }
213 constexpr bool empty() const noexcept { return size() == 0; }
214 CONSTEXPR_IF_NOT_DEBUG C &operator[](std::size_t pos) const noexcept {
215 ASSERT_IF_DEBUG(size() > pos);
216 return m_data[pos];
217 }
218 CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept {
219 ASSERT_IF_DEBUG(size() >= offset);
220 return Span<C>(m_data + offset, m_size - offset);
221 }
223 std::size_t count) const noexcept {
224 ASSERT_IF_DEBUG(size() >= offset + count);
225 return Span<C>(m_data + offset, count);
226 }
227 CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept {
229 return Span<C>(m_data, count);
230 }
231 CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept {
233 return Span<C>(m_data + m_size - count, count);
234 }
235
236 friend constexpr bool operator==(const Span &a, const Span &b) noexcept {
237 return a.size() == b.size() &&
238 std::equal(a.begin(), a.end(), b.begin());
239 }
240 friend constexpr bool operator!=(const Span &a, const Span &b) noexcept {
241 return !(a == b);
242 }
243 friend constexpr bool operator<(const Span &a, const Span &b) noexcept {
244 return std::lexicographical_compare(a.begin(), a.end(), b.begin(),
245 b.end());
246 }
247 friend constexpr bool operator<=(const Span &a, const Span &b) noexcept {
248 return !(b < a);
249 }
250 friend constexpr bool operator>(const Span &a, const Span &b) noexcept {
251 return (b < a);
252 }
253 friend constexpr bool operator>=(const Span &a, const Span &b) noexcept {
254 return !(a < b);
255 }
256
257 template <typename O> friend class Span;
258};
259
260// Deduction guides for Span
261// For the pointer/size based and iterator based constructor:
262template <typename T, typename EndOrSize> Span(T *, EndOrSize) -> Span<T>;
263// For the array constructor:
264template <typename T, std::size_t N> Span(T (&)[N]) -> Span<T>;
265// For the temporaries/rvalue references constructor, only supporting const
266// output.
267template <typename T>
268Span(T &&) -> Span<std::enable_if_t<
269 !std::is_lvalue_reference_v<T>,
270 const std::remove_pointer_t<decltype(std::declval<T &&>().data())>>>;
271// For (lvalue) references, supporting mutable output.
272template <typename T>
274
276template <typename T> T &SpanPopBack(Span<T> &span) {
277 size_t size = span.size();
279 T &back = span[size - 1];
280 span = Span<T>(span.data(), size - 1);
281 return back;
282}
283
286inline const std::byte *BytePtr(const void *data) {
287 return reinterpret_cast<const std::byte *>(data);
288}
289inline std::byte *BytePtr(void *data) {
290 return reinterpret_cast<std::byte *>(data);
291}
292
293// From C++20 as_bytes and as_writeable_bytes
294template <typename T> Span<const std::byte> AsBytes(Span<T> s) noexcept {
295 return {BytePtr(s.data()), s.size_bytes()};
296}
297template <typename T> Span<std::byte> AsWritableBytes(Span<T> s) noexcept {
298 return {BytePtr(s.data()), s.size_bytes()};
299}
300
301template <typename V> Span<const std::byte> MakeByteSpan(V &&v) noexcept {
302 return AsBytes(Span(std::forward<V>(v)));
303}
304template <typename V> Span<std::byte> MakeWritableByteSpan(V &&v) noexcept {
305 return AsWritableBytes(Span(std::forward<V>(v)));
306}
307
308// Helper functions to safely cast to uint8_t pointers.
309inline uint8_t *UCharCast(char *c) {
310 return (uint8_t *)c;
311}
312inline uint8_t *UCharCast(uint8_t *c) {
313 return c;
314}
315inline const uint8_t *UCharCast(const char *c) {
316 return (uint8_t *)c;
317}
318inline const uint8_t *UCharCast(const uint8_t *c) {
319 return c;
320}
321inline const uint8_t *UCharCast(const std::byte *c) {
322 return reinterpret_cast<const uint8_t *>(c);
323}
324
325// Helper function to safely convert a Span to a Span<[const] uint8_t>.
326template <typename T>
327constexpr auto UCharSpanCast(Span<T> s)
328 -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> {
329 return {UCharCast(s.data()), s.size()};
330}
331
336template <typename V>
337constexpr auto MakeUCharSpan(V &&v)
338 -> decltype(UCharSpanCast(Span{std::forward<V>(v)})) {
339 return UCharSpanCast(Span{std::forward<V>(v)});
340}
341
342#endif // BITCOIN_SPAN_H
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
CONSTEXPR_IF_NOT_DEBUG Span< C > last(std::size_t count) const noexcept
Definition: span.h:231
CONSTEXPR_IF_NOT_DEBUG C & back() const noexcept
Definition: span.h:205
constexpr std::size_t size() const noexcept
Definition: span.h:209
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:218
constexpr Span(C(&a)[N]) noexcept
Construct a Span from an array.
Definition: span.h:157
constexpr Span() noexcept
Definition: span.h:103
std::size_t m_size
Definition: span.h:95
CONSTEXPR_IF_NOT_DEBUG C & operator[](std::size_t pos) const noexcept
Definition: span.h:214
constexpr Span(const V &other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if< !is_Span< V >::value &&std::is_convertible< typename std::remove_pointer< decltype(std::declval< const V & >().data())>::type(*)[], C(*)[]>::value &&std::is_convertible< decltype(std::declval< const V & >().size()), std::size_t >::value, std::nullptr_t >::type=nullptr)
Definition: span.h:185
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:243
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:227
constexpr C * data() const noexcept
Definition: span.h:198
friend constexpr bool operator<=(const Span &a, const Span &b) noexcept
Definition: span.h:247
constexpr Span(T *begin, std::size_t size) noexcept
Construct a span from a begin pointer and a size.
Definition: span.h:114
CONSTEXPR_IF_NOT_DEBUG Span(T *begin, T *end) noexcept
Construct a span from a begin and end pointer.
Definition: span.h:126
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:250
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:236
C * m_data
Definition: span.h:94
constexpr Span(const Span &) noexcept=default
Default copy constructor.
constexpr std::size_t size_bytes() const noexcept
Definition: span.h:210
Span & operator=(const Span &other) noexcept=default
Default assignment operator.
constexpr C * begin() const noexcept
Definition: span.h:199
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:240
constexpr bool empty() const noexcept
Definition: span.h:213
constexpr C * end() const noexcept
Definition: span.h:200
friend constexpr bool operator>=(const Span &a, const Span &b) noexcept
Definition: span.h:253
CONSTEXPR_IF_NOT_DEBUG C & front() const noexcept
Definition: span.h:201
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset, std::size_t count) const noexcept
Definition: span.h:222
constexpr Span(const Span< O > &other) noexcept
Implicit conversion of spans between compatible types.
Definition: span.h:145
constexpr Span(V &other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if< !is_Span< V >::value &&std::is_convertible< typename std::remove_pointer< decltype(std::declval< V & >().data())>::type(*)[], C(*)[]>::value &&std::is_convertible< decltype(std::declval< V & >().size()), std::size_t >::value, std::nullptr_t >::type=nullptr)
Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector,...
Definition: span.h:171
Span< std::byte > AsWritableBytes(Span< T > s) noexcept
Definition: span.h:297
#define ASSERT_IF_DEBUG(x)
Definition: span.h:19
const std::byte * BytePtr(const void *data)
Convert a data pointer to a std::byte data pointer.
Definition: span.h:286
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:301
#define SPAN_ATTR_LIFETIMEBOUND
Definition: span.h:29
uint8_t * UCharCast(char *c)
Definition: span.h:309
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
Definition: span.h:337
T & SpanPopBack(Span< T > &span)
Pop the last element off a span, and return a reference to that element.
Definition: span.h:276
Span(T *, EndOrSize) -> Span< T >
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:294
constexpr auto UCharSpanCast(Span< T > s) -> Span< typename std::remove_pointer< decltype(UCharCast(s.data()))>::type >
Definition: span.h:327
#define CONSTEXPR_IF_NOT_DEBUG
Definition: span.h:18
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:304
static int count
Definition: tests.c:31