Bitcoin Core  0.18.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 
16 template<typename C>
17 class Span
18 {
19  C* m_data;
20  std::ptrdiff_t m_size;
21 
22 public:
23  constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
24  constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {}
25  constexpr Span(C* data, C* end) noexcept : m_data(data), m_size(end - data) {}
26 
27  constexpr C* data() const noexcept { return m_data; }
28  constexpr C* begin() const noexcept { return m_data; }
29  constexpr C* end() const noexcept { return m_data + m_size; }
30  constexpr std::ptrdiff_t size() const noexcept { return m_size; }
31  constexpr C& operator[](std::ptrdiff_t pos) const noexcept { return m_data[pos]; }
32 
33  constexpr Span<C> subspan(std::ptrdiff_t offset) const noexcept { return Span<C>(m_data + offset, m_size - offset); }
34  constexpr Span<C> subspan(std::ptrdiff_t offset, std::ptrdiff_t count) const noexcept { return Span<C>(m_data + offset, count); }
35  constexpr Span<C> first(std::ptrdiff_t count) const noexcept { return Span<C>(m_data, count); }
36  constexpr Span<C> last(std::ptrdiff_t count) const noexcept { return Span<C>(m_data + m_size - count, count); }
37 
38  friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
39  friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
40  friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
41  friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
42  friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
43  friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
44 };
45 
54 template<typename A, int N>
55 constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
56 
57 template<typename V>
59 
60 #endif
constexpr std::ptrdiff_t size() const noexcept
Definition: span.h:30
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
Definition: span.h:40
constexpr C * end() const noexcept
Definition: span.h:29
constexpr Span(C *data, std::ptrdiff_t size) noexcept
Definition: span.h:24
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:43
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
Definition: span.h:39
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
Definition: span.h:38
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
Definition: span.h:42
constexpr Span< C > last(std::ptrdiff_t count) const noexcept
Definition: span.h:36
constexpr Span(C *data, C *end) noexcept
Definition: span.h:25
constexpr Span< C > subspan(std::ptrdiff_t offset) const noexcept
Definition: span.h:33
constexpr Span() noexcept
Definition: span.h:23
constexpr Span< C > first(std::ptrdiff_t count) const noexcept
Definition: span.h:35
std::ptrdiff_t m_size
Definition: span.h:20
constexpr C * begin() const noexcept
Definition: span.h:28
constexpr C * data() const noexcept
Definition: span.h:27
static int count
Definition: tests.c:45
constexpr Span< C > subspan(std::ptrdiff_t offset, std::ptrdiff_t count) const noexcept
Definition: span.h:34
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:17
C * m_data
Definition: span.h:19
constexpr C & operator[](std::ptrdiff_t pos) const noexcept
Definition: span.h:31