cmp.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 !defined(_LIBCPP_HAS_NO_CONCEPTS)
  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
  27. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  28. , char8_t
  29. #endif
  30. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  31. , char16_t, char32_t
  32. #endif
  33. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  34. , wchar_t
  35. #endif
  36. >::value;
  37. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  38. _LIBCPP_INLINE_VISIBILITY constexpr
  39. bool cmp_equal(_Tp __t, _Up __u) noexcept
  40. {
  41. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  42. return __t == __u;
  43. else if constexpr (is_signed_v<_Tp>)
  44. return __t < 0 ? false : make_unsigned_t<_Tp>(__t) == __u;
  45. else
  46. return __u < 0 ? false : __t == make_unsigned_t<_Up>(__u);
  47. }
  48. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  49. _LIBCPP_INLINE_VISIBILITY constexpr
  50. bool cmp_not_equal(_Tp __t, _Up __u) noexcept
  51. {
  52. return !_VSTD::cmp_equal(__t, __u);
  53. }
  54. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  55. _LIBCPP_INLINE_VISIBILITY constexpr
  56. bool cmp_less(_Tp __t, _Up __u) noexcept
  57. {
  58. if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
  59. return __t < __u;
  60. else if constexpr (is_signed_v<_Tp>)
  61. return __t < 0 ? true : make_unsigned_t<_Tp>(__t) < __u;
  62. else
  63. return __u < 0 ? false : __t < make_unsigned_t<_Up>(__u);
  64. }
  65. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  66. _LIBCPP_INLINE_VISIBILITY constexpr
  67. bool cmp_greater(_Tp __t, _Up __u) noexcept
  68. {
  69. return _VSTD::cmp_less(__u, __t);
  70. }
  71. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  72. _LIBCPP_INLINE_VISIBILITY constexpr
  73. bool cmp_less_equal(_Tp __t, _Up __u) noexcept
  74. {
  75. return !_VSTD::cmp_greater(__t, __u);
  76. }
  77. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  78. _LIBCPP_INLINE_VISIBILITY constexpr
  79. bool cmp_greater_equal(_Tp __t, _Up __u) noexcept
  80. {
  81. return !_VSTD::cmp_less(__t, __u);
  82. }
  83. template<__is_safe_integral_cmp _Tp, __is_safe_integral_cmp _Up>
  84. _LIBCPP_INLINE_VISIBILITY constexpr
  85. bool in_range(_Up __u) noexcept
  86. {
  87. return _VSTD::cmp_less_equal(__u, numeric_limits<_Tp>::max()) &&
  88. _VSTD::cmp_greater_equal(__u, numeric_limits<_Tp>::min());
  89. }
  90. #endif
  91. _LIBCPP_END_NAMESPACE_STD
  92. _LIBCPP_POP_MACROS
  93. #endif // _LIBCPP___UTILITY_CMP_H