cmp.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___UTILITY_CMP_H
  9. #define _LIBCPP___UTILITY_CMP_H
  10. #include <__config>
  11. #include <__type_traits/disjunction.h>
  12. #include <__type_traits/is_integral.h>
  13. #include <__type_traits/is_same.h>
  14. #include <__type_traits/is_signed.h>
  15. #include <__type_traits/make_unsigned.h>
  16. #include <__utility/forward.h>
  17. #include <__utility/move.h>
  18. #include <limits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #if _LIBCPP_STD_VER > 17
  26. template<class _Tp, class... _Up>
  27. struct _IsSameAsAny : _Or<_IsSame<_Tp, _Up>...> {};
  28. template<class _Tp>
  29. concept __is_safe_integral_cmp = is_integral_v<_Tp> &&
  30. !_IsSameAsAny<_Tp, bool, char, char16_t, char32_t
  31. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  32. , char8_t
  33. #endif
  34. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  35. , wchar_t
  36. #endif
  37. >::value;
  38. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  39. _LIBCPP_INLINE_VISIBILITY constexpr
  40. bool cmp_equal(_Tp __t, _Up __u) noexcept
  41. {
  42. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  43. return __t == __u;
  44. else if constexpr (is_signed_v<_Tp>)
  45. return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
  46. else
  47. return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
  48. }
  49. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  50. _LIBCPP_INLINE_VISIBILITY constexpr
  51. bool cmp_not_equal(_Tp __t, _Up __u) noexcept
  52. {
  53. return !_VSTD::cmp_equal(__t, __u);
  54. }
  55. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  56. _LIBCPP_INLINE_VISIBILITY constexpr
  57. bool cmp_less(_Tp __t, _Up __u) noexcept
  58. {
  59. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  60. return __t < __u;
  61. else if constexpr (is_signed_v<_Tp>)
  62. return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
  63. else
  64. return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
  65. }
  66. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  67. _LIBCPP_INLINE_VISIBILITY constexpr
  68. bool cmp_greater(_Tp __t, _Up __u) noexcept
  69. {
  70. return _VSTD::cmp_less(__u, __t);
  71. }
  72. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  73. _LIBCPP_INLINE_VISIBILITY constexpr
  74. bool cmp_less_equal(_Tp __t, _Up __u) noexcept
  75. {
  76. return !_VSTD::cmp_greater(__t, __u);
  77. }
  78. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  79. _LIBCPP_INLINE_VISIBILITY constexpr
  80. bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
  81. {
  82. return !_VSTD::cmp_less(__t, __u);
  83. }
  84. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  85. _LIBCPP_INLINE_VISIBILITY constexpr
  86. bool in_range(_Up __u) noexcept
  87. {
  88. return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
  89. _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
  90. }
  91. #endif // _LIBCPP_STD_VER > 17
  92. _LIBCPP_END_NAMESPACE_STD
  93. _LIBCPP_POP_MACROS
  94. #endif // _LIBCPP___UTILITY_CMP_H