gpt4 book ai didi

c++ - uuid_t 的现有哈希函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:21 26 4
gpt4 key购买 nike

uuid.h(包:uuid-dev)是否有散列uuid_t变量的函数?

如果它存在的话,使用它会比我自己滚动更好。如果没有函数,关于可以产生相对可靠的唯一结果的散列过程有什么建议吗?

#ifdef WIN32
#define GUID UUID
#define generateGUID UuidCreate // returns RPC_S_OK, RPC_S_UUID_LOCAL_ONLY, RPC_S_UUID_NO_ADDRESS
#define hashGUID(gUid) hash<long>()(gUid.Data1) ^ (hash<short>()(gUid.Data2) << 1) >> 1 ^ hash<short>()(gUid.Data3) << 1 ^ hash<char>()(gUid.Data4[0]) << 1
#else
#define GUID uuid_t
#define generateGUID uuid_generate_random
#define hashGUID(gUid) ???
#endif // WIN32

namespace std
{
template<>
struct hash<GUID>
{
std::size_t operator()(const GUID& gUid) const
{
using std::size_t;
using std::hash;

return hashGUID(gUid);
}
};
}

编辑 用法:在标准容器中作为键

std::unordered_map<GUID, Component> components;

最佳答案

由于您想对 UUID 进行哈希处理以便将它们存储在哈希表中,因此我建议采用一些简单的方法:

 std::size_t operator()(const GUID& gUid) const
{
const uint64_t* half = reinterpret_cast<const uint64_t*>(&gUid);
return half[0] ^ half[1];
}

以上假定您使用的是 64 位平台(因此 uint64_t 与 size_t 一样宽)。

关于c++ - uuid_t 的现有哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com