简体   繁体   English

std :: unordered_map的哈希函数有哪些限制

[英]What are the limitations for hash functions with std::unordered_map

I am using std::unordered_map to represent data in a 3Dimensional spatial arrangement. 我正在使用std :: unordered_map来表示3维空间排列中的数据。 My hashing function is: 我的哈希函数是:

unsigned int x,y,z;
unsigned int a =1000;
unsigned int b = 1000*a;
unsigned int Hash = x + a*y + b*z;

Which should allow for up to 1000 units of x, 1000 units of y, before there are any collisions. 在发生任何碰撞之前,它最多应允许x的1000个单位,y的1000个单位。 My question is, are there any limits to the collision-free space of my hash function? 我的问题是,哈希函数的无冲突空间是否有限制? or can I set a and b to be large numbers, noting that this would easily exceed the memory of my system if it was all allocated? 还是可以将a和b设置为大数,请注意,如果全部分配,这很容易超出系统的内存?

cheers 干杯

First, some background on the operation of hashtables: 首先,了解散列表的操作背景:

Hashtables don't allocate enough buckets to hold the entire space of the hash function. 哈希表分配的存储桶不足以容纳哈希函数的整个空间。 That would indeed be wasteful (and probably also impossible). 那确实是浪费的(也许也是不可能的)。 They allocate a certain number of buckets (say 16) and then store each pair in the bucket that the key hashes to modulo the number of buckets. 他们分配一定数量的存储桶(例如16个),然后将每对存储在存储桶中,密钥散列以对存储桶的数量取模。

The number of buckets is increased when the map reaches a certain threshold (usually 75-85%) of occupied buckets. 当地图达到已占用存储桶的某个阈值(通常为75-85%)时,存储桶数量会增加。 This forces a rehash of all of the keys so they can be applied to a new modulo. 这将强制对所有键进行重新哈希处理,以便可以将它们应用于新的模数。

So if your hash function returns 50 for a particular key, and the hashtable has 16 buckets, the pair for that key is stored in bucket (50 mod 16) = 2. 因此,如果您的哈希函数为特定键返回50,并且哈希表有16个存储桶,则该键对存储在存储桶(50 mod 16)= 2中。

If the number of buckets is later increased to 32, the pair gets moved to the bucket (50 mod 32) = 18. 如果随后将存储桶数增加到32,则该对将移动到存储桶(50 mod 32)= 18。

can I set a and b to be large numbers 我可以将a和b设置为大数吗

Absolutely, because the hash modulo the number of allocated buckets is used to find the bucket for a particular key. 绝对地,因为哈希是对分配的存储桶的数量取模,用于查找特定密钥的存储桶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM