Collections-818: convert to characteristics flag (#329)

This commit is contained in:
Claude Warren 2022-08-16 09:54:33 +01:00 committed by GitHub
parent 032e0eade0
commit 9999261d27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 27 deletions

View File

@ -110,8 +110,8 @@ public final class ArrayCountingBloomFilter implements CountingBloomFilter {
} }
@Override @Override
public boolean isSparse() { public int characteristics() {
return true; return SPARSE;
} }
@Override @Override

View File

@ -29,6 +29,15 @@ import java.util.Objects;
*/ */
public interface BloomFilter extends IndexProducer, BitMapProducer { public interface BloomFilter extends IndexProducer, BitMapProducer {
/**
* The sparse characteristic used to determine the best method for matching.
* <p>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.</p>
*/
int SPARSE = 0x1;
/** /**
* Creates a new instance of the BloomFilter with the same properties as the current one. * Creates a new instance of the BloomFilter with the same properties as the current one.
* @return a copy of this BloomFilter * @return a copy of this BloomFilter
@ -38,17 +47,12 @@ public interface BloomFilter extends IndexProducer, BitMapProducer {
// Query Operations // Query Operations
/** /**
* This method is used to determine the best method for matching. * Returns the characteristics of the filter.
* * <p>
* <p>For `sparse` implementations * Characteristics are defined as bits within the characteristics integer.
* the {@code forEachIndex(IntConsumer consumer)} method is more efficient. For non `sparse` implementations * @return the characteristics for this bloom filter.
* 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.</p>
*
* @return {@code true} if the implementation is sparse {@code false} otherwise.
* @see BitMap
*/ */
boolean isSparse(); int characteristics();
/** /**
* Gets the shape that was used when the filter was built. * 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) { default boolean contains(BloomFilter other) {
Objects.requireNonNull(other, "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) { default boolean merge(Hasher hasher) {
Objects.requireNonNull(hasher, "hasher"); Objects.requireNonNull(hasher, "hasher");
Shape shape = getShape(); Shape shape = getShape();
// create the bloomfilter that is most likely to merge quickly with this one // create the Bloom filter that is most likely to merge quickly with this one
BloomFilter result = isSparse() ? new SparseBloomFilter(shape, hasher) : new SimpleBloomFilter(shape, hasher); BloomFilter result = (characteristics() & SPARSE) != 0 ? new SparseBloomFilter(shape, hasher) : new SimpleBloomFilter(shape, hasher);
return merge(result); return merge(result);
} }

View File

@ -65,7 +65,7 @@ public final class SimpleBloomFilter implements BloomFilter {
this.shape = other.getShape(); this.shape = other.getShape();
this.bitMap = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())]; this.bitMap = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())];
this.cardinality = 0; this.cardinality = 0;
if (other.isSparse()) { if ((other.characteristics() & SPARSE) != 0) {
merge((IndexProducer) other); merge((IndexProducer) other);
} else { } else {
merge((BitMapProducer) other); merge((BitMapProducer) other);
@ -194,7 +194,7 @@ public final class SimpleBloomFilter implements BloomFilter {
@Override @Override
public boolean merge(BloomFilter other) { public boolean merge(BloomFilter other) {
Objects.requireNonNull(other, "other"); Objects.requireNonNull(other, "other");
if (other.isSparse()) { if ((other.characteristics() & SPARSE) != 0) {
merge((IndexProducer) other); merge((IndexProducer) other);
} else { } else {
merge((BitMapProducer) other); merge((BitMapProducer) other);
@ -208,8 +208,8 @@ public final class SimpleBloomFilter implements BloomFilter {
} }
@Override @Override
public boolean isSparse() { public int characteristics() {
return false; return 0;
} }
@Override @Override

View File

@ -58,7 +58,7 @@ public final class SparseBloomFilter implements BloomFilter {
Objects.requireNonNull(other, "other"); Objects.requireNonNull(other, "other");
this.shape = other.getShape(); this.shape = other.getShape();
this.indices = new TreeSet<>(); this.indices = new TreeSet<>();
if (other.isSparse()) { if ((other.characteristics() & SPARSE) != 0) {
merge((IndexProducer) other); merge((IndexProducer) other);
} else { } else {
merge(IndexProducer.fromBitMapProducer(other)); merge(IndexProducer.fromBitMapProducer(other));
@ -169,7 +169,7 @@ public final class SparseBloomFilter implements BloomFilter {
@Override @Override
public boolean merge(BloomFilter other) { public boolean merge(BloomFilter other) {
Objects.requireNonNull(other, "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); merge(producer);
return true; return true;
} }
@ -180,8 +180,8 @@ public final class SparseBloomFilter implements BloomFilter {
} }
@Override @Override
public boolean isSparse() { public int characteristics() {
return true; return SPARSE;
} }
@Override @Override

View File

@ -186,8 +186,8 @@ public class DefaultBloomFilterTest extends AbstractBloomFilterTest<DefaultBloom
} }
@Override @Override
public boolean isSparse() { public int characteristics() {
return true; return SPARSE;
} }
@Override @Override
@ -217,8 +217,8 @@ public class DefaultBloomFilterTest extends AbstractBloomFilterTest<DefaultBloom
} }
@Override @Override
public boolean isSparse() { public int characteristics() {
return false; return 0;
} }
@Override @Override