2020#include <unistd.h> /* close */
2121#include "bloom.h"
2222
23+ typedef char * caddr_t ;
2324
2425#define CHECK_BIT_CHAR (c , k ) ((c) & (1 << (k)))
2526#define CHECK_BIT (A , k ) (CHECK_BIT_CHAR(A[((k) / 8)], ((k) % 8)))
@@ -46,10 +47,10 @@ static uint64_t* __default_hash(int num_hashes, const char *str);
4647static uint64_t __fnv_1a (const char * key , int seed );
4748static void __calculate_optimal_hashes (BloomFilter * bf );
4849static void __read_from_file (BloomFilter * bf , FILE * fp , short on_disk , const char * filename );
49- static void __write_to_file (BloomFilter * bf , FILE * fp , short on_disk );
50+ static void __write_to_file (const BloomFilter * bf , FILE * fp , short on_disk );
5051static void __update_elements_added_on_disk (BloomFilter * bf );
5152static int __sum_bits_set_char (unsigned char c );
52- static int __check_if_union_or_intersection_ok (BloomFilter * res , BloomFilter * bf1 , BloomFilter * bf2 );
53+ static int __check_if_union_or_intersection_ok (const BloomFilter * res , const BloomFilter * bf1 , const BloomFilter * bf2 );
5354
5455
5556int bloom_filter_init_alt (BloomFilter * bf , uint64_t estimated_elements , float false_positive_rate , BloomHashFunction hash_function ) {
@@ -119,7 +120,7 @@ int bloom_filter_clear(BloomFilter *bf) {
119120 return BLOOM_SUCCESS ;
120121}
121122
122- void bloom_filter_stats (BloomFilter * bf ) {
123+ void bloom_filter_stats (const BloomFilter * bf ) {
123124 const char * is_on_disk = (bf -> __is_on_disk == 0 ? "no" : "yes" );
124125 uint64_t size_on_disk = bloom_filter_export_size (bf );
125126
@@ -150,19 +151,19 @@ int bloom_filter_add_string(BloomFilter *bf, const char *str) {
150151}
151152
152153
153- int bloom_filter_check_string (BloomFilter * bf , const char * str ) {
154+ int bloom_filter_check_string (const BloomFilter * bf , const char * str ) {
154155 uint64_t * hashes = bloom_filter_calculate_hashes (bf , str , bf -> number_hashes );
155156 int res = bloom_filter_check_string_alt (bf , hashes , bf -> number_hashes );
156157 free (hashes );
157158 return res ;
158159}
159160
160- uint64_t * bloom_filter_calculate_hashes (BloomFilter * bf , const char * str , unsigned int number_hashes ) {
161+ uint64_t * bloom_filter_calculate_hashes (const BloomFilter * bf , const char * str , unsigned int number_hashes ) {
161162 return bf -> hash_function (number_hashes , str );
162163}
163164
164165/* Add a string to a bloom filter using the defined hashes */
165- int bloom_filter_add_string_alt (BloomFilter * bf , uint64_t * hashes , unsigned int number_hashes_passed ) {
166+ int bloom_filter_add_string_alt (BloomFilter * bf , const uint64_t * hashes , unsigned int number_hashes_passed ) {
166167 if (number_hashes_passed < bf -> number_hashes ) {
167168 fprintf (stderr , "Error: not enough hashes passed in to correctly check!\n" );
168169 return BLOOM_FAILURE ;
@@ -183,7 +184,7 @@ int bloom_filter_add_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned int
183184}
184185
185186/* Check if a string is in the bloom filter using the passed hashes */
186- int bloom_filter_check_string_alt (BloomFilter * bf , uint64_t * hashes , unsigned int number_hashes_passed ) {
187+ int bloom_filter_check_string_alt (const BloomFilter * bf , const uint64_t * hashes , unsigned int number_hashes_passed ) {
187188 if (number_hashes_passed < bf -> number_hashes ) {
188189 fprintf (stderr , "Error: not enough hashes passed in to correctly check!\n" );
189190 return BLOOM_FAILURE ;
@@ -201,14 +202,14 @@ int bloom_filter_check_string_alt(BloomFilter *bf, uint64_t *hashes, unsigned in
201202 return r ;
202203}
203204
204- float bloom_filter_current_false_positive_rate (BloomFilter * bf ) {
205+ float bloom_filter_current_false_positive_rate (const BloomFilter * bf ) {
205206 int num = bf -> number_hashes * bf -> elements_added ;
206207 double d = - num / (float ) bf -> number_bits ;
207208 double e = exp (d );
208209 return pow ((1 - e ), bf -> number_hashes );
209210}
210211
211- int bloom_filter_export (BloomFilter * bf , const char * filepath ) {
212+ int bloom_filter_export (const BloomFilter * bf , const char * filepath ) {
212213 // if the bloom is initialized on disk, no need to export it
213214 if (bf -> __is_on_disk == 1 ) {
214215 return BLOOM_SUCCESS ;
@@ -251,7 +252,7 @@ int bloom_filter_import_on_disk_alt(BloomFilter *bf, const char *filepath, Bloom
251252 return BLOOM_SUCCESS ;
252253}
253254
254- char * bloom_filter_export_hex_string (BloomFilter * bf ) {
255+ char * bloom_filter_export_hex_string (const BloomFilter * bf ) {
255256 uint64_t i , bytes = sizeof (uint64_t ) * 2 + sizeof (float ) + (bf -> bloom_length );
256257 char * hex = (char * )calloc ((bytes * 2 + 1 ), sizeof (char ));
257258 for (i = 0 ; i < bf -> bloom_length ; ++ i ) {
@@ -302,19 +303,19 @@ int bloom_filter_import_hex_string_alt(BloomFilter *bf, const char *hex, BloomHa
302303 return BLOOM_SUCCESS ;
303304}
304305
305- uint64_t bloom_filter_export_size (BloomFilter * bf ) {
306+ uint64_t bloom_filter_export_size (const BloomFilter * bf ) {
306307 return (uint64_t )(bf -> bloom_length * sizeof (unsigned char )) + (2 * sizeof (uint64_t )) + sizeof (float );
307308}
308309
309- uint64_t bloom_filter_count_set_bits (BloomFilter * bf ) {
310+ uint64_t bloom_filter_count_set_bits (const BloomFilter * bf ) {
310311 uint64_t i , res = 0 ;
311312 for (i = 0 ; i < bf -> bloom_length ; ++ i ) {
312313 res += __sum_bits_set_char (bf -> bloom [i ]);
313314 }
314315 return res ;
315316}
316317
317- uint64_t bloom_filter_estimate_elements (BloomFilter * bf ) {
318+ uint64_t bloom_filter_estimate_elements (const BloomFilter * bf ) {
318319 return bloom_filter_estimate_elements_by_values (bf -> number_bits , bloom_filter_count_set_bits (bf ), bf -> number_hashes );
319320}
320321
@@ -337,7 +338,7 @@ int bloom_filter_union(BloomFilter *res, BloomFilter *bf1, BloomFilter *bf2) {
337338 return BLOOM_SUCCESS ;
338339}
339340
340- uint64_t bloom_filter_count_union_bits_set (BloomFilter * bf1 , BloomFilter * bf2 ) {
341+ uint64_t bloom_filter_count_union_bits_set (const BloomFilter * bf1 , const BloomFilter * bf2 ) {
341342 // Ensure the bloom filters can be unioned
342343 if (__check_if_union_or_intersection_ok (bf1 , bf1 , bf2 ) == BLOOM_FAILURE ) { // use bf1 as res
343344 return BLOOM_FAILURE ;
@@ -367,7 +368,7 @@ void bloom_filter_set_elements_to_estimated(BloomFilter *bf) {
367368 __update_elements_added_on_disk (bf );
368369}
369370
370- uint64_t bloom_filter_count_intersection_bits_set (BloomFilter * bf1 , BloomFilter * bf2 ) {
371+ uint64_t bloom_filter_count_intersection_bits_set (const BloomFilter * bf1 , const BloomFilter * bf2 ) {
371372 // Ensure the bloom filters can be used in an intersection
372373 if (__check_if_union_or_intersection_ok (bf1 , bf1 , bf2 ) == BLOOM_FAILURE ) { // use bf1 as res
373374 return BLOOM_FAILURE ;
@@ -379,7 +380,7 @@ uint64_t bloom_filter_count_intersection_bits_set(BloomFilter *bf1, BloomFilter
379380 return res ;
380381}
381382
382- float bloom_filter_jaccard_index (BloomFilter * bf1 , BloomFilter * bf2 ) {
383+ float bloom_filter_jaccard_index (const BloomFilter * bf1 , const BloomFilter * bf2 ) {
383384 // Ensure the bloom filters can be used in an intersection and union
384385 if (__check_if_union_or_intersection_ok (bf1 , bf1 , bf2 ) == BLOOM_FAILURE ) { // use bf1 as res
385386 return (float )BLOOM_FAILURE ;
@@ -411,7 +412,7 @@ static int __sum_bits_set_char(unsigned char c) {
411412 return bits_set_table [c ];
412413}
413414
414- static int __check_if_union_or_intersection_ok (BloomFilter * res , BloomFilter * bf1 , BloomFilter * bf2 ) {
415+ static int __check_if_union_or_intersection_ok (const BloomFilter * res , const BloomFilter * bf1 , const BloomFilter * bf2 ) {
415416 if (res -> number_hashes != bf1 -> number_hashes || bf1 -> number_hashes != bf2 -> number_hashes ) {
416417 return BLOOM_FAILURE ;
417418 } else if (res -> number_bits != bf1 -> number_bits || bf1 -> number_bits != bf2 -> number_bits ) {
@@ -423,7 +424,7 @@ static int __check_if_union_or_intersection_ok(BloomFilter *res, BloomFilter *bf
423424}
424425
425426/* NOTE: this assumes that the file handler is open and ready to use */
426- static void __write_to_file (BloomFilter * bf , FILE * fp , short on_disk ) {
427+ static void __write_to_file (const BloomFilter * bf , FILE * fp , short on_disk ) {
427428 if (on_disk == 0 ) {
428429 fwrite (bf -> bloom , bf -> bloom_length , 1 , fp );
429430 } else {
0 commit comments