File indexing completed on 2026-05-03 08:13:19
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___ALGORITHM_RANGES_CLAMP_H
0010 #define _LIBCPP___CXX03___ALGORITHM_RANGES_CLAMP_H
0011
0012 #include <__cxx03/__assert>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__functional/identity.h>
0015 #include <__cxx03/__functional/invoke.h>
0016 #include <__cxx03/__functional/ranges_operations.h>
0017 #include <__cxx03/__iterator/concepts.h>
0018 #include <__cxx03/__iterator/projected.h>
0019 #include <__cxx03/__utility/forward.h>
0020
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 # pragma GCC system_header
0023 #endif
0024
0025 _LIBCPP_PUSH_MACROS
0026 #include <__cxx03/__undef_macros>
0027
0028 #if _LIBCPP_STD_VER >= 20
0029
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031
0032 namespace ranges {
0033 namespace __clamp {
0034 struct __fn {
0035 template <class _Type,
0036 class _Proj = identity,
0037 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
0038 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Type& operator()(
0039 const _Type& __value, const _Type& __low, const _Type& __high, _Comp __comp = {}, _Proj __proj = {}) const {
0040 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0041 !bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
0042 "Bad bounds passed to std::ranges::clamp");
0043
0044 auto&& __projected = std::invoke(__proj, __value);
0045 if (std::invoke(__comp, std::forward<decltype(__projected)>(__projected), std::invoke(__proj, __low)))
0046 return __low;
0047 else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward<decltype(__projected)>(__projected)))
0048 return __high;
0049 else
0050 return __value;
0051 }
0052 };
0053 }
0054
0055 inline namespace __cpo {
0056 inline constexpr auto clamp = __clamp::__fn{};
0057 }
0058 }
0059
0060 _LIBCPP_END_NAMESPACE_STD
0061
0062 #endif
0063
0064 _LIBCPP_POP_MACROS
0065
0066 #endif