-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression_movetofront.cpp
183 lines (155 loc) · 3.44 KB
/
compression_movetofront.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Move to front is technique to move the most recently seen character
* at the front of a queue.
*
*/
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#include <fstream>
#include <string>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#define DEBUG true
#ifdef DEBUG
#define out printf
#else
#define out
#endif
#define vi std::vector<int>
#define graphtp std::vector< vi >
#define R 256
inline char bit(char buf, char mask)
{
return !((buf & mask) == 0);
}
void print_byte(char b)
{
for(int i = 7; i >= 0; i--)
out("%d",(b>>i)&0x1);
out("\n");
}
void decode(int argc, char**argv)
{
int fin = 0; //stdin
int fout = 1; //stdout
if(argc < 2) {
return;
}
int tmp = open(argv[1], O_RDONLY);
out("\nDecoding: %s\n",argv[1]);
if(tmp < 0) {
printf("decode:Can not open file %s\n", argv[1]);
return;
}
else {
fin = tmp;
std::string s(argv[1]);
s += ".verif";
int tmp2 = creat(s.c_str(), S_IRUSR | S_IWUSR);
if(tmp2 < 0) {
printf("decode:Can not open file for writing %s\n", s.c_str());
return;
}
else
fout = tmp2;
}
uint8_t buf;
uint8_t chmap[R] = {0};
for(int i =0 ; i < R ; i++) {
chmap[i] = i;
}
while(read(fin, &buf, 1) > 0)
{
out("buf=");
print_byte(buf);
write(fout, &chmap[buf], 1); //0 of the other type (ex zeros)
int tmp = chmap[buf];
for(int i = buf; i != 0; i--) {
chmap[i] = chmap[i-1];
}
chmap[0] = tmp;
}
}
inline void swap(uint8_t & a, uint8_t & b)
{
uint8_t tmp = b;
b = a;
a = tmp;
}
void encode(int argc, char**argv)
{
int fin = 0; //stdin
int fout = 1; //stdout
if(argc > 1) {
int tmp = open(argv[1], O_RDONLY);
if(tmp < 0)
printf("encode:Can not open file %s\n", argv[1]);
else {
fin = tmp;
std::string s(argv[1]);
s += ".out";
int tmp2 = creat(s.c_str(), S_IRUSR | S_IWUSR);
if(tmp2 < 0)
printf("encode:Can not open file for writing %s\n", s.c_str());
else
fout = tmp2;
}
}
uint8_t buf;
uint8_t chmap[R] = {0};
for(int i =0 ; i < R ; i++) {
chmap[i] = i;
}
while(read(fin, &buf, 1) > 0)
{
out("buf=");
print_byte(buf);
uint8_t prev = chmap[0]; //save prev
for(int i = 0; i < R; i++) {
out("i%d%d",i,chmap[i]);
if(chmap[i] == buf) {
swap(chmap[i], prev);
write(fout, &i, 1);
chmap[0] = buf;
out("found at %d\n",i);
break;
}
swap(chmap[i], prev);
}
}
/*
0 1 2 ... a b c
a 0 1 2.... b c
b a 0 1 2.... c
*/
close(fin);
close(fout);
}
int main(int argc, char**argv)
{
out("starting ... front coding\n");
if(argc > 1) {
if(argv[1][0] == '-') {
encode(argc-1, argv+1);
return 0;
}
if(argv[1][0] == '+') {
decode(argc-1, argv+1);
return 0;
}
}
encode(argc, argv);
decode(argc, argv);
return 0;
}