Claudenw commented on a change in pull request #258: URL: https://github.com/apache/commons-collections/pull/258#discussion_r798307007
########## 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 name of the method is to differentiate it from the standard `merge()` method where the `merge()` returns a new Bloom filter, this method modifies the internal representation to be the result of the merge. It is a semantic statement from the outside looking in. I agree a true modification of the internal structure would make more sense. I will take that onboard and do more work on this section. -- 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]
