diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-14 00:02:39 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-05-14 00:41:51 +0200 |
commit | 088e03708afe4145a1903f0b20c53cab1899ad50 (patch) | |
tree | ad31304d28c12ede106fe9fc821d37fe2110bef6 /asl/types/array.hpp | |
parent | 5bca42b04941ce132426100b5f99da096b5402b6 (diff) |
Add array
Diffstat (limited to 'asl/types/array.hpp')
-rw-r--r-- | asl/types/array.hpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/asl/types/array.hpp b/asl/types/array.hpp new file mode 100644 index 0000000..5fb300a --- /dev/null +++ b/asl/types/array.hpp @@ -0,0 +1,70 @@ +// Copyright 2025 Steven Le Rouzic +// +// SPDX-License-Identifier: BSD-3-Clause + +#pragma once + +#include "asl/base/assert.hpp" +#include "asl/base/meta.hpp" +#include "asl/base/utility.hpp" +#include "asl/types/span.hpp" + +namespace asl +{ + +template<is_object T, int64_t kSize> +requires (kSize > 0) +struct array +{ + T m_data[kSize]; + + [[nodiscard]] constexpr bool is_empty() const { return false; } + + [[nodiscard]] constexpr int64_t size() const { return kSize; } + + constexpr auto data(this auto&& self) + { + using return_type = un_ref_t<copy_cref_t<decltype(self), T>>*; + return static_cast<return_type>(self.m_data); + } + + constexpr auto begin(this auto&& self) + { + return contiguous_iterator{self.data()}; + } + + constexpr auto end(this auto&& self) + { + return contiguous_iterator{self.data() + kSize}; + } + + template<int64_t kSpanSize> + requires (kSpanSize == kSize || kSpanSize == dynamic_size) + constexpr operator span<const T, kSpanSize>() const // NOLINT(*explicit*) + { + return as_span(); + } + + template<int64_t kSpanSize> + requires (kSpanSize == kSize || kSpanSize == dynamic_size) + constexpr operator span<T, kSpanSize>() // NOLINT(*explicit*) + { + return as_span(); + } + + constexpr auto as_span(this auto&& self) + { + using type = un_ref_t<copy_cref_t<decltype(self), T>>; + return span<type, kSize>{self.data(), self.size()}; + } + + constexpr auto&& operator[](this auto&& self, isize_t i) + { + ASL_ASSERT(i >= 0 && i <= self.size()); + return std::forward_like<decltype(self)>(std::forward<decltype(self)>(self).data()[i]); + } +}; + + +} // namespace asl + |