Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:09

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_RANGES_CLAMP_H
0010 #define _LIBCPP___ALGORITHM_RANGES_CLAMP_H
0011 
0012 #include <__assert>
0013 #include <__config>
0014 #include <__functional/identity.h>
0015 #include <__functional/invoke.h>
0016 #include <__functional/ranges_operations.h>
0017 #include <__iterator/concepts.h>
0018 #include <__iterator/projected.h>
0019 #include <__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 <__undef_macros>
0027 
0028 #if _LIBCPP_STD_VER >= 20
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 namespace ranges {
0033 struct __clamp {
0034   template <class _Type,
0035             class _Proj                                                      = identity,
0036             indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
0037   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Type& operator()(
0038       const _Type& __value, const _Type& __low, const _Type& __high, _Comp __comp = {}, _Proj __proj = {}) const {
0039     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0040         !bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
0041         "Bad bounds passed to std::ranges::clamp");
0042 
0043     auto&& __projected = std::invoke(__proj, __value);
0044     if (std::invoke(__comp, std::forward<decltype(__projected)>(__projected), std::invoke(__proj, __low)))
0045       return __low;
0046     else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward<decltype(__projected)>(__projected)))
0047       return __high;
0048     else
0049       return __value;
0050   }
0051 };
0052 
0053 inline namespace __cpo {
0054 inline constexpr auto clamp = __clamp{};
0055 } // namespace __cpo
0056 } // namespace ranges
0057 
0058 _LIBCPP_END_NAMESPACE_STD
0059 
0060 #endif // _LIBCPP_STD_VER >= 20
0061 
0062 _LIBCPP_POP_MACROS
0063 
0064 #endif // _LIBCPP___ALGORITHM_RANGES_CLAMP_H