哈希函数给了我极大的数字

编程入门 行业动态 更新时间:2024-10-23 17:31:42
本文介绍了哈希函数给了我极大的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为c使用djb2哈希函数,当我通过它运行一个名称时,我得到了成千上万的哈希数,我希望能够使用一个数组来将其放入哈希表中几千个或更小的东西,至少在很长的时间内.我对如何获取给我较小的哈希值同时又具有哈希完整性的功能感到困惑.我也很困惑如何决定要用于我的哈希表的数组的正确大小.预先谢谢你.

I am using the djb2 hash function for c, when I run a name through it I am getting hash numbers in the hundreds of thousands, I would like to get to be able to put this in a hash table using an array of a few thousand or something smaller at least inside a long. I am confused about how to get the function to give me smaller hashes while still having the integrity of the hash. Also I am confused about how to decide on the proper size of array to use for my hash table. Thank you in advance.

unsigned long hash(char* str) { unsigned long hash = 5381; int c; for (int i = 0; i < strlen(str); ++i) { c = (int) str[i]; hash = ((hash << 5) + hash) + c; } return hash; }

推荐答案

假定您的djb2版本返回unsigned long(例如,调用返回变量foo),并以该结果的模为模n使用表达式

Assuming that your version of djb2 returns an unsigned long (call the return variable foo, say), taking the modulus of that result modulo n using the expression

foo % n

会将结果限制为0到并包括n - 1.它应该具有与原始哈希值相似的理想统计属性,并且应该优于通过整数除法获得的结果.

will constrain the result from 0 to and including n - 1. This ought to have similar desirable statistical properties to the original hash value, and ought to be superior to a result obtained by integer division.

更多推荐

哈希函数给了我极大的数字

本文发布于:2023-10-31 06:00:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:给了我   函数   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!