aboutsummaryrefslogtreecommitdiff
path: root/src/span.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2018-04-23 12:17:29 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2018-07-27 11:52:18 -0700
commit29943a904a11607787d28b1f4288f500bd076dde (patch)
tree39f940fedeec32af5661b7ed4b453d9bb4b96aa0 /src/span.h
parent365384fd9f3145e604167fcb0b7358f2c67b26f6 (diff)
Add more methods to Span class
This introduces a rudimentary begin(), end(), operator[], and subspan to Span.
Diffstat (limited to 'src/span.h')
-rw-r--r--src/span.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/span.h b/src/span.h
index 707fc21918..77de059fa6 100644
--- a/src/span.h
+++ b/src/span.h
@@ -7,6 +7,7 @@
#include <type_traits>
#include <cstddef>
+#include <algorithm>
/** A Span is an object that can refer to a contiguous sequence of objects.
*
@@ -21,9 +22,25 @@ class Span
public:
constexpr Span() noexcept : m_data(nullptr), m_size(0) {}
constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {}
+ constexpr Span(C* data, C* end) noexcept : m_data(data), m_size(end - data) {}
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 std::ptrdiff_t size() const noexcept { return m_size; }
+ constexpr C& operator[](std::ptrdiff_t pos) const noexcept { return m_data[pos]; }
+
+ constexpr Span<C> subspan(std::ptrdiff_t offset) const noexcept { return Span<C>(m_data + offset, m_size - offset); }
+ constexpr Span<C> subspan(std::ptrdiff_t offset, std::ptrdiff_t count) const noexcept { return Span<C>(m_data + offset, count); }
+ constexpr Span<C> first(std::ptrdiff_t count) const noexcept { return Span<C>(m_data, count); }
+ constexpr Span<C> last(std::ptrdiff_t count) const noexcept { return Span<C>(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); }
};
/** Create a span to a container exposing data() and size().
@@ -34,6 +51,9 @@ public:
*
* std::span will have a constructor that implements this functionality directly.
*/
+template<typename A, int N>
+constexpr Span<A> MakeSpan(A (&a)[N]) { return Span<A>(a, N); }
+
template<typename V>
constexpr Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type> MakeSpan(V& v) { return Span<typename std::remove_pointer<decltype(std::declval<V>().data())>::type>(v.data(), v.size()); }