// Copyright (c) 2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_SPAN_H #define BITCOIN_SPAN_H #include #include #include #include #ifdef DEBUG_CORE #define CONSTEXPR_IF_NOT_DEBUG #define ASSERT_IF_DEBUG(x) assert((x)) #else #define CONSTEXPR_IF_NOT_DEBUG constexpr #define ASSERT_IF_DEBUG(x) #endif #if defined(__clang__) #if __has_attribute(lifetimebound) #define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]] #else #define SPAN_ATTR_LIFETIMEBOUND #endif #else #define SPAN_ATTR_LIFETIMEBOUND #endif /** A Span is an object that can refer to a contiguous sequence of objects. * * It implements a subset of C++20's std::span. */ template class Span { C* m_data; std::size_t m_size; template struct is_Span_int : public std::false_type {}; template struct is_Span_int> : public std::true_type {}; template struct is_Span : public is_Span_int::type>{}; public: constexpr Span() noexcept : m_data(nullptr), m_size(0) {} /** Construct a span from a begin pointer and a size. * * This implements a subset of the iterator-based std::span constructor in C++20, * which is hard to implement without std::address_of. */ template ::value, int>::type = 0> constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {} /** Construct a span from a begin and end pointer. * * This implements a subset of the iterator-based std::span constructor in C++20, * which is hard to implement without std::address_of. */ template ::value, int>::type = 0> CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin) { ASSERT_IF_DEBUG(end >= begin); } /** Implicit conversion of spans between compatible types. * * Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type * C, then permit implicit conversion of Span to Span. This matches the behavior of the corresponding * C++20 std::span constructor. * * For example this means that a Span can be converted into a Span. */ template ::value, int>::type = 0> constexpr Span(const Span& other) noexcept : m_data(other.m_data), m_size(other.m_size) {} /** Default copy constructor. */ constexpr Span(const Span&) noexcept = default; /** Default assignment operator. */ Span& operator=(const Span& other) noexcept = default; /** Construct a Span from an array. This matches the corresponding C++20 std::span constructor. */ template constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {} /** Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...). * * This implements a subset of the functionality provided by the C++20 std::span range-based constructor. * * To prevent surprises, only Spans for constant value types are supported when passing in temporaries. * Note that this restriction does not exist when converting arrays or other Spans (see above). */ template constexpr Span(V& other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if::value && std::is_convertible().data())>::type (*)[], C (*)[]>::value && std::is_convertible().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) : m_data(other.data()), m_size(other.size()){} template constexpr Span(const V& other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if::value && std::is_convertible().data())>::type (*)[], C (*)[]>::value && std::is_convertible().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) : m_data(other.data()), m_size(other.size()){} constexpr C* data() const noexcept { return m_data; } constexpr C* begin() const noexcept { return m_data; } constexpr C* end() const noexcept { return m_data + m_size; } CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept { ASSERT_IF_DEBUG(size() > 0); return m_data[0]; } CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept { ASSERT_IF_DEBUG(size() > 0); return m_data[m_size - 1]; } constexpr std::size_t size() const noexcept { return m_size; } CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept { ASSERT_IF_DEBUG(size() > pos); return m_data[pos]; } CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset) const noexcept { ASSERT_IF_DEBUG(size() >= offset); return Span(m_data + offset, m_size - offset); } CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset, std::size_t count) const noexcept { ASSERT_IF_DEBUG(size() >= offset + count); return Span(m_data + offset, count); } CONSTEXPR_IF_NOT_DEBUG Span first(std::size_t count) const noexcept { ASSERT_IF_DEBUG(size() >= count); return Span(m_data, count); } CONSTEXPR_IF_NOT_DEBUG Span last(std::size_t count) const noexcept { ASSERT_IF_DEBUG(size() >= count); return Span(m_data + m_size - count, count); } friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); } friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); } friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); } friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); } friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); } friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); } template friend class Span; }; // MakeSpan helps constructing a Span of the right type automatically. /** MakeSpan for arrays: */ template Span constexpr MakeSpan(A (&a)[N]) { return Span(a, N); } /** MakeSpan for temporaries / rvalue references, only supporting const output. */ template constexpr auto MakeSpan(V&& v SPAN_ATTR_LIFETIMEBOUND) -> typename std::enable_if::value, Span::type>>::type { return std::forward(v); } /** MakeSpan for (lvalue) references, supporting mutable output. */ template constexpr auto MakeSpan(V& v SPAN_ATTR_LIFETIMEBOUND) -> Span::type> { return v; } /** Pop the last element off a span, and return a reference to that element. */ template T& SpanPopBack(Span& span) { size_t size = span.size(); ASSERT_IF_DEBUG(size > 0); T& back = span[size - 1]; span = Span(span.data(), size - 1); return back; } // Helper functions to safely cast to unsigned char pointers. inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; } inline unsigned char* UCharCast(unsigned char* c) { return c; } inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; } inline const unsigned char* UCharCast(const unsigned char* c) { return c; } // Helper function to safely convert a Span to a Span<[const] unsigned char>. template constexpr auto UCharSpanCast(Span s) -> Span::type> { return {UCharCast(s.data()), s.size()}; } /** Like MakeSpan, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ template constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward(v)))) { return UCharSpanCast(MakeSpan(std::forward(v))); } #endif