|
@ -33,8 +33,8 @@ print_barray(bit_array_t *arr) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bit_array_t* |
|
|
bit_array_t* |
|
|
new_bitarray(int size) { |
|
|
empty_bfilter(int size) { |
|
|
int width = (size/32) + 1; |
|
|
int width = (size/32) + 1; // 32 for a 32 bit int
|
|
|
uint32_t *barray = malloc((sizeof (int)) * width); |
|
|
uint32_t *barray = malloc((sizeof (int)) * width); |
|
|
|
|
|
|
|
|
bit_array_t *result = malloc(sizeof (bit_array_t)); |
|
|
bit_array_t *result = malloc(sizeof (bit_array_t)); |
|
@ -52,15 +52,15 @@ new_bitarray(int size) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
int |
|
|
setbit(bit_array_t* arr, int k) { |
|
|
setbit(bit_array_t *arr, int k) { |
|
|
if ((uint32_t)k >= arr->num_elems) { |
|
|
if ((uint32_t)k >= arr->num_elems) { |
|
|
printf("Tried to set a bit beyond the current limit, limit = %zu, k = %d\nExiting...\n", arr->num_elems, k); |
|
|
printf("Tried to set a bit beyond the current limit, limit = %zu, k = %d\nExiting...\n", arr->num_elems, k); |
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
/* The position in the array of the int we're looking at */ |
|
|
/* The position in the int we're looking at */ |
|
|
int i = k/32; |
|
|
int i = k/32; |
|
|
|
|
|
|
|
|
/* The position of the bit in the int itself */ |
|
|
/* The position of the int in the array */ |
|
|
int pos = k % 32; |
|
|
int pos = k % 32; |
|
|
|
|
|
|
|
|
unsigned int flag = 1; |
|
|
unsigned int flag = 1; |
|
@ -74,7 +74,11 @@ setbit(bit_array_t* arr, int k) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
int |
|
|
unsetbit(bit_array_t* arr, int k) { |
|
|
unsetbit(bit_array_t *arr, int k) { |
|
|
|
|
|
if ((uint32_t)k >= arr->num_elems) { |
|
|
|
|
|
printf("Tried to set a bit beyond the current limit, limit = %zu, k = %d\nExiting...\n", arr->num_elems, k); |
|
|
|
|
|
exit(1); |
|
|
|
|
|
} |
|
|
int i = k/32; |
|
|
int i = k/32; |
|
|
|
|
|
|
|
|
int pos = k % 32; |
|
|
int pos = k % 32; |
|
@ -88,6 +92,24 @@ unsetbit(bit_array_t* arr, int k) { |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
|
getbit(bit_array_t *arr, int k) { |
|
|
|
|
|
int i = k/32; |
|
|
|
|
|
int pos = k % 32; |
|
|
|
|
|
unsigned int flag = 1; |
|
|
|
|
|
|
|
|
|
|
|
flag = flag << pos; |
|
|
|
|
|
|
|
|
|
|
|
if (arr->arr[i] & flag) { |
|
|
|
|
|
// k-th bit is 1
|
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
// k-th bit is 0
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
fnv_hashes_t |
|
|
fnv_hashes_t |
|
|
hash_fnv(const char* value) { |
|
|
hash_fnv(const char* value) { |
|
|
uint64_t hval; |
|
|
uint64_t hval; |
|
@ -128,14 +150,43 @@ hash(const char *input, uint32_t k, size_t m) { |
|
|
return hashes; |
|
|
return hashes; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bit_array_t |
|
|
|
|
|
bfilter_set(bit_array_t *filter, |
|
|
|
|
|
const char *key) { |
|
|
|
|
|
hashes_t hashes = hash(key, 5, filter->num_elems); |
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 5; i++) { |
|
|
|
|
|
setbit(filter, hashes[i]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
int |
|
|
main (void) { |
|
|
bfilter_get(bit_array_t *filter, |
|
|
bit_array_t *test = new_bitarray(100); |
|
|
const char*key) { |
|
|
const char *test_string = "what tf is this I can't even, lololol"; |
|
|
hashes_t hashes = hash(key, 5, filter->num_elems); |
|
|
hashes_t hashes = hash(test_string, 5, 100); |
|
|
|
|
|
|
|
|
int exists = 1; |
|
|
|
|
|
|
|
|
for(int i = 0; i < 5; i++) { |
|
|
for(int i = 0; i < 5; i++) { |
|
|
printf("%zu\n", hashes[i]); |
|
|
if (!getbit(filter, hashes[i])) { |
|
|
|
|
|
exists = 0; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return exists; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
|
main (void) { |
|
|
|
|
|
bit_array_t *test = empty_bfilter(100); |
|
|
|
|
|
const char *test_string = "what tf is this I can't even, lololol"; |
|
|
|
|
|
const char *test_string2 = "tf is this I can't even, lololol"; |
|
|
|
|
|
|
|
|
|
|
|
bfilter_set(test, test_string); |
|
|
|
|
|
bfilter_set(test, test_string2); |
|
|
|
|
|
|
|
|
|
|
|
printf("%d\n", bfilter_get(test, test_string2)); |
|
|
|
|
|
|
|
|
|
|
|
print_barray(test); |
|
|
|
|
|
|
|
|
return EXIT_SUCCESS; |
|
|
return EXIT_SUCCESS; |
|
|
} |
|
|
} |
|
|