Debian Codegraph @

current/ukmer/HashArrayUHybrid.java

raw symbols SCIP
1 package ukmer;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 import shared.KillSwitch;
7 import shared.Primes;
8 import shared.Shared;
9 import shared.Tools;
10 import structures.IntList2;
11
12 /**
13 * Stores kmers in a long[] and counts in an int[], with a victim cache.
14 * @author Brian Bushnell
15 * @date Oct 25, 2013
16 *
17 */
18 public final class HashArrayUHybrid extends HashArrayU {
19
20 /*--------------------------------------------------------------*/
21 /*---------------- Initialization ----------------*/
22 /*--------------------------------------------------------------*/
23
24 public HashArrayUHybrid(int[] schedule_, int k_, int kbig_){
25 super(schedule_, k_, kbig_, true);
26 values=allocInt1D(prime+extra);
27 setList=new IntList2();
28 setList.add(null);
29 setList.add(null);
30 }
31
32 // public HashArrayUHybrid(int initialSize, int k_, int kbig_, boolean autoResize_){
33 // super(initialSize, k_, kbig_, autoResize_, true);
34 // values=allocInt1D(prime+extra);
35 // setList=new IntList2();
36 // setList.add(null);
37 // setList.add(null);
38 // }
39
40 /*--------------------------------------------------------------*/
41 /*---------------- Public Methods ----------------*/
42 /*--------------------------------------------------------------*/
43
44 @Override
45 public final int increment(final Kmer kmer){
46 final int cell=findKmerOrEmpty(kmer);
47
48 if(cell==HASH_COLLISION){
49 int x=victims.increment(kmer);
50 if(autoResize && size+victims.size>sizeLimit){resize();}
51 return x;
52 }else if(arrays[0][cell]==NOT_PRESENT){
53 setKmer(kmer.key(), cell);
54 size++;
55 values[cell]=1;
56 if(autoResize && size+victims.size>sizeLimit){resize();}
57 return 1;
58 }else{
59 values[cell]++;
60 if(values[cell]<0){values[cell]=Integer.MAX_VALUE;}
61 return values[cell];
62 }
63 }
64
65 @Override
66 public final int incrementAndReturnNumCreated(final Kmer kmer){
67 final int cell=findKmerOrEmpty(kmer);
68
69 if(cell==HASH_COLLISION){
70 int x=victims.incrementAndReturnNumCreated(kmer);
71 if(autoResize && size+victims.size>sizeLimit){resize();}
72 return x;
73 }else if(arrays[0][cell]==NOT_PRESENT){
74 setKmer(kmer.key(), cell);
75 size++;
76 values[cell]=1;
77 if(autoResize && size+victims.size>sizeLimit){resize();}
78 return 1;
79 }else{
80 values[cell]++;
81 if(values[cell]<0){values[cell]=Integer.MAX_VALUE;}
82 return 0;
83 }
84 }
85
86 /*--------------------------------------------------------------*/
87 /*---------------- Nonpublic Methods ----------------*/
88 /*--------------------------------------------------------------*/
89
90 @Override
91 protected final int readCellValue(int cell) {
92 final int x=values[cell];
93 if(x>-2){return x;}
94 return setList.get(0-x)[0];
95 }
96
97 @Override
98 protected final int[] readCellValues(int cell, int[] singleton) {
99 final int x=values[cell];
100 if(x>-2){
101 singleton[0]=values[cell];
102 return singleton;
103 }
104 return setList.get(0-x);
105 }
106
107 @Override
108 protected final void insertValue(long[] kmer, int[] vals, int cell) {
109 if(verbose){System.err.println("insertValue("+kmer+", "+Arrays.toString(vals)+", "+cell+"); old="+values[cell]);}
110 assert(matches(kmer, cell));
111 if(vals.length==1){
112 if(verbose){System.err.println("A: length=1");}
113 insertValue(kmer, vals[0], cell);
114 return;
115 }
116 final int old=values[cell];
117 if(old==vals[0] && vals[1]==NOT_PRESENT){
118 if(verbose){System.err.println("B: old==vals[0] && vals[1]==-1");}
119 return; //Nothing to do
120 }else if(old<-1){//An array already exists
121 if(verbose){System.err.println("C: old<-1");}
122 for(int i : vals){
123 if(i==-1){break;}
124 insertIntoList(i, -old);
125 }
126 }else{//Add the list
127 final int[] temp;
128 if(old>0){//Move the old value to a new array. Note that this will probably never be used.
129 if(verbose){System.err.println("D: old>0");}
130 temp=allocInt1D(vals.length+1);
131 temp[0]=old;
132 for(int i=0; i<vals.length; i++){temp[i+1]=vals[i];}
133 }else{
134 if(verbose){System.err.println("E: old>0");}
135 temp=vals;
136 }
137 values[cell]=-setList.size;
138 setList.add(temp);
139 }
140 }
141
142 @Override
143 protected final void insertValue(long[] kmer, int v, int cell) {
144 assert(matches(kmer, cell));
145 assert(v>0);
146 final int cc=values[cell];
147 if(cc==v){
148 return;
149 }else if(cc<-1){
150 insertIntoList(v, -cc);
151 }else if(cc>0){
152 values[cell]=-setList.size;
153 setList.add(new int[] {cc, v, -1, -1});
154 }else{
155 values[cell]=v;
156 }
157 }
158
159 private final int insertIntoList(final int v, final int loc){
160
161 if(loc>=setList.size){
162 assert(loc==setList.size);
163 setList.add(null);
164 }
165
166 int[] set=setList.get(loc);
167 if(set==null){
168 set=new int[] {-1, -1};
169 setList.set(loc, set);
170 }
171
172 for(int i=0; i<set.length; i++){
173 if(set[i]==v){return 0;}
174 if(set[i]<0){set[i]=v;return 1;}
175 }
176 final int oldSize=set.length;
177 final int newSize=(int)Tools.min(Shared.MAX_ARRAY_LEN, oldSize*2L);
178 assert(newSize>set.length) : "Overflow.";
179 set=KillSwitch.copyOf(set, newSize);
180 set[oldSize]=v;
181 Arrays.fill(set, oldSize+1, newSize, -1);
182 setList.set(loc, set);
183 return 1;
184 }
185
186 /*--------------------------------------------------------------*/
187 /*---------------- Resizing and Rebalancing ----------------*/
188 /*--------------------------------------------------------------*/
189
190 @Override
191 public final boolean canRebalance() {return false;}
192
193 // @Override
194 // protected synchronized void resize(){
195 //
196 // if(verbose){
197 // System.err.println("Resizing from "+prime+"; load="+(size*1f/prime));
198 // }
199 //
200 //// assert(TESTMODE);
201 //// if(TESTMODE){
202 //// for(int i=0; i<ll.size; i++){
203 //// assert(contains(ll.get(i), il.get(i)));
204 //// assert(!contains(ll.get(i), Integer.MAX_VALUE));
205 //// }
206 //// }
207 //
208 //// System.err.println("Resizing from "+prime+"; load="+(size*1f/prime));
209 // if(prime>=maxPrime){
210 // sizeLimit=0xFFFFFFFFFFFFL;
211 // return;
212 // }
213 //
214 // final long oldSize=size, oldVSize=victims.size;
215 // final long totalSize=this.size+victims.size;
216 //
217 // final long maxAllowedByLoadFactor=(long)(totalSize*minLoadMult);
218 // final long minAllowedByLoadFactor=(long)(totalSize*maxLoadMult);
219 //
220 //// sizeLimit=Tools.min((long)(maxLoadFactor*prime), maxPrime);
221 //
222 // assert(maxAllowedByLoadFactor>=minAllowedByLoadFactor);
223 // if(maxAllowedByLoadFactor<prime){
224 // sizeLimit=(long)(maxLoadFactor*prime);
225 // return;
226 // }
227 //
228 // long x=10+(long)(prime*resizeMult);
229 // x=Tools.max(x, minAllowedByLoadFactor);
230 // x=Tools.min(x, maxAllowedByLoadFactor);
231 //
232 // int prime2=(int)Tools.min(maxPrime, Primes.primeAtLeast(x));
233 //
234 // if(prime2<=prime){
235 // sizeLimit=(long)(maxLoadFactor*prime);
236 // assert(prime2==prime) : "Resizing to smaller array? "+totalSize+", "+prime+", "+x;
237 // return;
238 // }
239 //
240 // prime=prime2;
241 //// System.err.println("Resized to "+prime+"; load="+(size*1f/prime));
242 // long[][] oldk=arrays;
243 // int[] oldc=values;
244 // arrays=KillSwitch.allocLong2D(mult, prime2+extra);
245 // for(int i=0; i<mult; i++){
246 // Arrays.fill(arrays[i], NOT_PRESENT);
247 // }
248 // IntList2 oldList=setList;
249 // setList=new IntList2();
250 // setList.add(null);
251 // setList.add(null);//TODO: May have to add 3 of them to avoid HASH_COLLISION at -2
252 // values=allocInt1D(prime2+extra);
253 // ArrayList<KmerNodeU> list=victims.toList();
254 // victims.clear();
255 // size=0;
256 // sizeLimit=Long.MAX_VALUE;
257 //
258 // final int[] singleton=new int[] {NOT_PRESENT};
259 // final Kmer kmer=new Kmer(kbig);
260 // {
261 // for(int i=0; i<oldk.length; i++){
262 // if(oldk[0][i]>NOT_PRESENT){
263 // final int v=oldc[i];
264 // fillKmer(i, kmer, oldk);
265 // if(v>=0){
266 // set(kmer, v);
267 // }else{
268 // set(kmer, oldList.get(-v));
269 // }
270 // }
271 // }
272 // }
273 //
274 // for(KmerNodeU n : list){
275 // if(n.pivot[0]>NOT_PRESENT){
276 // kmer.setFrom(n.pivot());
277 // if(n.numValues()>1){
278 // set(kmer, n.values(singleton));
279 // }else{
280 // set(kmer, n.value());
281 // }
282 // }else{assert(false);}
283 // }
284 //
285 // assert(oldSize+oldVSize==size+victims.size) : oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size);
286 //
287 // if(verbose){System.err.println("Resized to "+prime+". "+oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size));}
288 //
289 // sizeLimit=(long)(maxLoadFactor*prime);
290 //
291 //// assert(TESTMODE);
292 //// if(TESTMODE){
293 //// for(int i=0; i<ll.size; i++){
294 //// long[] kmer=ll.get(i);
295 //// int v=il.get(i);
296 //// assert(contains(kmer, v)) : i+", "+ll.size+", "+kmer+", "+v+", "+Arrays.toString(getValues(kmer, new int[1]));
297 //// assert(!contains(kmer, Integer.MAX_VALUE));
298 //// }
299 //// }
300 // }
301
302
303 @Override
304 protected synchronized void resize(){
305 if(verbose){System.err.println("Resizing from "+prime+"; load="+(size*1f/prime));}
306 if(prime>=maxPrime){
307 // sizeLimit=0xFFFFFFFFFFFFL;
308 // return;
309 KillSwitch.memKill(new OutOfMemoryError());
310 }
311
312 final int oldPrime=prime;
313 final long oldSize=size, oldVSize=victims.size;
314 final long totalSize=oldSize+oldVSize;
315
316 if(schedule!=null){
317 prime=nextScheduleSize();
318 if(prime<=oldPrime){KillSwitch.memKill(new OutOfMemoryError());}
319 sizeLimit=(long)((atMaxSize() ? maxLoadFactorFinal : maxLoadFactor)*prime);
320 }else{//Old method
321 final long maxAllowedByLoadFactor=(long)(totalSize*minLoadMult);
322 final long minAllowedByLoadFactor=(long)(totalSize*maxLoadMult);
323
324 // sizeLimit=Tools.min((long)(maxLoadFactor*prime), maxPrime);
325
326 assert(maxAllowedByLoadFactor>=minAllowedByLoadFactor);
327 if(maxAllowedByLoadFactor<prime){
328 sizeLimit=(long)(maxLoadFactor*prime);
329 return;
330 }
331
332 long x=10+(long)(prime*resizeMult);
333 x=Tools.max(x, minAllowedByLoadFactor);
334 x=Tools.min(x, maxAllowedByLoadFactor);
335
336 int prime2=(int)Tools.min(maxPrime, Primes.primeAtLeast(x));
337
338 if(prime2<=prime){
339 sizeLimit=(long)(maxLoadFactor*prime);
340 assert(prime2==prime) : "Resizing to smaller array? "+totalSize+", "+prime+", "+x;
341 return;
342 }
343
344 prime=prime2;
345 sizeLimit=(long)(maxLoadFactor*prime);
346 }
347
348
349
350 // System.err.println("Resized to "+prime+"; load="+(size*1f/prime));
351 long[][] oldk=arrays;
352 int[] oldc=values;
353 arrays=KillSwitch.allocLong2D(mult, prime+extra);
354 for(int i=0; i<mult; i++){
355 Arrays.fill(arrays[i], NOT_PRESENT);
356 }
357 IntList2 oldList=setList;
358 setList=new IntList2();
359 setList.add(null);
360 setList.add(null);//TODO: May have to add 3 of them to avoid HASH_COLLISION at -2
361 values=allocInt1D(prime+extra);
362 ArrayList<KmerNodeU> list=victims.toList();
363 victims.clear();
364 size=0;
365
366 final int[] singleton=new int[] {NOT_PRESENT};
367 final Kmer kmer=new Kmer(kbig);
368 {
369 for(int i=0; i<oldk.length; i++){
370 if(oldk[0][i]>NOT_PRESENT){
371 final int v=oldc[i];
372 fillKmer(i, kmer, oldk);
373 if(v>=0){
374 set(kmer, v);
375 }else{
376 set(kmer, oldList.get(-v));
377 }
378 }
379 }
380 }
381
382 for(KmerNodeU n : list){
383 if(n.pivot[0]>NOT_PRESENT){
384 kmer.setFrom(n.pivot());
385 if(n.numValues()>1){
386 set(kmer, n.values(singleton));
387 }else{
388 set(kmer, n.value());
389 }
390 }else{assert(false);}
391 }
392
393 assert(oldSize+oldVSize==size+victims.size) : oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size);
394
395 if(verbose){System.err.println("Resized to "+prime+". "+oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size));}
396
397 sizeLimit=(long)(maxLoadFactor*prime);
398
399 // assert(TESTMODE);
400 // if(TESTMODE){
401 // for(int i=0; i<ll.size; i++){
402 // long[] kmer=ll.get(i);
403 // int v=il.get(i);
404 // assert(contains(kmer, v)) : i+", "+ll.size+", "+kmer+", "+v+", "+Arrays.toString(getValues(kmer, new int[1]));
405 // assert(!contains(kmer, Integer.MAX_VALUE));
406 // }
407 // }
408 }
409
410
411 @Deprecated
412 @Override
413 public void rebalance(){
414 throw new RuntimeException("Unimplemented.");
415 }
416
417 @Deprecated
418 @Override
419 public long regenerate(final int limit){
420 throw new RuntimeException("Not supported.");
421 }
422
423 /*--------------------------------------------------------------*/
424 /*---------------- Fields ----------------*/
425 /*--------------------------------------------------------------*/
426
427 private int[] values;
428 private IntList2 setList;
429
430
431
432 }