我如何将字符串散列到特定数量的桶中
我正在尝试提出一种算法,将字符串散列到特定数量的桶中,但还没有想到如何做到这一点?
我有一个像这样的字符串列表:
a.jpg
b.htm
c.gif
d.jpg
e.swf
我想运行一个函数来根据字符串获取 1 到 4 之间的数字。
egajpg 将是 3
b.htm 将为 2
c.gif 将为 1
等
它需要保持一致,因此如果我在a.jpg上运行该函数,它总是返回3。
该算法将用于在服务器之间分割资源...
egajpg将从server3.mydomain.com访问
b.htm 可以从 server2.mydomain.com 访问
等等
有谁知道我会怎么做?
任何建议将不胜感激!
干杯
蒂姆
I'm trying to come up with an algorithm to hash a string into a specific number of buckets but haven't had any luck coming up with ideas on how to do this?
I have a list of strings like this:
a.jpg
b.htm
c.gif
d.jpg
e.swf
and i would like to run a function to get a number between 1 and 4 based on the string.
e.g. a.jpg would be 3
b.htm would be 2
c.gif would be 1
etc
it needs to be consistent so if i run the function on a.jpg it always returns 3.
this algorithm would be for splitting resources between servers...
e.g. a.jpg would be accessed from server3.mydomain.com
b.htm would be accessed from server2.mydomain.com
etc
Does anyone know how I would go about doing this?
Any advice would be much appreciated!
Cheers
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会发现以下内容博客文章很有用。提出的算法是:
You may find the following blog post useful. The proposed algorithm is:
(其中
s
是字符串)(where
s
is the string)标准 GetHashCode 和 % 将起作用:Math.Abs("aaaa".GetHashCode()) % numberOfBuckets。
编辑感谢 Thomas Levesque 提醒 GetHashCode() 返回 0. 添加了 Math.Abs 以获得正确的代码,但其他答案中的版本可能会更好。
Standard GetHashCode and % will work:
Math.Abs("aaaa".GetHashCode()) % numberOfBuckets
.EDIT thanks Thomas Levesque for reminding of GetHashCode() returning < 0. Added Math.Abs to have correct code, but versions in other answers are likely work better.
使用基于共享机器密钥的哈希算法。这将为每个字符串创建一个唯一的标识符。如果您需要整数,则使用字典对象将字符串映射到整数。每次添加新字符串时,将其键设置为当前字典长度。最后将字典存储在基于场的状态对象(例如共享会话)中,以便每个站点实例都可以引用它。
Use a hash algorithm based on a shared machine key. This will create a unique identifier per string. If you require integers then use a dictionary object to map strings to ints. Every time you add a new string set its key to the current dictionary length. Finally store the dictionary in a farm based state object such as a shared session so that each site instance can reference it.