Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __algorithm / ranges_clamp.h
blobe6c86207254a19f85af7100c40f9e711be6f886d
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_RANGES_CLAMP_H
10 #define _LIBCPP___ALGORITHM_RANGES_CLAMP_H
12 #include <__assert>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__functional/ranges_operations.h>
17 #include <__iterator/concepts.h>
18 #include <__iterator/projected.h>
19 #include <__utility/forward.h>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
23 #endif
25 #if _LIBCPP_STD_VER >= 20
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 namespace ranges {
30 namespace __clamp {
31 struct __fn {
32 template <class _Type,
33 class _Proj = identity,
34 indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
35 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr const _Type& operator()(
36 const _Type& __value, const _Type& __low, const _Type& __high, _Comp __comp = {}, _Proj __proj = {}) const {
37 _LIBCPP_ASSERT_UNCATEGORIZED(!bool(std::invoke(__comp, std::invoke(__proj, __high), std::invoke(__proj, __low))),
38 "Bad bounds passed to std::ranges::clamp");
40 auto&& __projected = std::invoke(__proj, __value);
41 if (std::invoke(__comp, std::forward<decltype(__projected)>(__projected), std::invoke(__proj, __low)))
42 return __low;
43 else if (std::invoke(__comp, std::invoke(__proj, __high), std::forward<decltype(__projected)>(__projected)))
44 return __high;
45 else
46 return __value;
49 } // namespace __clamp
51 inline namespace __cpo {
52 inline constexpr auto clamp = __clamp::__fn{};
53 } // namespace __cpo
54 } // namespace ranges
56 _LIBCPP_END_NAMESPACE_STD
58 #endif // _LIBCPP_STD_VER >= 20
60 #endif // _LIBCPP___ALGORITHM_RANGES_CLAMP_H