Debian Codegraph @

current/structures/ByteBuilder.java

raw symbols SCIP
1 package structures;
2
3 import java.io.Serializable;
4
5 import dna.AminoAcid;
6 import shared.KillSwitch;
7 import shared.LineParser;
8 import shared.Tools;
9 import ukmer.Kmer;
10
11 /**
12 * @author Brian Bushnell
13 * @date Oct 8, 2013
14 *
15 */
16 public final class ByteBuilder implements Serializable, CharSequence {
17
18 /**
19 *
20 */
21 private static final long serialVersionUID = -4786450129730831665L;
22
23 public ByteBuilder(){
24 array=KillSwitch.allocByte1D(32);
25 }
26
27 public ByteBuilder(int initial){
28 assert(initial>=1) : initial;
29 array=KillSwitch.allocByte1D(initial);
30 }
31
32 public ByteBuilder(Object o){
33 String s=o.toString();
34 array=KillSwitch.allocByte1D(s.length()+1);
35 append(s);
36 }
37
38 public ByteBuilder(byte[] array_){
39 assert(array_!=null);
40 array=array_;
41 length=array.length;
42 }
43
44 public ByteBuilder(ByteBuilder bb){
45 array=bb.toBytes();
46 length=bb.length();
47 }
48
49 @Override
50 public CharSequence subSequence(int start, int end) throws IndexOutOfBoundsException{
51 if(start<0 || end>=length()){throw new IndexOutOfBoundsException();}
52 return new ByteBuilder(KillSwitch.copyOfRange(array, start, end));
53 }
54
55 public int indexOf(char c) {return indexOf((byte)c);}
56 public int lastIndexOf(char c) {return lastIndexOf((byte)c);}
57 public int count(char c) {return count((byte)c);}
58
59 public int indexOf(byte c) {
60 for(int i=0; i<length; i++) {
61 if(array[i]==c) {return i;}
62 }
63 return -1;
64 }
65
66 public int lastIndexOf(byte c) {
67 for(int i=length-1; i>=0; i--) {
68 if(array[i]==c) {return i;}
69 }
70 return -1;
71 }
72
73 public int count(byte c) {
74 int count=0;
75 for(int i=0; i<length; i++) {
76 count+=(array[i]==c) ? 1 : 0;
77 }
78 return count;
79 }
80
81 // public ByteBuilder append(float x, int places){return append(Tools.format(decimalFormat[places], x));}
82 // public ByteBuilder append(double x, int places){return append(Tools.format(decimalFormat[places], x));}
83
84 public ByteBuilder appendSlow(float x){return append(Float.toString(x));}
85 public ByteBuilder appendSlow(double x){return append(Double.toString(x));}
86 public ByteBuilder appendSlow(float x, int decimals){return append(Tools.format("%."+decimals+"f", x));}
87 public ByteBuilder appendSlow(double x, int decimals){return append(Tools.format("%."+decimals+"f", x));}
88 public ByteBuilder append(boolean x){return append(x ? tbool : fbool);}
89
90 public ByteBuilder append(char a, char b) {
91 append(a);append(b);
92 return this;
93 }
94 public ByteBuilder append(byte a, byte b) {
95 append(a);append(b);
96 return this;
97 }
98 public ByteBuilder append(char a, char b, char c) {
99 append(a);append(b);append(c);
100 return this;
101 }
102 public ByteBuilder append(byte a, byte b, byte c) {
103 append(a);append(b);append(c);
104 return this;
105 }
106 public ByteBuilder append(char a, char b, char c, char d) {
107 append(a);append(b);append(c);append(d);
108 return this;
109 }
110 public ByteBuilder append(byte a, byte b, byte c, byte d) {
111 append(a);append(b);append(c);append(d);
112 return this;
113 }
114 public ByteBuilder append(char x, int y){
115 append(x); append(y); return this;
116 }
117 public ByteBuilder append(char x, long y){
118 append(x); append(y); return this;
119 }
120
121 public ByteBuilder append(char x){
122 if(length>=array.length){expand();}
123 array[length]=(byte)x;
124 length++;
125 return this;
126 }
127 public ByteBuilder append(byte x){
128 if(length>=array.length){expand();}
129 array[length]=x;
130 length++;
131 return this;
132 }
133
134 public ByteBuilder appendKmer(Kmer kmer) {
135 return appendKmer(kmer.array1(), kmer.k);
136 }
137
138 public ByteBuilder appendKmer(long[] kmer, int k) {
139 for(long subkmer : kmer){
140 appendKmer(subkmer, k);
141 }
142 return this;
143 }
144
145 /**
146 * @param kmer
147 * @param k
148 */
149 public ByteBuilder appendKmer(long kmer, int k) {
150 kmer=AminoAcid.reverseComplementBinaryFast(~kmer, k);
151 for(int i=0; i<k; i++){
152 int x=(int)(kmer&3);
153 append((char)AminoAcid.numberToBase[x]);
154 kmer>>=2;
155 }
156 return this;
157 }
158
159 public ByteBuilder appendTerm(LineParser lp, int term) {
160 return lp.appendTerm(this, term);
161 }
162
163 public ByteBuilder appendln(int x){
164 expand(12);
165 append(x);
166 return append('\n');
167 }
168
169 public ByteBuilder append(int x){
170 expand(11);
171 if(x<0){
172 if(x<=Integer.MIN_VALUE){
173 return append((long)x);
174 }else{
175 array[length]='-';
176 length++;
177 x=-x;
178 }
179 }else if(x==0){
180 array[length]='0';
181 length++;
182 return this;
183 }
184
185 // final int len=lengthOf(x);
186 // int pos=length+len-1;
187 // while(x>9){
188 // int y=x%100;
189 // x=x/100;
190 // array[pos]=ones100[y];
191 // pos--;
192 // array[pos]=tens100[y];
193 // pos--;
194 // }
195 // while(x>0){
196 // int y=x%10;
197 // x=x/10;
198 // array[pos]=numbers[y];
199 // pos--;
200 // }
201 // length+=len;
202
203 // final int initial=length;
204 // while(x>9){
205 // int y=x%100;
206 // x=x/100;
207 // array[length]=tens100[y];
208 // length--;
209 // array[length]=ones100[y];
210 // length--;
211 // }
212 // while(x>0){
213 // int y=x%10;
214 // x=x/10;
215 // array[length]=numbers[y];
216 // length++;
217 // }
218 //
219 // for(int i=initial, j=length-1; i<j; i++, j--){
220 // byte temp=array[i];
221 // array[i]=array[j];
222 // array[j]=temp;
223 // }
224
225
226
227 int pos=0;
228 while(x>9){
229 int y=x%100;
230 x=x/100;
231 numbuffer[pos]=ones100[y];
232 pos++;
233 numbuffer[pos]=tens100[y];
234 pos++;
235 }
236 while(x>0){
237 int y=x%10;
238 x=x/10;
239 numbuffer[pos]=ones100[y];
240 pos++;
241 }
242
243 while(pos>0){
244 pos--;
245 array[length]=numbuffer[pos];
246 length++;
247 }
248
249 return this;
250 }
251
252 public ByteBuilder append(long x){
253 if(x>Integer.MIN_VALUE && x<=Integer.MAX_VALUE){return append((int)x);}
254 expand(20);
255 if(x<0){
256 if(x==Long.MIN_VALUE){
257 return append(Long.toString(Long.MIN_VALUE));
258 }else{
259 array[length]='-';
260 length++;
261 x=-x;
262 }
263 }else if(x==0){
264 array[length]='0';
265 length++;
266 return this;
267 }
268
269 // final int len=lengthOf(x);
270 // int pos=length+len-1;
271 // while(x>9){
272 // int y=(int)(x%100);
273 // x=x/100;
274 // array[pos]=ones100[y];
275 // pos--;
276 // array[pos]=tens100[y];
277 // pos--;
278 // }
279 // while(x>0){
280 // int y=(int)(x%10);
281 // x=x/10;
282 // array[pos]=numbers[y];
283 // pos--;
284 // }
285 // length+=len;
286
287 int pos=0;
288 while(x>9){
289 int y=(int)(x%100);
290 x=x/100;
291 numbuffer[pos]=ones100[y];
292 pos++;
293 numbuffer[pos]=tens100[y];
294 pos++;
295 }
296 while(x>0){
297 int y=(int)(x%10);
298 x=x/10;
299 numbuffer[pos]=ones100[y];
300 pos++;
301 }
302
303 while(pos>0){
304 pos--;
305 array[length]=numbuffer[pos];
306 length++;
307 }
308
309 return this;
310 }
311
312 public ByteBuilder append(final double x0, final int decimals0){
313 return append(x0, decimals0, false);
314 }
315
316 public ByteBuilder append(final double x0, final int decimals0, boolean concise){
317 // if(true){return append(x0, decimals0);}
318 if(x0==(long)x0) {return append((long)x0);}
319 if(decimals0<1){return append((long)(x0+0.5));}
320 expand(21+decimals0);
321 double x=x0;
322 int decimals=decimals0;
323 if(x<0){
324 array[length]='-';
325 length++;
326 x=-x;
327 }
328 x=x+(0.5*decimalInvMult[decimals]);
329 long upper=(long)x;
330 long lower=(long)((x-upper)*decimalMult[decimals]);
331 x*=decimalMult[decimals];
332 x=x+0.5;
333 // long longRep=(long)x;
334 // assert(longRep==(long)(decimalMult[decimals]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0))))) :
335 // "\n"+longRep+"\n"+decimalMult[decimals]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0)))+"\n"+x0;
336
337 // long upper=longRep/longMult[decimals];
338 // long lower=longRep%longMult[decimals];
339 //
340 // append(upper);
341
342 int pos=0;
343
344 {
345 //Lower digits
346 for(; decimals>1; decimals-=2){
347 int y=(int)(lower%100);
348 lower=lower/100;
349 numbuffer[pos]=ones100[y];
350 pos++;
351 numbuffer[pos]=tens100[y];
352 pos++;
353 }
354 for(; decimals>0; decimals--){
355 int y=(int)(lower%10);
356 lower=lower/10;
357 numbuffer[pos]=ones100[y];
358 pos++;
359 }
360 numbuffer[pos]='.';
361 pos++;
362
363 //Upper digits
364 if(upper==0){
365 numbuffer[pos]='0';
366 pos++;
367 }else{
368 while(upper>9){
369 int y=(int)(upper%100);
370 upper=upper/100;
371 numbuffer[pos]=ones100[y];
372 pos++;
373 numbuffer[pos]=tens100[y];
374 pos++;
375 }
376 while(upper>0){
377 int y=(int)(upper%10);
378 upper=upper/10;
379 numbuffer[pos]=ones100[y];
380 pos++;
381 }
382 }
383 }
384
385 // assert(Tools.format("%."+decimals0+"f", x0).equals(new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos))))) :
386 // Tools.format("%."+decimals0+"f", x0)+"\n"+new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos)));
387
388 // longRep=(long)x;
389 // assert(longRep==(long)(decimalMult[decimals0]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0))))) :
390 // "\n"+longRep+"\n"+decimalMult[decimals0]*Double.parseDouble((Tools.format("%."+decimals0+"f", x0)))+
391 // "\n"+x0+"\n"+Tools.format("%."+decimals0+"f", x0)+"\n"+new String(Tools.reverseAndCopy(KillSwitch.copyOf(numbuffer, pos)));
392
393 int lim=0;
394 if(concise) {//eliminates trailing zeros. This would also be a good place to limit sig figs.
395 while(concise && numbuffer[lim]=='0' && lim<decimals0-1) {lim++;}
396 }
397
398 while(pos>lim){
399 pos--;
400 array[length]=numbuffer[pos];
401 length++;
402 }
403
404 return this;
405 }
406
407 public ByteBuilder append(String x){
408 if(x==null){return append(nullBytes);}
409 expand(x.length());
410 for(int i=0; i<x.length(); i++){
411 array[length]=(byte)x.charAt(i);
412 length++;
413 }
414 return this;
415 }
416
417 public ByteBuilder append(String x, int from, int toExclusive){
418 int len=toExclusive-from;
419 if(x==null || len<1){return append(nullBytes);}
420 expand(len);
421 for(int i=from; i<toExclusive; i++){
422 array[length]=(byte)x.charAt(i);
423 length++;
424 }
425 return this;
426 }
427
428 public ByteBuilder append(StringBuilder x){
429 if(x==null){return append(nullBytes);}
430 expand(x.length());
431 for(int i=0; i<x.length(); i++){
432 array[length]=(byte)x.charAt(i);
433 length++;
434 }
435 return this;
436 }
437
438 public ByteBuilder append(CharSequence x){
439 if(x==null){return append(nullBytes);}
440 expand(x.length());
441 for(int i=0; i<x.length(); i++){
442 array[length]=(byte)x.charAt(i);
443 length++;
444 }
445 return this;
446 }
447
448 public ByteBuilder appendln(CharSequence x){
449 expand(x.length()+1);
450 append(x);
451 array[length]='\n';
452 length++;
453 return this;
454 }
455
456 // public ByteBuilder append(Object x){
457 // if(x==null){return append(nullBytes);}
458 // return append(x.toString());
459 // }
460
461 public ByteBuilder append(byte[] x){
462 if(x==null){x=nullBytes;}
463 expand(x.length);
464 for(int i=0; i<x.length; i++){
465 array[length]=x[i];
466 length++;
467 }
468 return this;
469 }
470
471 public ByteBuilder appendln(byte[] x){
472 expand(x.length+1);
473 append(x);
474 array[length]='\n';
475 length++;
476 return this;
477 }
478
479 public ByteBuilder appendQuality(byte[] x){
480 if(x==null){return this;}
481 expand(x.length);
482 for(int i=0; i<x.length; i++){
483 array[length]=(byte)(x[i]+33);
484 length++;
485 }
486 return this;
487 }
488
489 public ByteBuilder appendQualityDif(byte[] x){
490 if(x==null){return this;}
491 expand(x.length);
492 int last=0;
493 for(int i=0; i<x.length; i++){
494 final int q=x[i];
495 assert(q<=44);
496 array[length]=(byte)((q-last)+77);
497 length++;
498 last=q;
499 }
500 return this;
501 }
502
503 public ByteBuilder append(ByteBuilder bb){
504 return append(bb.array, 0, bb.length);
505 }
506
507 public ByteBuilder appendln(ByteBuilder x){
508 expand(x.length+1);
509 append(x);
510 array[length]='\n';
511 length++;
512 return this;
513 }
514
515 public ByteBuilder append(byte[] x, int len){
516 return append(x, 0, len);
517 }
518
519 public ByteBuilder append(byte[] x, int start, int len){
520 // if(x==null){x=nullBytes;}
521 final int lim=(int)Tools.min(start+(long)len, x.length);
522 len=Tools.min(len, x.length-start);
523 expand(len);
524 for(int i=start; i<lim; i++){
525 array[length]=x[i];
526 length++;
527 }
528 return this;
529 }
530
531 public ByteBuilder append(char[] x){
532 if(x==null){return append(nullBytes);}
533 expand(x.length);
534 for(int i=0; i<x.length; i++){
535 array[length]=(byte)x[i];
536 length++;
537 }
538 return this;
539 }
540
541 public ByteBuilder appendln(char[] x){
542 expand(x.length+1);
543 append(x);
544 array[length]='\n';
545 length++;
546 return this;
547 }
548
549 /**
550 * Append a newline.
551 * @return This ByteBuilder.
552 */
553 public ByteBuilder nl(){return append('\n');}
554
555 /**
556 * Append a tab.
557 * @return This ByteBuilder.
558 */
559 public ByteBuilder tab(){return append('\t');}
560
561 /**
562 * Append a space.
563 * @return This ByteBuilder.
564 */
565 public ByteBuilder space(){return append(' ');}
566
567 /**
568 * Append an underscore.
569 * @return This ByteBuilder.
570 */
571 public ByteBuilder under(){return append('_');}
572
573 /**
574 * Append a dash (hyphen).
575 * @return This ByteBuilder.
576 */
577 public ByteBuilder dash(){return append('-');}
578
579 /**
580 * Append a period.
581 * @return This ByteBuilder.
582 */
583 public ByteBuilder dot(){return append('.');}
584
585 /**
586 * Append a comma.
587 * @return This ByteBuilder.
588 */
589 public ByteBuilder comma(){return append(',');}
590
591 /**
592 * Append a colon.
593 * @return This ByteBuilder.
594 */
595 public ByteBuilder colon(){return append(':');}
596
597 /**
598 * Append a colon.
599 * @return This ByteBuilder.
600 */
601 public ByteBuilder equals(){return append('=');}
602
603 /**
604 * Append a semicolon.
605 * @return This ByteBuilder.
606 */
607 public ByteBuilder semi(){return append(';');}
608
609 /**
610 * Append a plus.
611 * @return This ByteBuilder.
612 */
613 public ByteBuilder plus(){return append('+');}
614
615 /**
616 * Append a percent.
617 * @return This ByteBuilder.
618 */
619 public ByteBuilder percent(){return append('%');}
620
621 public byte get(int i){
622 assert(i<length);
623 return array[i];
624 }
625
626 public void set(int i, byte b){
627 assert(i<length);
628 array[i]=b;
629 }
630
631 @Override
632 public char charAt(int i){
633 assert(i<length);
634 return (char)array[i];
635 }
636
637 public boolean endsWith(char c) {
638 return length<1 ? false : array[length-1]==c;
639 }
640
641 public boolean startsWith(String s) {
642 return length>=s.length() && Tools.startsWith(array, s);
643 }
644
645 /**
646 * @param left Amount to trim from the left
647 * @param right Amount to trim from the right
648 */
649 public void trimByAmount(int left, int right) {
650 assert(left>=0 && right>=0);
651 int newlen=length-left-right;
652 if(newlen==length){return;}
653 length=Tools.max(newlen, 0);
654 if(length<1){return;}
655 for(int i=0, j=left; i<newlen; i++, j++){
656 array[i]=array[j];
657 }
658 }
659
660 @Override
661 public final String toString(){
662 return new String(array, 0, length);
663 }
664
665 public final byte[] toBytes(){
666 return KillSwitch.copyOf(array, length);
667 }
668
669 public final byte[] toBytes(int from, int to){
670 return KillSwitch.copyOfRange(array, from, to);
671 }
672
673 public final byte[] expelAndShift(int len, int overlap){
674 assert(overlap<=len) : overlap+", "+len;
675 assert(len<=length);
676 byte[] expel=KillSwitch.copyOf(array, len);
677 if(len>0){
678 for(int i=0, j=len-overlap; j<length; i++, j++){
679 array[i]=array[j];
680 }
681 }
682 length=length-len+overlap;
683 return expel;
684 }
685
686 public void shrinkTo(int maxLen) {
687 assert(maxLen>=0);
688 if(array.length<=maxLen){return;}
689 // assert(length<=maxLen) : length+", "+array.length+", "+maxLen;
690 if(length<1){
691 array=KillSwitch.allocByte1D(maxLen);
692 }else{
693 array=KillSwitch.copyOf(array, maxLen);
694 length=Tools.min(length, maxLen);
695 }
696 }
697
698 private final boolean isRoom(int x){
699 return array.length-length>=x;
700 }
701
702 private final void expand(){
703 long x=Tools.min(MAXLEN, array.length*2L);
704 if(x<=array.length){
705 throw new RuntimeException("Array overflow: "+x+"<="+array.length);
706 }
707 assert(((int)x)>array.length) : "Array overflow: "+x+"<="+array.length;
708 array=KillSwitch.copyOf(array, (int)x);
709 }
710
711 public ByteBuilder insert(int pos, byte c) {
712 assert(pos<=length);
713 if(pos==length){return append(c);}
714
715 expand(1);
716 for(int i=length-1; i>=pos; i--){
717 array[i+1]=array[i];
718 }
719 array[pos]=c;
720 length++;
721 return this;
722 }
723
724 private final void expand(int extra){
725 long x=array.length;
726 if(x>=length+extra){return;}
727 // System.err.println("x="+array.length+", extra="+extra+", length="+length);
728 while(x<=length+extra){
729 // System.err.println("*\t"+x+"-"+length+"<"+extra);
730 x<<=1;
731 }
732 x=Tools.min(MAXLEN, x);
733 assert(x>0 && ((int)x)>=array.length) : "Array overflow: "+x+"<array.length";
734 assert(x>array.length) : "Resizing to an non-longer array ("+array.length+"); probable array size overflow.";
735 array=KillSwitch.copyOf(array, (int)x);
736 }
737
738 public ByteBuilder reverse() {
739 Tools.reverseInPlace(array, 0, length);
740 return this;
741 }
742
743 public ByteBuilder reverseInPlace() {
744 Tools.reverseInPlace(array, 0, length);
745 return this;
746 }
747
748 public ByteBuilder reverseInPlace(int from, int toExclusive) {
749 Tools.reverseInPlace(array, from, toExclusive);
750 return this;
751 }
752
753 public ByteBuilder complementInPlace() {
754 AminoAcid.complementBasesInPlace(array, length);
755 return this;
756 }
757
758 public ByteBuilder reverseComplementInPlace() {
759 AminoAcid.reverseComplementBasesInPlace(array, length);
760 return this;
761 }
762
763 public final void ensureExtra(int extra){
764 if(array.length-length<extra){expand(extra);}
765 }
766
767 public boolean isEmpty(){return length==0;}
768 @Override
769 public int length(){return length;}
770 public ByteBuilder clear(){
771 length=0;
772 return this;
773 }
774 public ByteBuilder trimLast(int x){
775 assert(x>=0);
776 setLength(length-x);
777 return this;
778 }
779 public ByteBuilder setLength(int x){
780 assert(x>=0 && x<=array.length);
781 length=x;
782 return this;
783 }
784
785 public byte[] array;
786 public int length=0;
787 private final byte[] numbuffer=KillSwitch.allocByte1D(40);
788
789 public static final byte[] numbers=new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
790 public static final byte[] nullBytes="null".getBytes();
791 public static final byte[] fbool="false".getBytes();
792 public static final byte[] tbool="true".getBytes();
793
794 public static final byte[] ones100, tens100;
795
796 public static final double[] decimalMult, decimalInvMult;
797 public static final long[] longMult;
798 public static final String[] decimalFormat;
799 public static final int MAXLEN=Integer.MAX_VALUE-20;
800
801 static{
802 ones100=new byte[100];
803 tens100=new byte[100];
804 for(int i=0; i<100; i++){
805 ones100[i]=(byte)('0'+i%10);
806 tens100[i]=(byte)('0'+i/10);
807 }
808 longMult=new long[50];
809 decimalMult=new double[50];
810 decimalInvMult=new double[50];
811 decimalFormat=new String[50];
812 decimalMult[0]=decimalInvMult[0]=longMult[0]=1;
813 decimalFormat[0]="%.0f";
814 for(int i=1; i<50; i++){
815 longMult[i]=longMult[i-1]*10;
816 decimalMult[i]=decimalMult[i-1]*10;
817 decimalInvMult[i]=1/decimalMult[i];
818 decimalFormat[i]="%."+i+"f";
819 }
820 }
821
822 }