Claudenw commented on a change in pull request #258:
URL: 
https://github.com/apache/commons-collections/pull/258#discussion_r798879388



##########
File path: 
src/main/java/org/apache/commons/collections4/bloomfilter/SimpleBloomFilter.java
##########
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.collections4.bloomfilter;
+
+import java.util.Objects;
+import java.util.function.IntPredicate;
+import java.util.function.LongPredicate;
+
+import org.apache.commons.collections4.bloomfilter.hasher.Hasher;
+
+/**
+ * A bloom filter using an array of BitMaps to track enabled bits. This is a 
standard
+ * implementation and should work well for most Bloom filters.
+ * @since 4.5
+ */
+public class SimpleBloomFilter implements BloomFilter {
+
+    /**
+     * The array of BitMap longs that defines this Bloom filter.
+     */
+    private long[] bitMap;
+
+    /**
+     * The Shape of this Bloom filter
+     */
+    private final Shape shape;
+
+    /**
+     * The cardinality of this Bloom filter.
+     */
+    private int cardinality;
+
+    /**
+     * Constructs an empty SimpleBloomFilter.
+     *
+     * @param shape The shape for the filter.
+     */
+    public SimpleBloomFilter(Shape shape) {
+        Objects.requireNonNull(shape, "shape");
+        this.shape = shape;
+        this.bitMap = new long[0];
+        this.cardinality = 0;
+    }
+
+    /**
+     * Constructor.
+     * @param shape The shape for the filter.
+     * @param hasher the Hasher to initialize the filter with.
+     */
+    public SimpleBloomFilter(final Shape shape, Hasher hasher) {
+        Objects.requireNonNull(shape, "shape");
+        Objects.requireNonNull(hasher, "hasher");
+        this.shape = shape;
+        this.bitMap = new long[0];
+        mergeInPlace(hasher);
+    }
+
+    /**
+     * Constructor.
+     * @param shape The shape for the filter.
+     * @param producer the BitMap Producer to initialize the filter with.
+     * @throws IllegalArgumentException if the producer returns too many bit 
maps.
+     */
+    public SimpleBloomFilter(final Shape shape, BitMapProducer producer) {
+        Objects.requireNonNull(shape, "shape");
+        Objects.requireNonNull(producer, "producer");
+        this.shape = shape;
+
+        BitMapProducer.ArrayBuilder builder = new 
BitMapProducer.ArrayBuilder(shape);
+        try {
+            producer.forEachBitMap(builder);
+            this.bitMap = builder.getArray();
+        } catch (IndexOutOfBoundsException e) {
+            throw new IllegalArgumentException(String.format("BitMapProducer 
should only send %s maps",
+                    BitMap.numberOfBitMaps(shape.getNumberOfBits())), e);
+        }
+        this.cardinality = -1;
+    }
+
+    @Override
+    public boolean mergeInPlace(Hasher hasher) {
+        Objects.requireNonNull(hasher, "hasher");
+        Shape shape = getShape();
+
+        hasher.indices(shape).forEachIndex(idx -> {
+            int lidx = BitMap.getLongIndex(idx);
+            if (bitMap.length <= lidx) {
+                long[] newMap = new long[lidx + 1];
+                System.arraycopy(bitMap, 0, newMap, 0, bitMap.length);
+                bitMap = newMap;
+            }
+            BitMap.set(bitMap, idx);
+            return true;
+        });
+        this.cardinality = -1;
+        return true;
+    }
+
+    @Override
+    public boolean mergeInPlace(BloomFilter other) {
+        Objects.requireNonNull(other, "other");
+        BitMapProducer.ArrayBuilder builder = new 
BitMapProducer.ArrayBuilder(shape, this.bitMap);

Review comment:
       The process can't just "iterate on the other filter and merge additional 
bit maps" because the BitMapProducer is allowed to not emit zero based values 
at the end to the list of bit maps.  But there is a short cut here if we keep 
that in mind.
   
   I think that BloomFilter might benefit from a method to apply a 
BitMapProducer and a LongBiFunction.  I need a function like this in the 
multidimensional Bloom filter project.  Currently I go the long way around and 
create the array of longs and then manipulate that.  What I see is a method 
like:
   
   ```
       LongPredicate apply( LongBiFunction func ) {
           LongPredicate result = new LongPredicate {
               int i=0;
               boolean test( long other ) {
                   return func( bitmap[i++], other );
               }                
          }
   ```
   
   Then it can be used like
   ```
   BloomFilter filter = ....;
   BitMapProducer producer = ....;
   
   if ( producer.forEachBitMap( filter.apply( (x,y) -> x & y == y ))) {
       System.out.println( "Bloom filter match" );
   }
   if (producer.forEachBitMap( filter.apply( (x,y) -> x == y ))) {
       System.out.println( "Exact match" );
   }
   ```
   
   I perhaps this belongs in BitMapProducer.  So I am not certain about where 
to place the method, but I think that the method is needed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to