From 6d9edc18b480d9a64b352c0d77b441b8360ba453 Mon Sep 17 00:00:00 2001 From: cat Date: Tue, 6 Jan 2026 01:30:09 +0200 Subject: [PATCH] Tweaked UUID for hashing --- sourceCode/Types.hpp | 21 ++++++++++++++++++++- sourceCode/Types/UUID.cpp | 10 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sourceCode/Types.hpp b/sourceCode/Types.hpp index 44ef947..134eb79 100644 --- a/sourceCode/Types.hpp +++ b/sourceCode/Types.hpp @@ -9,15 +9,18 @@ #ifndef GUARD_TOURMALINE_TYPES_H #define GUARD_TOURMALINE_TYPES_H #include +#include #include namespace Tourmaline::Type { -struct UUID { +class UUID { +public: constexpr static uint8_t BitLength = 128; constexpr static uint8_t QWORDLength = BitLength / 64; constexpr static uint8_t ByteLength = BitLength / 8; [[nodiscard]] std::string asString() const; + bool operator==(const UUID &rhs) const; UUID(uint64_t firstHalf, uint64_t secondHalf); UUID(const std::string &uuid); @@ -30,6 +33,22 @@ struct UUID { private: std::unique_ptr data = std::make_unique(QWORDLength); + friend struct std::hash; }; } // namespace Tourmaline::Type + +namespace std { +template <> struct hash { + size_t operator()(const Tourmaline::Type::UUID &uuid) const noexcept { + const auto data = uuid.data.get(); + size_t h1 = std::hash{}(data[0]); + size_t h2 = std::hash{}(data[1]); + + // Combine the two hashes + return h1 ^ (h2 << 1); + } +}; + +} // namespace std + #endif diff --git a/sourceCode/Types/UUID.cpp b/sourceCode/Types/UUID.cpp index 2d157e9..6c41ff0 100644 --- a/sourceCode/Types/UUID.cpp +++ b/sourceCode/Types/UUID.cpp @@ -20,6 +20,16 @@ std::string UUID::asString() const { return std::format("{:016X}{:016X}", data[0], data[1]); } +bool UUID::operator==(const UUID &rhs) const { + // Since size may be increased + for (uint8_t index = 0; index < QWORDLength; index++) { + if (this->data[index] != rhs.data[index]) { + return false; + } + } + return true; +} + UUID::UUID(const UUID &uuid) { std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength); }