Skip to content

Fixes: #3114 Reduce memory usage of bloom filter #3115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
@@ -1,16 +1,18 @@
package com.thealgorithms.datastructures.bloomfilter;


import java.util.BitSet;

public class BloomFilter<T> {

private int numberOfHashFunctions;
private int [] bitArray;
private BitSet bitArray;
private Hash<T>[] hashFunctions;

public BloomFilter(int numberOfHashFunctions, int n) {
this.numberOfHashFunctions = numberOfHashFunctions;
hashFunctions = new Hash[numberOfHashFunctions];
bitArray = new int[n];
bitArray = new BitSet(n);
insertHash();
}

Expand All @@ -22,13 +24,15 @@ private void insertHash() {

public void insert(T key) {
for (Hash<T> hash : hashFunctions){
bitArray[hash.compute(key) % bitArray.length] = 1;
int position = hash.compute(key) % bitArray.size();
bitArray.set(position);
}
}

public boolean contains(T key) {
for (Hash<T> hash : hashFunctions){
if (bitArray[hash.compute(key) % bitArray.length] == 0){
int position = hash.compute(key) % bitArray.size();
if (!bitArray.get(position)) {
return false;
}
}
Expand Down