From 7732ede65ad50bc66e85088a0f7c82e4483b50f4 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Fri, 7 Jul 2023 18:14:42 +0200 Subject: [PATCH] Made flatten() part of BloomFilterProducer --- .../bloomfilter/BloomFilterProducer.java | 17 +++++++++++++++++ .../bloomfilter/WrappedBloomFilter.java | 5 +++++ .../AbstractBloomFilterProducerTest.java | 10 +++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilterProducer.java b/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilterProducer.java index 61692a5e69..908fc137d4 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilterProducer.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/BloomFilterProducer.java @@ -68,6 +68,23 @@ default boolean forEachBloomFilterPair(final BloomFilterProducer other, return other.forEachBloomFilter(p) && p.forEachRemaining(); } + /** + * Create a standard (non-layered) Bloom filter by merging all of the layers. If + * the filter is empty this method will return an empty Bloom filter. + * + * @return the merged bloom filter. + */ + default BloomFilter flatten() { + BloomFilter bf[] = {null}; + forEachBloomFilter( x -> { + if (bf[0]==null) { + bf[0] = new SimpleBloomFilter( x.getShape()); + } + return bf[0].merge( x ); + }); + return bf[0]; + } + /** * Creates a BloomFilterProducer from an array of Bloom filters. * diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java b/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java index 8e8ce99b7a..cff65d6e34 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/WrappedBloomFilter.java @@ -27,6 +27,11 @@ public abstract class WrappedBloomFilter implements BloomFilter { final BloomFilter wrapped; + /** + * Wraps a Bloom filter. The wrapped filter is maintained as a reference + * not a copy. Changes in one will be reflected in the other. + * @param bf The Bloom filter. + */ public WrappedBloomFilter(BloomFilter bf) { this.wrapped = bf; } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterProducerTest.java index cf4bb7b1c1..b7ad6bca4c 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterProducerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterProducerTest.java @@ -51,7 +51,7 @@ public void setup() { one.clear(); one.merge(IndexProducer.fromIndexArray(1)); two.clear(); - two.merge(IndexProducer.fromIndexArray(1, 2)); + two.merge(IndexProducer.fromIndexArray(2, 3)); nullCount[0] = 0; nullCount[1] = 0; equalityCount[0] = 0; @@ -123,4 +123,12 @@ public void testForEachPairReturnFalseEarly() { assertFalse(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one, two, one), (x, y) -> false)); } + + @Test + public void testFlatten() { + BloomFilter underTest = createUnderTest().flatten(); + BloomFilter expected = new SimpleBloomFilter(shape); + expected.merge(IndexProducer.fromIndexArray(1, 2, 3)); + assertArrayEquals(expected.asBitMapArray(), underTest.asBitMapArray()); + } }