| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public final class HashArrayHybridFast extends HashArray { |
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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){ |
| 125 |
if(verbose){System.err.println("B: old==vals[0] && vals[1]==-1");} |
| 126 |
return; |
| 127 |
}else if(old<-1){ |
| 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{ |
| 134 |
final int[] temp; |
| 135 |
if(old>0){ |
| 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 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if(prime>=maxPrime){ |
| 195 |
|
| 196 |
|
| 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{ |
| 207 |
final long totalSize=oldSize+oldVSize; |
| 208 |
|
| 209 |
final long maxAllowedByLoadFactor=(long)(totalSize*minLoadMult); |
| 210 |
final long minAllowedByLoadFactor=(long)(totalSize*maxLoadMult); |
| 211 |
|
| 212 |
|
| 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 |
|
| 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)); |
| 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 |
|
| 256 |
for(int i=0; i<oldKmers.length; i++){ |
| 257 |
final long kmer=oldKmers[i]; |
| 258 |
if(kmer!=-1){ |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
final int v=oldValues[i]; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
assert(v<-1 || v>0); |
| 268 |
if(v>=0){ |
| 269 |
|
| 270 |
|
| 271 |
set(kmer, v); |
| 272 |
|
| 273 |
|
| 274 |
}else{ |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
set(kmer, oldList.get(-v), oldList.getLen(-v)); |
| 282 |
|
| 283 |
|
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
final int[] singleton=new int[1]; |
| 290 |
|
| 291 |
for(KmerNode n : nodeList){ |
| 292 |
if(n.pivot>-1){ |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
if(n.numValues()>1){ |
| 297 |
|
| 298 |
|
| 299 |
set(n.pivot, n.values(singleton), n.numValues()); |
| 300 |
|
| 301 |
|
| 302 |
}else{ |
| 303 |
|
| 304 |
|
| 305 |
set(n.pivot, n.value()); |
| 306 |
|
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
} |
| 312 |
} |
| 313 |
|
| 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 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 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 |
|
| 344 |
|
| 345 |
|
| 346 |
private int[] values; |
| 347 |
private IntList3 setList; |
| 348 |
|
| 349 |
} |