Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ install:
@# Support Debian package building with fall-back default
python setup.py install --root $${DESTDIR:-/}

test:
python setup.py test


clean:
rm -rf build/
Expand Down
1 change: 1 addition & 0 deletions src/cbloomfilter.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cdef extern from "mmapbitarray.h":
MBArray * mbarray_And_Ternary(MBArray * dest, MBArray * a, MBArray * b)
MBArray * mbarray_Or_Ternary(MBArray * dest, MBArray * a, MBArray * b)
MBArray * mbarray_Xor_Ternary(MBArray * dest, MBArray * a, MBArray * b)
int mbarray_Popcount(MBArray *array)
int mbarray_Update(MBArray * array, char * data, int size)
int mbarray_FileSize(MBArray * array)
char * mbarray_CharData(MBArray * array)
Expand Down
16 changes: 16 additions & 0 deletions src/mmapbitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ int mbarray_Sync(MBArray * array)
return 0;
}

int popcount(DTYPE x)
{
int c = 0;
for (; x > 0; x &= x -1) c++;
return c;
}

int mbarray_Popcount(MBArray * array)
{
int pc = 0;
int offset = array->preamblesize;
for(int i = 0; i < array->size + offset; i++) {
pc += popcount(array->vector[offset + i]);
}
return pc;
}

int mbarray_ClearAll(MBArray * array)
{
Expand Down
2 changes: 2 additions & 0 deletions src/mmapbitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ MBArray * mbarray_Create_Mmap(BTYPE num_bits, const char * file, const char * he

void mbarray_Destroy(MBArray * array);

int mbarray_Popcount(MBArray * array);

int mbarray_ClearAll(MBArray * array);

int mbarray_Sync(MBArray * array);
Expand Down
947 changes: 514 additions & 433 deletions src/pybloomfilter.c

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/pybloomfilter.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (0, 3, 14)
VERSION = (0, 3, 15)
AUTHOR = "Michael Axiak"

__VERSION__ = VERSION
Expand Down Expand Up @@ -196,6 +196,12 @@ cdef class BloomFilter:
self._assert_open()
cbloomfilter.mbarray_Sync(self._bf.array)


def popcount(self):
self._assert_open()
return cbloomfilter.mbarray_Popcount(self._bf.array)


def clear_all(self):
self._assert_open()
cbloomfilter.mbarray_ClearAll(self._bf.array)
Expand Down
13 changes: 13 additions & 0 deletions tests/simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ def test_string_nofile(self):
bf.add("test")
self.assertEquals("test" in bf, True)

def test_popcount_nofile(self):
bf = pybloomfilter.BloomFilter(100, 0.01)
self.assertEquals(bf.popcount(), 0)
bf.add("hello")
self.assertNotEqual(bf.popcount(), 0)

def test_others_nofile(self):
bf = pybloomfilter.BloomFilter(100, 0.01)
for elem in (1.2, 2343L, (1, 2), object(), u'\u2131\u3184'):
Expand All @@ -197,6 +203,13 @@ def _test_large_file(self, filename):
bf.add(1234)
self.assertEquals(1234 in bf, True)

@with_test_file
def test_popcount(self, filename):
bf = pybloomfilter.BloomFilter(1000, 0.01, filename)
self.assertEqual(bf.popcount(), 0)
bf.add("popcount")
self.assertNotEqual(bf.popcount(), 0)

def test_name_does_not_segfault(self):
bf = pybloomfilter.BloomFilter(100, 0.01)
self.assertRaises(NotImplementedError, lambda: bf.name)
Expand Down