COLLECTIONS-831: Add BloomFilter clear() method
This commit is contained in:
parent
fe783da49f
commit
5a31023eae
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.apache.commons.collections4.bloomfilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.function.LongPredicate;
|
||||
|
@ -104,6 +105,11 @@ public final class ArrayCountingBloomFilter implements CountingBloomFilter {
|
|||
this.counts = source.counts.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
Arrays.fill(counts, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayCountingBloomFilter copy() {
|
||||
return new ArrayCountingBloomFilter(this);
|
||||
|
|
|
@ -60,6 +60,11 @@ public interface BloomFilter extends IndexProducer, BitMapProducer {
|
|||
*/
|
||||
Shape getShape();
|
||||
|
||||
/**
|
||||
* Resets the filter to its initial, unpopulated state.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this filter contains the specified filter.
|
||||
*
|
||||
|
|
|
@ -117,6 +117,12 @@ public final class SimpleBloomFilter implements BloomFilter {
|
|||
this.cardinality = source.cardinality;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
Arrays.fill(bitMap, 0L);
|
||||
cardinality = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long[] asBitMapArray() {
|
||||
return Arrays.copyOf(bitMap, bitMap.length);
|
||||
|
|
|
@ -174,6 +174,11 @@ public final class SparseBloomFilter implements BloomFilter {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
indices.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Shape getShape() {
|
||||
return shape;
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.commons.collections4.bloomfilter;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
@ -178,6 +179,14 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> {
|
|||
assertTrue(bf4.contains(bf1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
BloomFilter bf1 = createFilter(getTestShape(), from1);
|
||||
assertNotEquals(0, bf1.cardinality());
|
||||
bf1.clear();
|
||||
assertEquals(0, bf1.cardinality());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the andCardinality calculations are correct.
|
||||
*
|
||||
|
|
|
@ -112,6 +112,11 @@ public class DefaultBloomFilterTest extends AbstractBloomFilterTest<DefaultBloom
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
indices.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean forEachIndex(IntPredicate consumer) {
|
||||
for (Integer i : indices) {
|
||||
|
|
Loading…
Reference in New Issue