Debian Codegraph @

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