有没有桶空间效率更高的字符串Hash

0 views
Skip to first unread message

liuxinyu

unread,
Jun 25, 2008, 6:25:51 AM6/25/08
to TopLanguage
今天有人在cpper上问字符串的hash:
http://www.cpper.com/c/t4878.html
我给的算法是:
hash []=0
hash x:xs = x*256+hash xs
但是有一个问题就是当字符串很长时,显然我给出的算法非常容易溢出。
这应该是个成熟的问题,有没有桶空间效率更高的hash算法,

pi1ot

unread,
Jun 25, 2008, 11:18:46 AM6/25/08
to TopLanguage
真想不到cpper论坛居然还活到现在
hash算法往上常见的很多,多数是一些经验算法,我常用的是:

// DJB Hash Function
unsigned int DJBHash( char * str) {
unsigned int hash = 5381 ;
while ( * str) {
hash += (hash << 5 ) + ( * str ++ );
}
return (hash & 0x7FFFFFFF );
}

桶空间效率具体指什么?均匀分布?

SpitFire

unread,
Jun 25, 2008, 11:36:38 AM6/25/08
to pon...@googlegroups.com
cpper不能称为活了,已经基本没人,几个老骨头有时候上去看看
--
SpitFire

XiongJia Le

unread,
Jun 25, 2008, 11:55:42 AM6/25/08
to pon...@googlegroups.com
啥桶? HASH 冲撞时候的 基桶,溢出桶?

2008/6/25 liuxinyu <liuxi...@gmail.com>:

liuxinyu

unread,
Jun 25, 2008, 8:38:52 PM6/25/08
to TopLanguage
这个没有本质的区别,都是乘n加当前字符值。只不过最后去除了符号位,感觉没有通用的节约桶个数的方法。除非字符串很有特点,
比如如果肯定是英文串的话,可以根绝实际英语字符出现的频率略微优化一下。

liuxinyu

unread,
Jun 25, 2008, 8:48:26 PM6/25/08
to TopLanguage
自嘲一下:

这样也有好处,因为工作成家了多年后,现实生活已经不允许你天天像年轻人一样泡网了。
每天能拿出个半小时已经不易。如果由于工作忙,一周后上网一看,数百篇文章未读,一定是非常不爽。
cpper恰好满足了这样一部分人的生活和兴趣要求,所以有存在的空间。

什么时候上去看,总有一些有趣的内容,不铺天盖地,并且有都是几个熟悉的朋友,好比隔三差五,喝茶小聚,别有一番乐趣。

打个不恰当那个的比方,热闹的论坛如top language属于都市生活,cpper则属于田园生活。

On Jun 25, 11:36 pm, SpitFire <spitfi...@gmail.com> wrote:
> cpper不能称为活了,已经基本没人,几个老骨头有时候上去看看
>

XiongJia Le

unread,
Jun 25, 2008, 8:51:41 PM6/25/08
to pon...@googlegroups.com
o, 这么可怕...看来我要坚持住不成家...

2008/6/26 liuxinyu <liuxi...@gmail.com>:
Message has been deleted

Cheney Chen

unread,
Jul 1, 2008, 2:07:13 AM7/1/08
to pon...@googlegroups.com
推荐JsHash或ElfHash.

2008/6/26 星染流云 <spirit...@sina.com>:
cpper只看了两年,很好很强大的地方............


On 6月25日, 下午11时36分, SpitFire <spitfi...@gmail.com> wrote:
> cpper不能称为活了,已经基本没人,几个老骨头有时候上去看看
>
> 2008/6/25 pi1ot <pilot...@gmail.com>:
> SpitFire- 隐藏被引用文字 -
>
> - 显示引用的文字 -

pi1ot

unread,
Jul 1, 2008, 3:22:19 AM7/1/08
to TopLanguage
原因??

On 7月1日, 下午2时07分, "Cheney Chen" <cheneyt...@gmail.com> wrote:
> 推荐JsHash或ElfHash.
>
> 2008/6/26 星染流云 <spiritualw...@sina.com>:
> --
> Sincerely
> Cheney Chen

Cheney Chen

unread,
Jul 25, 2008, 11:00:39 PM7/25/08
to pon...@googlegroups.com
代码精简,效果好
--
Sincerely
Cheney Chen
Reply all
Reply to author
Forward
0 new messages