| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public final class HashArrayUHybrid extends HashArrayU { |
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 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; |
| 120 |
}else if(old<-1){ |
| 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{ |
| 127 |
final int[] temp; |
| 128 |
if(old>0){ |
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
@Override |
| 191 |
public final boolean canRebalance() {return false;} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 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 |
|
| 308 |
|
| 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{ |
| 321 |
final long maxAllowedByLoadFactor=(long)(totalSize*minLoadMult); |
| 322 |
final long minAllowedByLoadFactor=(long)(totalSize*maxLoadMult); |
| 323 |
|
| 324 |
|
| 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 |
|
| 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); |
| 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 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 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 |
|
| 425 |
|
| 426 |
|
| 427 |
private int[] values; |
| 428 |
private IntList2 setList; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
} |