-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlzw_serial_decode.cpp
More file actions
79 lines (67 loc) · 1.77 KB
/
Copy pathlzw_serial_decode.cpp
File metadata and controls
79 lines (67 loc) · 1.77 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
#include <unordered_map>
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
#define PRINT 0
void decoding(ifstream& ifile, ofstream& ofile)
{
#if PRINT
std::cout << "Decoding\n";
#endif
std::unordered_map<int, std::string> table;
for (int i = 0; i <= 255; i++) {
std::string ch = "";
ch += char(i);
table[i] = ch;
}
int old, n;
ifile >> old;
std::string s = table[old];
std::string c = "";
c += s[0];
ofile << s;
int count = 256;
while (ifile >> n) {
if (table.find(n) == table.end()) {
s = table[old];
s = s + c;
}
else {
s = table[n];
}
ofile << s;
c = "";
c += s[0];
table[count] = table[old] + c;
count++;
old = n;
}
#if PRINT
std::cout << "\nEND"<<std::endl;
#endif
}
int main(int argc, char** argv)
{
if (argc < 2 || argc > 3) {
cerr << "usage: ./lzw_serial_decode input_file [output_file]" << endl;
return 1;
}
string output_file = argc == 3 ? argv[2] : "decode_serial.out";
ifstream ifile(argv[1]);
if (ifile.fail()) {
cerr << "Cannot open input file" << endl;
return 1;
}
ofstream ofile(argv[2]);
struct timespec start, stop;
double time = 0;
if( clock_gettime(CLOCK_REALTIME, &start) == -1) { perror("clock gettime");}
decoding(ifile, ofile);
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) { perror("clock gettime");}
time = (stop.tv_sec - start.tv_sec)+ (double)(stop.tv_nsec - start.tv_nsec)/1e9;
std::cout << "Decoding time = " << time << " sec " <<std::endl;
return 0;
}