-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHuff_en.c
377 lines (327 loc) · 9.14 KB
/
Huff_en.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Huffman Coding - Encoder - Fangs124 */
//--------//
#include <stdio.h>
#include <stdlib.h>
//--------//
typedef struct COUNTERS count_t;
typedef struct QUES que_t;
typedef struct NODES node_t;
typedef struct TABLES table_t;
typedef struct LINKEDLIST list_t;
struct COUNTERS { //leaf nodes
node_t* parent;
unsigned char data;
unsigned int count;
};
struct QUES {
node_t *node;
unsigned int count;
que_t *prev;
que_t *next;
};
struct NODES { //huffman tree nodes
node_t* parent;
count_t* child;
node_t* left;
node_t* right;
};
struct TABLES {
unsigned char* sequence;
unsigned int bits;
};
struct LINKEDLIST {
unsigned char data;
list_t* prev;
list_t* next;
};
//--------//
int CompareFrequency(const void* a, const void* b);
que_t* GenerateQue(count_t* counter, unsigned int *total);
que_t* CreateQueElement(count_t *counter, unsigned int index);
node_t* CreateLeafNode(count_t *child);
node_t* CreateCombinedNode(node_t* node1, node_t* node2);
que_t* CreateNewElement(que_t* curr, que_t* prev, node_t* node);
void InsertNewElement(que_t* priority_q, que_t* new_element);
node_t* GenerateTree(que_t* priority_q, unsigned int *total);
void GenerateHuffTable(node_t* root, table_t* huff_table);
void TraverseTree(node_t* node, list_t* list, table_t* huff_table);
unsigned char* FindSequence(list_t* last);
//--------//
int main(int argc, char* argv[]){
/* initialize counters and table */
count_t counter[256]; //frequency counter
count_t debug_counter[256];
table_t huff_table[256];
unsigned int index = 0x00;
while(index <= 0xFF){
counter[index].data = (unsigned char) index;
counter[index].count = 0;
counter[index].parent = NULL;
debug_counter[index].data = (unsigned char) index;
debug_counter[index].count = 0;
debug_counter[index].parent = NULL;
huff_table[index].sequence = NULL;
huff_table[index].bits = 0;
index++;
}
/* step1 - count byte frequency */
unsigned int char_buffer;
unsigned char c;
while((char_buffer = fgetc(stdin)) != EOF){
c = (unsigned char) char_buffer;
counter[c].count++;
debug_counter[c].count++;
}
/* step2 - sort the array */
qsort(&counter[0x00], 256, sizeof(count_t), CompareFrequency);
/* step3 - generate priority que */
unsigned int total = 0;
que_t *priority_q = GenerateQue(counter, &total);
//print que
que_t *que_ptr = priority_q;
int loop_condition = 1;
unsigned int symbols = 0;
while(loop_condition){
symbols++;
//fprintf((stderr), "node->child->data = %02X\n", que_ptr->node->child->data);
//fprintf((stderr), "count = %u\n", que_ptr->count);
if(que_ptr->next == NULL){
loop_condition--;
}
que_ptr = que_ptr->next;
}
//que is generated correctly
/* step4 - construct huffman tree */
node_t *root = GenerateTree(priority_q, &total); //segafault here
fprintf(stderr, "tree-generated...\n");
/* step5 - generate huffman table */
GenerateHuffTable(root, huff_table);
fprintf(stderr, "table-generated...\n");
unsigned int temp;
unsigned int db_i;
for(db_i = 0x00; db_i <= 0xFF; db_i++){
if(huff_table[db_i].sequence != NULL){
fprintf(stderr, "(%02X): ", (unsigned char) db_i);
for(temp = 0; huff_table[db_i].sequence[temp] != '\0'; temp++){
if(huff_table[db_i].sequence[temp] == 0x0A){
fprintf(stderr, "0");
}
else if(huff_table[db_i].sequence[temp] == 0x0B){
fprintf(stderr, "1");
}
else{
return 1;
}
}
fprintf(stderr, "\n");
}
}
fprintf(stderr, "---end of table ---\n");
fprintf(stderr, "symbols = %d\n", symbols);
return 0;
}
//--------//
//coparison function for qsort
//sorts in terms of count, and secondarily by lexicon ordering
int CompareFrequency(const void* a, const void* b){
count_t left = *(count_t*) a;
count_t right = *(count_t*) b;
int result;
if(left.count == right.count){
result = (int) ((unsigned char) left.data - (unsigned char) right.data);
}
else{
result = (int) (left.count - right.count);
}
return result;
}
//--------//
que_t* GenerateQue(count_t* counter, unsigned int *total){
que_t *que_ptr = NULL;
que_t *priority_q;
unsigned int index;
for(index = 0x00; index <= 0xFF; index++){
/* process non-empty counters */
if(counter[index].count != 0){
*total += counter[index].count;
//create que element
if(que_ptr != NULL){
que_ptr->next = CreateQueElement(counter, index);
que_ptr->next->prev = que_ptr;
que_ptr = que_ptr->next;
}
else{
que_ptr = CreateQueElement(counter, index);
priority_q = que_ptr;
}
//que_ptr = que_ptr->next;
}
}
return priority_q;
}
//--------//
que_t* CreateQueElement(count_t *counter, unsigned int index){
que_t* element = (que_t*) malloc(sizeof(que_t));
element->prev = NULL;
element->next = NULL;
element->count = counter[index].count;
element->node = CreateLeafNode(&counter[index]);
counter[index].parent = element->node;
return element;
}
//--------//
node_t* CreateLeafNode(count_t *child){
node_t* node = (node_t*) malloc(sizeof(node_t));
node->parent = NULL;
node->child = child;
node->left = NULL;
node->right = NULL;
return node;
}
node_t* CreateCombinedNode(node_t* node1, node_t* node2){
/* allocate memory for new_node */
//fprintf(stderr, "here\n");
node_t* new_node = (node_t*) malloc(sizeof(node_t));
new_node->parent = NULL;
new_node->child = NULL;
/* link node1 with new_node */
new_node->left = node1;
node1->parent = new_node;
/* link node2 with new_node */
new_node->right = node2;
node2->parent = new_node;
return new_node;
}
que_t* CreateNewElement(que_t* curr, que_t* prev, node_t* node){
//assign values to curr node
curr->node = node;
curr->count += prev->count;
curr->prev = NULL;
curr->next = NULL;
//free prev pointer
prev->node = NULL;
prev->prev = NULL;
prev->next = NULL;
free(prev);
return curr;
}
void InsertNewElement(que_t* priority_q, que_t* new_element){
//use que_ptr to navigate priority que
que_t* que_ptr = priority_q;
//iterate until new_element can be inserted in the appropriate place
while(new_element->count > que_ptr->count){
if(que_ptr->next != NULL){
que_ptr = que_ptr->next;
}
else{
/* put new_element as last element */
que_ptr->next = new_element;
new_element->prev = que_ptr;
new_element->next = NULL;
fprintf(stderr, "blah\n");
return;
}
}
/* put new_element before que_ptr */
//handling previous element before que_ptr
if(que_ptr->prev != NULL){
new_element->prev = que_ptr->prev;
que_ptr->prev->next = new_element;
}
else if(que_ptr->prev == NULL){
new_element->prev = NULL;
priority_q = new_element;
}
new_element->next = que_ptr;
que_ptr->prev = new_element;
//priority_q = que_ptr;
return;
}
//segfault here
node_t* GenerateTree(que_t* priority_q, unsigned int* total){
//to generate tree, take 2 elements from que
que_t* que_ptr1 = priority_q; //curr
que_t* que_ptr2; //prev
node_t* root;
while(priority_q->next->next != NULL){//work with stack
/* take two elements from que and combine them */
que_ptr2 = priority_q;
que_ptr1 = priority_q->next;
priority_q = priority_q->next->next; //need to check bound here
if(que_ptr1 == NULL){
fprintf(stderr, "que_ptr1 == NULL\n");
}
if(que_ptr2 == NULL){
fprintf(stderr, "que_ptr2 == NULL\n");
}
if(que_ptr1->node == NULL){
fprintf(stderr, "que_ptr1->node == NULL\n");
}
/*if(que_ptr2->node == NULL){
fprintf(stderr, "que_ptr2->node == NULL\n");
}*/
//fprintf(stderr, "here1\n");
root = CreateCombinedNode(que_ptr1->node, que_ptr2->node);
//fprintf(stderr, "here2\n");
que_ptr1 = CreateNewElement(que_ptr1, que_ptr2, root);
//fprintf(stderr, "here3\n");
/* create new que elements for the new node */
if(priority_q->node == NULL){
fprintf(stderr, "priority_q->node == NULL\n");
}
//fprintf(stderr, "here4\n");
/* insert new element to the priority que */
InsertNewElement(priority_q, que_ptr1);
//fprintf(stderr, "here5\n");
//que_ptr1 = priority_q;
}
return root;
}
void GenerateHuffTable(node_t* root, table_t* huff_table){
list_t* list = (list_t*) malloc(sizeof(list_t));
list->prev = NULL;
TraverseTree(root, list, huff_table);
//fprintf(stderr, "here generate\n");
return;
}
void TraverseTree(node_t* node, list_t* list, table_t* huff_table){
//fprintf(stderr, "here traverse\n");
if(node->child != NULL){
unsigned char index = node->child->data;
huff_table[index].sequence = FindSequence(list);
}
else{ //traverse tree
if(node->left != NULL){
list->data = 0x0A; //represents bit-0
list->next = (list_t*) malloc(sizeof(list_t));
list->next->prev = list;
TraverseTree(node->left, list->next, huff_table);
}
if(node->right != NULL){
list->data = 0x0B; //represents bit-1
list->next = (list_t*) malloc(sizeof(list_t));
list->next->prev = list;
TraverseTree(node->right, list->next, huff_table);
}
//free(list->next); //bad idea?
}
return;
}
unsigned char* FindSequence(list_t* last){
//fprintf(stderr, "finding sequence\n");
unsigned int length = 0;
unsigned int i;
unsigned char* sequence;
list_t* ptr = last;
while(ptr->prev != NULL){
length++;
ptr = ptr->prev;
}
sequence = (unsigned char*) malloc(sizeof(char) * (length+1));
sequence[length] = '\0';
for(i = 0; i < length; i++){
sequence[i] = (unsigned char) ptr->data;
ptr = ptr->next;
}
return sequence;
}