guid-inl.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef GUID_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include guid.h"
  3. // For the sake of sane code completion.
  4. #include "guid.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. Y_FORCE_INLINE constexpr TGuid::TGuid()
  9. : Parts32{}
  10. { }
  11. Y_FORCE_INLINE constexpr TGuid::TGuid(ui32 part0, ui32 part1, ui32 part2, ui32 part3)
  12. : Parts32{part0, part1, part2, part3}
  13. { }
  14. Y_FORCE_INLINE constexpr TGuid::TGuid(ui64 part0, ui64 part1)
  15. : Parts64{part0, part1}
  16. { }
  17. Y_FORCE_INLINE bool TGuid::IsEmpty() const
  18. {
  19. return Parts64[0] == 0 && Parts64[1] == 0;
  20. }
  21. Y_FORCE_INLINE TGuid::operator bool() const
  22. {
  23. return !IsEmpty();
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////
  26. Y_FORCE_INLINE bool operator == (TGuid lhs, TGuid rhs) noexcept
  27. {
  28. return
  29. lhs.Parts64[0] == rhs.Parts64[0] &&
  30. lhs.Parts64[1] == rhs.Parts64[1];
  31. }
  32. Y_FORCE_INLINE std::strong_ordering operator <=> (TGuid lhs, TGuid rhs) noexcept
  33. {
  34. #ifdef __GNUC__
  35. ui64 lhs0 = __builtin_bswap64(lhs.Parts64[0]);
  36. ui64 rhs0 = __builtin_bswap64(rhs.Parts64[0]);
  37. if (lhs0 < rhs0) {
  38. return std::strong_ordering::less;
  39. }
  40. if (lhs0 > rhs0) {
  41. return std::strong_ordering::greater;
  42. }
  43. ui64 lhs1 = __builtin_bswap64(lhs.Parts64[1]);
  44. ui64 rhs1 = __builtin_bswap64(rhs.Parts64[1]);
  45. if (lhs1 < rhs1) {
  46. return std::strong_ordering::less;
  47. }
  48. if (lhs1 > rhs1) {
  49. return std::strong_ordering::greater;
  50. }
  51. return std::strong_ordering::equal;
  52. #else
  53. int cmp = memcmp(&lhs, &rhs, sizeof(TGuid));
  54. if (cmp < 0) {
  55. return std::strong_ordering::less;
  56. }
  57. if (cmp > 0) {
  58. return std::strong_ordering::greater;
  59. }
  60. return std::strong_ordering::equal;
  61. #endif
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. } // namespace NYT
  65. Y_FORCE_INLINE size_t THash<NYT::TGuid>::operator()(const NYT::TGuid& guid) const
  66. {
  67. const size_t p = 1000000009; // prime number
  68. return guid.Parts32[0] +
  69. guid.Parts32[1] * p +
  70. guid.Parts32[2] * p * p +
  71. guid.Parts32[3] * p * p * p;
  72. }