-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
235 lines (134 loc) · 3.45 KB
/
functions.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
#include<stdio.h>
#include<stdlib.h>
#include"functions.h"
#include"algorithms.h"
//This function returns a node pointer by creating a node according to the given parameters
trie_node * get_node(char c, int d){
trie_node * node ;
node = (trie_node *)(malloc(sizeof(trie_node)));
for(int i = 0; i <= 255; i++)
{
node->child[i] = NULL;
}
node->data = c;
node->index = d;
return node;
}
//initializes the dictionary containing single characters
void init_encoding_dictionary(encoding_dictionary * t){
trie_node * new_node;
new_node = (trie_node *)(malloc(sizeof(trie_node)));
for(int i = 0; i <= 255 ; i++){
trie_node* tmp_node;
tmp_node = get_node( '\0' + i ,i);
new_node->child[i] = tmp_node;
}
//put the index of root as -1.
new_node->index = -1;
*t = new_node;
return ;
}
/*
This function inserts the pattern
also updates the dictionary_size after inserting the pattern
*/
void insert_pattern(encoding_dictionary *t, char * s){
trie_node * node = *t;
int i = 0;
int flg = 0;
int ret = 0;
while(1){
if(s[i] == '\0' || flg == 1)
{
break ;
}
int pos = s[i];
if( node->child[pos] == NULL ){
flg = 1;
dictionary_size++;
trie_node *tmp_node = get_node(s[i] , dictionary_size );
node->child[pos] = tmp_node;
}
else {
node = node->child[pos];
ret = node->index;
i++;
}
}
}
/*
This function searches a pattern in the dictionary
returns -1, if the pattern is not present
returns the code for pattern if present
*/
int search_pattern(encoding_dictionary t, char *p)
{
int i = 0;
trie_node * node = t;
int flg = 0;
int ret = 0;
while(p[i]!='\0')
{
char c = p[i];
int pos = c;
if(node->child[pos] != NULL )
{
node = node->child[pos];
ret = node->index;
}
else
{
flg = 1;
break;
}
i++;
}
if(flg)return -1;
return ret;
}
void add_element_to_dictionary(int p, char c, dict_element * decoding_dictionary)
{
dictionary_size++;
dict_element tmp ;
tmp.curr_char = c;
tmp.prefix_code = p;
decoding_dictionary[dictionary_size] = tmp;
return;
}
void init_decoding_dictionary(dict_element * decoding_dictionary)
{
for(int i=0;i<=255;i++)
{
dict_element dt;
dt.prefix_code = -1;
dt.curr_char = '\0'+i;
decoding_dictionary[i] = dt;
}
}
void destroy_encoding_dictionary(encoding_dictionary t){
for(int i=0;i <=255;i++){
if(t->child[i] != NULL){
destroy_encoding_dictionary(t->child[i]);
}
}
free(t);
}
void get_output(int code, char *t, FILE * op, dict_element* decoding_dictionary)
{
char output[32768];
int i = 0;
while(1)
{
if(code == -1)break;
output[i] = decoding_dictionary[code].curr_char;
i++;
code = decoding_dictionary[code].prefix_code;
}
i--;
*t = output[i];
for(int j = i; j>=0; j--) //writing the given string in reverse manner
{
char c = output[j];
fputc(c,op); //one character at a time
}
}