-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashmap_crc32.c
58 lines (51 loc) · 1.73 KB
/
hashmap_crc32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_crc32.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stoupin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/10 14:59:11 by stoupin #+# #+# */
/* Updated: 2018/01/10 14:59:13 by stoupin ### ########.fr */
/* */
/* ************************************************************************** */
#include "hashmap.h"
/*
** Return a 32-bit CRC of the contents of the buffer.
*/
static unsigned long crc32(t_hashmap_env *env,
const unsigned char *s)
{
unsigned int i;
unsigned long crc32val;
crc32val = 0;
i = 0;
while (s[i] != '\0')
{
crc32val = env->crc32_tab[(crc32val ^ s[i]) & 0xff] ^ (crc32val >> 8);
i++;
}
return (crc32val);
}
/*
** Hashing function for a string
** using Robert Jenkins' 32 bit Mix Function
** and Knuth's Multiplicative Method
*/
unsigned int hashmap_hash_int(t_hashmap_env *env,
t_hashmap *m,
char *keystring)
{
unsigned long key;
key = crc32(env, (unsigned char*)(keystring));
key += (key << 12);
key ^= (key >> 22);
key += (key << 4);
key ^= (key >> 9);
key += (key << 10);
key ^= (key >> 2);
key += (key << 7);
key ^= (key >> 12);
key = (key >> 3) * 2654435761;
return (key % m->table_size);
}