unordered_set:指针地址是一个好的哈希吗?
我想在哈希集中存储一组(智能)指针,
。经过 10 秒钟的思考,我想出了这个哈希函数:
typedef boost::shared_ptr<myType> ref_t;
struct SharedPtrHash : public std::unary_function<ref_t, std::size_t> {
std::size_t operator()(ref_t const& obj) const {
return reinterpret_cast<std::size_t>( obj.get() );
}
};
我的问题是:这个哈希是个好主意吗?我很高兴这个哈希值将有零或很少的碰撞(也许引擎盖下有一些素数模数破坏了我所有的乐趣)。
有关目的的更多详细信息:哈希的目的是回收大对象的存储,因此我需要一种快速的方法来检测大对象是否已经在垃圾箱中。
如果不是,那么指针的理想散列是什么,无论是智能指针还是愚蠢指针?
i want to store a set of (smart) pointers in a hash set, either <boost/unordered_set>
. After 10 seconds of thought, i came up with this hash function:
typedef boost::shared_ptr<myType> ref_t;
struct SharedPtrHash : public std::unary_function<ref_t, std::size_t> {
std::size_t operator()(ref_t const& obj) const {
return reinterpret_cast<std::size_t>( obj.get() );
}
};
My question is: is this hash a good idea? i'm entertaining the thought that this hash will have zero or very few collisions (maybe there is some prime-number modulus under the hood spoiling all my fun).
Further Details on purpose: The purpose of the hash is for recycling storage of big objects, so i need a fast way to detect if a big object is already in the bin.
in case it is not, what would be an ideal hash for pointers, either smart or dumb ones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要检测不相同的对象,即使它们的内容可能相同,您也别无选择,只能使用散列中对象的地址。唯一的问题是直接使用该地址还是通过公式运行它。除以
sizeof(mytype)
会缩小分布中的漏洞。编辑:这是一个未经测试的模板实现,应该适用于所有
shared_ptr
类型,以及一个equal_to
函数来完成std 的要求::unordered_set
。如果您有其他对象需要基于值而不是指针的哈希,请不要使用此通用实现。If you want to detect objects that are not identical even though their contents might be equal, you have no choice but to use the address of the object in the hash. The only question is whether to use the address directly or to run it through a formula. Dividing by
sizeof(mytype)
would tighten up the holes in the distribution.Edit: Here's an untested template implementation that should work with all
shared_ptr
types, along with anequal_to
function to complete the requirements forstd::unordered_set
. Don't use this generic implementation if you have other objects that require a hash based on the value instead of the pointer.以下代码可以完美编译(GCC 4.7,Boost 1.47):
The following code compiles perfectly (GCC 4.7, Boost 1.47):
整型的默认
Boost.Hash
hash
函数是恒等函数,所以我认为对指针做同样的事情不是一个坏主意。它将具有相同的碰撞率。The default
Boost.Hash
hash
function for integral types is the identity function, so I don't think doing the same for pointers is a bad idea. It would have the same collision ratio.