简体   繁体   English

hash map 中的单独链接

[英]Separate chaining in hash map

My understanding of separate chaining is that we convert a large number into a small number so that we can use it as an index.我对分离链的理解是,我们将一个大数转换成一个小数,这样我们就可以将它用作索引。 But how do we deal with the large index?但是我们如何处理大索引呢? For example, the current size of my hash_map is 10, a new index calculated by the hash function is 55. So do I need to resize my hash_map every time when the new index is too large?比如我的hash_map当前的大小是10,一个新的索引由hash function计算出来是55。那么每次新索引太大时我需要重新调整我的hash_map的大小吗?

Thanks!谢谢!

A common technique is to have a hash function that computes some integer (typically 32-bit or 64-bit) and then to reduce the number to a valid index in the hash table by modding that integer by the table size. A common technique is to have a hash function that computes some integer (typically 32-bit or 64-bit) and then to reduce the number to a valid index in the hash table by modding that integer by the table size. For example, if you have a 10-element hash table and your hash code is 55, you'd compute 55 mod 10 = 5 and place the item at index 5.例如,如果您有一个包含 10 个元素的 hash 表,并且您的 hash 代码为 55,则您将计算 55 mod 10 = 5 并将项目放在索引 5 处。

Depending on your programming language, there may be some edge cases to handle here (say, if the hash code could be negative, you need to ensure that your index is positive), but this general idea works pretty well and is used in many common hash table implementations.根据您的编程语言,这里可能需要处理一些边缘情况(例如,如果 hash 代码可能是负数,您需要确保您的索引是正数),但是这个一般想法非常有效,并且在许多常见的hash 表实现。

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

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