E
- Object type that is to be inserted into the Bloom filter, e.g. String or Integer.public class BloomFilterMagnus<E>
extends java.lang.Object
implements java.io.Serializable
Constructor and Description |
---|
BloomFilterMagnus(double falsePositiveProbability,
int expectedNumberOfElements)
Constructs an empty Bloom filter with a given false positive probability.
|
BloomFilterMagnus(double c,
int n,
int k)
Constructs an empty Bloom filter.
|
BloomFilterMagnus(int bitSetSize,
int expectedNumberOElements)
Constructs an empty Bloom filter.
|
BloomFilterMagnus(int bitSetSize,
int expectedNumberOfFilterElements,
int actualNumberOfFilterElements,
java.util.BitSet filterData)
Construct a new Bloom filter based on existing Bloom filter data.
|
Modifier and Type | Method and Description |
---|---|
void |
add(byte[] bytes)
Adds an array of bytes to the Bloom filter.
|
void |
add(E element)
Adds an object to the Bloom filter.
|
void |
addAll(java.util.Collection<? extends E> c)
Adds all elements from a Collection to the Bloom filter.
|
void |
clear()
Sets all bits to false in the Bloom filter.
|
boolean |
contains(byte[] bytes)
Returns true if the array of bytes could have been inserted into the Bloom filter.
|
boolean |
contains(E element)
Returns true if the element could have been inserted into the Bloom filter.
|
boolean |
containsAll(java.util.Collection<? extends E> c)
Returns true if all the elements of a Collection could have been inserted
into the Bloom filter.
|
int |
count()
Returns the number of elements added to the Bloom filter after it
was constructed or after clear() was called.
|
static int |
createHash(byte[] data)
Generates a digest based on the contents of an array of bytes.
|
static int |
createHash(java.lang.String val)
Generates a digest based on the contents of a String.
|
static int |
createHash(java.lang.String val,
java.nio.charset.Charset charset)
Generates a digest based on the contents of a String.
|
static int[] |
createHashes(byte[] data,
int hashes)
Generates digests based on the contents of an array of bytes and splits the result into 4-byte int's and store them in an array.
|
boolean |
equals(java.lang.Object obj)
Compares the contents of two instances to see if they are equal.
|
double |
expectedFalsePositiveProbability()
Calculates the expected probability of false positives based on
the number of expected filter elements and the size of the Bloom filter.
|
boolean |
getBit(int bit)
Read a single bit from the Bloom filter.
|
java.util.BitSet |
getBitSet()
Return the bit set used to store the Bloom filter.
|
double |
getBitsPerElement()
Get actual number of bits per element based on the number of elements that have currently been inserted and the length
of the Bloom filter.
|
double |
getExpectedBitsPerElement()
Get expected number of bits per element when the Bloom filter is full.
|
int |
getExpectedNumberOfElements()
Returns the expected number of elements to be inserted into the filter.
|
double |
getFalsePositiveProbability()
Get the current probability of a false positive.
|
double |
getFalsePositiveProbability(double numberOfElements)
Calculate the probability of a false positive given the specified
number of inserted elements.
|
int |
getK()
Returns the value chosen for K.
|
int |
hashCode()
Calculates a hash code for this class.
|
void |
setBit(int bit,
boolean value)
Set a single bit in the Bloom filter.
|
int |
size()
Returns the number of bits in the Bloom filter.
|
public BloomFilterMagnus(double c, int n, int k)
c
- is the number of bits used per element.n
- is the expected number of elements the filter will contain.k
- is the number of hash functions used.public BloomFilterMagnus(int bitSetSize, int expectedNumberOElements)
bitSetSize
- defines how many bits should be used in total for the filter.expectedNumberOElements
- defines the maximum number of elements the filter is expected to contain.public BloomFilterMagnus(double falsePositiveProbability, int expectedNumberOfElements)
falsePositiveProbability
- is the desired false positive probability.expectedNumberOfElements
- is the expected number of elements in the Bloom filter.public BloomFilterMagnus(int bitSetSize, int expectedNumberOfFilterElements, int actualNumberOfFilterElements, java.util.BitSet filterData)
bitSetSize
- defines how many bits should be used for the filter.expectedNumberOfFilterElements
- defines the maximum number of elements the filter is expected to contain.actualNumberOfFilterElements
- specifies how many elements have been inserted into the filterData
BitSet.filterData
- a BitSet representing an existing Bloom filter.public static int createHash(java.lang.String val, java.nio.charset.Charset charset)
val
- specifies the input data.charset
- specifies the encoding of the input data.public static int createHash(java.lang.String val)
val
- specifies the input data. The encoding is expected to be UTF-8.public static int createHash(byte[] data)
data
- specifies input data.public static int[] createHashes(byte[] data, int hashes)
data
- specifies input data.hashes
- number of hashes/int's to produce.public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- is the object to compare to.public int hashCode()
hashCode
in class java.lang.Object
public double expectedFalsePositiveProbability()
public double getFalsePositiveProbability(double numberOfElements)
numberOfElements
- number of inserted elements.public double getFalsePositiveProbability()
public int getK()
public void clear()
public void add(E element)
element
- is an element to register in the Bloom filter.public void add(byte[] bytes)
bytes
- array of bytes to add to the Bloom filter.public void addAll(java.util.Collection<? extends E> c)
c
- Collection of elements.public boolean contains(E element)
element
- element to check.public boolean contains(byte[] bytes)
bytes
- array of bytes to check.public boolean containsAll(java.util.Collection<? extends E> c)
c
- elements to check.public boolean getBit(int bit)
bit
- the bit to read.public void setBit(int bit, boolean value)
bit
- is the bit to set.value
- If true, the bit is set. If false, the bit is cleared.public java.util.BitSet getBitSet()
public int size()
public int count()
public int getExpectedNumberOfElements()
public double getExpectedBitsPerElement()
public double getBitsPerElement()