码迷,mamicode.com
首页 > 其他好文 > 详细

字符串Hash模板

时间:2017-07-27 00:57:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:uart   span   ash   ++   nbsp   for   mha   ber   seed   

 1 unsigned int SDBMHash(char *str) {
 2     unsigned int hash = 0;
 3     while (*str)
 4         // equivalent to: hash = 65599*hash + (*str++);
 5         hash = (*str++) + (hash << 6) + (hash << 16) - hash;
 6 
 7     return (hash & 0x7FFFFFFF);
 8 }
 9  
10 // RS Hash Function
11 unsigned int RSHash(char *str) {
12     unsigned int b = 378551;
13     unsigned int a = 63689;
14     unsigned int hash = 0;
15 
16     while (*str) {
17         hash = hash * a + (*str++);
18         a *= b;
19     }
20     return (hash & 0x7FFFFFFF);
21 }
22  
23 // JS Hash Function
24 unsigned int JSHash(char *str) {
25     unsigned int hash = 1315423911;
26  
27     while (*str)
28         hash ^= ((hash << 5) + (*str++) + (hash >> 2));
29  
30     return (hash & 0x7FFFFFFF);
31 }
32  
33 // P. J. Weinberger Hash Function
34 unsigned int PJWHash(char *str) {
35     unsigned int BitsInUnignedInt = (unsigned int) (sizeof(unsigned int) * 8);
36     unsigned int ThreeQuarters    = (unsigned int) ((BitsInUnignedInt  * 3) / 4);
37     unsigned int OneEighth        = (unsigned int) (BitsInUnignedInt / 8);
38     unsigned int HighBits         = (unsigned int) (0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
39     unsigned int hash             = 0;
40     unsigned int test             = 0;
41  
42     while (*str) {
43         hash = (hash << OneEighth) + (*str++);
44         if ((test = hash & HighBits) != 0)
45             hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
46     }
47     return (hash & 0x7FFFFFFF);
48 }
49  
50 // ELF Hash Function
51 unsigned int ELFHash(char *str) {
52     unsigned int hash = 0;
53     unsigned int x    = 0;
54  
55     while (*str) {
56         hash = (hash << 4) + (*str++);
57         if ((x = hash & 0xF0000000L) != 0) {
58             hash ^= (x >> 24);
59             hash &= ~x;
60         }
61     }
62  
63     return (hash & 0x7FFFFFFF);
64 }
65  
66 // BKDR Hash Function
67 unsigned int BKDRHash(char *str) {
68     unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
69     unsigned int hash = 0;
70  
71     while (*str)
72         hash = hash * seed + (*str++);
73  
74     return (hash & 0x7FFFFFFF);
75 }
76  
77 // DJB Hash Function
78 unsigned int DJBHash(char *str) {
79     unsigned int hash = 5381;
80  
81     while (*str)
82         hash += (hash << 5) + (*str++);
83  
84     return (hash & 0x7FFFFFFF);
85 }
86  
87 // AP Hash Function
88 unsigned int APHash(char *str) {
89     unsigned int hash = 0;
90  
91     for (int i = 0; *str; ++i)
92         if ((i & 1) == 0)
93             hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
94         else
95             hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
96  
97     return (hash & 0x7FFFFFFF);
98 }

 

字符串Hash模板

标签:uart   span   ash   ++   nbsp   for   mha   ber   seed   

原文地址:http://www.cnblogs.com/backlit/p/7242448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!