From ba30caa9b24ee7dbebb524a218d97ec28fe8a0f5 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 30 Sep 2023 13:38:04 -0500 Subject: [PATCH] [ox/std] Add SpanView --- deps/ox/src/ox/std/span.hpp | 117 +++++++++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 10 deletions(-) diff --git a/deps/ox/src/ox/std/span.hpp b/deps/ox/src/ox/std/span.hpp index d317e969..397849c5 100644 --- a/deps/ox/src/ox/std/span.hpp +++ b/deps/ox/src/ox/std/span.hpp @@ -17,7 +17,7 @@ namespace ox { template -class Span { +class SpanView { private: const T *m_items{}; @@ -31,24 +31,125 @@ class Span { using iterator = SpanIterator; template - constexpr Span(ox::Array const&a) noexcept: + constexpr SpanView(ox::Array const&a) noexcept: m_items(a.data()), m_size(a.size()) { } - template - constexpr Span(ox::Vector const&v) noexcept: + template + constexpr SpanView(ox::Vector const&v) noexcept: m_items(v.data()), m_size(v.size()) { } template - constexpr Span(const T a[sz]) noexcept: + constexpr SpanView(const T a[sz]) noexcept: m_items(a), m_size(sz) { } - constexpr Span(const T *a, std::size_t sz) noexcept: + constexpr SpanView(const T *a, std::size_t sz) noexcept: + m_items(a), + m_size(sz) { + } + + constexpr iterator<> begin() noexcept { + return iterator<>(m_items, 0, m_size); + } + + constexpr iterator<> end() noexcept { + return iterator<>(m_items, m_size, m_size); + } + + [[nodiscard]] + constexpr iterator begin() const noexcept { + return iterator(m_items, 0, m_size); + } + + [[nodiscard]] + constexpr iterator end() const noexcept { + return iterator(m_items, m_size, m_size); + } + + [[nodiscard]] + constexpr iterator cbegin() const noexcept { + return iterator(m_items, 0, m_size); + } + + [[nodiscard]] + constexpr iterator cend() const noexcept { + return iterator(m_items, m_size, m_size); + } + + [[nodiscard]] + constexpr iterator crbegin() const noexcept { + return iterator(m_items, m_size - 1, m_size); + } + + [[nodiscard]] + constexpr iterator crend() const noexcept { + return iterator(m_items, MaxValue, m_size); + } + + [[nodiscard]] + constexpr iterator rbegin() const noexcept { + return iterator(m_items, m_size - 1, m_size); + } + + [[nodiscard]] + constexpr iterator rend() const noexcept { + return iterator(m_items, MaxValue, m_size); + } + + constexpr const T &operator[](std::size_t i) const noexcept { + return m_items[i]; + } + + [[nodiscard]] + constexpr std::size_t size() const noexcept { + return m_size; + } + + [[nodiscard]] + constexpr bool empty() const noexcept { + return m_size == 0; + } + +}; + +template +class Span { + + private: + T *m_items{}; + const std::size_t m_size{}; + + public: + using value_type = T; + using size_type = std::size_t; + + template + using iterator = SpanIterator; + + template + constexpr Span(ox::Array &a) noexcept: + m_items(a.data()), + m_size(a.size()) { + } + + template + constexpr Span(ox::Vector &v) noexcept: + m_items(v.data()), + m_size(v.size()) { + } + + template + constexpr Span(T a[sz]) noexcept: + m_items(a), + m_size(sz) { + } + + constexpr Span(T *a, std::size_t sz) noexcept: m_items(a), m_size(sz) { } @@ -111,10 +212,6 @@ class Span { return iterator(m_items, MaxValue, m_size); } - constexpr T &operator[](std::size_t i) noexcept { - return m_items[i]; - } - constexpr const T &operator[](std::size_t i) const noexcept { return m_items[i]; }