Skip to content
This repository has been archived by the owner on Jul 7, 2020. It is now read-only.

Add intersect method to bloom filter #136

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ public Filter merge(Filter... filters) {
return merged;
}

/**
* Intersect a Bloom Filter with a number of other bloom filters.
* @param filters
* @return a new Bloom Filter that has each filter bit set if and only if all the input bloom filters had that bit set
*/
public Filter intersect(Filter... filters) {
BitSet intersection = (BitSet) this.filter().clone();
BloomFilter intersectionBloomFilter = new BloomFilter(this.getHashCount(), intersection);
for (Filter otherFilter : filters) {
if (!(otherFilter instanceof BloomFilter) || this.hashCount != otherFilter.hashCount) {
throw new IllegalArgumentException(("Cannot merge filters of different class or size"));
}
intersection.and(((BloomFilter) otherFilter).filter());
}
return intersectionBloomFilter;
}

/**
* @return a BloomFilter that always returns a positive match, for testing
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public BloomFilterTest() {
@Before
public void clear() {
bf.clear();
bf2.clear();
}

@Test
Expand All @@ -76,6 +77,18 @@ public void testMerge() {
assertTrue(mergeBf.isPresent("c"));
}

@Test
public void testIntersect() {
bf.add("a");
bf.add("b");
bf2.add("a");
bf2.add("c");
BloomFilter intersectionBf = (BloomFilter) bf.intersect(bf2);
assertTrue(intersectionBf.isPresent("a"));
assertFalse(intersectionBf.isPresent("b"));
assertFalse(intersectionBf.isPresent("c"));
}

@Test(expected=IllegalArgumentException.class)
public void testMergeException() {
BloomFilter bf3 = new BloomFilter(ELEMENTS*10, 1);
Expand Down