tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
clamp.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_CLAMP_HPP
5#define TETL_ALGORITHM_CLAMP_HPP
6
7#include <etl/_functional/less.hpp>
8
9namespace etl {
10
11/// \ingroup algorithm
12/// @{
13
14/// \brief If v compares less than lo, returns lo; otherwise if hi compares less
15/// than v, returns hi; otherwise returns v. Uses operator< to compare the
16/// values.
17template <typename Type, typename Compare>
18[[nodiscard]] constexpr auto clamp(Type const& v, Type const& lo, Type const& hi, Compare comp) -> Type const&
19{
20 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
21}
22
23template <typename Type>
24[[nodiscard]] constexpr auto clamp(Type const& v, Type const& lo, Type const& hi) noexcept -> Type const&
25{
26 return etl::clamp(v, lo, hi, etl::less<Type>());
27}
28
29/// @}
30
31} // namespace etl
32
33#endif // TETL_ALGORITHM_CLAMP_HPP
constexpr auto clamp(Type const &v, Type const &lo, Type const &hi, Compare comp) -> Type const &
If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise r...
Definition clamp.hpp:18
constexpr auto clamp(Type const &v, Type const &lo, Type const &hi) noexcept -> Type const &
If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise r...
Definition clamp.hpp:24
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15