3
3
import java .nio .charset .StandardCharsets ;
4
4
import java .util .Arrays ;
5
5
import java .util .Iterator ;
6
+ import java .util .List ;
6
7
import java .util .NoSuchElementException ;
7
8
8
9
/**
@@ -546,8 +547,19 @@ static void subBitsRangeCheck(int fromIndex, int toIndex, int size) {
546
547
") > toIndex(" + toIndex + ")" );
547
548
}
548
549
550
+ @ Deprecated
549
551
public Bit [] table (){
550
- return this .table ;
552
+ return toArray ();
553
+ }
554
+
555
+ public Bit [] toArray () {
556
+ return Arrays .copyOf (table , used );
557
+ }
558
+
559
+ public List <Bit > toList () {
560
+ Bit [] usedTable = new Bit [used ];
561
+ System .arraycopy (table , 0 , usedTable , 0 , used );
562
+ return Arrays .asList (usedTable );
551
563
}
552
564
553
565
@ Override
@@ -593,6 +605,14 @@ public Bit get(int index) {
593
605
return table [index ];
594
606
}
595
607
608
+ public Bit set (int index , Bit bit ) {
609
+ if (index < 0 || index >= used )
610
+ throw new IndexOutOfBoundsException ();
611
+ Bit oldValue = table [index ];
612
+ table [index ] = bit ;
613
+ return oldValue ;
614
+ }
615
+
596
616
@ Override
597
617
public Bits clone () {
598
618
Bits dest = null ;
@@ -618,4 +638,77 @@ public boolean equals(Object o) {
618
638
public int hashCode () {
619
639
return Arrays .hashCode (table );
620
640
}
641
+
642
+ public static void copy (Bits src , int srcPos , Bits dest , int destPos , int length ) {
643
+ System .arraycopy (src .table , srcPos , dest .table , destPos , length );
644
+ }
645
+
646
+ public static class Encoder {
647
+ public static Bits encodeIntValue (int value ) {
648
+ Bits bits = new Bits ();
649
+ int div = 0 ;
650
+ while ((div = (value / 2 )) != 0 ) {
651
+ int left = value - div * 2 ;
652
+ bits .append (left == 0 ? Bit .ZERO : Bit .ONE );
653
+ value = div ;
654
+ }
655
+ bits .append (value == 1 ? Bit .ONE : Bit .ZERO );
656
+ return Bits .reverse (bits );
657
+ }
658
+
659
+ public static Bits encodeDecimalValue (double value ) {
660
+ Bits bits = new Bits ();
661
+ while (!firstNumAfterDotIsZero (value )) {
662
+ int left = (int )(value * 2 );
663
+ bits .append (left == 0 ? Bit .ZERO : Bit .ONE );
664
+ value = value * 2 - left ;
665
+ }
666
+ return bits ;
667
+ }
668
+
669
+ private static boolean firstNumAfterDotIsZero (double value ) {
670
+ String doubleValue = "" + value ;
671
+ int dotIndex = -1 ;
672
+ if ((dotIndex = doubleValue .indexOf ("." )) != -1 ) {
673
+ if (dotIndex + 1 < doubleValue .length ()) {
674
+ return doubleValue .charAt (dotIndex +1 ) == '0' ;
675
+ }
676
+ }
677
+ return false ;
678
+ }
679
+
680
+ public static Bits encodeFloatValue (float value ) {
681
+ int intValue = (int )value ;
682
+ float dotValue = value - intValue ;
683
+ Bits bits1 = encodeIntValue (intValue );
684
+ Bits bits2 = encodeDecimalValue (dotValue );
685
+ Bits S = value < 0 ? Bits .ofOne () : Bits .ofZero ();
686
+ Bits E = Bits .ofZero (8 );
687
+ Bits eValue = encodeIntValue (bits1 .length () - 1 + 127 );
688
+ Bits .copy (eValue , 0 , E , 0 ,8 );
689
+ Bits M = Bits .ofZero (23 );
690
+ Bits .copy (bits1 , 1 , M , 0 , bits1 .length () - 1 );
691
+ Bits .copy (bits2 , 0 , M , bits1 .length () - 1 , bits2 .length ());
692
+ return S .append (E ).append (M );
693
+ }
694
+
695
+ public static Bits encodeDoubleValue (double value ) {
696
+ int intValue = (int )value ;
697
+ double dotValue = value - intValue ;
698
+ Bits bits1 = encodeIntValue (intValue );
699
+ Bits bits2 = encodeDecimalValue (dotValue );
700
+ Bits S = value < 0 ? Bits .ofOne () : Bits .ofZero ();
701
+ Bits E = Bits .ofZero (11 );
702
+ Bits eValue = encodeIntValue (bits1 .length () - 1 + 1023 );
703
+ Bits .copy (eValue , 0 , E , 0 ,11 );
704
+ Bits M = Bits .ofZero (52 );
705
+ Bits .copy (bits1 , 1 , M , 0 , bits1 .length () - 1 );
706
+ Bits .copy (bits2 , 0 , M , bits1 .length () - 1 , bits2 .length ());
707
+ return S .append (E ).append (M );
708
+ }
709
+
710
+ public static Bits encodeStringValue (String value ) {
711
+ return Bits .ofByte (value .getBytes (StandardCharsets .UTF_8 ));
712
+ }
713
+ }
621
714
}
0 commit comments