aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2023-11-25 17:05:18 +0300
committerbabenko <babenko@yandex-team.com>2023-11-25 17:31:15 +0300
commitef0f7daa4fdd7a8866514cdd37dafdb5ceaa9724 (patch)
tree1fbfae235497e65647a2078d38b50eaec157367e
parent6f361c0dc7ed733827aa265049b1204b54292e2e (diff)
downloadydb-ef0f7daa4fdd7a8866514cdd37dafdb5ceaa9724.tar.gz
More TGuid helpers
-rw-r--r--library/cpp/yt/misc/guid-inl.h45
-rw-r--r--library/cpp/yt/misc/guid.cpp11
-rw-r--r--library/cpp/yt/misc/guid.h21
-rw-r--r--library/cpp/yt/misc/unittests/guid_ut.cpp31
4 files changed, 82 insertions, 26 deletions
diff --git a/library/cpp/yt/misc/guid-inl.h b/library/cpp/yt/misc/guid-inl.h
index 1c71d794bd..0e79a3aaa8 100644
--- a/library/cpp/yt/misc/guid-inl.h
+++ b/library/cpp/yt/misc/guid-inl.h
@@ -32,42 +32,43 @@ Y_FORCE_INLINE TGuid::operator bool() const
////////////////////////////////////////////////////////////////////////////////
-Y_FORCE_INLINE bool LessImpl(TGuid lhs, TGuid rhs) noexcept
+Y_FORCE_INLINE bool operator == (TGuid lhs, TGuid rhs) noexcept
+{
+ return
+ lhs.Parts64[0] == rhs.Parts64[0] &&
+ lhs.Parts64[1] == rhs.Parts64[1];
+}
+
+Y_FORCE_INLINE std::strong_ordering operator <=> (TGuid lhs, TGuid rhs) noexcept
{
#ifdef __GNUC__
ui64 lhs0 = __builtin_bswap64(lhs.Parts64[0]);
ui64 rhs0 = __builtin_bswap64(rhs.Parts64[0]);
if (lhs0 < rhs0) {
- return true;
+ return std::strong_ordering::less;
}
if (lhs0 > rhs0) {
- return false;
+ return std::strong_ordering::greater;
}
ui64 lhs1 = __builtin_bswap64(lhs.Parts64[1]);
ui64 rhs1 = __builtin_bswap64(rhs.Parts64[1]);
- return lhs1 < rhs1;
+ if (lhs1 < rhs1) {
+ return std::strong_ordering::less;
+ }
+ if (lhs1 > rhs1) {
+ return std::strong_ordering::greater;
+ }
+ return std::strong_ordering::equal;
#else
- return memcmp(&lhs, &rhs, sizeof(TGuid)) < 0;
-#endif
-}
-
-Y_FORCE_INLINE bool operator == (const TGuid& lhs, const TGuid& rhs) noexcept
-{
- return lhs.Parts64[0] == rhs.Parts64[0] &&
- lhs.Parts64[1] == rhs.Parts64[1];
-}
-
-Y_FORCE_INLINE std::strong_ordering operator <=> (const TGuid& lhs, const TGuid& rhs) noexcept
-{
- if (LessImpl(lhs, rhs)) {
+ int cmp = memcmp(&lhs, &rhs, sizeof(TGuid));
+ if (cmp < 0) {
return std::strong_ordering::less;
}
-
- if (lhs == rhs) {
- return std::strong_ordering::equal;
+ if (cmp > 0) {
+ return std::strong_ordering::greater;
}
-
- return std::strong_ordering::greater;
+ return std::strong_ordering::equal;
+#endif
}
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/misc/guid.cpp b/library/cpp/yt/misc/guid.cpp
index 882787d7a2..02c396b015 100644
--- a/library/cpp/yt/misc/guid.cpp
+++ b/library/cpp/yt/misc/guid.cpp
@@ -200,4 +200,15 @@ char* WriteGuidToBuffer(char* ptr, TGuid value)
////////////////////////////////////////////////////////////////////////////////
+TFormattableGuid::TFormattableGuid(TGuid guid)
+ : End_(WriteGuidToBuffer(Buffer_.data(), guid))
+{ }
+
+TStringBuf TFormattableGuid::ToStringBuf() const
+{
+ return {Buffer_.data(), End_};
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
} // namespace NYT
diff --git a/library/cpp/yt/misc/guid.h b/library/cpp/yt/misc/guid.h
index 328503f902..05844ba49c 100644
--- a/library/cpp/yt/misc/guid.h
+++ b/library/cpp/yt/misc/guid.h
@@ -5,6 +5,8 @@
#include <library/cpp/yt/exception/exception.h>
+#include <array>
+
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
@@ -79,9 +81,8 @@ struct TGuid
static bool FromStringHex32(TStringBuf str, TGuid* guid);
};
-bool operator == (const TGuid& lhs, const TGuid& rhs) noexcept;
-
-std::strong_ordering operator <=> (const TGuid& lhs, const TGuid& rhs) noexcept;
+bool operator == (TGuid lhs, TGuid rhs) noexcept;
+std::strong_ordering operator <=> (TGuid lhs, TGuid rhs) noexcept;
////////////////////////////////////////////////////////////////////////////////
@@ -90,6 +91,20 @@ char* WriteGuidToBuffer(char* ptr, TGuid value);
////////////////////////////////////////////////////////////////////////////////
+//! Enables TGuid-to-TStringBuf conversion without allocation.
+class TFormattableGuid
+{
+public:
+ explicit TFormattableGuid(TGuid guid);
+ TStringBuf ToStringBuf() const;
+
+private:
+ std::array<char, MaxGuidStringSize> Buffer_;
+ const char* const End_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
} // namespace NYT
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/misc/unittests/guid_ut.cpp b/library/cpp/yt/misc/unittests/guid_ut.cpp
index ce9ee52109..b0ec7effbf 100644
--- a/library/cpp/yt/misc/unittests/guid_ut.cpp
+++ b/library/cpp/yt/misc/unittests/guid_ut.cpp
@@ -2,18 +2,47 @@
#include <library/cpp/yt/misc/guid.h>
+#include <util/generic//algorithm.h>
+
namespace NYT {
namespace {
////////////////////////////////////////////////////////////////////////////////
-TEST(TGuidTest, RandomGuids)
+TEST(TGuidTest, CreateRandom)
{
auto guid = TGuid::Create();
auto otherGuid = TGuid::Create();
EXPECT_FALSE(guid == otherGuid);
}
+TEST(TGuidTest, FormattableGuid)
+{
+ EXPECT_EQ(TFormattableGuid(TGuid::FromString("1-2-3-4")).ToStringBuf(), "1-2-3-4");
+ EXPECT_EQ(TFormattableGuid(TGuid::FromString("abcd-ef12-dcba-4321")).ToStringBuf(), "abcd-ef12-dcba-4321");
+}
+
+TEST(TGuidTest, StarshipOperator)
+{
+ std::vector<TGuid> guids{
+ TGuid::FromString("abcd-ef12-dcba-4321"),
+ TGuid::FromString("1-2-3-4"),
+ TGuid::FromString("1-2-3-4"),
+ TGuid::FromString("bada-13aa-abab-ffff"),
+ };
+ Sort(guids);
+ for (int index = 0; index < std::ssize(guids) - 1; index++) {
+ auto first = guids[index];
+ auto second = guids[index];
+ if (first == second) {
+ EXPECT_EQ(first <=> second, std::strong_ordering::equal);
+ } else {
+ EXPECT_EQ(first <=> second, std::strong_ordering::less);
+ EXPECT_EQ(second <=> first, std::strong_ordering::greater);
+ }
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////
} // namespace