Skip to content
Permalink
Browse files
Merge pull request #315 from Claudenw/remove_merge_in_place
replaced merge with old mergeInPlace
  • Loading branch information
Claudenw committed Jul 28, 2022
2 parents ef728ec + c104fd4 commit ebc66db42635b901a5dce7f5286ba3b2427fc119
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 178 deletions.
@@ -120,28 +120,7 @@ public int cardinality() {
}

@Override
public CountingBloomFilter merge(BloomFilter other) {
Objects.requireNonNull(other, "other");
CountingBloomFilter filter = copy();
filter.add(BitCountProducer.from(other));
return filter;
}

@Override
public CountingBloomFilter merge(Hasher hasher) {
Objects.requireNonNull(hasher, "hasher");
ArrayCountingBloomFilter filter = copy();
try {
filter.add(BitCountProducer.from(hasher.uniqueIndices(shape)));
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException(
String.format("Filter only accepts values in the [0,%d) range", shape.getNumberOfBits()));
}
return filter;
}

@Override
public boolean mergeInPlace(final BloomFilter other) {
public boolean merge(final BloomFilter other) {
Objects.requireNonNull(other, "other");
try {
return add(BitCountProducer.from(other));
@@ -151,7 +130,7 @@ public boolean mergeInPlace(final BloomFilter other) {
}

@Override
public boolean mergeInPlace(final Hasher hasher) {
public boolean merge(final Hasher hasher) {
Objects.requireNonNull(hasher, "hasher");
try {
return add(BitCountProducer.from(hasher.uniqueIndices(shape)));
@@ -112,38 +112,6 @@ default boolean contains(BitMapProducer bitMapProducer) {

// update operations

/**
* Merges the specified Bloom filter with this Bloom filter creating a new Bloom filter.
*
* <p>Specifically all bit indexes that are enabled in the {@code other} and in @code this} filter will be
* enabled in the resulting filter.</p>
*
* @param other the other Bloom filter
* @return The new Bloom filter.
*/
default BloomFilter merge(BloomFilter other) {
Objects.requireNonNull(other, "other");
BloomFilter result = copy();
result.mergeInPlace(other);
return result;
}

/**
* Merges the specified Hasher with this Bloom filter and returns a new Bloom filter.
*
* <p>Specifically all bit indexes that are identified by the {@code hasher} and in {@code this} Bloom filter
* be enabled in the resulting filter.</p>
*
* @param hasher the hasher to provide the indices
* @return the new Bloom filter.
*/
default BloomFilter merge(Hasher hasher) {
Objects.requireNonNull(hasher, "hasher");
BloomFilter result = copy();
result.mergeInPlace(hasher);
return result;
}

/**
* Merges the specified Bloom filter into this Bloom filter.
*
@@ -158,7 +126,7 @@ default BloomFilter merge(Hasher hasher) {
* @param other The bloom filter to merge into this one.
* @return true if the merge was successful
*/
boolean mergeInPlace(BloomFilter other);
boolean merge(BloomFilter other);

/**
* Merges the specified hasher into this Bloom filter. Specifically all
@@ -172,12 +140,12 @@ default BloomFilter merge(Hasher hasher) {
* @param hasher The hasher to merge.
* @return true if the merge was successful
*/
default boolean mergeInPlace(Hasher hasher) {
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);
return mergeInPlace(result);
return merge(result);
}

// Counting Operations
@@ -231,7 +199,9 @@ default int estimateN() {
*/
default int estimateUnion(BloomFilter other) {
Objects.requireNonNull(other, "other");
return this.merge(other).estimateN();
BloomFilter cpy = this.copy();
cpy.merge(other);
return cpy.estimateN();
}

/**
@@ -144,38 +144,11 @@ public interface CountingBloomFilter extends BloomFilter, BitCountProducer {
*/
boolean subtract(BitCountProducer other);

/**
* Merges the specified Bloom filter into this Bloom filter to produce a new CountingBloomFilter.
*
* <p>Specifically the new Bloom filter will contain all the counts of this filter and in addition
* all bit indexes that are enabled in the {@code other} filter will be incremented
* by one in the new filter.</p>
*
* <p>Note: the validity of the resulting filter is not guaranteed. When in doubt {@code isValid()}
* should be called on the new filter.</p>
*
* @param other the other Bloom filter
* @return A new CountingBloomFilter instance.
*/
@Override
CountingBloomFilter merge(BloomFilter other);

/**
* Merges the specified hasher with this Bloom filter to create a new CountingBloomFilter.
*
* <p>Specifically the new Bloom filter will contain all the counts of this filter and in addition
* all bit indexes specified by the {@code hasher} will be incremented
* by one in the new filter.</p>
*
* <p>For HasherCollections each enclosed Hasher will be considered a single item and increment
* the counts separately.</p>
*
* <p>Note: the validity of the resulting filter is not guaranteed. When in doubt {@code isValid()}
* should be called on the new filter.</p>
*
* @param hasher the hasher to provide the indexes
* @return A new CountingBloomFilter instance.
* Creates a new instance of the CountingBloomFilter with the same properties as the current one.
* @return a copy of this CountingBloomFilter
*/
@Override
CountingBloomFilter merge(Hasher hasher);
CountingBloomFilter copy();
}
@@ -66,9 +66,9 @@ public SimpleBloomFilter(BloomFilter other) {
this.bitMap = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())];
this.cardinality = 0;
if (other.isSparse()) {
mergeInPlace((IndexProducer) other);
merge((IndexProducer) other);
} else {
mergeInPlace((BitMapProducer) other);
merge((BitMapProducer) other);
}
}

@@ -80,7 +80,7 @@ public SimpleBloomFilter(BloomFilter other) {
public SimpleBloomFilter(final Shape shape, Hasher hasher) {
this(shape);
Objects.requireNonNull(hasher, "hasher");
mergeInPlace(hasher);
merge(hasher);
}

/**
@@ -92,7 +92,7 @@ public SimpleBloomFilter(final Shape shape, Hasher hasher) {
public SimpleBloomFilter(final Shape shape, IndexProducer indices) {
this(shape);
Objects.requireNonNull(indices, "indices");
mergeInPlace(indices);
merge(indices);
}

/**
@@ -104,7 +104,7 @@ public SimpleBloomFilter(final Shape shape, IndexProducer indices) {
public SimpleBloomFilter(final Shape shape, BitMapProducer bitMaps) {
this(shape);
Objects.requireNonNull(bitMaps, "bitMaps");
mergeInPlace(bitMaps);
merge(bitMaps);
}

/**
@@ -134,11 +134,11 @@ public SimpleBloomFilter copy() {
}

/**
* Performs a merge in place using an IndexProducer.
* Performs a merge using an IndexProducer.
* @param indexProducer the IndexProducer to merge from.
* @throws IllegalArgumentException if producer sends illegal value.
*/
private void mergeInPlace(IndexProducer indexProducer) {
private void merge(IndexProducer indexProducer) {
indexProducer.forEachIndex(idx -> {
if (idx < 0 || idx >= shape.getNumberOfBits()) {
throw new IllegalArgumentException(String.format(
@@ -151,11 +151,11 @@ private void mergeInPlace(IndexProducer indexProducer) {
}

/**
* Performs a merge in place using an BitMapProducer.
* Performs a merge using an BitMapProducer.
* @param bitMapProducer the BitMapProducer to merge from.
* @throws IllegalArgumentException if producer sends illegal value.
*/
private void mergeInPlace(BitMapProducer bitMapProducer) {
private void merge(BitMapProducer bitMapProducer) {
try {
int[] idx = new int[1];
bitMapProducer.forEachBitMap(value -> {
@@ -185,19 +185,19 @@ private void mergeInPlace(BitMapProducer bitMapProducer) {
}

@Override
public boolean mergeInPlace(Hasher hasher) {
public boolean merge(Hasher hasher) {
Objects.requireNonNull(hasher, "hasher");
mergeInPlace(hasher.indices(shape));
merge(hasher.indices(shape));
return true;
}

@Override
public boolean mergeInPlace(BloomFilter other) {
public boolean merge(BloomFilter other) {
Objects.requireNonNull(other, "other");
if (other.isSparse()) {
mergeInPlace((IndexProducer) other);
merge((IndexProducer) other);
} else {
mergeInPlace((BitMapProducer) other);
merge((BitMapProducer) other);
}
return true;
}
@@ -59,9 +59,9 @@ public SparseBloomFilter(BloomFilter other) {
this.shape = other.getShape();
this.indices = new TreeSet<>();
if (other.isSparse()) {
mergeInPlace((IndexProducer) other);
merge((IndexProducer) other);
} else {
mergeInPlace(IndexProducer.fromBitMapProducer(other));
merge(IndexProducer.fromBitMapProducer(other));
}
}

@@ -108,7 +108,7 @@ public SparseBloomFilter(Shape shape, IndexProducer indices) {
public SparseBloomFilter(Shape shape, BitMapProducer bitMaps) {
this(shape);
Objects.requireNonNull(bitMaps, "bitMaps");
mergeInPlace(IndexProducer.fromBitMapProducer(bitMaps));
merge(IndexProducer.fromBitMapProducer(bitMaps));
}

private SparseBloomFilter(SparseBloomFilter source) {
@@ -141,11 +141,11 @@ private boolean add(int idx) {
}

/**
* Performs a merge in place using an IndexProducer.
* Performs a merge using an IndexProducer.
* @param indexProducer the IndexProducer to merge from.
* @throws IllegalArgumentException if producer sends illegal value.
*/
private void mergeInPlace(IndexProducer indexProducer) {
private void merge(IndexProducer indexProducer) {
indexProducer.forEachIndex(this::add);
if (!this.indices.isEmpty()) {
if (this.indices.last() >= shape.getNumberOfBits()) {
@@ -160,17 +160,17 @@ private void mergeInPlace(IndexProducer indexProducer) {
}

@Override
public boolean mergeInPlace(Hasher hasher) {
public boolean merge(Hasher hasher) {
Objects.requireNonNull(hasher, "hasher");
mergeInPlace(hasher.indices(shape));
merge(hasher.indices(shape));
return true;
}

@Override
public boolean mergeInPlace(BloomFilter other) {
public boolean merge(BloomFilter other) {
Objects.requireNonNull(other, "other");
IndexProducer producer = other.isSparse() ? (IndexProducer) other : IndexProducer.fromBitMapProducer(other);
mergeInPlace(producer);
merge(producer);
return true;
}

0 comments on commit ebc66db

Please sign in to comment.