Browse Source

function to get k hashes

master
wes 8 years ago
parent
commit
ccd54e1224
  1. 41
      bfilter.c
  2. 10
      bfilter.h

41
bfilter.c

@ -88,11 +88,11 @@ unsetbit(bit_array_t* arr, int k) {
return 0; return 0;
} }
bfilter_hashes_t fnv_hashes_t
hash(const char* value) { hash_fnv(const char* value) {
uint64_t hval; uint64_t hval;
bfilter_hashes_t hashes; fnv_hashes_t hashes;
fnv64Init(&hval); fnv64Init(&hval);
@ -107,18 +107,35 @@ hash(const char* value) {
return hashes; return hashes;
} }
uint32_t
nth_hash(fnv_hashes_t hashes,
uint32_t i,
size_t m) {
return (hashes.hash_1 + hashes.hash_2 * i) % m;
}
hashes_t
hash(const char *input, uint32_t k, size_t m) {
fnv_hashes_t fnv = hash_fnv(input);
hashes_t hashes = malloc((sizeof (uint32_t)) * k);
hashes[0] = fnv.hash_1 % m;
hashes[1] = fnv.hash_2 % m;
for(uint32_t i = 0; i < (k-2); i++) {
hashes[i+2] = nth_hash(fnv, i+2, m);
}
return hashes;
}
int int
main (void) { main (void) {
bit_array_t *test = new_bitarray(100); bit_array_t *test = new_bitarray(100);
setbit(test, 31); const char *test_string = "what tf is this I can't even, lololol";
setbit(test, 63); hashes_t hashes = hash(test_string, 5, 100);
setbit(test, 95); for(int i = 0; i < 5; i++) {
setbit(test, 127); printf("%zu\n", hashes[i]);
print_barray(test); }
const char *test_string = "what blah is this I can't even, lololol";
printf("%zu\n", hash(test_string).hash_1);
printf("%zu\n", hash(test_string).hash_2);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

10
bfilter.h

@ -11,7 +11,9 @@ typedef
uint32_t hash_1; uint32_t hash_1;
uint32_t hash_2; uint32_t hash_2;
} }
bfilter_hashes_t; fnv_hashes_t;
typedef uint32_t* hashes_t;
int printbits(uint32_t, size_t); int printbits(uint32_t, size_t);
@ -19,7 +21,11 @@ int print_barray(bit_array_t*);
bit_array_t* new_bitarray(int); bit_array_t* new_bitarray(int);
bfilter_hashes_t hash(const char*); fnv_hashes_t hash_fnv(const char*);
uint32_t nth_hash(fnv_hashes_t, uint32_t, size_t);
hashes_t hash(const char *, uint32_t, size_t);
int setbit(bit_array_t*, int); int setbit(bit_array_t*, int);
int unsetbit(bit_array_t*, int); int unsetbit(bit_array_t*, int);

Loading…
Cancel
Save