Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __algorithm / clamp.h
blob1a5a3d0744be9c35d8aa2222a87d0580b4d6b42a
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_CLAMP_H
10 #define _LIBCPP___ALGORITHM_CLAMP_H
12 #include <__algorithm/comp.h>
13 #include <__assert>
14 #include <__config>
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 # pragma GCC system_header
18 #endif
20 _LIBCPP_BEGIN_NAMESPACE_STD
22 #if _LIBCPP_STD_VER >= 17
23 template <class _Tp, class _Compare>
24 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
25 clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
26 _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
27 _LIBCPP_LIFETIMEBOUND const _Tp& __hi,
28 _Compare __comp) {
29 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
30 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
33 template <class _Tp>
34 [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
35 clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
36 _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
37 _LIBCPP_LIFETIMEBOUND const _Tp& __hi) {
38 return std::clamp(__v, __lo, __hi, __less<>());
40 #endif
42 _LIBCPP_END_NAMESPACE_STD
44 #endif // _LIBCPP___ALGORITHM_CLAMP_H