From 9999261d2757298739a7e5d92ce282ae21dad6b7 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Tue, 16 Aug 2022 09:54:33 +0100 Subject: [PATCH] Collections-818: convert to characteristics flag (#329) --- .../bloomfilter/ArrayCountingBloomFilter.java | 4 +-- .../collections4/bloomfilter/BloomFilter.java | 30 +++++++++++-------- .../bloomfilter/SimpleBloomFilter.java | 8 ++--- .../bloomfilter/SparseBloomFilter.java | 8 ++--- .../bloomfilter/DefaultBloomFilterTest.java | 8 ++--- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayCountingBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayCountingBloomFilter.java index a46d7665f..4202f6d86 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayCountingBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayCountingBloomFilter.java @@ -110,8 +110,8 @@ public final class ArrayCountingBloomFilter implements CountingBloomFilter { } @Override - public boolean isSparse() { - return true; + public int characteristics() { + return SPARSE; } @Override diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilter.java index e13260b99..58ed6bcb5 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilter.java @@ -29,6 +29,15 @@ import java.util.Objects; */ public interface BloomFilter extends IndexProducer, BitMapProducer { + /** + * The sparse characteristic used to determine the best method for matching. + *

For `sparse` implementations + * the {@code forEachIndex(IntConsumer consumer)} method is more efficient. For non `sparse` implementations + * the {@code forEachBitMap(LongConsumer consumer)} is more efficient. Implementers should determine if it is easier + * for the implementation to produce indexes of bit map blocks.

+ */ + int SPARSE = 0x1; + /** * Creates a new instance of the BloomFilter with the same properties as the current one. * @return a copy of this BloomFilter @@ -38,17 +47,12 @@ public interface BloomFilter extends IndexProducer, BitMapProducer { // Query Operations /** - * This method is used to determine the best method for matching. - * - *

For `sparse` implementations - * the {@code forEachIndex(IntConsumer consumer)} method is more efficient. For non `sparse` implementations - * the {@code forEachBitMap(LongConsumer consumer)} is more efficient. Implementers should determine if it is easier - * for the implementation to produce indexes of bit map blocks.

- * - * @return {@code true} if the implementation is sparse {@code false} otherwise. - * @see BitMap + * Returns the characteristics of the filter. + *

+ * Characteristics are defined as bits within the characteristics integer. + * @return the characteristics for this bloom filter. */ - boolean isSparse(); + int characteristics(); /** * Gets the shape that was used when the filter was built. @@ -69,7 +73,7 @@ public interface BloomFilter extends IndexProducer, BitMapProducer { */ default boolean contains(BloomFilter other) { Objects.requireNonNull(other, "other"); - return isSparse() ? contains((IndexProducer) other) : contains((BitMapProducer) other); + return (characteristics() & SPARSE) != 0 ? contains((IndexProducer) other) : contains((BitMapProducer) other); } /** @@ -143,8 +147,8 @@ public interface BloomFilter extends IndexProducer, BitMapProducer { default boolean merge(Hasher hasher) { Objects.requireNonNull(hasher, "hasher"); Shape shape = getShape(); - // create the bloomfilter that is most likely to merge quickly with this one - BloomFilter result = isSparse() ? new SparseBloomFilter(shape, hasher) : new SimpleBloomFilter(shape, hasher); + // create the Bloom filter that is most likely to merge quickly with this one + BloomFilter result = (characteristics() & SPARSE) != 0 ? new SparseBloomFilter(shape, hasher) : new SimpleBloomFilter(shape, hasher); return merge(result); } diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java index b78ca999a..c351f40c1 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java @@ -65,7 +65,7 @@ public final class SimpleBloomFilter implements BloomFilter { this.shape = other.getShape(); this.bitMap = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())]; this.cardinality = 0; - if (other.isSparse()) { + if ((other.characteristics() & SPARSE) != 0) { merge((IndexProducer) other); } else { merge((BitMapProducer) other); @@ -194,7 +194,7 @@ public final class SimpleBloomFilter implements BloomFilter { @Override public boolean merge(BloomFilter other) { Objects.requireNonNull(other, "other"); - if (other.isSparse()) { + if ((other.characteristics() & SPARSE) != 0) { merge((IndexProducer) other); } else { merge((BitMapProducer) other); @@ -208,8 +208,8 @@ public final class SimpleBloomFilter implements BloomFilter { } @Override - public boolean isSparse() { - return false; + public int characteristics() { + return 0; } @Override diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/SparseBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/SparseBloomFilter.java index 794e3bcda..9cfa034eb 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/SparseBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/SparseBloomFilter.java @@ -58,7 +58,7 @@ public final class SparseBloomFilter implements BloomFilter { Objects.requireNonNull(other, "other"); this.shape = other.getShape(); this.indices = new TreeSet<>(); - if (other.isSparse()) { + if ((other.characteristics() & SPARSE) != 0) { merge((IndexProducer) other); } else { merge(IndexProducer.fromBitMapProducer(other)); @@ -169,7 +169,7 @@ public final class SparseBloomFilter implements BloomFilter { @Override public boolean merge(BloomFilter other) { Objects.requireNonNull(other, "other"); - IndexProducer producer = other.isSparse() ? (IndexProducer) other : IndexProducer.fromBitMapProducer(other); + IndexProducer producer = (other.characteristics() & SPARSE) != 0 ? (IndexProducer) other : IndexProducer.fromBitMapProducer(other); merge(producer); return true; } @@ -180,8 +180,8 @@ public final class SparseBloomFilter implements BloomFilter { } @Override - public boolean isSparse() { - return true; + public int characteristics() { + return SPARSE; } @Override diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBloomFilterTest.java index 26862bb19..ff258ae80 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBloomFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBloomFilterTest.java @@ -186,8 +186,8 @@ public class DefaultBloomFilterTest extends AbstractBloomFilterTest