-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.cpp
65 lines (42 loc) · 1.11 KB
/
hash.cpp
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
#include <stdio.h>
#include <string.h>
#include "hash.h"
#include "macros.h"
#include "assert.h"
typedef unsigned int Hash;
#if defined(_MSC_VER)
typedef unsigned long long uint64;
#else
typedef unsigned long uint64;
#endif
Hash ComputeHash(const void* mem, int size){
const unsigned char* data = (const unsigned char*)mem;
uint64 hashVal = 0xcbf29ce484222325ULL;
for(int i = 0; i < size; i++){
hashVal ^= data[i];
hashVal *= 0x100000001b3ULL;
}
return (Hash)hashVal;
}
Hash ComputeHash(const char* str){
return ComputeHash(str, str ? (int)strlen(str) : 0);
}
#if defined(HASH_TEST_MAIN)
CREATE_TEST_CASE("Hash collisions check") {
const int outputCount = 48238123;
int* counts = new int[outputCount];
for (int i = 0; i < outputCount; i++) {
counts[i] = 0;
}
for(unsigned int i = 0; i < 5000000; i++){
Hash intHash = ComputeHash(&i, sizeof(i));
int idx = intHash % outputCount;
counts[idx]++;
}
for(unsigned int i = 0; i < outputCount; i++){
ASSERT_MSG(counts[i] < 3, "Hash count for %d is %d, not within tolerance.", i, counts[i]);
}
delete[] counts;
return 0;
}
#endif