-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLZW.java
More file actions
59 lines (57 loc) · 1.99 KB
/
LZW.java
File metadata and controls
59 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LZW {
private List<Integer> compressed;
private String decompressed;
public LZW (String input){
this.compressed = encode1(input);
this.decompressed = decode1(compressed);
}
public static List<Integer> encode1(String text) {
int dictSize = 256;
Map<String, Integer> dictionary = new HashMap<>();
for (int i = 0; i < dictSize; i++) {
dictionary.put(String.valueOf((char) i), i);
}
String foundchars = "";
List<Integer> result = new ArrayList<>();
for (char character : text.toCharArray()) {
String charsToAdd = foundchars + character;
if (dictionary.containsKey(charsToAdd)) {
foundchars = charsToAdd;
} else {
result.add(dictionary.get(foundchars));
dictionary.put(charsToAdd, dictSize++);
foundchars = String.valueOf(character);
}
}
if (!foundchars.isEmpty()) {
result.add(dictionary.get(foundchars));
}
return result;
}
public static String decode1(List<Integer> encodedText) {
int dictSize = 256;
Map<Integer, String> dictionary = new HashMap<>();
for (int i = 0; i < dictSize; i++) {
dictionary.put(i, String.valueOf((char) i));
}
String ch = String.valueOf((char) encodedText.remove(0).intValue());
StringBuilder result = new StringBuilder(ch);
for (int code:encodedText) {
String entry = dictionary.containsKey(code) ? dictionary.get(code):ch+ch.charAt(0);
result.append(entry);
dictionary.put(dictSize++,ch+entry.charAt(0));
ch=entry;
}
return result.toString();
}
public List<Integer> getCompressed() {
return compressed;
}
public String getDecompressed() {
return decompressed;
}
}