Skip to content

Commit

Permalink
add a new test-add method that doesn't write unless it has to
Browse files Browse the repository at this point in the history
  • Loading branch information
piskvorky committed Sep 23, 2015
1 parent 458e554 commit 39fac58
Show file tree
Hide file tree
Showing 5 changed files with 3,819 additions and 2,125 deletions.
17 changes: 17 additions & 0 deletions src/bloomfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ static inline int bloomfilter_Test(BloomFilter * bf, Key * key)
__attribute__((always_inline))


static inline int bloomfilter_Test_Add(BloomFilter * bf, Key * key)
{
BTYPE (*hashfunc) (uint32_t, Key *) = key->shash == NULL ? _hash_long : _hash_char;
BTYPE mod = bf->array->bits;
int i;
int result = 1;
BTYPE hash_res;

for (i = bf->num_hashes - 1; i >= 0; --i) {
hash_res = (*hashfunc)(bf->hash_seeds[i], key) % mod;
result &= mbarray_Test_Set(bf->array, hash_res);
}
if (!result && bf->count_correct) {
bf->elem_count ++;
}
return result;
}
__attribute__((always_inline))

#endif
15 changes: 8 additions & 7 deletions src/cbloomfilter.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ cdef extern from "bloomfilter.h":
double error_rate,
char * fname, long num_bits,
int oflags, int perms,
int * hash_seeds, int num_hashes)
int * hash_seeds, int num_hashes) nogil
BloomFilter * bloomfilter_Create_Malloc(long max_num_elem,
double error_rate,
long num_bits,
int * hash_seeds, int num_hashes)
void bloomfilter_Destroy(BloomFilter * bf)
int bloomfilter_Add(BloomFilter * bf, Key * key)
int bloomfilter_Test(BloomFilter * bf, Key * key)
int bloomfilter_Update(BloomFilter * bf, char * data, int size)
BloomFilter * bloomfilter_Copy_Template(BloomFilter * src, char * filename, int perms)
int * hash_seeds, int num_hashes) nogil
void bloomfilter_Destroy(BloomFilter * bf) nogil
int bloomfilter_Add(BloomFilter * bf, Key * key) nogil
int bloomfilter_Test(BloomFilter * bf, Key * key) nogil
int bloomfilter_Test_Add(BloomFilter * bf, Key * key) nogil
int bloomfilter_Update(BloomFilter * bf, char * data, int size) nogil
BloomFilter * bloomfilter_Copy_Template(BloomFilter * src, char * filename, int perms) nogil
17 changes: 17 additions & 0 deletions src/mmapbitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,21 @@ static inline int mbarray_Test(MBArray * array, BTYPE bit)
__attribute__((always_inline))


static inline int mbarray_Test_Set(MBArray * array, BTYPE bit)
{
if (bit > array->bits) {
errno = EINVAL;
return -1;
}
DTYPE *chunk = array->vector + (size_t)(array->preamblesize + bit / (sizeof(DTYPE) << 3));
int byte = 1 << (bit % (sizeof(DTYPE) << 3));
int result = (*chunk & byte) != 0;
if (!result) {
*chunk |= byte;
}
return result;
}
__attribute__((always_inline))


#endif
Loading

0 comments on commit 39fac58

Please sign in to comment.