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_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept
155  {
156  ASSERT_IF_DEBUG(size() > pos);
157  return m_data[pos];
158  }
159  CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept
160  {
161  ASSERT_IF_DEBUG(size() >= offset);
162  return Span<C>(m_data + offset, m_size - offset);
163  }
164  CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset, std::size_t count) const noexcept
165  {
166  ASSERT_IF_DEBUG(size() >= offset + count);
167  return Span<C>(m_data + offset, count);
168  }
169  CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept
170  {
171  ASSERT_IF_DEBUG(size() >= count);
172  return Span<C>(m_data, count);
173  }
174  CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept
175  {
176  ASSERT_IF_DEBUG(size() >= count);
177  return Span<C>(m_data + m_size - count, count);
178  }
179 
180  friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
181  friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
182  friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
183  friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
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 !(a < b); }
186 
187  template <typename O> friend class Span;
188 };
189 
190 // MakeSpan helps constructing a Span of the right type automatically.
192 template <typename A, int N> Span<A> constexpr MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
194 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); }
196 template <typename V> constexpr auto MakeSpan(V& v) -> Span<typename std::remove_pointer<decltype(v.data())>::type> { return v; }
197 
199 template <typename T>
201 {
202  size_t size = span.size();
203  ASSERT_IF_DEBUG(size > 0);
204  T& back = span[size - 1];
205  span = Span<T>(span.data(), size - 1);
206  return back;
207 }
208 
209 #endif
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:169
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:159
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:182
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:183
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:185
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:181
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:180
#define ASSERT_IF_DEBUG(x)
Definition: span.h:18
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:184
CONSTEXPR_IF_NOT_DEBUG C & operator[](std::size_t pos) const noexcept
Definition: span.h:154
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 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:200
static int count
Definition: tests.c:35
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:174
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset, std::size_t count) const noexcept
Definition: span.h:164
Span< A > constexpr MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:192