-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_structure.c
399 lines (311 loc) · 9.24 KB
/
data_structure.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "data_structure.h"
// define the hash table capacity
// 4096 (12 bits) * capacity factor
// in order to reduce hash collision
#define CAPACITY (SIZE_LIMIT * CAPACITY_FACTOR)
// dictionary, create an empty node
Node node_create(const Key k, const CodeWord cw, Node next);
// dictionary, delete the node
Node node_delete(Node);
// calculate the hash value
Index calculate_index(const char* s){
assert(s != NULL);
Index val = 0;
int ch;
// iterate around the string, extract char one by one
while (*s != '\0'){
// extract this char, make sure it is positive
ch = *s;
if (ch < 0){
ch += 256;
}
// left shift the last value and then add the new char
val = (val << 3) + ch;
// move to the next char
s += 1;
}
// extract the index, index must be >= 0
val = val % CAPACITY;
while (val < 0){
val += CAPACITY;
}
return val;
}
// create the dictionary, insert 256 chars + 1 for EOF and 1 for reflux
// so that the 256 and 257 index are reserved
// size = 4096, current_num = 258, so next index start at 258 during insert
Dictionary dictionary_create(void){
Dictionary d = (struct _Dictionary*) malloc (sizeof(struct _Dictionary));
assert(d != NULL);
d->nodes = (Node*) malloc (CAPACITY * sizeof(Node));
assert(d->nodes != NULL);
for (Index i = 0; i < CAPACITY; i++){
d->nodes[i] = NULL;
}
// fill the hash table with initial values 0 - 255
d->current_num = 256 + 2; // initially 256 char + 1 for EOF + 1 for reflush
return d;
}
// release memory of the dictionary
Dictionary dictionary_destroy(Dictionary d){
assert(d != NULL);
// use recursion to free all nodes
for (Index i = 0; i < CAPACITY; i++){
if (d->nodes[i] != NULL){
node_delete(d->nodes[i]);
}
}
free(d->nodes);
d->nodes = NULL;
free(d);
d = NULL;
return d;
}
// reset dictionary if it is full,
// to reflect more local characteristics.
// reset = destroy + create
Dictionary dictionary_reset(Dictionary d){
assert(d != NULL);
d = dictionary_destroy(d);
d = dictionary_create();
return d;
}
// check if is full, if full, need reset
bool dictionary_is_full(Dictionary d){
assert(d != NULL);
// note that the capacity is a multiple of 4096
// but when we reach 4096 items, we do the reflush !!!
return d->current_num == SIZE_LIMIT;
}
// check if the key exist,
// if exist return the index, if not return -1
CodeWord dictionary_search(Dictionary d, const Key k){
assert(d != NULL && k != NULL);
CodeWord result;
// if it is a single char, simply return its int
if (strlen(k) == 1){
int idx = k[0];
if (idx < 0){
idx += 256;
}
result = idx;
}
else{
// for string of length >= 2
Index hidx = calculate_index(k);
if (d->nodes[hidx] == NULL){
result = -1;
}
else{
// if see the exact key, then return the index
Node n = d->nodes[hidx];
while (n != NULL && strcmp(n->k, k) != 0){
n = n->next;
}
if (n == NULL){
result = -1;
}
else{
result = n->cw; // find the key, return the codeword
}
}
}
return result;
}
// insert key-cw pair into the dictionary
// if hash index position is null, insert
// if not null, find the end of the linked table, then insert
CodeWord dictionary_insert(Dictionary d, const Key k){
assert(d != NULL && k != NULL);
// calculate the hash index
Index hidx = calculate_index(k);
assert(hidx >= 0);
// assign the codeword
CodeWord cw = d->current_num;
if (d->nodes[hidx] == NULL){
d->nodes[hidx] = node_create(k, cw, NULL);
}
else{
Node n = d->nodes[hidx];
while (n->next != NULL){
n = n->next;
}
n->next = node_create(k, cw, NULL);
}
// update the counter
d->current_num += 1;
return cw;
}
// debug use: print the dictionary
void dictionary_print(Dictionary d){
assert(d != NULL);
fprintf(stdout, "-----Dictionary print-----\n");
fprintf(stdout, "Size = %d, current_num = %ld\n", SIZE_LIMIT, d->current_num);
for (Index i = 0; i < CAPACITY; i++){
if (i <= 255){
// single char range
fprintf(stdout, "[%ld] %c => %ld ", i, (int)i, i);
if (d->nodes[i] != NULL){
Node n = d->nodes[i];
while (n != NULL){
fprintf(stdout, "%s => %ld ", n->k, n->cw);
n = n->next;
}
}
fprintf(stdout, "\n");
}
else if (d->nodes[i] != NULL){
Node n = d->nodes[i];
fprintf(stdout, "[%ld] ", i);
while (n != NULL){
fprintf(stdout, "%s => %ld ", n->k, n->cw);
n = n->next;
}
fprintf(stdout, "\n");
}
}
fprintf(stdout, "-----End-----\n");
return;
}
// create a node, initialize with NULL next pointer
Node node_create(const Key k, const CodeWord cw, Node next){
Node n = (Node) malloc (sizeof(struct _Node));
assert(n != NULL);
// allocate memory
n->k = (char*)malloc((strlen(k)+1)*sizeof(char)); // add 1 for \0
assert(n->k != NULL);
// copy the key
strcpy(n->k, k);
// create the index and next pointer
n->cw = cw;
n->next = next;
return n;
}
// recursive to delete all nodes of the dictionary
Node node_delete(Node n){
if (n == NULL){
return n;
}
else{
node_delete(n->next);
free(n->k);
free(n);
n = NULL;
return n;
}
}
// array functions
// create
// 0-255 leaves empty, also reserve 256 for EOF and 257 for Reflush
Array array_create(void){
Array a = (struct _Array*) malloc (sizeof(struct _Array));
assert(a != NULL);
// occupy the first 258+2 positions, but do not set anything
// so that the whole ram usage can be smaller
a->current_num = 256 + 2;
// here we only need 4096 items
// since here we do not perform hash, so no hash collision
a->nodes = (char**) malloc (SIZE_LIMIT * sizeof(char*));
assert(a->nodes != NULL);
for (Index i = 0; i < SIZE_LIMIT; i++){
a->nodes[i] = NULL;
}
return a;
}
// clean the array
Array array_destroy(Array a){
assert(a != NULL);
for (Index i = 0; i < SIZE_LIMIT; i++){
if (a->nodes[i] != NULL){
free(a->nodes[i]);
a->nodes[i] = NULL;
}
}
free(a->nodes);
free(a);
a = NULL;
return a;
}
// reset = destroy + create
Array array_reset(Array a){
assert(a != NULL);
a = array_destroy(a);
a = array_create();
return a;
}
// check if full
bool array_is_full(const Array a){
assert(a != NULL);
return a->current_num == SIZE_LIMIT;
}
// search using index, return the string
// Key prev is included to avoid the unseen mistake
// that during compression, the key is created then used straight away
// without any time gap.
// Also need to take care with the return key
// if simply print it, it is fine
// but if need to modify, make sure use strcpy first
// do not change on the returned pointer directly
//
// malloc and copy for all answers.
Key array_search(Array a, const Index idx){
assert(a != NULL && idx >= 0);
Key result;
// if the index <= 255, create the key and return
// since the bucket is actually NULL
if (idx <= 255){
result = (Key) malloc (2 * sizeof(char));
assert(result != NULL);
result[0] = idx;
result[1] = '\0';
}
else{
assert(a->nodes[idx] != NULL);
// malloc and then strcmp
result = (char*)malloc((strlen(a->nodes[idx])+1)*sizeof(char));
assert(result != NULL);
strcpy(result, a->nodes[idx]);
}
return result;
}
// insert, no codeword required, simply insert in first in order
Index array_insert(Array a, const Key k){
assert(a != NULL && k != NULL);
assert(! array_is_full(a));
a->nodes[a->current_num] = (char*) malloc ((strlen(k)+1)*sizeof(char));
assert(a->nodes[a->current_num] != NULL);
strcpy(a->nodes[a->current_num], k);
a->current_num += 1; // update the counter
return a->current_num - 1;
}
// check if the index is in the array or not
// i.e. the array has corresponding codeword or not
bool array_has_this_codeword(Array a, const Index idx){
assert(a != NULL && idx >= 0);
if (idx < 256){
return true;
}
else{
return a->nodes[idx] != NULL;
}
}
// debug use
void array_print(const Array a){
assert(a != NULL);
fprintf(stdout, "-----Array print-----\n");
fprintf(stdout, "Size = %d, current_num = %ld\n", SIZE_LIMIT, a->current_num);
for (Index i = 0; i < SIZE_LIMIT; i++){
if (i <= 255){
fprintf(stdout, "%ld => %c\n", i, (int) i);
}
else if (a->nodes[i] != NULL){
fprintf(stdout, "%ld => %s\n", i, a->nodes[i]);
}
}
fprintf(stdout, "-----End-----\n");
return;
}