Branch data Line data Source code
1 : : // Copyright (c) 2018-2022 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 <type_traits>
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 : :
21 : : #if defined(__clang__)
22 : : #if __has_attribute(lifetimebound)
23 : : #define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]]
24 : : #else
25 : : #define SPAN_ATTR_LIFETIMEBOUND
26 : : #endif
27 : : #else
28 : : #define SPAN_ATTR_LIFETIMEBOUND
29 : : #endif
30 : :
31 : : /** A Span is an object that can refer to a contiguous sequence of objects.
32 : : *
33 : : * This file implements a subset of C++20's std::span. It can be considered
34 : : * temporary compatibility code until C++20 and is designed to be a
35 : : * self-contained abstraction without depending on other project files. For this
36 : : * reason, Clang lifetimebound is defined here instead of including
37 : : * <attributes.h>, which also defines it.
38 : : *
39 : : * Things to be aware of when writing code that deals with Spans:
40 : : *
41 : : * - Similar to references themselves, Spans are subject to reference lifetime
42 : : * issues. The user is responsible for making sure the objects pointed to by
43 : : * a Span live as long as the Span is used. For example:
44 : : *
45 : : * std::vector<int> vec{1,2,3,4};
46 : : * Span<int> sp(vec);
47 : : * vec.push_back(5);
48 : : * printf("%i\n", sp.front()); // UB!
49 : : *
50 : : * may exhibit undefined behavior, as increasing the size of a vector may
51 : : * invalidate references.
52 : : *
53 : : * - One particular pitfall is that Spans can be constructed from temporaries,
54 : : * but this is unsafe when the Span is stored in a variable, outliving the
55 : : * temporary. For example, this will compile, but exhibits undefined behavior:
56 : : *
57 : : * Span<const int> sp(std::vector<int>{1, 2, 3});
58 : : * printf("%i\n", sp.front()); // UB!
59 : : *
60 : : * The lifetime of the vector ends when the statement it is created in ends.
61 : : * Thus the Span is left with a dangling reference, and using it is undefined.
62 : : *
63 : : * - Due to Span's automatic creation from range-like objects (arrays, and data
64 : : * types that expose a data() and size() member function), functions that
65 : : * accept a Span as input parameter can be called with any compatible
66 : : * range-like object. For example, this works:
67 : : *
68 : : * void Foo(Span<const int> arg);
69 : : *
70 : : * Foo(std::vector<int>{1, 2, 3}); // Works
71 : : *
72 : : * This is very useful in cases where a function truly does not care about the
73 : : * container, and only about having exactly a range of elements. However it
74 : : * may also be surprising to see automatic conversions in this case.
75 : : *
76 : : * When a function accepts a Span with a mutable element type, it will not
77 : : * accept temporaries; only variables or other references. For example:
78 : : *
79 : : * void FooMut(Span<int> arg);
80 : : *
81 : : * FooMut(std::vector<int>{1, 2, 3}); // Does not compile
82 : : * std::vector<int> baz{1, 2, 3};
83 : : * FooMut(baz); // Works
84 : : *
85 : : * This is similar to how functions that take (non-const) lvalue references
86 : : * as input cannot accept temporaries. This does not work either:
87 : : *
88 : : * void FooVec(std::vector<int>& arg);
89 : : * FooVec(std::vector<int>{1, 2, 3}); // Does not compile
90 : : *
91 : : * The idea is that if a function accepts a mutable reference, a meaningful
92 : : * result will be present in that variable after the call. Passing a temporary
93 : : * is useless in that context.
94 : : */
95 : : template<typename C>
96 : : class Span
97 : : {
98 : : C* m_data;
99 : 0 : std::size_t m_size{0};
100 : :
101 : : template <class T>
102 : : struct is_Span_int : public std::false_type {};
103 : : template <class T>
104 : : struct is_Span_int<Span<T>> : public std::true_type {};
105 : : template <class T>
106 : : struct is_Span : public is_Span_int<typename std::remove_cv<T>::type>{};
107 : :
108 : :
109 : : public:
110 : 0 : constexpr Span() noexcept : m_data(nullptr) {}
111 : :
112 : : /** Construct a span from a begin pointer and a size.
113 : : *
114 : : * This implements a subset of the iterator-based std::span constructor in C++20,
115 : : * which is hard to implement without std::address_of.
116 : : */
117 : : template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
118 : 265040 : constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {}
119 : :
120 : : /** Construct a span from a begin and end pointer.
121 : : *
122 : : * This implements a subset of the iterator-based std::span constructor in C++20,
123 : : * which is hard to implement without std::address_of.
124 : : */
125 : : template <typename T, typename std::enable_if<std::is_convertible<T (*)[], C (*)[]>::value, int>::type = 0>
126 : 0 : CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin)
127 : : {
128 : : ASSERT_IF_DEBUG(end >= begin);
129 : 0 : }
130 : :
131 : : /** Implicit conversion of spans between compatible types.
132 : : *
133 : : * Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type
134 : : * C, then permit implicit conversion of Span<O> to Span<C>. This matches the behavior of the corresponding
135 : : * C++20 std::span constructor.
136 : : *
137 : : * For example this means that a Span<T> can be converted into a Span<const T>.
138 : : */
139 : : template <typename O, typename std::enable_if<std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0>
140 : 0 : constexpr Span(const Span<O>& other) noexcept : m_data(other.m_data), m_size(other.m_size) {}
141 : :
142 : : /** Default copy constructor. */
143 : : constexpr Span(const Span&) noexcept = default;
144 : :
145 : : /** Default assignment operator. */
146 : : Span& operator=(const Span& other) noexcept = default;
147 : :
148 : : /** Construct a Span from an array. This matches the corresponding C++20 std::span constructor. */
149 : : template <int N>
150 : 24787 : constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {}
151 : :
152 : : /** Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...).
153 : : *
154 : : * This implements a subset of the functionality provided by the C++20 std::span range-based constructor.
155 : : *
156 : : * To prevent surprises, only Spans for constant value types are supported when passing in temporaries.
157 : : * Note that this restriction does not exist when converting arrays or other Spans (see above).
158 : : */
159 : : template <typename V>
160 : 29410 : constexpr Span(V& other SPAN_ATTR_LIFETIMEBOUND,
161 : : typename std::enable_if<!is_Span<V>::value &&
162 : : std::is_convertible<typename std::remove_pointer<decltype(std::declval<V&>().data())>::type (*)[], C (*)[]>::value &&
163 : : std::is_convertible<decltype(std::declval<V&>().size()), std::size_t>::value, std::nullptr_t>::type = nullptr)
164 : 29410 : : m_data(other.data()), m_size(other.size()){}
165 : :
166 : : template <typename V>
167 : 66900 : constexpr Span(const V& other SPAN_ATTR_LIFETIMEBOUND,
168 : : typename std::enable_if<!is_Span<V>::value &&
169 : : std::is_convertible<typename std::remove_pointer<decltype(std::declval<const V&>().data())>::type (*)[], C (*)[]>::value &&
170 : : std::is_convertible<decltype(std::declval<const V&>().size()), std::size_t>::value, std::nullptr_t>::type = nullptr)
171 : 66900 : : m_data(other.data()), m_size(other.size()){}
172 : :
173 : 348506 : constexpr C* data() const noexcept { return m_data; }
174 : 17313 : constexpr C* begin() const noexcept { return m_data; }
175 : 57106 : constexpr C* end() const noexcept { return m_data + m_size; }
176 : 1156 : CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept
177 : : {
178 : : ASSERT_IF_DEBUG(size() > 0);
179 : 1156 : return m_data[0];
180 : : }
181 : 0 : CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept
182 : : {
183 : : ASSERT_IF_DEBUG(size() > 0);
184 : 0 : return m_data[m_size - 1];
185 : : }
186 : 389066 : constexpr std::size_t size() const noexcept { return m_size; }
187 : 146324 : constexpr std::size_t size_bytes() const noexcept { return sizeof(C) * m_size; }
188 : 23850 : constexpr bool empty() const noexcept { return size() == 0; }
189 : 18 : CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept
190 : : {
191 : : ASSERT_IF_DEBUG(size() > pos);
192 : 18 : return m_data[pos];
193 : : }
194 : 31160 : CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset) const noexcept
195 : : {
196 : : ASSERT_IF_DEBUG(size() >= offset);
197 : 31160 : return Span<C>(m_data + offset, m_size - offset);
198 : : }
199 : 0 : CONSTEXPR_IF_NOT_DEBUG Span<C> subspan(std::size_t offset, std::size_t count) const noexcept
200 : : {
201 : : ASSERT_IF_DEBUG(size() >= offset + count);
202 : 0 : return Span<C>(m_data + offset, count);
203 : : }
204 : 5694 : CONSTEXPR_IF_NOT_DEBUG Span<C> first(std::size_t count) const noexcept
205 : : {
206 : : ASSERT_IF_DEBUG(size() >= count);
207 : 5694 : return Span<C>(m_data, count);
208 : : }
209 : 592 : CONSTEXPR_IF_NOT_DEBUG Span<C> last(std::size_t count) const noexcept
210 : : {
211 : : ASSERT_IF_DEBUG(size() >= count);
212 : 592 : return Span<C>(m_data + m_size - count, count);
213 : : }
214 : :
215 [ # # ][ # # ]: 0 : friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
[ # # ][ # # ]
216 : 0 : friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
217 [ # # ]: 0 : friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
218 : 0 : friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
219 : 0 : friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
220 : 0 : friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
221 : :
222 : : template <typename O> friend class Span;
223 : : };
224 : :
225 : : // Deduction guides for Span
226 : : // For the pointer/size based and iterator based constructor:
227 : : template <typename T, typename EndOrSize> Span(T*, EndOrSize) -> Span<T>;
228 : : // For the array constructor:
229 : : template <typename T, std::size_t N> Span(T (&)[N]) -> Span<T>;
230 : : // For the temporaries/rvalue references constructor, only supporting const output.
231 : : template <typename T> Span(T&&) -> Span<std::enable_if_t<!std::is_lvalue_reference_v<T>, const std::remove_pointer_t<decltype(std::declval<T&&>().data())>>>;
232 : : // For (lvalue) references, supporting mutable output.
233 : : template <typename T> Span(T&) -> Span<std::remove_pointer_t<decltype(std::declval<T&>().data())>>;
234 : :
235 : : /** Pop the last element off a span, and return a reference to that element. */
236 : : template <typename T>
237 : 0 : T& SpanPopBack(Span<T>& span)
238 : : {
239 : 0 : size_t size = span.size();
240 : : ASSERT_IF_DEBUG(size > 0);
241 : 0 : T& back = span[size - 1];
242 : 0 : span = Span<T>(span.data(), size - 1);
243 : 0 : return back;
244 : : }
245 : :
246 : : // From C++20 as_bytes and as_writeable_bytes
247 : : template <typename T>
248 : 122244 : Span<const std::byte> AsBytes(Span<T> s) noexcept
249 : : {
250 : 122244 : return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
251 : : }
252 : : template <typename T>
253 : 24081 : Span<std::byte> AsWritableBytes(Span<T> s) noexcept
254 : : {
255 : 24081 : return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
256 : : }
257 : :
258 : : template <typename V>
259 : 38477 : Span<const std::byte> MakeByteSpan(V&& v) noexcept
260 : : {
261 [ + - ][ # # ]: 38477 : return AsBytes(Span{std::forward<V>(v)});
[ + - ][ # # ]
[ # # ]
[ + - # # ]
[ # # ][ # # ]
[ + - ]
262 : : }
263 : : template <typename V>
264 : 17960 : Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
265 : : {
266 [ + - ][ # # ]: 17960 : return AsWritableBytes(Span{std::forward<V>(v)});
[ # # ][ # # ]
[ + - # # ]
[ # # ][ # # ]
[ # # ]
267 : : }
268 : :
269 : : // Helper functions to safely cast to unsigned char pointers.
270 : 0 : inline unsigned char* UCharCast(char* c) { return reinterpret_cast<unsigned char*>(c); }
271 : 5980 : inline unsigned char* UCharCast(unsigned char* c) { return c; }
272 : 4351 : inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast<unsigned char*>(c); }
273 : 0 : inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); }
274 : 27336 : inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
275 : 146162 : inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }
276 : :
277 : : // Helper function to safely convert a Span to a Span<[const] unsigned char>.
278 : 6050 : 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()}; }
279 : :
280 : : /** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
281 : 6049 : template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward<V>(v)})) { return UCharSpanCast(Span{std::forward<V>(v)}); }
282 : :
283 : : #endif // BITCOIN_SPAN_H
|