-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComp.java
More file actions
90 lines (85 loc) · 2.53 KB
/
Copy pathComp.java
File metadata and controls
90 lines (85 loc) · 2.53 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
import java.io.*;
import java.lang.*;
public class Comp {
public static Trie t;
public static void main(String[] args) {
if (args[0].equals("c"))
compress(args[1]);
else if (args[0].equals("d"))
decompress(args[1]);
else
System.out.println("Please enter either 'c' or 'd' in your first argument");
}
public static void compress(String file) {
IO.Compressor c;
try {
c = new IO.Compressor(file);
} catch (Exception e) {
throw new RuntimeException();
}
char[] text;
try {
text = c.giveArray();
} catch (Exception e) {
throw new RuntimeException();
}
t = new Trie();
StringBuilder word = new StringBuilder();
int index = 1;
for (int i = 0; i < text.length - 1; i++) {
word.append(text[i]);
System.out.println("word:" + word.toString());
if (!t.contains(word.toString())) {
System.out.println("new entry");
String prefix = word.substring(0, word.length() - 1); // determines n's parent.
// System.out.println("prefix: " + prefix);
Trie.Node parent = t.get(prefix);
t.add(t.new Node(word.toString(), index), parent);
index++;
int prefIndex = parent.getIndex();
try {
c.encode(prefIndex, text[i]); // one transmission
System.out.println(word.toString() + " encoded.");
} catch (Exception e) {
System.err.println("Y U NO ENCODE???? " + e.getMessage());
throw new RuntimeException();
}
word = new StringBuilder();
}
}
try {
c.done();
} catch (Exception e) {
throw new RuntimeException();
}
}
public static void decompress(String file) {
IO.Decompressor d;
ArrayList<String> dic = new ArrayList<String>();
dic.add("");
try {
d = new IO.Decompressor(file);
} catch (Exception e) {
throw new RuntimeException();
}
IO.pair next = d.decode();
while(next.valid) {
String output = dic.get(next.index) + next.extension;
dic.add(output);
try {
d.append(output); // why doesn't this shit append the last pair, even though it's decoded properly? -.-
} catch (Exception e) {
throw new RuntimeException();
}
next = d.decode();
// System.out.println("Next pair: (" + next.index + "," + next.extension + ")");
// System.out.println("Valid? " + next.valid);
}
try {
d.done();
} catch (Exception e) {
throw new RuntimeException();
}
}
}