Skip to content

Commit 8e47167

Browse files
committed
add more method for Bits
1 parent 94e36c4 commit 8e47167

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
2626

2727
group = 'com.github.myibu'
2828
archivesBaseName = "algorithm-java"
29-
version = "1.0.0e"
29+
version = "1.0.0f"
3030

3131
repositories {
3232
mavenCentral()

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Reference to: [HoffmanAndGolombCoding.pdf](./docs/HoffmanAndGolombCoding.pdf)
5757
<dependency>
5858
<groupId>com.github.myibu</groupId>
5959
<artifactId>algorithm-java</artifactId>
60-
<version>1.0.0e</version>
60+
<version>1.0.0f</version>
6161
</dependency>
6262
```
6363

src/main/java/com/github/myibu/algorithm/data/Bits.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.charset.StandardCharsets;
44
import java.util.Arrays;
55
import java.util.Iterator;
6+
import java.util.List;
67
import java.util.NoSuchElementException;
78

89
/**
@@ -546,8 +547,19 @@ static void subBitsRangeCheck(int fromIndex, int toIndex, int size) {
546547
") > toIndex(" + toIndex + ")");
547548
}
548549

550+
@Deprecated
549551
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);
551563
}
552564

553565
@Override
@@ -593,6 +605,14 @@ public Bit get(int index) {
593605
return table[index];
594606
}
595607

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+
596616
@Override
597617
public Bits clone() {
598618
Bits dest = null;
@@ -618,4 +638,77 @@ public boolean equals(Object o) {
618638
public int hashCode() {
619639
return Arrays.hashCode(table);
620640
}
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+
}
621714
}

0 commit comments

Comments
 (0)