xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_clamp.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
161cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
261cfbce3SDimitry Andric //
361cfbce3SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
461cfbce3SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
561cfbce3SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
661cfbce3SDimitry Andric //
761cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
861cfbce3SDimitry Andric 
961cfbce3SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_CLAMP_H
1061cfbce3SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_CLAMP_H
1161cfbce3SDimitry Andric 
1261cfbce3SDimitry Andric #include <__assert>
1361cfbce3SDimitry Andric #include <__config>
1461cfbce3SDimitry Andric #include <__functional/identity.h>
1561cfbce3SDimitry Andric #include <__functional/invoke.h>
1661cfbce3SDimitry Andric #include <__functional/ranges_operations.h>
1761cfbce3SDimitry Andric #include <__iterator/concepts.h>
1861cfbce3SDimitry Andric #include <__iterator/projected.h>
1961cfbce3SDimitry Andric #include <__utility/forward.h>
2061cfbce3SDimitry Andric 
2161cfbce3SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2261cfbce3SDimitry Andric #  pragma GCC system_header
2361cfbce3SDimitry Andric #endif
2461cfbce3SDimitry Andric 
25b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
26b3edf446SDimitry Andric #include <__undef_macros>
27b3edf446SDimitry Andric 
2806c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
2961cfbce3SDimitry Andric 
3061cfbce3SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3161cfbce3SDimitry Andric 
3261cfbce3SDimitry Andric namespace ranges {
3361cfbce3SDimitry Andric namespace __clamp {
3461cfbce3SDimitry Andric struct __fn {
3561cfbce3SDimitry Andric   template <class _Type,
3661cfbce3SDimitry Andric             class _Proj                                                      = identity,
3761cfbce3SDimitry Andric             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
38*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Type& operator()(
3906c3fb27SDimitry Andric       const _Type& __value, const _Type& __low, const _Type& __high, _Comp __comp = {}, _Proj __proj = {}) const {
407a6dacacSDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
417a6dacacSDimitry Andric         !bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
4261cfbce3SDimitry Andric         "Bad bounds passed to std::ranges::clamp");
4361cfbce3SDimitry Andric 
445f757f3fSDimitry Andric     auto&& __projected = std::invoke(__proj, __value);
455f757f3fSDimitry Andric     if (std::invoke(__comp, std::forward<decltype(__projected)>(__projected), std::invoke(__proj, __low)))
4661cfbce3SDimitry Andric       return __low;
475f757f3fSDimitry Andric     else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward<decltype(__projected)>(__projected)))
4861cfbce3SDimitry Andric       return __high;
4961cfbce3SDimitry Andric     else
5061cfbce3SDimitry Andric       return __value;
5161cfbce3SDimitry Andric   }
5261cfbce3SDimitry Andric };
5361cfbce3SDimitry Andric } // namespace __clamp
5461cfbce3SDimitry Andric 
5561cfbce3SDimitry Andric inline namespace __cpo {
5661cfbce3SDimitry Andric inline constexpr auto clamp = __clamp::__fn{};
5761cfbce3SDimitry Andric } // namespace __cpo
5861cfbce3SDimitry Andric } // namespace ranges
5961cfbce3SDimitry Andric 
6061cfbce3SDimitry Andric _LIBCPP_END_NAMESPACE_STD
6161cfbce3SDimitry Andric 
6206c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
6361cfbce3SDimitry Andric 
64b3edf446SDimitry Andric _LIBCPP_POP_MACROS
65b3edf446SDimitry Andric 
6661cfbce3SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_CLAMP_H
67