diff options
Diffstat (limited to 'include/fud_algorithm_no_dep.hpp')
-rw-r--r-- | include/fud_algorithm_no_dep.hpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/include/fud_algorithm_no_dep.hpp b/include/fud_algorithm_no_dep.hpp new file mode 100644 index 0000000..a69761c --- /dev/null +++ b/include/fud_algorithm_no_dep.hpp @@ -0,0 +1,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 |