字符串Hash模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串Hash模板相关的知识,希望对你有一定的参考价值。

 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模板的主要内容,如果未能解决你的问题,请参考以下文章

第一弹HASH算法模板以及简单的入门题总结

VSCode自定义代码片段——.vue文件的模板

字符串hash模板

如何从URL获取片段标识符(hash#之后的值)?

hash 模板

VSCode自定义代码片段1——vue主模板