package ukmer; import java.util.ArrayList; import java.util.Arrays; import shared.KillSwitch; import shared.Primes; import shared.Shared; import shared.Tools; import structures.IntList2; /** * Stores kmers in a long[] and counts in an int[], with a victim cache. * @author Brian Bushnell * @date Oct 25, 2013 * */ public final class HashArrayUHybrid extends HashArrayU { /*--------------------------------------------------------------*/ /*---------------- Initialization ----------------*/ /*--------------------------------------------------------------*/ public HashArrayUHybrid(int[] schedule_, int k_, int kbig_){ super(schedule_, k_, kbig_, true); values=allocInt1D(prime+extra); setList=new IntList2(); setList.add(null); setList.add(null); } // public HashArrayUHybrid(int initialSize, int k_, int kbig_, boolean autoResize_){ // super(initialSize, k_, kbig_, autoResize_, true); // values=allocInt1D(prime+extra); // setList=new IntList2(); // setList.add(null); // setList.add(null); // } /*--------------------------------------------------------------*/ /*---------------- Public Methods ----------------*/ /*--------------------------------------------------------------*/ @Override public final int increment(final Kmer kmer){ final int cell=findKmerOrEmpty(kmer); if(cell==HASH_COLLISION){ int x=victims.increment(kmer); if(autoResize && size+victims.size>sizeLimit){resize();} return x; }else if(arrays[0][cell]==NOT_PRESENT){ setKmer(kmer.key(), cell); size++; values[cell]=1; if(autoResize && size+victims.size>sizeLimit){resize();} return 1; }else{ values[cell]++; if(values[cell]<0){values[cell]=Integer.MAX_VALUE;} return values[cell]; } } @Override public final int incrementAndReturnNumCreated(final Kmer kmer){ final int cell=findKmerOrEmpty(kmer); if(cell==HASH_COLLISION){ int x=victims.incrementAndReturnNumCreated(kmer); if(autoResize && size+victims.size>sizeLimit){resize();} return x; }else if(arrays[0][cell]==NOT_PRESENT){ setKmer(kmer.key(), cell); size++; values[cell]=1; if(autoResize && size+victims.size>sizeLimit){resize();} return 1; }else{ values[cell]++; if(values[cell]<0){values[cell]=Integer.MAX_VALUE;} return 0; } } /*--------------------------------------------------------------*/ /*---------------- Nonpublic Methods ----------------*/ /*--------------------------------------------------------------*/ @Override protected final int readCellValue(int cell) { final int x=values[cell]; if(x>-2){return x;} return setList.get(0-x)[0]; } @Override protected final int[] readCellValues(int cell, int[] singleton) { final int x=values[cell]; if(x>-2){ singleton[0]=values[cell]; return singleton; } return setList.get(0-x); } @Override protected final void insertValue(long[] kmer, int[] vals, int cell) { if(verbose){System.err.println("insertValue("+kmer+", "+Arrays.toString(vals)+", "+cell+"); old="+values[cell]);} assert(matches(kmer, cell)); if(vals.length==1){ if(verbose){System.err.println("A: length=1");} insertValue(kmer, vals[0], cell); return; } final int old=values[cell]; if(old==vals[0] && vals[1]==NOT_PRESENT){ if(verbose){System.err.println("B: old==vals[0] && vals[1]==-1");} return; //Nothing to do }else if(old<-1){//An array already exists if(verbose){System.err.println("C: old<-1");} for(int i : vals){ if(i==-1){break;} insertIntoList(i, -old); } }else{//Add the list final int[] temp; if(old>0){//Move the old value to a new array. Note that this will probably never be used. if(verbose){System.err.println("D: old>0");} temp=allocInt1D(vals.length+1); temp[0]=old; for(int i=0; i0");} temp=vals; } values[cell]=-setList.size; setList.add(temp); } } @Override protected final void insertValue(long[] kmer, int v, int cell) { assert(matches(kmer, cell)); assert(v>0); final int cc=values[cell]; if(cc==v){ return; }else if(cc<-1){ insertIntoList(v, -cc); }else if(cc>0){ values[cell]=-setList.size; setList.add(new int[] {cc, v, -1, -1}); }else{ values[cell]=v; } } private final int insertIntoList(final int v, final int loc){ if(loc>=setList.size){ assert(loc==setList.size); setList.add(null); } int[] set=setList.get(loc); if(set==null){ set=new int[] {-1, -1}; setList.set(loc, set); } for(int i=0; iset.length) : "Overflow."; set=KillSwitch.copyOf(set, newSize); set[oldSize]=v; Arrays.fill(set, oldSize+1, newSize, -1); setList.set(loc, set); return 1; } /*--------------------------------------------------------------*/ /*---------------- Resizing and Rebalancing ----------------*/ /*--------------------------------------------------------------*/ @Override public final boolean canRebalance() {return false;} // @Override // protected synchronized void resize(){ // // if(verbose){ // System.err.println("Resizing from "+prime+"; load="+(size*1f/prime)); // } // //// assert(TESTMODE); //// if(TESTMODE){ //// for(int i=0; i "+size+" + "+victims.size+" = "+(size+victims.size); // // if(verbose){System.err.println("Resized to "+prime+". "+oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size));} // // sizeLimit=(long)(maxLoadFactor*prime); // //// assert(TESTMODE); //// if(TESTMODE){ //// for(int i=0; i "+size+" + "+victims.size+" = "+(size+victims.size); if(verbose){System.err.println("Resized to "+prime+". "+oldSize+" + "+oldVSize+" = "+(oldSize+oldVSize)+" -> "+size+" + "+victims.size+" = "+(size+victims.size));} sizeLimit=(long)(maxLoadFactor*prime); // assert(TESTMODE); // if(TESTMODE){ // for(int i=0; i