Skip to content

Commit 4fd7e2a

Browse files
authored
Hotfix/default hash with seed (#19)
* add seed to default hash and simplify hashing * update CHANGELOG * version bump
1 parent 20a5de7 commit 4fd7e2a

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
## Current Version
1+
# Count-Min Sketch
2+
3+
### Version 0.2.0
24
* ***BACKWARD INCOMPATIBLE CHANGES***
3-
* Update to the FNV_1a hash function **NOTE:** Breaks backwards compatibility with
4-
previously exported blooms using the default hash!
5+
* **NOTE:** Breaks backwards compatibility with previously exported blooms using the default hash!
6+
* Update to the FNV_1a hash function
7+
* Simplified hashing at depth by using a seed value
58

69
### Version 0.1.8
710
* Added in-depth testing

src/count_min_sketch.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************
22
*** Author: Tyler Barrus
33
4-
*** Version: 0.1.8
4+
*** Version: 0.2.0
55
*** License: MIT 2017
66
*******************************************************************************/
77

@@ -24,7 +24,7 @@ static void __read_from_file(CountMinSketch* cms, FILE *fp, short on_disk, const
2424
static void __merge_cms(CountMinSketch* base, int num_sketches, va_list* args);
2525
static int __validate_merge(CountMinSketch* base, int num_sketches, va_list* args);
2626
static uint64_t* __default_hash(unsigned int num_hashes, const char* key);
27-
static uint64_t __fnv_1a(const char* key);
27+
static uint64_t __fnv_1a(const char* key, int seed);
2828
static int __compare(const void * a, const void * b);
2929
static int32_t __safe_add(int32_t a, uint32_t b);
3030
static int32_t __safe_sub(int32_t a, uint32_t b);
@@ -389,20 +389,18 @@ static int __validate_merge(CountMinSketch* base, int num_sketches, va_list* arg
389389

390390
/* NOTE: The caller will free the results */
391391
static uint64_t* __default_hash(unsigned int num_hashes, const char* str) {
392-
uint64_t *results = (uint64_t*)calloc(num_hashes, sizeof(uint64_t));
393-
char key[17] = {0}; // largest value is 7FFF,FFFF,FFFF,FFFF
394-
results[0] = __fnv_1a(str);
395-
for (unsigned int i = 1; i < num_hashes; ++i) {
396-
sprintf(key, "%" PRIx64 "", results[i-1]);
397-
results[i] = __fnv_1a(key);
392+
uint64_t* results = (uint64_t*)calloc(num_hashes, sizeof(uint64_t));
393+
int i;
394+
for (i = 0; i < num_hashes; ++i) {
395+
results[i] = __fnv_1a(str, i);
398396
}
399397
return results;
400398
}
401399

402-
static uint64_t __fnv_1a(const char* key) {
400+
static uint64_t __fnv_1a(const char* key, int seed) {
403401
// FNV-1a hash (http://www.isthe.com/chongo/tech/comp/fnv/)
404402
int i, len = strlen(key);
405-
uint64_t h = 14695981039346656037ULL; // FNV_OFFSET 64 bit
403+
uint64_t h = 14695981039346656037ULL + (31 * seed); // FNV_OFFSET 64 bit with magic number seed
406404
for (i = 0; i < len; ++i){
407405
h = h ^ (unsigned char) key[i];
408406
h = h * 1099511628211ULL; // FNV_PRIME 64 bit

src/count_min_sketch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/*******************************************************************************
55
*** Author: Tyler Barrus
66
7-
*** Version: 0.1.8
7+
*** Version: 0.2.0
88
*** License: MIT 2017
99
*******************************************************************************/
1010

tests/test_cms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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("a53b06b40aae73ae2b8fc6c9dd113781", digest);
273+
mu_assert_string_eq("fb1c39dd1a73f1ef0d7fc79f60fc028e", digest);
274274
remove("./tests/test.cms");
275275
}
276276

0 commit comments

Comments
 (0)