diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-04 18:11:37 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | cb5967d8f46fbba7c7e30f436032fef0ed671fe9 (patch) | |
tree | 2039919b237f5ccc7b24f9eb81cd0f15b90b47ea /asl/span.hpp | |
parent | 5682cb422c39eea6b4d5d54c2f31260785dc256f (diff) |
More work on span
Diffstat (limited to 'asl/span.hpp')
-rw-r--r-- | asl/span.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/asl/span.hpp b/asl/span.hpp index f3d4036..efc9eec 100644 --- a/asl/span.hpp +++ b/asl/span.hpp @@ -3,6 +3,7 @@ #include "asl/meta.hpp" #include "asl/annotations.hpp" #include "asl/layout.hpp" +#include "asl/assert.hpp" namespace asl { @@ -22,6 +23,46 @@ class span public: constexpr span() = default; + constexpr span(T* data, int64_t size) + requires kIsDynamic + : m_data{data} + , m_size{size} + {} + + constexpr explicit span(T* data, int64_t size) + requires (!kIsDynamic) + : m_data{data} + { + ASL_ASSERT(size == kSize); + } + + template<int64_t N> + constexpr span(T (&array)[N]) // NOLINT(*-explicit-conversions) + requires (kIsDynamic) + : m_data{array} + , m_size{N} + {} + + template<int64_t N> + constexpr span(T (&array)[N]) // NOLINT(*-explicit-conversions) + requires (!kIsDynamic) && (N == kSize) + : m_data{array} + {} + + template<is_object U, int64_t kOtherSize> + constexpr explicit(!kIsDynamic) + span(const span<U, kOtherSize>& other) // NOLINT(*-explicit-conversions) + requires ( + ( + kIsDynamic || + span<U, kOtherSize>::kIsDynamic || + kOtherSize == kSize + ) && convertible_from<T(&)[], U(&)[]> + ) + : span{static_cast<U*>(other.data()), other.size()} + { + } + constexpr span(const span&) = default; constexpr span(span&&) = default; @@ -39,6 +80,23 @@ public: constexpr int64_t size_bytes() const { return size() * size_of<T>; } constexpr bool is_empty() const { return size() == 0; } + + constexpr T* data() const { return m_data; } + + constexpr T* begin() const { return m_data; } + constexpr T* end() const { return m_data + size(); } + + constexpr T& operator[](int64_t i) const + { + ASL_ASSERT(i >= 0 && i < size()); + return m_data[i]; // NOLINT(*-pointer-arithmetic) + } + + // @Todo subspan, first, last + // @Todo as_(mutable_)bytes + + template<is_object U, int64_t kOtherSize> + friend class span; }; } // namespace asl |