blob: da5d78863953c1ddbf354e6f4e5066087d5193af (
plain)
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
|
#include <stdlib.h>
/*
*
* 32 bit FNV-1a algorithm
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*
*/
static size_t hash_key(const char *key, const size_t max_hash_value) {
uint32_t hash = 2166136261U; /* FNV-1a 32-bit offset basis */
/*
* We'll explicitly use unsigned chars here, since we're doing arithmetic. The
* C standard doesn't state whether an implementation must use signed or
* unsigned values for characters, so this conversion ensures we get
* correct behavior. If signed characters are used, we'll get overflows
* and other odd issues in the hash calculation.
*/
const unsigned char *p = (unsigned char *) key;
while (*p) {
hash ^= *p++;
hash *= 16777619U; /* FNV-1a prime */
}
return hash % max_hash_value;
}
static size_t test_hash(const char *key, const size_t max_hash_value) {
(void) max_hash_value;
return key[0] % 3;
}
|