Bitcoin ABC  0.24.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 
83 template <typename C> class Span {
84  C *m_data;
85  std::size_t m_size;
86 
87 public:
88  constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
89 
96  template <typename T,
97  typename std::enable_if<
98  std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
99  constexpr Span(T *begin, std::size_t size) noexcept
100  : m_data(begin), m_size(size) {}
101 
108  template <typename T,
109  typename std::enable_if<
110  std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
112  : m_data(begin), m_size(end - begin) {
114  }
115 
127  template <typename O,
128  typename std::enable_if<
129  std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0>
130  constexpr Span(const Span<O> &other) noexcept
131  : m_data(other.m_data), m_size(other.m_size) {}
132 
134  constexpr Span(const Span &) noexcept = default;
135 
137  Span &operator=(const Span &other) noexcept = default;
138 
141  template <int N>
142  constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {}
143 
155  template <
156  typename V,
157  typename std::enable_if<
158  (std::is_const<C>::value || std::is_lvalue_reference<V>::value) &&
159  std::is_convertible<
160  typename std::remove_pointer<
161  decltype(std::declval<V &>().data())>::type (*)[],
162  C (*)[]>::value &&
163  std::is_convertible<decltype(std::declval<V &>().size()),
164  std::size_t>::value,
165  int>::type = 0>
166  constexpr Span(V &&v) noexcept : m_data(v.data()), m_size(v.size()) {}
167 
168  constexpr C *data() const noexcept { return m_data; }
169  constexpr C *begin() const noexcept { return m_data; }
170  constexpr C *end() const noexcept { return m_data + m_size; }
171  CONSTEXPR_IF_NOT_DEBUG C &front() const noexcept {
172  ASSERT_IF_DEBUG(size() > 0);
173  return m_data[0];
174  }
175  CONSTEXPR_IF_NOT_DEBUG C &back() const noexcept {
176  ASSERT_IF_DEBUG(size() > 0);
177  return m_data[m_size - 1];
178  }
179  constexpr std::size_t size() const noexcept { return m_size; }
180  constexpr bool empty() const noexcept { return size() == 0; }
181  CONSTEXPR_IF_NOT_DEBUG C &operator[](std::size_t pos) const noexcept {
182  ASSERT_IF_DEBUG(size() > pos);
183  return m_data[pos];
184  }
185  CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept {
186  ASSERT_IF_DEBUG(size() >= offset);
187  return Span<C>(m_data + offset, m_size - offset);
188  }
190  std::size_t count) const noexcept {
191  ASSERT_IF_DEBUG(size() >= offset + count);
192  return Span<C>(m_data + offset, count);
193  }
194  CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept {
195  ASSERT_IF_DEBUG(size() >= count);
196  return Span<C>(m_data, count);
197  }
198  CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept {
199  ASSERT_IF_DEBUG(size() >= count);
200  return Span<C>(m_data + m_size - count, count);
201  }
202 
203  friend constexpr bool operator==(const Span &a, const Span &b) noexcept {
204  return a.size() == b.size() &&
205  std::equal(a.begin(), a.end(), b.begin());
206  }
207  friend constexpr bool operator!=(const Span &a, const Span &b) noexcept {
208  return !(a == b);
209  }
210  friend constexpr bool operator<(const Span &a, const Span &b) noexcept {
211  return std::lexicographical_compare(a.begin(), a.end(), b.begin(),
212  b.end());
213  }
214  friend constexpr bool operator<=(const Span &a, const Span &b) noexcept {
215  return !(b < a);
216  }
217  friend constexpr bool operator>(const Span &a, const Span &b) noexcept {
218  return (b < a);
219  }
220  friend constexpr bool operator>=(const Span &a, const Span &b) noexcept {
221  return !(a < b);
222  }
223 
224  template <typename O> friend class Span;
225 };
226 
227 // MakeSpan helps constructing a Span of the right type automatically.
229 template <typename A, int N> Span<A> constexpr MakeSpan(A (&a)[N]) {
230  return Span<A>(a, N);
231 }
234 template <typename V>
235 constexpr auto MakeSpan(V &&v) -> typename std::enable_if<
236  !std::is_lvalue_reference<V>::value,
237  Span<const typename std::remove_pointer<decltype(v.data())>::type>>::type {
238  return std::forward<V>(v);
239 }
241 template <typename V>
242 constexpr auto MakeSpan(V &v)
243  -> Span<typename std::remove_pointer<decltype(v.data())>::type> {
244  return v;
245 }
246 
248 template <typename T> T &SpanPopBack(Span<T> &span) {
249  size_t size = span.size();
250  ASSERT_IF_DEBUG(size > 0);
251  T &back = span[size - 1];
252  span = Span<T>(span.data(), size - 1);
253  return back;
254 }
255 
256 // Helper functions to safely cast to uint8_t pointers.
257 inline uint8_t *UCharCast(char *c) {
258  return (uint8_t *)c;
259 }
260 inline uint8_t *UCharCast(uint8_t *c) {
261  return c;
262 }
263 inline const uint8_t *UCharCast(const char *c) {
264  return (uint8_t *)c;
265 }
266 inline const uint8_t *UCharCast(const uint8_t *c) {
267  return c;
268 }
269 
270 // Helper function to safely convert a Span to a Span<[const] uint8_t>.
271 template <typename T>
272 constexpr auto UCharSpanCast(Span<T> s)
273  -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> {
274  return {UCharCast(s.data()), s.size()};
275 }
276 
279 template <typename V>
280 constexpr auto MakeUCharSpan(V &&v)
281  -> decltype(UCharSpanCast(MakeSpan(std::forward<V>(v)))) {
282  return UCharSpanCast(MakeSpan(std::forward<V>(v)));
283 }
284 
285 #endif // BITCOIN_SPAN_H
UCharSpanCast
constexpr auto UCharSpanCast(Span< T > s) -> Span< typename std::remove_pointer< decltype(UCharCast(s.data()))>::type >
Definition: span.h:272
Span::operator==
constexpr friend bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:203
Span::m_data
C * m_data
Definition: span.h:84
count
static int count
Definition: tests.c:41
Span::Span
constexpr Span(C(&a)[N]) noexcept
Construct a Span from an array.
Definition: span.h:142
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) uint8_t member types only.
Definition: span.h:280
SpanPopBack
T & SpanPopBack(Span< T > &span)
Pop the last element off a span, and return a reference to that element.
Definition: span.h:248
UCharCast
uint8_t * UCharCast(char *c)
Definition: span.h:257
Span::Span
constexpr Span(const Span< O > &other) noexcept
Implicit conversion of spans between compatible types.
Definition: span.h:130
Span::Span
constexpr Span(T *begin, std::size_t size) noexcept
Construct a span from a begin pointer and a size.
Definition: span.h:99
Span::operator[]
CONSTEXPR_IF_NOT_DEBUG C & operator[](std::size_t pos) const noexcept
Definition: span.h:181
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:179
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:83
CONSTEXPR_IF_NOT_DEBUG
#define CONSTEXPR_IF_NOT_DEBUG
Definition: span.h:18
Span::m_size
std::size_t m_size
Definition: span.h:85
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:169
Span::Span
friend class Span
Definition: span.h:224
ASSERT_IF_DEBUG
#define ASSERT_IF_DEBUG(x)
Definition: span.h:19
Span::empty
constexpr bool empty() const noexcept
Definition: span.h:180
Span::front
CONSTEXPR_IF_NOT_DEBUG C & front() const noexcept
Definition: span.h:171
Span::operator<=
constexpr friend bool operator<=(const Span &a, const Span &b) noexcept
Definition: span.h:214
Span::operator>
constexpr friend bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:217
Span::data
constexpr C * data() const noexcept
Definition: span.h:168
Span::operator!=
constexpr friend bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:207
Span::Span
constexpr Span(V &&v) noexcept
Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector,...
Definition: span.h:166
Span::back
CONSTEXPR_IF_NOT_DEBUG C & back() const noexcept
Definition: span.h:175
Span::Span
constexpr Span() noexcept
Definition: span.h:88
Span::Span
CONSTEXPR_IF_NOT_DEBUG Span(T *begin, T *end) noexcept
Construct a span from a begin and end pointer.
Definition: span.h:111
Span::last
CONSTEXPR_IF_NOT_DEBUG Span< C > last(std::size_t count) const noexcept
Definition: span.h:198
Span::operator>=
constexpr friend bool operator>=(const Span &a, const Span &b) noexcept
Definition: span.h:220
Span::first
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:194
Span::operator<
constexpr friend bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:210
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:229
Span::operator=
Span & operator=(const Span &other) noexcept=default
Default assignment operator.
Span::subspan
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset, std::size_t count) const noexcept
Definition: span.h:189
Span::end
constexpr C * end() const noexcept
Definition: span.h:170
Span::subspan
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:185