From b2dbcb55e2832c373fecb4033a3ed77e5dbc77aa Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Mon, 21 Oct 2024 12:49:43 -0500 Subject: Add vector and option. --- include/fud_algorithm.hpp | 36 ++- include/fud_array.hpp | 10 +- include/fud_memory.hpp | 16 ++ include/fud_option.hpp | 229 +++++++++++++++++ include/fud_span.hpp | 10 +- include/fud_status.hpp | 66 ++--- include/fud_vector.hpp | 628 +++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 939 insertions(+), 56 deletions(-) create mode 100644 include/fud_option.hpp (limited to 'include') diff --git a/include/fud_algorithm.hpp b/include/fud_algorithm.hpp index e3d5d3b..0ad71d5 100644 --- a/include/fud_algorithm.hpp +++ b/include/fud_algorithm.hpp @@ -18,11 +18,11 @@ #ifndef FUD_ALGORITHM_HPP #define FUD_ALGORITHM_HPP +#include "fud_option.hpp" #include "fud_span.hpp" #include #include -#include #include namespace fud { @@ -48,30 +48,41 @@ class Iota { { } - constexpr std::optional operator()() noexcept + constexpr Iota(const Iota& rhs) noexcept = default; + + constexpr Iota(Iota&& rhs) noexcept = default; + + ~Iota() noexcept = default; + + Iota& operator=(const Iota& rhs) = default; + + Iota& operator=(Iota&& rhs) = default; + + constexpr Option operator()() noexcept { auto value = m_value; if (m_increment > 0) { if (m_limit - m_increment < m_value) { - return std::nullopt; + return NullOpt; } } else { if (m_limit + m_increment + 1 >= m_value) { - return std::nullopt; + return NullOpt; } } m_value += m_increment; return value; } - void set(T value) { + void set(T value) + { m_value = value; } private: T m_value; - const T m_increment; - const T m_limit; + T m_increment; + T m_limit; }; template @@ -91,18 +102,19 @@ Span mapTo(Span input, Span output, Func&& mapFunc) output[idx] = std::forward(mapFunc)(input[idx]); } - return input; + return output; } -template +template auto map(Span input, Func&& mapFunc, Builder&& builder) -> decltype(std::forward(builder)()) { + using Output = decltype(std::forward(builder)()); Output output{std::forward(builder)()}; for (auto idx = 0; idx < input.size() && idx < output.size(); ++idx) { output[idx] = std::forward(mapFunc)(input[idx]); } - return input; + return output; } template @@ -132,7 +144,7 @@ bool allOf(Generator&& generator, Func&& predicate) { bool result = true; while (auto val = std::forward(generator)()) { - result = result && std::forward(predicate)(*val); + result = result && std::forward(predicate)(val.value()); } return result; } @@ -152,7 +164,7 @@ bool anyOf(Generator&& generator, Func&& predicate) { bool result = false; while (auto val = std::forward(generator)()) { - result = result || std::forward(predicate)(*val); + result = result || std::forward(predicate)(val.value()); } return result; } diff --git a/include/fud_array.hpp b/include/fud_array.hpp index 807621a..dcbd54a 100644 --- a/include/fud_array.hpp +++ b/include/fud_array.hpp @@ -18,9 +18,10 @@ #ifndef FUD_ARRAY_HPP #define FUD_ARRAY_HPP -#include - #include "fud_memory.hpp" +#include "fud_span.hpp" + +#include namespace fud { @@ -106,6 +107,11 @@ struct Array { constexpr bool operator==(const Array&) const noexcept = default; constexpr auto operator<=>(const Array& other) const noexcept = default; + + Span span() + { + return Span{data(), Size}; + } }; } // namespace fud diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index 97328a9..6ce6312 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -57,6 +57,22 @@ constexpr void setMemory(Container& container, const T& value) } } +template