moved ArrayTracker and BitMapTracker to their own files.

This commit is contained in:
Claude Warren 2022-06-29 09:54:17 +01:00
parent a79ea005b1
commit d83704ab30
6 changed files with 94 additions and 60 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -113,61 +113,5 @@ public interface Hasher {
} }
return tracker.test(number) ? consumer.test(number) : true; 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;
}
}
} }
} }

View File

@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.ArrayTracker;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**

View File

@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter.BitMapTracker;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**

View File

@ -29,8 +29,6 @@ import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.collections4.bloomfilter.Hasher.IndexFilter; 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.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;