Skip to content

Commit 6ee7833

Browse files
committed
add lz77 compress
1 parent 5814ed5 commit 6ee7833

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/main/java/com/github/myibu/algorithm/compress/LZ77Compressor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.github.myibu.algorithm.compress;
22

3+
import com.github.myibu.algorithm.data.Bits;
4+
import com.github.myibu.algorithm.endode.GolombEncoder;
5+
6+
import java.util.ArrayList;
37
import java.util.Arrays;
8+
import java.util.List;
49

510
/**
611
* LZ77 compress algorithm
@@ -48,6 +53,7 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
4853
System.arraycopy(in_data, 0, out_data, 0, in_len);
4954
return in_len;
5055
}
56+
List<List<Integer>> tuples = new ArrayList<>();
5157
// search buffer
5258
byte[] sBuf = new byte[s];
5359
// look ahead window
@@ -87,6 +93,7 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
8793
// byte[] tuple = String.format("(%d,%d,%s)", minIndex + 1, minMatched, new String(new byte[]{lWindow[minMatched]})).getBytes();
8894
// System.arraycopy(tuple, 0, out_data, (op++) * tuple.length, tuple.length);
8995
System.out.println(String.format("(%d, %d, %s)", minIndex + 1, minMatched, new String(new byte[]{lWindow[minMatched]})));
96+
tuples.add(Arrays.asList( minIndex + 1, minMatched, (int)lWindow[minMatched]));
9097
sp += (minMatched + 1);
9198
// if (sp > s) {
9299
// sp = s-1;
@@ -101,8 +108,23 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
101108
// byte[] tuple = String.format("(%d,%d,%s)", 0, 0, new String(new byte[]{lWindow[0]})).getBytes();
102109
// System.arraycopy(tuple, 0, out_data, (op++) * tuple.length, tuple.length);
103110
System.out.println(String.format("(%d, %d, %s)", 0, 0, new String(new byte[]{lWindow[0]})));
111+
tuples.add(Arrays.asList(0, 0, (int)lWindow[0]));
104112
}
105113
}
114+
System.out.println(tuples);
115+
int sum = 0;
116+
GolombEncoder encoder = new GolombEncoder();
117+
for (List<Integer> tuple: tuples) {
118+
Bits bits = new Bits();
119+
bits.append(encoder.encodeToBinary(tuple.get(0), (int)(Math.ceil(Math.log(s) / Math.log(2)))));
120+
System.out.println("1" + bits);
121+
bits.append(encoder.encode(tuple.get(1), 5));
122+
System.out.println("2" + bits);
123+
bits.append(Bits.ofByte((byte)tuple.get(2).intValue()));
124+
System.out.println("3" + bits);
125+
sum += bits.length();
126+
}
127+
System.out.println("compressed length: " + sum);
106128
return 0;
107129
}
108130

src/main/java/com/github/myibu/algorithm/endode/GolombEncoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Bits encode(int n, int m) {
3535
}
3636
}
3737

38-
private Bits encodeToTruncatedBinary(int x, int n) {
38+
public static Bits encodeToTruncatedBinary(int x, int n) {
3939
// Set k = floor(log2(n)), i.e., k such that 2^k <= n < 2^(k+1).
4040
int k = 0, t = n;
4141
while (t > 1) { k++; t >>= 1; }
@@ -47,7 +47,7 @@ private Bits encodeToTruncatedBinary(int x, int n) {
4747
else return encodeToBinary(x+u, k+1);
4848
}
4949

50-
private Bits encodeToBinary(int x, int len) {
50+
public static Bits encodeToBinary(int x, int len) {
5151
Bits s = new Bits();
5252
while (x != 0) {
5353
if ((x & 0x01) == 0) s = Bits.ofZero().append(s);
@@ -91,7 +91,7 @@ public int decode(Bits bits, int m) {
9191
return q * m + r;
9292
}
9393

94-
public int decodeTruncatedBinary(Bits bits, int m) {
94+
public static int decodeTruncatedBinary(Bits bits, int m) {
9595
// Set k = floor(log2(n)), i.e., k such that 2^k <= n < 2^(k+1).
9696
int k = 0, t = m;
9797
while (t > 1) { k++; t >>= 1; }
@@ -101,7 +101,7 @@ public int decodeTruncatedBinary(Bits bits, int m) {
101101
return (x < u) ? x : (x - u);
102102
}
103103

104-
private int encodeToBinary(Bits bits) {
104+
public static int encodeToBinary(Bits bits) {
105105
int x = 0;
106106
for (int i = 0; i < bits.length(); i++) {
107107
x += (bits.get(i).value() << (bits.length() - i - 1));

src/test/java/com/github/myibu/algorithm/AlgorithmTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ public void testLZ77Compressor() {
181181
public void testGolombEncoder() {
182182
int m = 4;
183183
GolombEncoder encoder = new GolombEncoder();
184+
System.out.println(encoder.encode(0, 5));
185+
System.out.println(encoder.encode(1, 5));
186+
System.out.println(encoder.encode(4, 5));
184187
List<Bits> encodeList = new ArrayList<>();
185188
for (int i = 1; i <= 9; i++) {
186189
encodeList.add(encoder.encode(i, m));

0 commit comments

Comments
 (0)