From 088e03708afe4145a1903f0b20c53cab1899ad50 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 14 May 2025 00:02:39 +0200 Subject: Add array --- asl/types/array.hpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 asl/types/array.hpp (limited to 'asl/types/array.hpp') 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 +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>*; + return static_cast(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 + requires (kSpanSize == kSize || kSpanSize == dynamic_size) + constexpr operator span() const // NOLINT(*explicit*) + { + return as_span(); + } + + template + requires (kSpanSize == kSize || kSpanSize == dynamic_size) + constexpr operator span() // NOLINT(*explicit*) + { + return as_span(); + } + + constexpr auto as_span(this auto&& self) + { + using type = un_ref_t>; + return span{self.data(), self.size()}; + } + + constexpr auto&& operator[](this auto&& self, isize_t i) + { + ASL_ASSERT(i >= 0 && i <= self.size()); + return std::forward_like(std::forward(self).data()[i]); + } +}; + + +} // namespace asl + -- cgit