xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/clamp.h (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_CLAMP_H
10 #define _LIBCPP___ALGORITHM_CLAMP_H
11 
12 #include <__config>
13 #include <__debug>
14 #include <__algorithm/comp.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
19 
20 _LIBCPP_PUSH_MACROS
21 #include <__undef_macros>
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 #if _LIBCPP_STD_VER > 14
26 // clamp
27 template<class _Tp, class _Compare>
28 _LIBCPP_NODISCARD_EXT inline
29 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
30 const _Tp&
31 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
32 {
33     _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
34     return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
35 
36 }
37 
38 template<class _Tp>
39 _LIBCPP_NODISCARD_EXT inline
40 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
41 const _Tp&
42 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
43 {
44     return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
45 }
46 #endif
47 
48 _LIBCPP_END_NAMESPACE_STD
49 
50 _LIBCPP_POP_MACROS
51 
52 #endif // _LIBCPP___ALGORITHM_CLAMP_H
53