This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringTable.c
329 lines (279 loc) · 8.64 KB
/
StringTable.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
/* StringTable data structure for the Lempel-Ziv-Welch algorithm
* by Jared Katzman (9/16/14)
*
* Stores code in a Trie based on (PREFIX, K) and hashes the
* nodes based on CODE to a dynamic-length array
*
* stringTable can store at most 2^MAXBITS codes
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include "StringTable.h"
#include "code.h"
struct node {
unsigned char K;
int Prefix, Code, Count, nChild;
Node *Child;
};
static Node *stringTable; // Dynamical-Size Array (CODE, NODE)
static Node stringTrie; // Trie of (PREFIX,CODE,K)
static int nCodes = 0; // # of codes in the stringTable
static int tSize = 2; // 2^tSize - current table size
int MAXBITS = 12; // Table Capacity: 2^MAXBITS codes
int PRUNE = false; // PRUNE flag
int ESCAPE = false; // ESCAPE flag
// Debug - Stage MODE flag
static int MODE = 3;
void initializeTable (int e_flag) {
tSize = e_flag ? 2 : CHAR_BIT+1; // Mininum size to hold initial codes
stringTable = calloc (sizeof(Node), (1 << tSize)); // 2 ^ tSize
stringTrie = createNode ('\0', 0, EMPTY); // initialize EMPTY
nCodes = 0;
stringTable[nCodes++] = stringTrie;
stringTable[nCodes++] = createNode('\0', 0, ESC); // ESC
stringTable[nCodes++] = createNode('\0',0, END); // END
if (!e_flag) { // -e Flag
for (int i = 0; i < (1 << CHAR_BIT); i++) { // 0 - 255 ASCII
unsigned char k = i;
insert (EMPTY, k, false);
}
}
}
Node createNode (unsigned char k, int prefix, int code) {
Node t = (Node) malloc(sizeof(struct node));
if (t == NULL)
return NULL;
t->K = k;
t->Prefix = prefix;
t->Code = code;
t->Count = 1;
t->Child = NULL;
t->nChild = 0;
return t;
}
Node insert (int code, unsigned char k, int prune) {
if (code < 0)
return NULL;
if (nCodes >= (1 << tSize)) { // Resize Table
if(tSize >= MAXBITS) // Table @ Max Size
return NULL;
stringTable = realloc (stringTable, sizeof(Node)*(1 << ++tSize));
memset (stringTable + (1 << (tSize-1)), 0, (1 << (tSize-1)));
}
// Create new Node
Node prefix = stringTable[code];
Node t = createNode (k, code, nCodes);
// Insert Node
stringTable[nCodes] = t;
// Inserts Nodes into Prefix->Child using insertion sort
if (prefix->nChild == 0) { // Initialize children array
prefix->Child = malloc (sizeof(Node));
}
else {
prefix->Child = realloc (prefix->Child, \
sizeof(Node)*(prefix->nChild + 1));
}
prefix->Child[prefix->nChild++] = t;
for (int j = 1; j < prefix->nChild; j++) {
for (int i = j-1; i >= 0; i--) {
Node temp, *R = &(prefix->Child[i+1]), *L = &(prefix->Child[i]);
if ((*R)->K < (*L)->K) { // Is Left child > Right Child?
temp = *L; // Swap L and R nodes
*L = *R;
*R = temp;
}
else break;
}
}
nCodes++;
if (prune) {
if (nCodes == (1 << MAXBITS) && PRUNE) { // Full Table and Prune Flag
pruneTable();
}
}
return t;
}
int find (int code, unsigned char k) {
Node prefix, temp = NULL;
if (code >= (1 << tSize) || (prefix = stringTable[code]) == NULL)
return -1;
temp = binarySearch (k, prefix->Child, 0, prefix->nChild - 1);
if (temp == NULL)
return -1;
else {
temp->Count++;
return temp->Code;
}
}
Node binarySearch (unsigned char key, Node *c, int l, int u) {
if (l > u || c == NULL)
return NULL;
int m = (l + u) / 2;
Node temp = c[m];
if (temp->K == key)
return temp;
else if (temp->K > key)
return binarySearch(key, c, l, m - 1);
else
return binarySearch(key, c, m + 1, u);
}
void pruneTable (void) {
Node *table_copy = stringTable;
Node oldT, newT;
int n = nCodes, count, oldP, newP;
/*printTable(stringTable);*/
initializeTable(true); // Reset stringTable and stringTrie
for (int i = nCodes; i < n; i++) {
oldT = table_copy[i];
count = oldT->Count / 2; // #appearance / 2 (int division)
oldP = oldT->Prefix;
if (count > 0 || (!ESCAPE && oldP == 0)) {
newP = table_copy[oldP]->Code;
newT = insert (newP,oldT->K, false); // Check : what if you prune a table and you don't delete anything? Should you prune again? Probably not.
newT->Count = count;
freeNode (oldT);
table_copy[i] = newT;
}
else
freeNode (oldT);
}
for (int i = 0; i < 3; i++) { // Free SPECIAL codes of old stringTable
freeNode (table_copy[i]);
}
free (table_copy); // Clean-up
/*printTable(stringTable);*/
return;
}
void freeNode (Node n) {
if (n == NULL)
return;
free (n->Child);
free (n);
}
void destroyTable () {
for (int i = 0; i < nCodes; i++) {
if (stringTable[i] != NULL)
freeNode (stringTable[i]);
}
free (stringTable);
}
bool findCode (int code) {
if (code == EMPTY || code == ESC || code == END || code < 0)
exit (fprintf(stderr, "decode: invalid input\n"));
if (ESCAPE && nCodes == 3 && code != ESC)
exit (fprintf(stderr, "decode: invalid input\n"));
if (code >= nCodes) {
if (code > nCodes + 1)
exit (fprintf(stderr, "decode: invalid input\n"));
return false;
}
if (stringTable[code] == NULL)
return false;
return true;
}
unsigned char findK (int code, bool output) {
if (code >= (1 << tSize))
return code;
Node temp = stringTable[code];
if (temp == NULL)
return code;
if (output)
temp->Count++;
unsigned char finalK;
if (temp->Prefix == EMPTY) {
if (output)
putchar (temp->K);
return temp->K;
}
finalK = findK (temp->Prefix, output);
if (output)
putchar (temp->K);
return finalK;
}
int codeToChar (int code, int look_ahead) {
findK (code, true);
if ((nCodes + look_ahead) >= (1 << MAXBITS) && PRUNE) {
pruneTable();
return EMPTY;
}
if (nCodes >= (1 << tSize)) { // expands table after last code insertion
if (tSize < MAXBITS) {
stringTable = realloc (stringTable, sizeof(Node)*(1 << ++tSize));
memset (stringTable + (1 << (tSize-1)), 0, (1 << (tSize-1))*sizeof(Node));
}
}
return code;
}
void printTable (void) {
//int i = 0;
Node temp;
printf("\ntSize: %d\nnCodes: %d\n",tSize,nCodes);
printf("C PREF CHAR COUNT\n");
for (int i = 0; i < nCodes; i++) {
temp = stringTable[i];
printf("%5d %5d %d (%c) %5d\n", temp->Code, temp->Prefix,(int)temp->K, temp->K,temp->Count);
}
}
int getcode (int nBits) {
// Stage 3
int temp;
char k;
nBits = (!nBits) ? tSize : nBits;
switch (MODE) {
case 3:
return getBits (nBits);
break;
case 2:
// Stage 2
temp = 0;
while (true) {
if (isdigit((k = getchar()))){
temp = temp * 10;
temp = temp + k - '0';
}
else if (k == ':')
temp = 0;
else
break;
}
if (k == EOF)
return END;
return temp;
break;
case 1:
// Stage 1:
temp = 0;
while ( isdigit((k = getchar()))) {
temp = temp * 10;
temp = temp + k - '0';
}
if (k == EOF)
return END;
return temp;
break;
}
return -1;
}
void output (int c, int nBits) {
// Stage 3:
switch (MODE) {
case 3:
nBits = (!nBits) ? tSize : nBits;
putBits (nBits,c);
break;
case 2:
// Stage 2:
nBits = (!nBits) ? tSize : nBits;
printf("%d:%d\n",nBits, c);
break;
case 1:
// Stage 1:
printf("%d\n", c);
break;
}
}