-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.cpp
More file actions
305 lines (280 loc) · 8.47 KB
/
encoder.cpp
File metadata and controls
305 lines (280 loc) · 8.47 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <vector>
#define SIZE 7302
using namespace std;
class bitstream { //비트 단위로 출력하기 위한 클래스
std::streambuf* sbuf;
int count = 0;
unsigned char byte = 0;
public:
bitstream(std::ostream& out) : sbuf(out.rdbuf()) {}
~bitstream() {
if (this->count) {
this->sbuf->sputc(this->byte);
}
}
bitstream& operator<< (bool b) { //연산자 오버라이딩을 통해 버퍼에 비트 조작
this->byte = (this->byte << 1) | b;
if (++this->count == 8) {
this->sbuf->sputc(this->byte);
this->count = 0;
}
return *this;
}
};
typedef struct data { // 데이터 구조체
int amount = 0;
unsigned int data;
}Data;
typedef struct node { //노드 구조체
bool bit;
Data data;
struct node* parent;
struct node* child[2];
}Node;
bool compare(Node* node1, Node* node2); // sort 함수 비교 함수
void readFile(string& txtData); // 파일 읽는 함수
void dataProcess(string txtData, vector<vector<Node*>>& cData, vector<Node*>& nData); // 데이터 전처리
void makeTable(vector<Node*>& Data, vector<string>& tableCode); // 테이블 생성 함수
void makeNTableFile(vector<string> table); //파일에 일반 테이블 입력
void makeCTableFile(vector<vector<string>> table, vector<string>nTable); //파일에 Adaptive 테이블 입력
void Encode(vector<vector<string>> table, vector<string>nTable); //부호화를 진행하는 함수
int main() {
string txtData; //텍스트 데이터
vector<Node*> ref; //할당 해제를 위한 레퍼런스
vector<Node*> nData(128); //일반 테이블 데이터
vector<string> nTable(128); //일반 테이블
vector<vector<Node*>> cData(128); //Adaptive 테이블 데이터
vector<vector<string>> tableCode(128); //Adaptive 테이블
for (int i = 0; i < 128; i++) { //노드 할당
nData[i] = new Node;
ref.push_back(nData[i]);
}
for (int i = 0; i < 128; i++) { //Adaptive 테이블 전처리
cData[i].resize(128);
for (int j = 0; j < 128; j++) {
cData[i][j] = new Node;
ref.push_back(cData[i][j]);
}
}
readFile(txtData);
dataProcess(txtData, cData, nData);
for (int i = 0; i < 128; i++) { //테이블을 하나씩 만들어서 넣어줌
cout << i << endl;
makeTable(cData[i], tableCode[i]);
}
makeTable(nData, nTable); //일반 테이블 생성
makeNTableFile(nTable); //테이블 저장
makeCTableFile(tableCode, nTable);
Encode(tableCode, nTable);
for (int i = 0; i < ref.size(); i++) { //할당 해제
delete ref[i];
}
}
bool compare(Node* node1, Node* node2) {
return node1->data.amount < node2->data.amount; //데이터릐 양에 따라 정렬
}
void readFile(string& txtData) {
ifstream File("input_data.txt"); //입력 데이터 파일
if (File.is_open()) {
txtData.resize(SIZE);
File.read(&txtData[0], SIZE);
txtData += (char)3; //EOD 데이터도 포함시키기 위해 포함
}
File.close();
return;
}
void dataProcess(string txtData, vector<vector<Node*>>& cData, vector<Node*>& nData) {
int txtlen = txtData.length();
for (int i = 0; i < txtlen - 1; i++) {
nData[txtData[i]]->data.amount++; //일반 테이블 파일용 데이터
nData[txtData[i]]->data.data = txtData[i];
cData[txtData[i]][txtData[i + 1]]->data.amount++; //Adaptive 테이블 파일용 데이터
cData[txtData[i]][txtData[i + 1]]->data.data = txtData[i + 1];
}
nData[txtData[txtlen - 1]]->data.data = txtData[txtlen - 1];
nData[txtData[txtlen - 1]]->data.amount++;
}
void makeTable(vector<Node*>& Data, vector<string>& tableCode) {
int check = 0;
Node* current;
vector<Node*> ref;
vector<Node*> trace(128);
sort(Data.begin(), Data.end(), compare);
vector<Node*>::iterator iter = Data.begin();
for (int i = 0; i < 128; i++) {
if (Data[i]->data.amount == 0) {
check++;
delete Data[i];
}
}
Data.erase(iter, iter + check);
iter = Data.begin();
for (int i = 0; i < Data.size(); i++) {
trace[Data[i]->data.data] = Data[i];
}
if (Data.size() > 0) {
int scale = 0;
Node* tmp;
while (Data.size() != 1) { //호프만 트리 작성
tmp = new Node;
ref.push_back(tmp);
tmp->bit = 0;
tmp->data.data = 0;
tmp->parent = NULL;
Data[0]->bit = 0;
Data[1]->bit = 1;
tmp->child[0] = Data[0];
tmp->child[1] = Data[1];
tmp->data.amount = Data[0]->data.amount + Data[1]->data.amount;
Data[0]->parent = tmp;
Data[1]->parent = tmp;
Data.erase(iter, iter + 2), iter = Data.begin();
Data.insert(iter, tmp), iter = Data.begin();
sort(Data.begin(), Data.end(), compare);
}
for (int i = 0; i < 128; i++) {
if (trace[i] != NULL)
scale++;
}
string code; // 코드 삽입
current = NULL;
tableCode.resize(128);
if (scale > 1) {
for (int i = 0; i < trace.size(); i++) {
code = "";
current = trace[i];
while (current && current->parent) {
if (current->bit) {
code = "1" + code;
}
else {
code = "0" + code;
}
current = current->parent;
}
tableCode[i] = code;
}
}
else {
for (int i = 0; i < 128; i++) {
if (trace[i] != NULL) {
tableCode[i] = "0";
}
}
}
for (int i = 0; i < ref.size(); i++) {
delete ref[i];
}
}
return;
}
void makeNTableFile(vector<string> table) {
int check = 0;
ofstream tableFile("HuffmanTable.hbs", ios::out | ios::binary);
bitstream bitFile(tableFile);
for (int i = 0; i < 128; i++) {
if (table[i].length() != 0) {
for (int j = 7; j >= 0; --j) {// 8비트 단위로 입력(ascii 데이터)
int word = i >> j & 1;
bitFile << word;
check++;
}
for (int j = 7; j >= 0; --j) {// 8비트 단위로 입력(코드 길이 데이터)
int word = table[i].length() >> j & 1;
bitFile << word;
check++;
}
for (int j = 0; j < table[i].length(); j++) { // 코드를 비트로 입력
if (table[i][j] == '1') bitFile << true;
else bitFile << false;
check++;
}
}
}
if (check % 8) { //byte align 을 위한 stuffing bit 추가
for (int i = 0; i < 8 - check % 8; i++) bitFile << false;
}
tableFile.close();
return;
}
void makeCTableFile(vector<vector<string>> table, vector<string>nTable) {
int check = 0;
ofstream tableFile("AdaptiveHuffmanTable.hbs", ios::out | ios::binary);
bitstream bitFile(tableFile);
for (int k = 7; k >= 0; --k) { // 8비트 단위로 입력(테이블 크기 입력)
int word = (char)table.size() >> k & 1;
bitFile << word;
check++;
}
for (int i = 0; i < 128; i++) {
if (table[i].size() != 0) {
for (int k = 7; k >= 0; --k) { // 선행문자 입력
int word = char(i) >> k & 1;
bitFile << word;
check++;
}
for (int j = 0; j < 128; j++) {
if (table[i][j].length() != 0) {
for (int k = 7; k >= 0; --k) { // 8비트 단위로 입력(ascii 데이터)
int word = j >> k & 1;
bitFile << word;
check++;
}
for (int k = 7; k >= 0; --k) { // 8비트 단위로 입력(코드 길이 데이터)
int word = table[i][j].length() >> k & 1;
bitFile << word;
check++;
}
for (int k = 0; k < table[i][j].length(); k++) { // 코드를 비트로 입력
if (table[i][j][k] == '1') bitFile << true;
else bitFile << false;
check++;
}
}
}
for (int j = 0; j < nTable[3].size(); j++) { // EOD 코드 입력
if (nTable[3][j] == '1')bitFile << true;
else bitFile << false;
check++;
}
}
}
if (check % 8) { //byte align 을 위한 stuffing bit 추가
for (int j = 0; j < 8 - check % 8; j++) bitFile << false;
}
tableFile.close();
}
void Encode(vector<vector<string>> table, vector<string>nTable) {
int check = 0;
ifstream dataFile("input_data.txt");
ofstream File("encoded.hbs", ios::out | ios::binary);
bitstream bitFile(File);
string txtData;
if (dataFile.is_open()) {
txtData.resize(SIZE);
dataFile.read(&txtData[0], SIZE);
txtData += (char)3; //데이터 마지막에 EOD 삽입
}
dataFile.close();
for (int i = 0; i < nTable[txtData[0]].size(); i++) {// 첫 데이터는 일반 테이블로 입력
if (nTable[txtData[0]][i] == '1') bitFile << true;
else bitFile << false;
check++;
}
for (int i = 1; i < txtData.size(); i++) { //데이터를 Adaptive 테이블에 따라 입력
for (int j = 0; j < table[txtData[i - 1]][txtData[i]].size(); j++) {
if (table[txtData[i - 1]][txtData[i]][j] == '1') bitFile << true;
else bitFile << false;
check++;
}
}
if (check % 8) { //byte align 을 위한 stuffing bit 추가
for (int i = 0; i < 8 - check % 8; i++) bitFile << false;
}
File.close();
return;
}