Skip to content

Commit 20a5de7

Browse files
authored
update fnv-1a seed value (#18)
* update fnv-1a seed value * update README
1 parent a966e58 commit 20a5de7

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## Current Version
2+
* ***BACKWARD INCOMPATIBLE CHANGES***
3+
* Update to the FNV_1a hash function **NOTE:** Breaks backwards compatibility with
4+
previously exported blooms using the default hash!
25

36
### Version 0.1.8
47
* Added in-depth testing

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,36 @@ cms_destroy(&cms);
8888
8989
## Required Compile Flags
9090
-lm
91+
92+
93+
## Backward Compatible Hash Function
94+
To use the older count-min sketch (v0.1.8 or lower) that utilized the default hashing
95+
algorithm, then change use the following code as the hash function:
96+
97+
``` c
98+
/* NOTE: The caller will free the results */
99+
static uint64_t* original_default_hash(unsigned int num_hashes, const char* str) {
100+
uint64_t *results = (uint64_t*)calloc(num_hashes, sizeof(uint64_t));
101+
char key[17] = {0}; // largest value is 7FFF,FFFF,FFFF,FFFF
102+
results[0] = __fnv_1a(str);
103+
for (unsigned int i = 1; i < num_hashes; ++i) {
104+
sprintf(key, "%" PRIx64 "", results[i-1]);
105+
results[i] = old_fnv_1a(key);
106+
}
107+
return results;
108+
}
109+
110+
static uint64_t old_fnv_1a(const char* key) {
111+
// FNV-1a hash (http://www.isthe.com/chongo/tech/comp/fnv/)
112+
int i, len = strlen(key);
113+
uint64_t h = 14695981039346656073ULL; // FNV_OFFSET 64 bit
114+
for (i = 0; i < len; ++i){
115+
h = h ^ (unsigned char) key[i];
116+
h = h * 1099511628211ULL; // FNV_PRIME 64 bit
117+
}
118+
return h;
119+
}
120+
```
121+
122+
If using only older count-min sketch, then you can update the // FNV_OFFSET 64 bit
123+
to use `14695981039346656073ULL`

src/count_min_sketch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static uint64_t* __default_hash(unsigned int num_hashes, const char* str) {
402402
static uint64_t __fnv_1a(const char* key) {
403403
// FNV-1a hash (http://www.isthe.com/chongo/tech/comp/fnv/)
404404
int i, len = strlen(key);
405-
uint64_t h = 14695981039346656073ULL; // FNV_OFFSET 64 bit
405+
uint64_t h = 14695981039346656037ULL; // FNV_OFFSET 64 bit
406406
for (i = 0; i < len; ++i){
407407
h = h ^ (unsigned char) key[i];
408408
h = h * 1099511628211ULL; // FNV_PRIME 64 bit

tests/test_cms.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ MU_TEST(test_insertions_different) {
9494
}
9595

9696
MU_TEST(test_insertions_max) {
97-
uint32_t too_large = (uint32_t)INT32_MAX + 5;
97+
uint64_t too_large = (uint32_t)INT32_MAX + 5;
9898
mu_assert_int_eq(INT32_MAX, cms_add_inc(&cms, "this is a test", too_large));
9999
mu_assert_int_eq(too_large, cms.elements_added);
100100
mu_assert_int_eq(INT32_MAX, cms_add_inc(&cms, "this is a test", 2));
@@ -270,7 +270,7 @@ MU_TEST(test_cms_export) {
270270
cms_export(&cms, "./tests/test.cms");
271271
char digest[33] = {0};
272272
calculate_md5sum("./tests/test.cms", digest);
273-
mu_assert_string_eq("61d2ea9d0cb09b7bb284e1cf1a860449", digest);
273+
mu_assert_string_eq("a53b06b40aae73ae2b8fc6c9dd113781", digest);
274274
remove("./tests/test.cms");
275275
}
276276

0 commit comments

Comments
 (0)