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