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



##########
File path: 
src/main/java/org/apache/commons/collections4/bloomfilter/SparseBloomFilter.java
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.List;
+import java.util.Objects;
+import java.util.TreeSet;
+import java.util.function.IntPredicate;
+import java.util.function.LongPredicate;
+
+import org.apache.commons.collections4.bloomfilter.hasher.Hasher;
+
+/**
+ * A bloom filter using a TreeSet of integers to track enabled bits. This is a 
standard
+ * implementation and should work well for most low cardinality Bloom filters.
+ * @since 4.5
+ */
+public class SparseBloomFilter implements BloomFilter {
+
+    /**
+     * The bitSet that defines this BloomFilter.
+     */
+    private final TreeSet<Integer> indices;
+
+    /**
+     * The shape of this BloomFilter
+     */
+    private final Shape shape;
+
+    /**
+     * Constructs an empty BitSetBloomFilter.
+     *
+     * @param shape The shape of the filter.
+     */
+    public SparseBloomFilter(Shape shape) {
+        Objects.requireNonNull(shape, "shape");
+        this.shape = shape;
+        this.indices = new TreeSet<Integer>();
+    }
+
+    /**
+     * Addes the index to the indices.
+     * @param idx the index to add.
+     * @return {@code true} always
+     */
+    private boolean add(int idx) {
+        indices.add(idx);
+        return true;
+    }
+
+    /**
+     * Constructs a populated Bloom filter.
+     * @param shape the shape for the bloom filter.
+     * @param hasher the hasher to provide the initial data.
+     */
+    public SparseBloomFilter(final Shape shape, Hasher hasher) {
+        this(shape);
+        Objects.requireNonNull(hasher, "hasher");
+        hasher.indices(shape).forEachIndex(this::add);
+    }
+
+    /**
+     * Constructs a populated Bloom filter.
+     * @param shape the shape of the filter.
+     * @param indices a list of indices to to enable.
+     * @throws IllegalArgumentException if indices contains a value greater 
than the number
+     * of bits in the shape.
+     */
+    public SparseBloomFilter(Shape shape, List<Integer> indices) {
+        this(shape);
+        Objects.requireNonNull(indices, "indices");
+        this.indices.addAll(indices);
+        if (!this.indices.isEmpty()) {
+            if (this.indices.last() >= shape.getNumberOfBits()) {
+                throw new IllegalArgumentException(String.format("Value in 
list %s is greater than maximum value (%s)",
+                        this.indices.last(), shape.getNumberOfBits()));
+            }
+            if (this.indices.first() < 0) {
+                throw new IllegalArgumentException(
+                        String.format("Value in list %s is less than 0", 
this.indices.first()));
+            }
+        }
+    }
+
+    /**
+     * Constructs a populated Bloom filter.
+     * @param shape the shape of the filter.
+     * @param indices an index producer for the indices to to enable.
+     * @throws IllegalArgumentException if indices contains a value greater 
than the number
+     * of bits in the shape.
+     */
+    public SparseBloomFilter(Shape shape, IndexProducer indices) {

Review comment:
       The following constructors are now defined:
   
   ```
   public SparseBloomFilter(Shape shape)
   public SparseBloomFilter(BloomFilter bloomFilter)
   public SparseBloomFilter(final Shape shape, Hasher hasher)
   public SparseBloomFilter(Shape shape, BitMapProducer bitMaps)
   public SparseBloomFilter(Shape shape, IndexProducer indices)
   
   public SimpleBloomFilter(Shape shape)
   public SimpleBloomFilter(BloomFilter bloomFilter)
   public SimpleBloomFilter(final Shape shape, Hasher hasher)
   public SimpleBloomFilter(Shape shape, BitMapProducer bitMaps)
   public SimpleBloomFilter(Shape shape, IndexProducer indices)
   ```
   




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to