Bitcoin Core  0.20.99
P2P Digital Currency
span.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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 <type_traits>
9 #include <cstddef>
10 #include <algorithm>
11 #include <assert.h>
12 
13 #ifdef DEBUG
14 #define CONSTEXPR_IF_NOT_DEBUG
15 #define ASSERT_IF_DEBUG(x) assert((x))
16 #else
17 #define CONSTEXPR_IF_NOT_DEBUG constexpr
18 #define ASSERT_IF_DEBUG(x)
19 #endif
20 
81 template<typename C>
82 class Span
83 {
84  C* m_data;
85  std::size_t m_size;
86 
87 public:
88  constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
89 
95  template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
96  constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {}
97 
103  template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
105  {
107  }
108 
117  template <typename O, typename std::enable_if<std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0>
118  constexpr Span(const Span<O>& other) noexcept : m_data(other.m_data), m_size(other.m_size) {}
119 
121  constexpr Span(const Span&) noexcept = default;
122 
124  Span& operator=(const Span& other) noexcept = default;
125 
127  template <int N>
128  constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {}
129 
137  template <typename V, typename std::enable_if<(std::is_const<C>::value || std::is_lvalue_reference<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, int>::type = 0>
138  constexpr Span(V&& v) noexcept : m_data(v.data()), m_size(v.size()) {}
139 
140  constexpr C* data() const noexcept { return m_data; }
141  constexpr C* begin() const noexcept { return m_data; }
142  constexpr C* end() const noexcept { return m_data + m_size; }
143  CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept
144  {
145  ASSERT_IF_DEBUG(size() > 0);
146  return m_data[0];
147  }
148  CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept
149  {
150  ASSERT_IF_DEBUG(size() > 0);
151  return m_data[m_size - 1];
152  }
153  constexpr std::size_t size() const noexcept { return m_size; }
154  constexpr bool empty() const noexcept { return size() == 0; }
155  CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept
156  {
157  ASSERT_IF_DEBUG(size() > pos);
158  return m_data[pos];
159  }
160  CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept
161  {
162  ASSERT_IF_DEBUG(size() >= offset);
163  return Span<C>(m_data + offset, m_size - offset);
164  }
165  CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset, std::size_t count) const noexcept
166  {
167  ASSERT_IF_DEBUG(size() >= offset + count);
168  return Span<C>(m_data + offset, count);
169  }
170  CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept
171  {
172  ASSERT_IF_DEBUG(size() >= count);
173  return Span<C>(m_data, count);
174  }
175  CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept
176  {
177  ASSERT_IF_DEBUG(size() >= count);
178  return Span<C>(m_data + m_size - count, count);
179  }
180 
181  friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
182  friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
183  friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
184  friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
185  friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
186  friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
187 
188  template <typename O> friend class Span;
189 };
190 
191 // MakeSpan helps constructing a Span of the right type automatically.
193 template <typename A, int N> Span<A> constexpr MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
195 template <typename V> constexpr auto MakeSpan(V&& v) -> typename std::enable_if<!std::is_lvalue_reference<V>::value, Span<const typename std::remove_pointer<decltype(v.data())>::type>>::type { return std::forward<V>(v); }
197 template <typename V> constexpr auto MakeSpan(V& v) -> Span<typename std::remove_pointer<decltype(v.data())>::type> { return v; }
198 
200 template <typename T>
202 {
203  size_t size = span.size();
204  ASSERT_IF_DEBUG(size > 0);
205  T& back = span[size - 1];
206  span = Span<T>(span.data(), size - 1);
207  return back;
208 }
209 
210 // Helper functions to safely cast to unsigned char pointers.
211 inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; }
212 inline unsigned char* UCharCast(unsigned char* c) { return c; }
213 inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; }
214 inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
215 
216 // Helper function to safely convert a Span to a Span<[const] unsigned char>.
217 template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; }
218 
220 template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward<V>(v)))) { return UCharSpanCast(MakeSpan(std::forward<V>(v))); }
221 
222 #endif
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:170
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:160
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:183
constexpr C * end() const noexcept
Definition: span.h:142
constexpr Span(C(&a)[N]) noexcept
Construct a Span from an array.
Definition: span.h:128
friend constexpr bool operator<=(const Span &a, const Span &b) noexcept
Definition: span.h:184
constexpr Span(T *begin, std::size_t size) noexcept
Construct a span from a begin pointer and a size.
Definition: span.h:96
constexpr std::size_t size() const noexcept
Definition: span.h:153
constexpr Span(const Span< O > &other) noexcept
Implicit conversion of spans between compatible types.
Definition: span.h:118
friend constexpr bool operator>=(const Span &a, const Span &b) noexcept
Definition: span.h:186
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:182
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:181
#define ASSERT_IF_DEBUG(x)
Definition: span.h:18
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:185
CONSTEXPR_IF_NOT_DEBUG C & operator[](std::size_t pos) const noexcept
Definition: span.h:155
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
Definition: span.h:220
std::size_t m_size
Definition: span.h:85
constexpr Span() noexcept
Definition: span.h:88
CONSTEXPR_IF_NOT_DEBUG C & front() const noexcept
Definition: span.h:143
CONSTEXPR_IF_NOT_DEBUG Span(T *begin, T *end) noexcept
Construct a span from a begin and end pointer.
Definition: span.h:104
constexpr C * begin() const noexcept
Definition: span.h:141
constexpr auto UCharSpanCast(Span< T > s) -> Span< typename std::remove_pointer< decltype(UCharCast(s.data()))>::type >
Definition: span.h:217
constexpr C * data() const noexcept
Definition: span.h:140
#define CONSTEXPR_IF_NOT_DEBUG
Definition: span.h:17
CONSTEXPR_IF_NOT_DEBUG C & back() const noexcept
Definition: span.h:148
Span & operator=(const Span &other) noexcept=default
Default assignment operator.
T & SpanPopBack(Span< T > &span)
Pop the last element off a span, and return a reference to that element.
Definition: span.h:201
constexpr bool empty() const noexcept
Definition: span.h:154
static int count
Definition: tests.c:35
unsigned char * UCharCast(char *c)
Definition: span.h:211
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:82
constexpr Span(V &&v) noexcept
Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...).
Definition: span.h:138
C * m_data
Definition: span.h:84
CONSTEXPR_IF_NOT_DEBUG Span< C > last(std::size_t count) const noexcept
Definition: span.h:175
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset, std::size_t count) const noexcept
Definition: span.h:165
Span< A > constexpr MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:193