IndexFilter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.bloomfilter;

  18. import java.util.function.IntPredicate;

  19. /**
  20.  * A convenience class for Hasher implementations to filter out duplicate indices.
  21.  *
  22.  * <p><em>If the index is negative the behavior is not defined.</em></p>
  23.  *
  24.  * <p>This is conceptually a unique filter implemented as an {@link IntPredicate}.</p>
  25.  *
  26.  * @since 4.5.0-M1
  27.  */
  28. public final class IndexFilter {

  29.     /**
  30.      * An IndexTracker implementation that uses an array of integers to track whether or not a
  31.      * number has been seen. Suitable for Shapes that have few hash functions.
  32.      * @since 4.5.0
  33.      */
  34.     static class ArrayTracker implements IntPredicate {
  35.         private final int[] seen;
  36.         private int populated;

  37.         /**
  38.          * Constructs the tracker based on the shape.
  39.          * @param shape the shape to build the tracker for.
  40.          */
  41.         ArrayTracker(final Shape shape) {
  42.             seen = new int[shape.getNumberOfHashFunctions()];
  43.         }

  44.         @Override
  45.         public boolean test(final int number) {
  46.             if (number < 0) {
  47.                 throw new IndexOutOfBoundsException("number may not be less than zero. " + number);
  48.             }
  49.             for (int i = 0; i < populated; i++) {
  50.                 if (seen[i] == number) {
  51.                     return false;
  52.                 }
  53.             }
  54.             seen[populated++] = number;
  55.             return true;
  56.         }
  57.     }

  58.     /**
  59.      * An IndexTracker implementation that uses an array of bit maps to track whether or not a
  60.      * number has been seen.
  61.      */
  62.     static class BitMapTracker implements IntPredicate {
  63.         private final long[] bits;

  64.         /**
  65.          * Constructs a bit map based tracker for the specified shape.
  66.          * @param shape The shape that is being generated.
  67.          */
  68.         BitMapTracker(final Shape shape) {
  69.             bits = BitMaps.newBitMap(shape);
  70.         }

  71.         @Override
  72.         public boolean test(final int number) {
  73.             final boolean retval = !BitMaps.contains(bits, number);
  74.             BitMaps.set(bits, number);
  75.             return retval;
  76.         }
  77.     }

  78.     /**
  79.      * Creates an instance optimized for the specified shape.
  80.      *
  81.      * @param shape The shape that is being generated.
  82.      * @param consumer The consumer to accept the values.
  83.      * @return an IndexFilter optimized for the specified shape.
  84.      */
  85.     public static IntPredicate create(final Shape shape, final IntPredicate consumer) {
  86.         return new IndexFilter(shape, consumer)::test;
  87.     }

  88.     private final IntPredicate tracker;

  89.     private final int size;

  90.     private final IntPredicate consumer;

  91.     /**
  92.      * Creates an instance optimized for the specified shape.
  93.      *
  94.      * @param shape The shape that is being generated.
  95.      * @param consumer The consumer to accept the values.
  96.      */
  97.     private IndexFilter(final Shape shape, final IntPredicate consumer) {
  98.         this.size = shape.getNumberOfBits();
  99.         this.consumer = consumer;
  100.         if (BitMaps.numberOfBitMaps(shape) * Long.BYTES < (long) shape.getNumberOfHashFunctions() * Integer.BYTES) {
  101.             this.tracker = new BitMapTracker(shape);
  102.         } else {
  103.             this.tracker = new ArrayTracker(shape);
  104.         }
  105.     }

  106.     /**
  107.      * Test if the number should be processed by the {@code consumer}.
  108.      *
  109.      * <p>If the number has <em>not</em> been seen before it is passed to the {@code consumer} and the result returned.
  110.      * If the number has been seen before the {@code consumer} is not called and {@code true} returned.</p>
  111.      *
  112.      * <p><em>If the input is not in the range [0,size) an IndexOutOfBoundsException exception is thrown.</em></p>
  113.      *
  114.      * @param number the number to check.
  115.      * @return {@code true} if processing should continue, {@code false} otherwise.
  116.      */
  117.     public boolean test(final int number) {
  118.         if (number >= size) {
  119.             throw new IndexOutOfBoundsException(String.format("number too large %d >= %d", number, size));
  120.         }
  121.         if (tracker.test(number)) {
  122.             return consumer.test(number);
  123.         }
  124.         return true;
  125.     }
  126. }