| 1 |
package bloom; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
public class KCountArray3 extends KCountArray { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
private static final long serialVersionUID = -5466091642729698944L; |
| 14 |
|
| 15 |
public KCountArray3(long cells_, int bits_){ |
| 16 |
super(cells_, bits_); |
| 17 |
long words=cells/cellsPerWord; |
| 18 |
int wordsPerArray=(int)(words/numArrays); |
| 19 |
matrix=new int[numArrays][wordsPerArray]; |
| 20 |
} |
| 21 |
|
| 22 |
@Override |
| 23 |
public int read(long key){ |
| 24 |
if(verbose){System.err.println("Reading "+key);} |
| 25 |
|
| 26 |
int arrayNum=(int)(key&arrayMask); |
| 27 |
|
| 28 |
key>>>=arrayBits; |
| 29 |
|
| 30 |
int[] array=matrix[arrayNum]; |
| 31 |
int index=(int)(key>>>indexShift); |
| 32 |
|
| 33 |
int word=array[index]; |
| 34 |
|
| 35 |
int cellShift=(int)(cellBits*key); |
| 36 |
|
| 37 |
int value=(int)((word>>>cellShift)&valueMask); |
| 38 |
if(verbose){System.err.println("Read "+value);} |
| 39 |
return value; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void write(long key, int value){ |
| 44 |
if(verbose){System.err.println("Writing "+key+", "+value);} |
| 45 |
int arrayNum=(int)(key&arrayMask); |
| 46 |
key>>>=arrayBits; |
| 47 |
int[] array=matrix[arrayNum]; |
| 48 |
int index=(int)(key>>>indexShift); |
| 49 |
int word=array[index]; |
| 50 |
int cellShift=(int)(cellBits*key); |
| 51 |
word=(value<<cellShift)|(word&~((valueMask)<<cellShift)); |
| 52 |
array[index]=word; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
@Override |
| 57 |
public void increment(long key, int incr){ |
| 58 |
if(verbose){System.err.println("*** Incrementing "+key);} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
int arrayNum=(int)(key&arrayMask); |
| 64 |
key>>>=arrayBits; |
| 65 |
int[] array=matrix[arrayNum]; |
| 66 |
int index=(int)(key>>>indexShift); |
| 67 |
int word=array[index]; |
| 68 |
int cellShift=(int)(cellBits*key); |
| 69 |
int value=((word>>>cellShift)&valueMask); |
| 70 |
if(value==0 && incr>0){cellsUsed++;} |
| 71 |
else if(incr<0 && value+incr==0){cellsUsed--;} |
| 72 |
value=min(value+incr, maxValue); |
| 73 |
word=(value<<cellShift)|(word&~((valueMask)<<cellShift)); |
| 74 |
array[index]=word; |
| 75 |
if(verbose){System.err.println("Returning "+value);} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
@Override |
| 81 |
public int incrementAndReturnUnincremented(long key, int incr){ |
| 82 |
if(verbose){System.err.println("Incrementing2 "+key);} |
| 83 |
int arrayNum=(int)(key&arrayMask); |
| 84 |
key>>>=arrayBits; |
| 85 |
int[] array=matrix[arrayNum]; |
| 86 |
int index=(int)(key>>>indexShift); |
| 87 |
int word=array[index]; |
| 88 |
int cellShift=(int)(cellBits*key); |
| 89 |
final int value=((word>>>cellShift)&valueMask); |
| 90 |
final int value2=min(value+incr, maxValue); |
| 91 |
word=(value2<<cellShift)|(word&~((valueMask)<<cellShift)); |
| 92 |
array[index]=word; |
| 93 |
if(verbose){System.err.println("Returning "+value);} |
| 94 |
return value; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public long[] transformToFrequency(){ |
| 99 |
return transformToFrequency(matrix); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public String toContentsString(){ |
| 104 |
StringBuilder sb=new StringBuilder(); |
| 105 |
sb.append("["); |
| 106 |
String comma=""; |
| 107 |
for(int[] array : matrix){ |
| 108 |
for(int i=0; i<array.length; i++){ |
| 109 |
int word=array[i]; |
| 110 |
for(int j=0; j<cellsPerWord; j++){ |
| 111 |
int x=word&valueMask; |
| 112 |
sb.append(comma); |
| 113 |
sb.append(x); |
| 114 |
word>>>=cellBits; |
| 115 |
comma=", "; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
sb.append("]"); |
| 120 |
return sb.toString(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public double usedFraction(){return cellsUsed/(double)cells;} |
| 125 |
|
| 126 |
@Override |
| 127 |
public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;} |
| 128 |
|
| 129 |
@Override |
| 130 |
public long cellsUsed(int mindepth){ |
| 131 |
long count=0; |
| 132 |
for(int[] array : matrix){ |
| 133 |
if(array!=null){ |
| 134 |
for(int word : array){ |
| 135 |
while(word>0){ |
| 136 |
int x=word&valueMask; |
| 137 |
if(x>=mindepth){count++;} |
| 138 |
word>>>=cellBits; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
return count; |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
long hash(long x, int y) { |
| 148 |
assert(false) : "Unsupported."; |
| 149 |
return x; |
| 150 |
} |
| 151 |
|
| 152 |
private long cellsUsed; |
| 153 |
private final int[][] matrix; |
| 154 |
|
| 155 |
} |