Debian Codegraph @

current/kmer/HashArrayHybridFast.java

raw symbols SCIP
1 package kmer;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 import shared.KillSwitch;
7 import shared.Primes;
8 import shared.Tools;
9 import structures.IntList3;
10
11 /**
12 * Stores kmers in a long[] and counts in an int[], with a victim cache.
13 * @author Brian Bushnell
14 * @date Oct 25, 2013
15 *
16 */
17 public final class HashArrayHybridFast extends HashArray {
18
19 /*--------------------------------------------------------------*/
20 /*---------------- Initialization ----------------*/
21 /*--------------------------------------------------------------*/
22
23 public HashArrayHybridFast(int[] schedule_, long coreMask_){
24 super(schedule_, coreMask_, true);
25 values=allocInt1D(prime+extra);
26 setList=new IntList3();
27 setList.add(null, 0);
28 setList.add(null, 0);
29 }
30
31 // public HashArrayHybrid(int initialSize, int maxSize, long mask, boolean autoResize_){
32 // super(initialSize, maxSize, mask, autoResize_, true);
33 // values=allocInt1D(prime+extra);
34 // setList=new IntList3();
35 // setList.add(null);
36 // setList.add(null);
37 // }
38
39 /*--------------------------------------------------------------*/
40 /*---------------- Public Methods ----------------*/
41 /*--------------------------------------------------------------*/
42
43 @Override
44 public final int increment(final long kmer, final int incr){
45 int cell=kmerToCell(kmer);
46
47 for(final int max=cell+extra; cell<max; cell++){
48 long n=array[cell];
49 assert(n>-2);
50 if(n==kmer){
51 assert(values[cell]>=0);
52 values[cell]+=incr;
53 if(values[cell]<0){values[cell]=Integer.MAX_VALUE;}
54 return values[cell];
55 }else if(n==NOT_PRESENT){
56 array[cell]=kmer;
57 size++;
58 values[cell]=incr;
59 if(autoResize && size+victims.size>sizeLimit){resize();}
60 return incr;
61 }
62 }
63 int x=victims.increment(kmer, incr);
64 if(autoResize && size+victims.size>sizeLimit){resize();}
65 return x;
66 }
67
68 @Override
69 public final int incrementAndReturnNumCreated(final long kmer, final int incr){
70 int cell=kmerToCell(kmer);
71
72 for(final int max=cell+extra; cell<max; cell++){
73 long n=array[cell];
74 assert(n>-2);
75 if(n==kmer){
76 assert(values[cell]>=0);
77 values[cell]+=incr;
78 if(values[cell]<0){values[cell]=Integer.MAX_VALUE;}
79 return 0;
80 }else if(n==NOT_PRESENT){
81 array[cell]=kmer;
82 size++;
83 values[cell]=incr;
84 if(autoResize && size+victims.size>sizeLimit){resize();}
85 return 1;
86 }
87 }
88 int x=victims.incrementAndReturnNumCreated(kmer, incr);
89 if(autoResize && size+victims.size>sizeLimit){resize();}
90 return x;
91 }
92
93 /*--------------------------------------------------------------*/
94 /*---------------- Nonpublic Methods ----------------*/
95 /*--------------------------------------------------------------*/
96
97 @Override
98 protected final int readCellValue(int cell) {
99 final int x=values[cell];
100 if(x>-2){return x;}
101 return setList.get(0-x)[0];
102 }
103
104 @Override
105 protected final int[] readCellValues(int cell, int[] singleton) {
106 final int x=values[cell];
107 if(x>-2){
108 singleton[0]=values[cell];
109 return singleton;
110 }
111 return setList.get(0-x);
112 }
113
114 @Override
115 protected final void insertValue(long kmer, int[] vals, int cell, int vlen) {
116 if(verbose){System.err.println("insertValue("+kmer+", "+Arrays.toString(vals)+", "+cell+"); old="+values[cell]);}
117 assert(array[cell]==kmer);
118 if(vals.length==1){
119 if(verbose){System.err.println("A: length=1");}
120 insertValue(kmer, vals[0], cell);
121 return;
122 }
123 final int old=values[cell];
124 if(old==vals[0] && vals[1]==NOT_PRESENT){//Maybe this is an attempt to accelerate the most common case? It's only for when there is no array.
125 if(verbose){System.err.println("B: old==vals[0] && vals[1]==-1");}
126 return; //Nothing to do
127 }else if(old<-1){//An array already exists
128 if(verbose){System.err.println("C: old<-1");}
129 for(int i : vals){
130 if(i==NOT_PRESENT){break;}
131 insertIntoList(i, -old);
132 }
133 }else{//Make a new array
134 final int[] temp;
135 if(old>0){//Move the old value to a new array. Note that this will probably never be used.
136 if(verbose){System.err.println("D: old>0");}
137 temp=allocInt1D(vals.length+1);
138 temp[0]=old;
139 for(int i=0; i<vals.length; i++){temp[i+1]=vals[i];}
140 vlen++;
141 }else{
142 if(verbose){System.err.println("E: old>0");}
143 temp=vals;
144 }
145 values[cell]=-setList.size;
146 setList.add(temp, vlen);
147 }
148 }
149
150 @Override
151 protected final void insertValue(long kmer, int v, int cell) {
152 assert(array[cell]==kmer);
153 assert(v>0);
154 final int cc=values[cell];
155 if(cc==v){
156 return;
157 }else if(cc<-1){
158 insertIntoList(v, -cc);
159 }else if(cc>0){
160 values[cell]=-setList.size;
161 setList.add(new int[] {cc, v, -1, -1}, 2);
162 }else{
163 values[cell]=v;
164 }
165 }
166
167 private final int insertIntoList(final int v, final int loc){
168 return setList.insertIntoList(v, loc);
169 }
170
171 /*--------------------------------------------------------------*/
172 /*---------------- Resizing and Rebalancing ----------------*/
173 /*--------------------------------------------------------------*/
174
175 @Override
176 public final boolean canRebalance() {return false;}
177
178 @Override
179 protected synchronized void resize(){
180
181 if(verbose){
182 System.err.println("Resizing from "+prime+"; load="+(size*1f/prime));
183 }
184
185 // assert(TESTMODE);
186 // if(TESTMODE){
187 // for(int i=0; i<ll.size; i++){
188 // assert(contains(ll.get(i), il.get(i)));
189 // assert(!contains(ll.get(i), Integer.MAX_VALUE));
190 // }
191 // }
192
193 // System.err.println("Resizing from "+prime+"; load="+(size*1f/prime));
194 if(prime>=maxPrime){
195 // sizeLimit=0xFFFFFFFFFFFFL;
196 // return;
197 KillSwitch.memKill(new OutOfMemoryError());
198 }
199
200 final long oldSize=size, oldVSize=victims.size;
201 if(schedule!=null){
202 final long oldPrime=prime;
203 prime=nextScheduleSize();
204 if(prime<=oldPrime){KillSwitch.memKill(new OutOfMemoryError());}
205 sizeLimit=(long)((atMaxSize() ? maxLoadFactorFinal : maxLoadFactor)*prime);
206 }else{//Old method
207 final long totalSize=oldSize+oldVSize;
208
209 final long maxAllowedByLoadFactor=(long)(totalSize*minLoadMult);
210 final long minAllowedByLoadFactor=(long)(totalSize*maxLoadMult);
211
212 // sizeLimit=Tools.min((long)(maxLoadFactor*prime), maxPrime);
213
214 assert(maxAllowedByLoadFactor>=minAllowedByLoadFactor);
215 if(maxAllowedByLoadFactor<prime){
216 sizeLimit=(long)(maxLoadFactor*prime);
217 return;
218 }
219
220 long x=10+(long)(prime*resizeMult);
221 x=Tools.max(x, minAllowedByLoadFactor);
222 x=Tools.min(x, maxAllowedByLoadFactor);
223
224 int prime2=(int)Tools.min(maxPrime, Primes.primeAtLeast(x));
225
226 if(prime2<=prime){
227 sizeLimit=(long)(maxLoadFactor*prime);
228 assert(prime2==prime) : "Resizing to smaller array? "+totalSize+", "+prime+", "+x;
229 return;
230 }
231
232 prime=prime2;
233 sizeLimit=(long)(maxLoadFactor*prime);
234 }
235
236 // System.err.println("Resized to "+prime+"; load="+(size*1f/prime));
237 long[] oldKmers=array;
238 int[] oldValues=values;
239 IntList3 oldList=setList;
240 setList=new IntList3();
241 setList.add(null, 0);
242 setList.add(null, 0);
243 KmerNode[] oldVictims=victims.array;
244 array=allocLong1D(prime+extra);
245 Arrays.fill(array, -1);
246 values=allocInt1D(prime+extra);
247 ArrayList<KmerNode> nodeList=new ArrayList<KmerNode>((int)(victims.size)); //Can fail if more than Integer.MAX_VALUE
248 for(int i=0; i<oldVictims.length; i++){
249 if(oldVictims[i]!=null){oldVictims[i].traverseInfix(nodeList);}
250 }
251 Arrays.fill(oldVictims, null);
252 victims.size=0;
253 size=0;
254
255 // long added=0;
256 for(int i=0; i<oldKmers.length; i++){
257 final long kmer=oldKmers[i];
258 if(kmer!=-1){
259 // final int[] old=getValues(kmer, new int[1]);
260 // final long oldsize=(size+victims.size);
261 // assert(old==null);
262 final int v=oldValues[i];
263 // added++;
264
265 // System.err.println("Found "+kmer+"->"+v);
266
267 assert(v<-1 || v>0);
268 if(v>=0){
269 // assert(!contains(kmer));
270 // long olds=size+victims.size; //123
271 set(kmer, v);
272 // assert(contains(kmer));
273 // assert(size+victims.size==olds+1);
274 }else{
275 // if(verbose){
276 // System.err.println("i="+i+", v="+v+", old="+Arrays.toString(oldList.get(-v))+", current="+Arrays.toString(setList.get(-v))+
277 // ", get()="+Arrays.toString(getValues(kmer, new int[1])));
278 // }
279 // assert(!contains(kmer));
280 // long olds=size+victims.size; //123
281 set(kmer, oldList.get(-v), oldList.getLen(-v));
282 // assert(contains(kmer));
283 // assert(size+victims.size==olds+1);
284 }
285 }
286 }
287 // assert(added==oldSize);
288
289 final int[] singleton=new int[1];
290 // added=0;
291 for(KmerNode n : nodeList){
292 if(n.pivot>-1){
293 // added++;
294 // final int[] old=getValues(n.pivot, new int[1]);
295 // assert(old==null);
296 if(n.numValues()>1){
297 // assert(!contains(n.pivot()));
298 // long olds=size+victims.size; //123
299 set(n.pivot, n.values(singleton), n.numValues());
300 // assert(size+victims.size==olds+1);
301 // assert(contains(n.pivot()));
302 }else{
303 // assert(!contains(n.pivot()));
304 // long olds=size+victims.size; //123
305 set(n.pivot, n.value());
306 // assert(size+victims.size==olds+1);
307 // assert(contains(n.pivot()));
308 }
309 // assert(old==null || contains(n.pivot, old));
310 // assert(contains(n.pivot, n.value()));
311 }
312 }
313 // assert(added==oldVSize);
314
315 assert(oldSize+oldVSize==size+victims.size) : oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size);
316
317 if(verbose){System.err.println("Resized to "+prime+". "+oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size));}
318
319 // assert(TESTMODE);
320 // if(TESTMODE){
321 // for(int i=0; i<ll.size; i++){
322 // long kmer=ll.get(i);
323 // int v=il.get(i);
324 // assert(contains(kmer, v)) : i+", "+ll.size+", "+kmer+", "+v+", "+Arrays.toString(getValues(kmer, new int[1]));
325 // assert(!contains(kmer, Integer.MAX_VALUE));
326 // }
327 // }
328 }
329
330 @Deprecated
331 @Override
332 public void rebalance(){
333 throw new RuntimeException("Unimplemented.");
334 }
335
336 @Deprecated
337 @Override
338 public long regenerate(final int limit){
339 throw new RuntimeException("Not supported.");
340 }
341
342 /*--------------------------------------------------------------*/
343 /*---------------- Fields ----------------*/
344 /*--------------------------------------------------------------*/
345
346 private int[] values;
347 private IntList3 setList;
348
349 }