cmp.h 3.1 KB

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