From d83704ab30de5799fc88e82bd0a1db10e152d57f Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Wed, 29 Jun 2022 09:54:17 +0100 Subject: [PATCH] moved ArrayTracker and BitMapTracker to their own files. --- .../bloomfilter/ArrayTracker.java | 51 +++++++++++++++++ .../bloomfilter/BitMapTracker.java | 43 ++++++++++++++ .../collections4/bloomfilter/Hasher.java | 56 ------------------- .../bloomfilter/ArrayTrackerTest.java | 1 - .../bloomfilter/BitMapTrackerTest.java | 1 - .../bloomfilter/IndexFilterTest.java | 2 - 6 files changed, 94 insertions(+), 60 deletions(-) create mode 100644 src/main/java/org/apache/commons/collections4/bloomfilter/ArrayTracker.java create mode 100644 src/main/java/org/apache/commons/collections4/bloomfilter/BitMapTracker.java diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayTracker.java b/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayTracker.java new file mode 100644 index 000000000..bffb67f85 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/ArrayTracker.java @@ -0,0 +1,51 @@ +/* + * 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.function.IntPredicate; + +/** + * An IndexTracker implementation that uses an array of integers to track whether or not a + * number has been seen. Suitable for Shapes that have few hash functions. + * @since 4.5 + */ +public class ArrayTracker implements IntPredicate { + private int[] seen; + private int populated; + + /** + * Constructs the tracker based on the shape. + * @param shape the shape to build the tracker for. + */ + ArrayTracker(Shape shape) { + seen = new int[shape.getNumberOfHashFunctions()]; + } + + @Override + public boolean test(int number) { + if (number < 0) { + throw new IndexOutOfBoundsException("number may not be less than zero. " + number); + } + for (int i = 0; i < populated; i++) { + if (seen[i] == number) { + return false; + } + } + seen[populated++] = number; + return true; + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/BitMapTracker.java b/src/main/java/org/apache/commons/collections4/bloomfilter/BitMapTracker.java new file mode 100644 index 000000000..9b082169d --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/BitMapTracker.java @@ -0,0 +1,43 @@ +/* + * 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.function.IntPredicate; + +/** + * An IndexTracker implementation that uses an array of bit maps to track whether or not a + * number has been seen. + * @since 4.5 + */ +public class BitMapTracker implements IntPredicate { + private long[] bits; + + /** + * Constructs a bit map based tracker for the specified shape. + * @param shape The shape that is being generated. + */ + BitMapTracker(Shape shape) { + bits = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())]; + } + + @Override + public boolean test(int number) { + boolean retval = !BitMap.contains(bits, number); + BitMap.set(bits, number); + return retval; + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/commons/collections4/bloomfilter/Hasher.java b/src/main/java/org/apache/commons/collections4/bloomfilter/Hasher.java index 3afd9fbe0..7ffa39f96 100644 --- a/src/main/java/org/apache/commons/collections4/bloomfilter/Hasher.java +++ b/src/main/java/org/apache/commons/collections4/bloomfilter/Hasher.java @@ -113,61 +113,5 @@ public interface Hasher { } return tracker.test(number) ? consumer.test(number) : true; } - - /** - * An IndexTracker implementation that uses an array of integers to track whether or not a - * number has been seen. Suitable for Shapes that have few hash functions. - * @since 4.5 - */ - static class ArrayTracker implements IntPredicate { - private int[] seen; - private int populated; - - /** - * Constructs the tracker based on the shape. - * @param shape the shape to build the tracker for. - */ - ArrayTracker(Shape shape) { - seen = new int[shape.getNumberOfHashFunctions()]; - } - - @Override - public boolean test(int number) { - if (number < 0) { - throw new IndexOutOfBoundsException("number may not be less than zero. " + number); - } - for (int i = 0; i < populated; i++) { - if (seen[i] == number) { - return false; - } - } - seen[populated++] = number; - return true; - } - } - - /** - * An IndexTracker implementation that uses an array of bit maps to track whether or not a - * number has been seen. - * @since 4.5 - */ - static class BitMapTracker implements IntPredicate { - private long[] bits; - - /** - * Constructs a bit map based tracker for the specified shape. - * @param shape The shape that is being generated. - */ - BitMapTracker(Shape shape) { - bits = new long[BitMap.numberOfBitMaps(shape.getNumberOfBits())]; - } - - @Override - public boolean test(int number) { - boolean retval = !BitMap.contains(bits, number); - BitMap.set(bits, number); - return retval; - } - } } } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/ArrayTrackerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/ArrayTrackerTest.java index d34050540..5b3080a11 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/ArrayTrackerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/ArrayTrackerTest.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.function.IntPredicate; -import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.ArrayTracker; import org.junit.jupiter.api.Test; /** diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTrackerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTrackerTest.java index 8d6df5c3d..f1dac177e 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTrackerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/BitMapTrackerTest.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.function.IntPredicate; -import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.BitMapTracker; import org.junit.jupiter.api.Test; /** diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java index 80c93c144..d61f58eba 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/IndexFilterTest.java @@ -29,8 +29,6 @@ import java.util.SplittableRandom; import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter; -import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.ArrayTracker; -import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.BitMapTracker; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource;