diff --git a/src/span.h b/src/span.h index 77de059fa6340..9379b15c816f1 100644 --- a/src/span.h +++ b/src/span.h @@ -8,6 +8,7 @@ #include #include #include +#include /** A Span is an object that can refer to a contiguous sequence of objects. * @@ -27,6 +28,8 @@ class Span 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 C& front() const noexcept { return m_data[0]; } + constexpr C& back() const noexcept { return m_data[m_size - 1]; } constexpr std::ptrdiff_t size() const noexcept { return m_size; } constexpr C& operator[](std::ptrdiff_t pos) const noexcept { return m_data[pos]; } @@ -57,4 +60,15 @@ constexpr Span MakeSpan(A (&a)[N]) { return Span(a, N); } template constexpr Span().data())>::type> MakeSpan(V& v) { return Span().data())>::type>(v.data(), v.size()); } +/** 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(size > 0); + T& back = span[size - 1]; + span = Span(span.data(), size - 1); + return back; +} + #endif