2024-04-06 02:30:11 +08:00
|
|
|
/**
|
|
|
|
* File: simple_hash.c
|
|
|
|
* Created Time: 2023-09-09
|
|
|
|
* Author: Gonglja (glj0@outlook.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../utils/common.h"
|
|
|
|
|
|
|
|
/* 加法雜湊 */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 乘法雜湊 */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 互斥或雜湊 */
|
|
|
|
int xorHash(char *key) {
|
|
|
|
int hash = 0;
|
|
|
|
const int MODULUS = 1000000007;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
2024-05-31 16:39:06 +08:00
|
|
|
char *key = "Hello 演算法";
|
2024-04-06 02:30:11 +08:00
|
|
|
|
|
|
|
int hash = addHash(key);
|
|
|
|
printf("加法雜湊值為 %d\n", hash);
|
|
|
|
|
|
|
|
hash = mulHash(key);
|
|
|
|
printf("乘法雜湊值為 %d\n", hash);
|
|
|
|
|
|
|
|
hash = xorHash(key);
|
|
|
|
printf("互斥或雜湊值為 %d\n", hash);
|
|
|
|
|
|
|
|
hash = rotHash(key);
|
|
|
|
printf("旋轉雜湊值為 %d\n", hash);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|