// Path: src/main/java/com/googlecode/javaewah32/BitmapStorage32.java
// public interface BitmapStorage32 {
// 
//     /**
//      * Adding words directly to the bitmap (for expert use).
//      * 
//      * This is normally how you add data to the array. So you add bits in
//      * streams of 8*8 bits.
//      *
//      * @param newData the word
//      */
//     void addWord(final int newData);
//     
//     /**
//      * Adding literal words directly to the bitmap (for expert use).
//      *
//      * @param newData the word
//      */
//     void addLiteralWord(final int newData);
// 
//     /**
//      * if you have several literal words to copy over, this might be faster.
//      *
//      * @param buffer the buffer wrapping the literal words
//      * @param start  the starting point in the array
//      * @param number the number of literal words to add
//      */
//     void addStreamOfLiteralWords(final Buffer32 buffer, final int start,
//                                         final int number);
// 
//     /**
//      * For experts: You want to add many zeroes or ones? This is the method
//      * you use.
//      *
//      * @param v      zeros or ones
//      * @param number how many to words add
//      */
//     void addStreamOfEmptyWords(final boolean v, final int number);
// 
//     /**
//      * Like "addStreamOfLiteralWords" but negates the words being added.
//      *
//      * @param buffer the buffer wrapping the literal words
//      * @param start  the starting point in the array
//      * @param number the number of literal words to add
//      */
//     void addStreamOfNegatedLiteralWords(final Buffer32 buffer, final int start,
//                                                final int number);
// 
//     /**
//      * Empties the container.
//      */
//     void clear();
// 
//     /**
//      * Sets the size in bits of the bitmap as an *uncompressed* bitmap.
//      * Normally, this is used to reduce the size of the bitmaps within
//      * the scope of the last word. Specifically, this means that
//      * (sizeInBits()+31)/32 must be equal to (size+31)/32.
//      * If needed, the bitmap can be further padded with zeroes. 
//      *  
//      * @param size         the size in bits
//      */
//     void setSizeInBitsWithinLastWord(final int size);
// }


import com.googlecode.javaewah32.BitmapStorage32;
import java.util.Arrays;
package com.googlecode.javaewah32.symmetric;



/**
 * A threshold Boolean function returns true if the number of true values exceed
 * a threshold. It is a symmetric Boolean function.
 * 
 * This class implements an algorithm described in the following paper:
 * 
 * Owen Kaser and Daniel Lemire, Compressed bitmap indexes: beyond unions and intersections
 * <a href="http://arxiv.org/abs/1402.4466">http://arxiv.org/abs/1402.4466</a>
 * 
 * It is not thread safe: you should use one object per thread.
 *
 * @author Daniel Lemire
 * @see <a
 * href="http://en.wikipedia.org/wiki/Symmetric_Boolean_function">http://en.wikipedia.org/wiki/Symmetric_Boolean_function</a>
 * @since 0.8.2
 */
public final class ThresholdFuncBitmap32 extends UpdateableBitmapFunction32 {
    private final int min;
    private int[] buffers;
    private int bufferUsed;
    private final int[] bufcounters = new int[64];
    private static final int[] zeroes64 = new int[64];

    /**
     * Construction a threshold function with a given threshold
     *
     * @param min threshold
     */
    public ThresholdFuncBitmap32(final int min) {
        super();
        this.min = min;
        this.buffers = new int[16];
        this.bufferUsed = 0;
    }

    @Override
// Path: src/main/java/com/googlecode/javaewah32/BitmapStorage32.java
// public interface BitmapStorage32 {
// 
//     /**
//      * Adding words directly to the bitmap (for expert use).
//      * 
//      * This is normally how you add data to the array. So you add bits in
//      * streams of 8*8 bits.
//      *
//      * @param newData the word
//      */
//     void addWord(final int newData);
//     
//     /**
//      * Adding literal words directly to the bitmap (for expert use).
//      *
//      * @param newData the word
//      */
//     void addLiteralWord(final int newData);
// 
//     /**
//      * if you have several literal words to copy over, this might be faster.
//      *
//      * @param buffer the buffer wrapping the literal words
//      * @param start  the starting point in the array
//      * @param number the number of literal words to add
//      */
//     void addStreamOfLiteralWords(final Buffer32 buffer, final int start,
//                                         final int number);
// 
//     /**
//      * For experts: You want to add many zeroes or ones? This is the method
//      * you use.
//      *
//      * @param v      zeros or ones
//      * @param number how many to words add
//      */
//     void addStreamOfEmptyWords(final boolean v, final int number);
// 
//     /**
//      * Like "addStreamOfLiteralWords" but negates the words being added.
//      *
//      * @param buffer the buffer wrapping the literal words
//      * @param start  the starting point in the array
//      * @param number the number of literal words to add
//      */
//     void addStreamOfNegatedLiteralWords(final Buffer32 buffer, final int start,
//                                                final int number);
// 
//     /**
//      * Empties the container.
//      */
//     void clear();
// 
//     /**
//      * Sets the size in bits of the bitmap as an *uncompressed* bitmap.
//      * Normally, this is used to reduce the size of the bitmaps within
//      * the scope of the last word. Specifically, this means that
//      * (sizeInBits()+31)/32 must be equal to (size+31)/32.
//      * If needed, the bitmap can be further padded with zeroes. 
//      *  
//      * @param size         the size in bits
//      */
//     void setSizeInBitsWithinLastWord(final int size);
// }


// Path: src/main/java/com/googlecode/javaewah32/symmetric/ThresholdFuncBitmap32.java
import com.googlecode.javaewah32.BitmapStorage32;
import java.util.Arrays;

package com.googlecode.javaewah32.symmetric;



/**
 * A threshold Boolean function returns true if the number of true values exceed
 * a threshold. It is a symmetric Boolean function.
 * 
 * This class implements an algorithm described in the following paper:
 * 
 * Owen Kaser and Daniel Lemire, Compressed bitmap indexes: beyond unions and intersections
 * <a href="http://arxiv.org/abs/1402.4466">http://arxiv.org/abs/1402.4466</a>
 * 
 * It is not thread safe: you should use one object per thread.
 *
 * @author Daniel Lemire
 * @see <a
 * href="http://en.wikipedia.org/wiki/Symmetric_Boolean_function">http://en.wikipedia.org/wiki/Symmetric_Boolean_function</a>
 * @since 0.8.2
 */
public final class ThresholdFuncBitmap32 extends UpdateableBitmapFunction32 {
    private final int min;
    private int[] buffers;
    private int bufferUsed;
    private final int[] bufcounters = new int[64];
    private static final int[] zeroes64 = new int[64];

    /**
     * Construction a threshold function with a given threshold
     *
     * @param min threshold
     */
    public ThresholdFuncBitmap32(final int min) {
        super();
        this.min = min;
        this.buffers = new int[16];
        this.bufferUsed = 0;
    }

    @Override
    public void dispatch(BitmapStorage32 out, int runBegin, int runend) {