-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLZ78Main.java
More file actions
167 lines (142 loc) · 4.33 KB
/
Copy pathLZ78Main.java
File metadata and controls
167 lines (142 loc) · 4.33 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package LZ78;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
public class LZ78Main {
public static ArrayList<tag> compress(String data, binNum poi) {
ArrayList<tag> list = new ArrayList<tag>();
data += '\1';
String temp = "";
char ch;
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("", 0);
for (int i = 0, j = 0; i < data.length(); i++) {
ch = data.charAt(i);
if (temp == "" && ch == '\1')
break;
if (!map.containsKey(temp + ch)) {
tag ta = new tag();
ta.cha = ch;
ta.pointer = map.get(temp);
poi.maxNum(ta.pointer);
list.add(ta);
map.put(temp + ch, ++j);
temp = "";
ch = '\1';
continue;
}
temp += ch;
}
return list;
}
public static String deCompress(ArrayList<tag> l) {
String data = "" , temp ;
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "");
for (int i = 0 , j = 0; i < l.size(); i++ ) {
temp = map.get(l.get(i).pointer) + l.get(i).cha ;
map.put( ++j , temp) ;
data += temp;
}
return data;
}
public static void main(String[] args) {
// ** // GUI
new LZ78APP();
// ** // number Handling
// binNum x = new binNum();
// ** // write to file
// FileOutputStream outPut = new FileOutputStream("Copress.txt");
// DataOutputStream out = new DataOutputStream(outPut);
// out.close();
// ** // read from file
// File inpt = new File("Copress.txt");
// InputStream in = new FileInputStream(inpt);
// binNum le = new binNum();
// le.SetMaxBinLen(in.read());
// binNum poi = new binNum();
// poi.SetMaxBinLen(in.read());
// in .close();
// ** // comprassing
// binNum poi = new binNum(), le = new binNum();
// String data = "aablkjfslf;lawkvjnsdkhnsiovjcoisdjviwhiuwhjoiajpdoshbokooooonfbhddlifabab";
// ArrayList<tag> l = compress(data, poi);
// // ArrayList<tag> l = compress("abaa");
// for (int i = 0; i < l.size(); i++) {
// System.out.println(l.get(i).toString());
// }
// ** // decomprassing
// String deCompresdData = deCompress(l);
// System.out.println(deCompresdData);
// System.out.print(data);
// ** // testing
// int x= 608571914;
// while(x > 0 ){
// System.out.print(x % 2);
// x /= 2;
// }
// System.out.print(Integer.toBinaryString(5));
// String data = "";
// //System.out.println(1 %2 + " "+0%2);
// Scanner reader = new Scanner(new File("mld.txt"));
// while (reader.hasNext()) {
// data += (reader.nextLine() + "\n");
// }
// //System.out.println(data);
//
// binNum le = new binNum(), poi = new binNum();
// FileOutputStream outPut = new FileOutputStream("1.Lz77");
// DataOutputStream out = new DataOutputStream(outPut);
//
// ArrayList<tag> l = compress(data, poi, le);
//
// le.genrate();
// poi.genrate();
//
// for (int i = 0; i < l.size(); i++) {
// l.get(i).prepareTag(poi, le);
// //System.out.println(l.get(i).toString());
// }
//
// // System.out.println(poi.binLen + "*" + le.binLen);
// tag.saveTag(out, poi, le);
// out.close();
// binNum le = new binNum(), poi = new binNum();
// String deCompreseddata = "";
// InputStream i1n = new FileInputStream(new File("1.Lz77"));
// DataInputStream in = new DataInputStream(i1n);
//
// tag.readTag(in, poi, le);
// ArrayList<tag> kk = tag.deGenrate(poi, le) ;
// in.close();
//
// //tag.saveTag(out, poi, le);
//
// deCompreseddata = deCompress(kk);
// System.out.println(data);
// System.out.print(deCompreseddata);
// for (int i = 0; i < kk.size(); i++) {
// System.out.println(l.get(i).toString());
// }
// Scanner x = new Scanner(new File("1.Lz77"));
// System.out.println(x.nextBoolean());
//
// for (int i1 = 0; i1 < kk.size(); i1++) {
// System.out.println(kk.get(i1).toString());
// }
}
}
/*
* for (int j = i - temp.length() - 1; j >= 0; j--) { //
* System.out.println(data.substring(j, j + // temp.length()+ 1) + "_" + temp +
* (ch == '\1' ? "" : // ch)); if (data.substring(j, j + temp.length() +
* 1).equals( (temp + (ch == '\1' ? "" : ch)))) { point = j;
* //System.out.println("Yes" + j); break; } else System.out.println("No" + j);
* }
*/