package structures; import java.io.Serializable; import dna.AminoAcid; import shared.KillSwitch; import shared.LineParser; import shared.Tools; import ukmer.Kmer; /** * @author Brian Bushnell * @date Oct 8, 2013 * */ public final class ByteBuilder implements Serializable, CharSequence { /** * */ private static final long serialVersionUID = -4786450129730831665L; public ByteBuilder(){ array=KillSwitch.allocByte1D(32); } public ByteBuilder(int initial){ assert(initial>=1) : initial; array=KillSwitch.allocByte1D(initial); } public ByteBuilder(Object o){ String s=o.toString(); array=KillSwitch.allocByte1D(s.length()+1); append(s); } public ByteBuilder(byte[] array_){ assert(array_!=null); array=array_; length=array.length; } public ByteBuilder(ByteBuilder bb){ array=bb.toBytes(); length=bb.length(); } @Override public CharSequence subSequence(int start, int end) throws IndexOutOfBoundsException{ if(start<0 || end>=length()){throw new IndexOutOfBoundsException();} return new ByteBuilder(KillSwitch.copyOfRange(array, start, end)); } public int indexOf(char c) {return indexOf((byte)c);} public int lastIndexOf(char c) {return lastIndexOf((byte)c);} public int count(char c) {return count((byte)c);} public int indexOf(byte c) { for(int i=0; i=0; i--) { if(array[i]==c) {return i;} } return -1; } public int count(byte c) { int count=0; for(int i=0; i=array.length){expand();} array[length]=(byte)x; length++; return this; } public ByteBuilder append(byte x){ if(length>=array.length){expand();} array[length]=x; length++; return this; } public ByteBuilder appendKmer(Kmer kmer) { return appendKmer(kmer.array1(), kmer.k); } public ByteBuilder appendKmer(long[] kmer, int k) { for(long subkmer : kmer){ appendKmer(subkmer, k); } return this; } /** * @param kmer * @param k */ public ByteBuilder appendKmer(long kmer, int k) { kmer=AminoAcid.reverseComplementBinaryFast(~kmer, k); for(int i=0; i>=2; } return this; } public ByteBuilder appendTerm(LineParser lp, int term) { return lp.appendTerm(this, term); } public ByteBuilder appendln(int x){ expand(12); append(x); return append('\n'); } public ByteBuilder append(int x){ expand(11); if(x<0){ if(x<=Integer.MIN_VALUE){ return append((long)x); }else{ array[length]='-'; length++; x=-x; } }else if(x==0){ array[length]='0'; length++; return this; } // final int len=lengthOf(x); // int pos=length+len-1; // while(x>9){ // int y=x%100; // x=x/100; // array[pos]=ones100[y]; // pos--; // array[pos]=tens100[y]; // pos--; // } // while(x>0){ // int y=x%10; // x=x/10; // array[pos]=numbers[y]; // pos--; // } // length+=len; // final int initial=length; // while(x>9){ // int y=x%100; // x=x/100; // array[length]=tens100[y]; // length--; // array[length]=ones100[y]; // length--; // } // while(x>0){ // int y=x%10; // x=x/10; // array[length]=numbers[y]; // length++; // } // // for(int i=initial, j=length-1; i9){ int y=x%100; x=x/100; numbuffer[pos]=ones100[y]; pos++; numbuffer[pos]=tens100[y]; pos++; } while(x>0){ int y=x%10; x=x/10; numbuffer[pos]=ones100[y]; pos++; } while(pos>0){ pos--; array[length]=numbuffer[pos]; length++; } return this; } public ByteBuilder append(long x){ if(x>Integer.MIN_VALUE && x<=Integer.MAX_VALUE){return append((int)x);} expand(20); if(x<0){ if(x==Long.MIN_VALUE){ return append(Long.toString(Long.MIN_VALUE)); }else{ array[length]='-'; length++; x=-x; } }else if(x==0){ array[length]='0'; length++; return this; } // final int len=lengthOf(x); // int pos=length+len-1; // while(x>9){ // int y=(int)(x%100); // x=x/100; // array[pos]=ones100[y]; // pos--; // array[pos]=tens100[y]; // pos--; // } // while(x>0){ // int y=(int)(x%10); // x=x/10; // array[pos]=numbers[y]; // pos--; // } // length+=len; int pos=0; while(x>9){ int y=(int)(x%100); x=x/100; numbuffer[pos]=ones100[y]; pos++; numbuffer[pos]=tens100[y]; pos++; } while(x>0){ int y=(int)(x%10); x=x/10; numbuffer[pos]=ones100[y]; pos++; } while(pos>0){ pos--; array[length]=numbuffer[pos]; length++; } return this; } public ByteBuilder append(final double x0, final int decimals0){ return append(x0, decimals0, false); } public ByteBuilder append(final double x0, final int decimals0, boolean concise){ // if(true){return append(x0, decimals0);} if(x0==(long)x0) {return append((long)x0);} if(decimals0<1){return append((long)(x0+0.5));} expand(21+decimals0); double x=x0; int decimals=decimals0; if(x<0){ array[length]='-'; length++; x=-x; } x=x+(0.5*decimalInvMult[decimals]); long upper=(long)x; long lower=(long)((x-upper)*decimalMult[decimals]); x*=decimalMult[decimals]; x=x+0.5; // long longRep=(long)x; // assert(longRep==(long)(decimalMult[decimals]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0))))) : // "\n"+longRep+"\n"+decimalMult[decimals]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0)))+"\n"+x0; // long upper=longRep/longMult[decimals]; // long lower=longRep%longMult[decimals]; // // append(upper); int pos=0; { //Lower digits for(; decimals>1; decimals-=2){ int y=(int)(lower%100); lower=lower/100; numbuffer[pos]=ones100[y]; pos++; numbuffer[pos]=tens100[y]; pos++; } for(; decimals>0; decimals--){ int y=(int)(lower%10); lower=lower/10; numbuffer[pos]=ones100[y]; pos++; } numbuffer[pos]='.'; pos++; //Upper digits if(upper==0){ numbuffer[pos]='0'; pos++; }else{ while(upper>9){ int y=(int)(upper%100); upper=upper/100; numbuffer[pos]=ones100[y]; pos++; numbuffer[pos]=tens100[y]; pos++; } while(upper>0){ int y=(int)(upper%10); upper=upper/10; numbuffer[pos]=ones100[y]; pos++; } } } // assert(Tools.format("%."+decimals0+"f", x0).equals(new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos))))) : // Tools.format("%."+decimals0+"f", x0)+"\n"+new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos))); // longRep=(long)x; // assert(longRep==(long)(decimalMult[decimals0]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0))))) : // "\n"+longRep+"\n"+decimalMult[decimals0]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0)))+ // "\n"+x0+"\n"+Tools.format("%."+decimals0+"f", x0)+"\n"+new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos))); int lim=0; if(concise) {//eliminates trailing zeros. This would also be a good place to limit sig figs. while(concise && numbuffer[lim]=='0' && limlim){ pos--; array[length]=numbuffer[pos]; length++; } return this; } public ByteBuilder append(String x){ if(x==null){return append(nullBytes);} expand(x.length()); for(int i=0; i=s.length() && Tools.startsWith(array, s); } /** * @param left Amount to trim from the left * @param right Amount to trim from the right */ public void trimByAmount(int left, int right) { assert(left>=0 && right>=0); int newlen=length-left-right; if(newlen==length){return;} length=Tools.max(newlen, 0); if(length<1){return;} for(int i=0, j=left; i0){ for(int i=0, j=len-overlap; j=0); if(array.length<=maxLen){return;} // assert(length<=maxLen) : length+", "+array.length+", "+maxLen; if(length<1){ array=KillSwitch.allocByte1D(maxLen); }else{ array=KillSwitch.copyOf(array, maxLen); length=Tools.min(length, maxLen); } } private final boolean isRoom(int x){ return array.length-length>=x; } private final void expand(){ long x=Tools.min(MAXLEN, array.length*2L); if(x<=array.length){ throw new RuntimeException("Array overflow: "+x+"<="+array.length); } assert(((int)x)>array.length) : "Array overflow: "+x+"<="+array.length; array=KillSwitch.copyOf(array, (int)x); } public ByteBuilder insert(int pos, byte c) { assert(pos<=length); if(pos==length){return append(c);} expand(1); for(int i=length-1; i>=pos; i--){ array[i+1]=array[i]; } array[pos]=c; length++; return this; } private final void expand(int extra){ long x=array.length; if(x>=length+extra){return;} // System.err.println("x="+array.length+", extra="+extra+", length="+length); while(x<=length+extra){ // System.err.println("*\t"+x+"-"+length+"<"+extra); x<<=1; } x=Tools.min(MAXLEN, x); assert(x>0 && ((int)x)>=array.length) : "Array overflow: "+x+"array.length) : "Resizing to an non-longer array ("+array.length+"); probable array size overflow."; array=KillSwitch.copyOf(array, (int)x); } public ByteBuilder reverse() { Tools.reverseInPlace(array, 0, length); return this; } public ByteBuilder reverseInPlace() { Tools.reverseInPlace(array, 0, length); return this; } public ByteBuilder reverseInPlace(int from, int toExclusive) { Tools.reverseInPlace(array, from, toExclusive); return this; } public ByteBuilder complementInPlace() { AminoAcid.complementBasesInPlace(array, length); return this; } public ByteBuilder reverseComplementInPlace() { AminoAcid.reverseComplementBasesInPlace(array, length); return this; } public final void ensureExtra(int extra){ if(array.length-length=0); setLength(length-x); return this; } public ByteBuilder setLength(int x){ assert(x>=0 && x<=array.length); length=x; return this; } public byte[] array; public int length=0; private final byte[] numbuffer=KillSwitch.allocByte1D(40); public static final byte[] numbers=new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static final byte[] nullBytes="null".getBytes(); public static final byte[] fbool="false".getBytes(); public static final byte[] tbool="true".getBytes(); public static final byte[] ones100, tens100; public static final double[] decimalMult, decimalInvMult; public static final long[] longMult; public static final String[] decimalFormat; public static final int MAXLEN=Integer.MAX_VALUE-20; static{ ones100=new byte[100]; tens100=new byte[100]; for(int i=0; i<100; i++){ ones100[i]=(byte)('0'+i%10); tens100[i]=(byte)('0'+i/10); } longMult=new long[50]; decimalMult=new double[50]; decimalInvMult=new double[50]; decimalFormat=new String[50]; decimalMult[0]=decimalInvMult[0]=longMult[0]=1; decimalFormat[0]="%.0f"; for(int i=1; i<50; i++){ longMult[i]=longMult[i-1]*10; decimalMult[i]=decimalMult[i-1]*10; decimalInvMult[i]=1/decimalMult[i]; decimalFormat[i]="%."+i+"f"; } } }