summaryrefslogtreecommitdiff
path: root/include/fud_algorithm_no_dep.hpp
blob: a69761c56e6b882777d8bf229c882547a85e6a69 (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
/*
 * libfud
 * Copyright 2025 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.
 */

/** \file This file breaks a dependency chain; min and max are intended to be part of fud_algorithm.
 *
 * Consumers are encouraged to include fud_algorithm.hpp instead.
 */

#ifndef FUD_ALGORITHM_NO_DEP_HPP
#define FUD_ALGORITHM_NO_DEP_HPP

#include <concepts>
#include <utility>

namespace fud {

// NOLINTBEGIN(bugprone-return-const-ref-from-parameter)

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

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

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

template <typename T, typename... Args>
constexpr const T& max(const T& first, const T& second, Args&&... args)
{
    return max(first, max(second, std::forward<Args>(args)...));
}

template <typename T, typename... Args>
constexpr const T& min(const T& first, const T& second, Args&&... args)
{
    return min(first, min(second, std::forward<Args>(args)...));
}

// NOLINTEND(bugprone-return-const-ref-from-parameter)

} // namespace fud

#endif