summaryrefslogtreecommitdiff
path: root/include/fud_algorithm.hpp
blob: 01cc5d3d4265f7e5804f0972a5a5a36fafad5651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * libfud
 * Copyright 2024 Dominick Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FUD_ALGORITHM_HPP
#define FUD_ALGORITHM_HPP

#include "fud_option.hpp"
#include "fud_span.hpp"

#include <concepts>
#include <limits>
#include <type_traits>

namespace fud {

template<typename T>
concept LessThanComparable =
    requires(T lhs, T rhs) {
        { lhs < rhs } -> std::convertible_to<bool>;
};

template <LessThanComparable T>
inline const T& min(const T& lhs, const T& rhs) {
    if (lhs < rhs) {
        return lhs;
    }
    return rhs;
}

template <std::integral T>
class Iota {
  public:
    constexpr Iota() noexcept : m_value{}, m_increment{static_cast<T>(1)}, m_limit{std::numeric_limits<T>::max()}
    {
    }

    constexpr Iota(T value) noexcept :
        m_value{value}, m_increment{static_cast<T>(1)}, m_limit{std::numeric_limits<T>::max()}
    {
    }

    constexpr Iota(T value, T increment) noexcept :
        m_value{value}, m_increment{increment}, m_limit{std::numeric_limits<T>::max()}
    {
    }

    constexpr Iota(T value, T increment, T limit) noexcept : m_value{value}, m_increment{increment}, m_limit{limit}
    {
    }

    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 NullOpt;
            }
        } else {
            if (m_limit + m_increment + 1 >= m_value) {
                return NullOpt;
            }
        }
        m_value += m_increment;
        return value;
    }

    void set(T value)
    {
        m_value = value;
    }

  private:
    T m_value;
    T m_increment;
    T m_limit;
};

template <typename T, size_t Size, typename Func>
Span<T, Size> forEach(Span<T, Size> input, Func&& mapFunc)
{
    for (auto& element : input) {
        element = std::forward<Func>(mapFunc)(element);
    }

    return input;
}

template <typename T, typename U, size_t Size, typename Func>
Span<T, Size> mapTo(Span<T, Size> input, Span<U, Size> output, Func&& mapFunc)
{
    for (auto idx = 0; idx < input.size(); ++idx) {
        output[idx] = std::forward<Func>(mapFunc)(input[idx]);
    }

    return 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 output;
}

template <typename Generator, typename Builder>
auto generate(Builder&& builder, Generator&& generator) -> decltype(std::forward<Builder>(builder)())
{
    using Output = decltype(std::forward<Builder>(builder)());
    Output output{std::forward<Builder>(builder)()};
    for (auto idx = 0; idx < output.size(); ++idx) {
        output[idx] = std::forward<Generator>(generator)();
    }

    return output;
}

template <typename T, size_t Size, typename Func>
bool allOf(Span<T, Size> input, Func&& predicate)
{
    bool result = input.size() > 0;
    for (size_t idx = 0; result && idx < input.size(); ++idx) {
        result = result && std::forward<Func>(predicate)(input[idx]);
    }
    return result;
}

template <typename Generator, typename Func>
bool allOf(Generator&& generator, Func&& predicate)
{
    bool result = true;
    while (auto val = std::forward<Generator>(generator)()) {
        result = result && std::forward<Func>(predicate)(val.value());
    }
    return result;
}

template <typename T, size_t Size, typename Func>
bool anyOf(Span<T, Size> input, Func&& predicate)
{
    bool result = !(input.size() > 0);
    for (size_t idx = 0; result && idx < input.size(); ++idx) {
        result = result || std::forward<Func>(predicate)(input[idx]);
    }
    return result;
}

template <typename Generator, typename Func>
bool anyOf(Generator&& generator, Func&& predicate)
{
    bool result = false;
    while (auto val = std::forward<Generator>(generator)()) {
        result = result || std::forward<Func>(predicate)(val.value());
    }
    return result;
}

} // namespace fud

#endif