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