Search in sources :

Example 1 with Hasher

use of org.apache.commons.collections4.bloomfilter.hasher.Hasher in project commons-collections by apache.

the class AbstractBloomFilterTest method constructorTest_Hasher.

/**
 * Tests that creating a filter with a hasher works as expected.
 */
@Test
public final void constructorTest_Hasher() {
    final List<Integer> lst = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
    final Hasher hasher = new StaticHasher(lst.iterator(), shape);
    final BloomFilter bf = createFilter(hasher, shape);
    final long[] lb = bf.getBits();
    assertEquals(0x1FFFF, lb[0]);
    assertEquals(1, lb.length);
}
Also used : StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Test(org.junit.jupiter.api.Test)

Example 2 with Hasher

use of org.apache.commons.collections4.bloomfilter.hasher.Hasher in project commons-collections by apache.

the class AbstractBloomFilterTest method xorCardinalityTest.

/**
 * Tests that the andCardinality calculations are correct.
 *
 * @param filterFactory the factory function to create the filter
 */
private void xorCardinalityTest(final BiFunction<Hasher, Shape, BloomFilter> filterFactory) {
    final List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
    final Hasher hasher = new StaticHasher(lst.iterator(), shape);
    final BloomFilter bf = createFilter(hasher, shape);
    final List<Integer> lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
    final Hasher hasher2 = new StaticHasher(lst2.iterator(), shape);
    final BloomFilter bf2 = filterFactory.apply(hasher2, shape);
    assertEquals(20, bf.xorCardinality(bf2));
}
Also used : StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher)

Example 3 with Hasher

use of org.apache.commons.collections4.bloomfilter.hasher.Hasher in project commons-collections by apache.

the class AbstractBloomFilterTest method orCardinalityTest.

/**
 * Tests that the andCardinality calculations are correct.
 *
 * @param filterFactory the factory function to create the filter
 */
private void orCardinalityTest(final BiFunction<Hasher, Shape, BloomFilter> filterFactory) {
    final List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
    final Hasher hasher = new StaticHasher(lst.iterator(), shape);
    final AbstractBloomFilter bf = createFilter(hasher, shape);
    final List<Integer> lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
    final Hasher hasher2 = new StaticHasher(lst2.iterator(), shape);
    final BloomFilter bf2 = filterFactory.apply(hasher2, shape);
    assertEquals(27, bf.orCardinality(bf2));
}
Also used : StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher)

Example 4 with Hasher

use of org.apache.commons.collections4.bloomfilter.hasher.Hasher in project commons-collections by apache.

the class AbstractBloomFilterTest method andCardinalityTest.

/**
 * Tests that the andCardinality calculations are correct.
 *
 * @param filterFactory the factory function to create the filter
 */
private void andCardinalityTest(final BiFunction<Hasher, Shape, BloomFilter> filterFactory) {
    final List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
    final Hasher hasher = new StaticHasher(lst.iterator(), shape);
    final BloomFilter bf = createFilter(hasher, shape);
    final List<Integer> lst2 = Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
    final Hasher hasher2 = new StaticHasher(lst2.iterator(), shape);
    final BloomFilter bf2 = filterFactory.apply(hasher2, shape);
    assertEquals(7, bf.andCardinality(bf2));
}
Also used : StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher)

Example 5 with Hasher

use of org.apache.commons.collections4.bloomfilter.hasher.Hasher in project commons-collections by apache.

the class AbstractBloomFilterTest method constructorTest_WrongShape.

/**
 * Tests that creating a Bloom filter with a Static hasher that has one shape and a
 * different specified shape fails.
 */
@Test
public final void constructorTest_WrongShape() {
    final Shape anotherShape = new Shape(testFunctionX, 3, 72, 17);
    final List<Integer> lst = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
    final Hasher hasher = new StaticHasher(lst.iterator(), anotherShape);
    assertThrows(IllegalArgumentException.class, () -> createFilter(hasher, shape), "Should throw IllegalArgumentException");
}
Also used : StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Hasher(org.apache.commons.collections4.bloomfilter.hasher.Hasher) Shape(org.apache.commons.collections4.bloomfilter.hasher.Shape) StaticHasher(org.apache.commons.collections4.bloomfilter.hasher.StaticHasher) Test(org.junit.jupiter.api.Test)

Aggregations

Hasher (org.apache.commons.collections4.bloomfilter.hasher.Hasher)37 Test (org.junit.jupiter.api.Test)35 StaticHasher (org.apache.commons.collections4.bloomfilter.hasher.StaticHasher)34 Shape (org.apache.commons.collections4.bloomfilter.hasher.Shape)10 OfInt (java.util.PrimitiveIterator.OfInt)2 IntConsumer (java.util.function.IntConsumer)2 DynamicHasher (org.apache.commons.collections4.bloomfilter.hasher.DynamicHasher)2 IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 HashFunctionIdentity (org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity)1 MD5Cyclic (org.apache.commons.collections4.bloomfilter.hasher.function.MD5Cyclic)1 EmptyIterator (org.apache.commons.collections4.iterators.EmptyIterator)1