jpayne@68: package bloom; jpayne@68: jpayne@68: import java.util.Locale; jpayne@68: jpayne@68: /** jpayne@68: * @author Brian Bushnell jpayne@68: * @date Jul 5, 2012 jpayne@68: */ jpayne@68: public class KCountArray2 { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: KCountArray2 kca=new KCountArray2(1024, 16); jpayne@68: } jpayne@68: jpayne@68: public KCountArray2(long cells_, int bits_){ jpayne@68: this(cells_, bits_, 0); jpayne@68: } jpayne@68: jpayne@68: public KCountArray2(long cells_, int bits_, int gap_){ jpayne@68: gap=gap_; jpayne@68: assert(bits_<=32); jpayne@68: assert(Integer.bitCount(bits_)==1); jpayne@68: assert(Long.bitCount(cells_)==1); jpayne@68: jpayne@68: while(bits_*cells_<32*numArrays){ jpayne@68: assert(false); jpayne@68: bits_*=2; jpayne@68: } //Increases bits per cell so that at minimum each array is size 1 jpayne@68: jpayne@68: assert(bits_!=32); jpayne@68: jpayne@68: cells=cells_; jpayne@68: cellBits=bits_; jpayne@68: valueMask=~((-1)<>>=cellBits; jpayne@68: comma=", "; jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: sb.append("]"); jpayne@68: return sb.toString(); jpayne@68: } jpayne@68: jpayne@68: public double usedFraction(){return cellsUsed/(double)cells;} jpayne@68: jpayne@68: public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;} jpayne@68: jpayne@68: public long cellsUsed(int mindepth){ jpayne@68: long count=0; jpayne@68: for(int[] array : matrix){ jpayne@68: if(array!=null){ jpayne@68: for(int word : array){ jpayne@68: while(word>0){ jpayne@68: int x=word&valueMask; jpayne@68: if(x>=mindepth){count++;} jpayne@68: word>>>=cellBits; jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: return count; jpayne@68: } jpayne@68: jpayne@68: public String mem(){ jpayne@68: long mem=(cells*cellBits)/8; jpayne@68: if(mem<(1<<20)){ jpayne@68: return (String.format(Locale.ROOT, "%.2f KB", mem*1d/(1<<10))); jpayne@68: }else if(mem<(1<<30)){ jpayne@68: return (String.format(Locale.ROOT, "%.2f MB", mem*1d/(1<<20))); jpayne@68: }else{ jpayne@68: return (String.format(Locale.ROOT, "%.2f GB", mem*1d/(1<<30))); jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: public static final int min(int x, int y){return xy ? x : y;} jpayne@68: public static final long min(long x, long y){return xy ? x : y;} jpayne@68: jpayne@68: private long cellsUsed; jpayne@68: jpayne@68: public final long cells; jpayne@68: public final int cellBits; jpayne@68: public final int maxValue; jpayne@68: public final int gap; //Set this for convenience on gapped tables to make sure you're using the right table. jpayne@68: jpayne@68: private final int cellsPerWord; jpayne@68: private final int indexShift; jpayne@68: private final int valueMask; jpayne@68: private final int[][] matrix; jpayne@68: jpayne@68: private static final int arrayBits=2; jpayne@68: private static final int numArrays=1<