xref: /llvm-project/libcxx/include/__algorithm/clamp.h (revision 7ed7d4ccb8991e2b5b95334b508f8cec2faee737)
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 
14 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
15 #pragma GCC system_header
16 #endif
17 
18 _LIBCPP_PUSH_MACROS
19 #include <__undef_macros>
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 #if _LIBCPP_STD_VER > 14
24 // clamp
25 template<class _Tp, class _Compare>
26 _LIBCPP_NODISCARD_EXT inline
27 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
28 const _Tp&
29 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
30 {
31     _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
32     return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
33 
34 }
35 
36 template<class _Tp>
37 _LIBCPP_NODISCARD_EXT inline
38 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
39 const _Tp&
40 clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
41 {
42     return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
43 }
44 #endif
45 
46 _LIBCPP_END_NAMESPACE_STD
47 
48 _LIBCPP_POP_MACROS
49 
50 #endif // _LIBCPP___ALGORITHM_CLAMP_H
51