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
public boolean isSparse() {
return true;
public int characteristics() {
return SPARSE;
}
@Override

View File

@ -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.
* <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.
* @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.
*
* <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>
*
* @return {@code true} if the implementation is sparse {@code false} otherwise.
* @see BitMap
* Returns the characteristics of the filter.
* <p>
* 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);
}

View File

@ -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

View File

@ -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

View File

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