-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathhash_multi.h
70 lines (61 loc) · 1.61 KB
/
hash_multi.h
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
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* HASH BY MULTIPLICATION
*
* Features:
* 1. h(k) = (A*k mod 2^w) >> (w-r)
* 2. bucket size(m): 2^r, eg, m = 8 = 2^3
* 3. w: word size (usually 32-bit)
* 4. the value of A is chosen between 2^(w-r) and 2^w odd number.
*
******************************************************************************/
#ifndef ALGO_HASH_MULTIPLICATION_H__
#define ALGO_HASH_MULTIPLICATION_H__
#include <math.h>
#include <string.h>
#include "generic.h"
#include "prime.h"
namespace alg {
struct MultiHash {
uint64_t A;
uint32_t r; // prime, init your hash table with size -> r
};
static const short BITWIDTH = 32;
static inline uint32_t multi_hash_table_size(const struct MultiHash * ht) { return 1<<(ht->r); }
/**
* multi_hash.
*/
static inline uint32_t multi_hash(const struct MultiHash * ht, uint32_t key) {
uint32_t hash;
hash = ((ht->A * key)&0xFFFFFFFF)>>(BITWIDTH-ht->r); //mod 2^w equals logic bitmask ops
return hash;
}
#ifdef _MSC_VER
#define log2(x) (log(x) / log(2.0))
#endif
/**
* init a hash table with size specified.
*/
static MultiHash * multi_hash_init(uint32_t size) {
// find prime larger than log2(size)
uint32_t r = ceil(log2((double)size));
int i;
for (i = r; ;i++) {
if (is_prime(i)) {
r = i;
break;
}
}
MultiHash * ht = new MultiHash;
uint32_t a = 1 << (BITWIDTH-r);
ht->A = a+1;
ht->r = r;
return ht;
}
}
#endif //