1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___UTILITY_CMP_H
10 #define _LIBCPP___UTILITY_CMP_H
13 #include <__type_traits/disjunction.h>
14 #include <__type_traits/is_integral.h>
15 #include <__type_traits/is_same.h>
16 #include <__type_traits/is_signed.h>
17 #include <__type_traits/make_unsigned.h>
18 #include <__utility/forward.h>
19 #include <__utility/move.h>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
27 #include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 #if _LIBCPP_STD_VER >= 20
32 template<class _Tp
, class... _Up
>
33 struct _IsSameAsAny
: _Or
<_IsSame
<_Tp
, _Up
>...> {};
36 concept __is_safe_integral_cmp
= is_integral_v
<_Tp
> &&
37 !_IsSameAsAny
<_Tp
, bool, char, char16_t
, char32_t
38 #ifndef _LIBCPP_HAS_NO_CHAR8_T
41 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
46 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
47 _LIBCPP_INLINE_VISIBILITY
constexpr
48 bool cmp_equal(_Tp __t
, _Up __u
) noexcept
50 if constexpr (is_signed_v
<_Tp
> == is_signed_v
<_Up
>)
52 else if constexpr (is_signed_v
<_Tp
>)
53 return __t
< 0 ? false : make_unsigned_t
<_Tp
>(__t
) == __u
;
55 return __u
< 0 ? false : __t
== make_unsigned_t
<_Up
>(__u
);
58 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
59 _LIBCPP_INLINE_VISIBILITY
constexpr
60 bool cmp_not_equal(_Tp __t
, _Up __u
) noexcept
62 return !_VSTD::cmp_equal(__t
, __u
);
65 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
66 _LIBCPP_INLINE_VISIBILITY
constexpr
67 bool cmp_less(_Tp __t
, _Up __u
) noexcept
69 if constexpr (is_signed_v
<_Tp
> == is_signed_v
<_Up
>)
71 else if constexpr (is_signed_v
<_Tp
>)
72 return __t
< 0 ? true : make_unsigned_t
<_Tp
>(__t
) < __u
;
74 return __u
< 0 ? false : __t
< make_unsigned_t
<_Up
>(__u
);
77 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
78 _LIBCPP_INLINE_VISIBILITY
constexpr
79 bool cmp_greater(_Tp __t
, _Up __u
) noexcept
81 return _VSTD::cmp_less(__u
, __t
);
84 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
85 _LIBCPP_INLINE_VISIBILITY
constexpr
86 bool cmp_less_equal(_Tp __t
, _Up __u
) noexcept
88 return !_VSTD::cmp_greater(__t
, __u
);
91 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
92 _LIBCPP_INLINE_VISIBILITY
constexpr
93 bool cmp_greater_equal(_Tp __t
, _Up __u
) noexcept
95 return !_VSTD::cmp_less(__t
, __u
);
98 template<__is_safe_integral_cmp _Tp
, __is_safe_integral_cmp _Up
>
99 _LIBCPP_INLINE_VISIBILITY
constexpr
100 bool in_range(_Up __u
) noexcept
102 return _VSTD::cmp_less_equal(__u
, numeric_limits
<_Tp
>::max()) &&
103 _VSTD::cmp_greater_equal(__u
, numeric_limits
<_Tp
>::min());
105 #endif // _LIBCPP_STD_VER >= 20
107 _LIBCPP_END_NAMESPACE_STD
111 #endif // _LIBCPP___UTILITY_CMP_H