From db10f5875e64efcf924f8afe2b955dfe1f64577e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 26 Jun 2020 13:29:27 +0200 Subject: [PATCH] Rename narray_view to narray_span and allow creation from a const container. --- README.md | 2 +- include/lol/private/base/narray.h | 54 ++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 76b8ca33..24874352 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ The header-only part of the Lol Engine framework. | header | description | examples | |--------|-------------|----------| -| `` | n-dimensional dynamic array containers | ● `lol::narray`
● `lol::array2d`, `lol::array3d`
● `lol::narray_view`
● `lol::array2d_view`, `lol::array3d_view` | +| `` | n-dimensional dynamic array containers | ● `lol::narray`
● `lol::array2d`, `lol::array3d`
● `lol::narray_span`
● `lol::span2d`, `lol::span3d` | ## Math diff --git a/include/lol/private/base/narray.h b/include/lol/private/base/narray.h index 0ac52600..a36f427d 100644 --- a/include/lol/private/base/narray.h +++ b/include/lol/private/base/narray.h @@ -21,15 +21,18 @@ #include // lol::vec_t #include // std::vector #include // std::index_sequence +#include // std::remove_const namespace lol { +template class narray_span; + // -// Common base class for narray and narray_view +// Common base class for narray and narray_span // -template +template typename container_type> class [[nodiscard]] narray_base { public: @@ -84,12 +87,23 @@ public: // Use CRTP to access data() from the child class inline value_type *data() { - return static_cast(this)->data(); + return reinterpret_cast &>(*this).data(); } inline value_type const *data() const { - return static_cast(this)->data(); + return reinterpret_cast const &>(*this).data(); + } + + // Create span (similar to std::span) with some const correctness + inline narray_span span() + { + return narray_span(*this); + } + + inline narray_span span() const + { + return narray_span(*this); } protected: @@ -121,24 +135,24 @@ protected: // C++11 iterators // -template +template typename U> T *begin(narray_base &a) { return a.data(); } -template +template typename U> T *end(narray_base &a) { return a.data() + a.size(); } -template +template typename U> T const *begin(narray_base const &a) { return a.data(); } -template +template typename U> T const *end(narray_base const &a) { return a.data() + a.size(); } // -// Array view +// N-dimensional array // template -class [[nodiscard]] narray : public narray_base> +class [[nodiscard]] narray : public narray_base { public: using value_type = T; @@ -190,17 +204,25 @@ template using array2d = narray; template using array3d = narray; // -// Array view +// N-dimensional array span // template -class [[nodiscard]] narray_view : public narray_base> +class [[nodiscard]] narray_span : public narray_base { public: using value_type = T; - template - inline narray_view(narray_base &other) + template typename U> + inline narray_span(narray_base &other) + : m_data(other.data()) + { + this->m_sizes = vec_t(other.sizes()); + } + + // Create an narray_span from a const narray_base + template typename U, bool V = std::is_const::value> + inline narray_span(narray_base::type, N, U> const &other) : m_data(other.data()) { this->m_sizes = vec_t(other.sizes()); @@ -214,8 +236,8 @@ private: T *m_data; }; -template using array2d_view = narray_view; -template using array3d_view = narray_view; +template using span2d = narray_span; +template using span3d = narray_span; } // namespace lol