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 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'include/fud_algorithm.hpp') 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; } -- cgit v1.2.3