hello-algo/en/codes/c/chapter_hashing/simple_hash.c

69 lines
1.4 KiB
C

/**
* File: simple_hash.c
* Created Time: 2023-09-09
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* Additive hash */
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;
}
/* Multiplicative 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;
}
/* XOR 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;
}
/* Rotational hash */
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() {
char *key = "Hello 算法";
int hash = addHash(key);
printf("Additive hash value = %d\n", hash);
hash = mulHash(key);
printf("Multiplicative hash value = %d\n", hash);
hash = xorHash(key);
printf("XOR hash value = %d\n", hash);
hash = rotHash(key);
printf("Rotational hash value = %d\n", hash);
return 0;
}