diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-21 12:49:43 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-21 12:49:43 -0500 |
commit | b2dbcb55e2832c373fecb4033a3ed77e5dbc77aa (patch) | |
tree | 1f294fcf1d85a02db86de3eea2b03393fd89ca5a /include/fud_algorithm.hpp | |
parent | 6a27a2a4032e88fa9154ef0f0741edc584f7a701 (diff) |
Add vector and option.
Diffstat (limited to 'include/fud_algorithm.hpp')
-rw-r--r-- | include/fud_algorithm.hpp | 36 |
1 files changed, 24 insertions, 12 deletions
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 <concepts> #include <limits> -#include <optional> #include <type_traits> namespace fud { @@ -48,30 +48,41 @@ class Iota { { } - constexpr std::optional<T> 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<T> 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 <typename T, size_t Size, typename Func> @@ -91,18 +102,19 @@ Span<T, Size> mapTo(Span<T, Size> input, Span<U, Size> output, Func&& mapFunc) output[idx] = std::forward<Func>(mapFunc)(input[idx]); } - return input; + return output; } -template <typename T, size_t Size, typename Func, typename Builder, typename Output> +template <typename T, size_t Size, typename Func, typename Builder> auto map(Span<T, Size> input, Func&& mapFunc, Builder&& builder) -> decltype(std::forward<Builder>(builder)()) { + using Output = decltype(std::forward<Builder>(builder)()); Output output{std::forward<Builder>(builder)()}; for (auto idx = 0; idx < input.size() && idx < output.size(); ++idx) { output[idx] = std::forward<Func>(mapFunc)(input[idx]); } - return input; + return output; } template <typename Generator, typename Builder> @@ -132,7 +144,7 @@ bool allOf(Generator&& generator, Func&& predicate) { bool result = true; while (auto val = std::forward<Generator>(generator)()) { - result = result && std::forward<Func>(predicate)(*val); + result = result && std::forward<Func>(predicate)(val.value()); } return result; } @@ -152,7 +164,7 @@ bool anyOf(Generator&& generator, Func&& predicate) { bool result = false; while (auto val = std::forward<Generator>(generator)()) { - result = result || std::forward<Func>(predicate)(*val); + result = result || std::forward<Func>(predicate)(val.value()); } return result; } |