This commit is contained in:
krahets
2023-09-10 18:37:26 +08:00
parent 4307372a5b
commit e48716c883
6 changed files with 192 additions and 19 deletions

View File

@@ -115,7 +115,6 @@ index = hash(key) % capacity
int hash = 0;
const int MODULUS = 1000000007;
for (unsigned char c : key) {
cout << (int)c << endl;
hash ^= (int)c;
}
return hash & MODULUS;
@@ -511,13 +510,47 @@ index = hash(key) % capacity
=== "C"
```c title="simple_hash.c"
[class]{}-[func]{addHash}
/* 加法哈希 */
int addHash(char *key) {
long long hash = 0;
const int MODULUS = 1000000007;
for (int i = 0; i < strlen(key); i++) {
hash = (hash + (unsigned char)key[i]) % MODULUS;
}
return (int)hash;
}
[class]{}-[func]{mulHash}
/* 乘法哈希 */
int mulHash(char *key) {
long long hash = 0;
const int MODULUS = 1000000007;
for (int i = 0; i < strlen(key); i++) {
hash = (31 * hash + (unsigned char)key[i]) % MODULUS;
}
return (int)hash;
}
[class]{}-[func]{xorHash}
/* 异或哈希 */
int xorHash(char *key) {
int hash = 0;
const int MODULUS = 1000000007;
[class]{}-[func]{rotHash}
for (int i = 0; i < strlen(key); i++) {
hash ^= (unsigned char)key[i];
}
return hash & MODULUS;
}
/* 旋转哈希 */
int rotHash(char *key) {
long long hash = 0;
const int MODULUS = 1000000007;
for (int i = 0; i < strlen(key); i++) {
hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;
}
return (int)hash;
}
```
=== "Zig"
@@ -789,7 +822,7 @@ $$
=== "C"
```c title="built_in_hash.c"
// C 未提供内置 hash code 函数
```
=== "Zig"