我如何将字符串散列到特定数量的桶中

发布于 2024-10-25 05:29:44 字数 448 浏览 1 评论 0原文

我正在尝试提出一种算法,将字符串散列到特定数量的桶中,但还没有想到如何做到这一点?

我有一个像这样的字符串列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福丶如此 2024-11-01 05:29:44

您可能会发现以下内容博客文章很有用。提出的算法是:

int bucketIndex = (int)((uint)"d.jpg".GetHashCode() % (uint)buckets.Length);

You may find the following blog post useful. The proposed algorithm is:

int bucketIndex = (int)((uint)"d.jpg".GetHashCode() % (uint)buckets.Length);
人间☆小暴躁 2024-11-01 05:29:44
int bucket = (int)(unchecked(((uint)s.GetHashCode())) % 4 + 1)

(其中 s 是字符串)

int bucket = (int)(unchecked(((uint)s.GetHashCode())) % 4 + 1)

(where s is the string)

爱,才寂寞 2024-11-01 05:29:44

标准 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.

旧伤慢歌 2024-11-01 05:29:44

使用基于共享机器密钥的哈希算法。这将为每个字符串创建一个唯一的标识符。如果您需要整数,则使用字典对象将字符串映射到整数。每次添加新字符串时,将其键设置为当前字典长度。最后将字典存储在基于场的状态对象(例如共享会话)中,以便每个站点实例都可以引用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文