Bitcoin Core  0.19.99
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 <type_traits>
9 #include <cstddef>
10 #include <algorithm>
11 #include <assert.h>
12 
17 template<typename C>
18 class Span
19 {
20  C* m_data;
21  std::ptrdiff_t m_size;
22 
23 public:
24  constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
25  constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {}
26  constexpr Span(C* data, C* end) noexcept : m_data(data), m_size(end - data) {}
27 
28  constexpr C* data() const noexcept { return m_data; }
29  constexpr C* begin() const noexcept { return m_data; }
30  constexpr C* end() const noexcept { return m_data + m_size; }
31  constexpr C& front() const noexcept { return m_data[0]; }
32  constexpr C& back() const noexcept { return m_data[m_size - 1]; }
33  constexpr std::ptrdiff_t size() const noexcept { return m_size; }
34  constexpr C& operator[](std::ptrdiff_t pos) const noexcept { return m_data[pos]; }
35 
36  constexpr Span<C> subspan(std::ptrdiff_t offset) const noexcept { return Span<C>(m_data + offset, m_size - offset); }
37  constexpr Span<C> subspan(std::ptrdiff_t offset, std::ptrdiff_t count) const noexcept { return Span<C>(m_data + offset, count); }
38  constexpr Span<C> first(std::ptrdiff_t count) const noexcept { return Span<C>(m_data, count); }
39  constexpr Span<C> last(std::ptrdiff_t count) const noexcept { return Span<C>(m_data + m_size - count, count); }
40 
41  friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
42  friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
43  friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
44  friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
45  friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
46  friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
47 };
48 
57 template<typename A, int N>
58 constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
59 
60 template<typename V>
62 
64 template <typename T>
66 {
67  size_t size = span.size();
68  assert(size > 0);
69  T& back = span[size - 1];
70  span = Span<T>(span.data(), size - 1);
71  return back;
72 }
73 
74 #endif
constexpr std::ptrdiff_t size() const noexcept
Definition: span.h:33
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:43
constexpr C * end() const noexcept
Definition: span.h:30
constexpr Span(C *data, std::ptrdiff_t size) noexcept
Definition: span.h:25
friend constexpr bool operator<=(const Span &a, const Span &b) noexcept
Definition: span.h:44
friend constexpr bool operator>=(const Span &a, const Span &b) noexcept
Definition: span.h:46
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:58
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:42
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:41
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:45
constexpr Span< C > last(std::ptrdiff_t count) const noexcept
Definition: span.h:39
constexpr Span(C *data, C *end) noexcept
Definition: span.h:26
constexpr Span< C > subspan(std::ptrdiff_t offset) const noexcept
Definition: span.h:36
constexpr Span() noexcept
Definition: span.h:24
constexpr Span< C > first(std::ptrdiff_t count) const noexcept
Definition: span.h:38
std::ptrdiff_t m_size
Definition: span.h:21
constexpr C * begin() const noexcept
Definition: span.h:29
constexpr C * data() const noexcept
Definition: span.h:28
T & SpanPopBack(Span< T > &span)
Pop the last element off a span, and return a reference to that element.
Definition: span.h:65
static int count
Definition: tests.c:45
constexpr C & front() const noexcept
Definition: span.h:31
constexpr Span< C > subspan(std::ptrdiff_t offset, std::ptrdiff_t count) const noexcept
Definition: span.h:37
constexpr C & back() const noexcept
Definition: span.h:32
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:18
C * m_data
Definition: span.h:20
constexpr C & operator[](std::ptrdiff_t pos) const noexcept
Definition: span.h:34