Tweaked UUID for hashing
This commit is contained in:
@@ -9,15 +9,18 @@
|
|||||||
#ifndef GUARD_TOURMALINE_TYPES_H
|
#ifndef GUARD_TOURMALINE_TYPES_H
|
||||||
#define GUARD_TOURMALINE_TYPES_H
|
#define GUARD_TOURMALINE_TYPES_H
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
namespace Tourmaline::Type {
|
namespace Tourmaline::Type {
|
||||||
struct UUID {
|
class UUID {
|
||||||
|
public:
|
||||||
constexpr static uint8_t BitLength = 128;
|
constexpr static uint8_t BitLength = 128;
|
||||||
constexpr static uint8_t QWORDLength = BitLength / 64;
|
constexpr static uint8_t QWORDLength = BitLength / 64;
|
||||||
constexpr static uint8_t ByteLength = BitLength / 8;
|
constexpr static uint8_t ByteLength = BitLength / 8;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::string asString() const;
|
std::string asString() const;
|
||||||
|
bool operator==(const UUID &rhs) const;
|
||||||
|
|
||||||
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
UUID(uint64_t firstHalf, uint64_t secondHalf);
|
||||||
UUID(const std::string &uuid);
|
UUID(const std::string &uuid);
|
||||||
@@ -30,6 +33,22 @@ struct UUID {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
|
||||||
|
friend struct std::hash<Tourmaline::Type::UUID>;
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Type
|
} // namespace Tourmaline::Type
|
||||||
|
|
||||||
|
namespace std {
|
||||||
|
template <> struct hash<Tourmaline::Type::UUID> {
|
||||||
|
size_t operator()(const Tourmaline::Type::UUID &uuid) const noexcept {
|
||||||
|
const auto data = uuid.data.get();
|
||||||
|
size_t h1 = std::hash<uint64_t>{}(data[0]);
|
||||||
|
size_t h2 = std::hash<uint64_t>{}(data[1]);
|
||||||
|
|
||||||
|
// Combine the two hashes
|
||||||
|
return h1 ^ (h2 << 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,6 +20,16 @@ std::string UUID::asString() const {
|
|||||||
return std::format("{:016X}{:016X}", data[0], data[1]);
|
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) {
|
UUID::UUID(const UUID &uuid) {
|
||||||
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
std::memcpy(data.get(), uuid.data.get(), UUID::ByteLength);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user