-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.h
229 lines (214 loc) · 5.39 KB
/
trie.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/mman.h>
#define boolean unsigned short int
#define INITIALIZE_NODE(n, character) \
n=(tnode*)malloc(sizeof(tnode)); \
n->right=n->child=NULL; \
n->value=character;\
n->index=0;
#define INITIALIZE_DATA(input, output, len) \
assert(input!=NULL);\
len=strlen(input); \
output=(char*)malloc(sizeof(char)*(len+1));\
strncpy(output, input, len);\
if (output[len-1] == '\n'){\
output[len-1]='\0';\
}\
else {\
output[len]='\0';\
len=len+1;\
}
typedef struct _tnode{
char value;
struct _tnode* right;
struct _tnode* child;
unsigned short int index;
}tnode;
int data_file_size=0;
int max_buffer_size=100000;
void serialize_t(tnode* root, void** data);
void write_to_file(FILE *data_file, tnode *root){
void * data;
data = (char*)malloc(sizeof(char)*max_buffer_size);
//void* save_data_pointer = data;
serialize_t(root, &data);
fwrite(data-data_file_size, data_file_size, 1, data_file);
}
//mmap(NULL, length, PROT_READ || PROT_WRITE, MAP_PRIVATE, fd, offset);
void write_t(void *data, int data_size, void** buffer){
if (data_file_size+data_size>max_buffer_size){
void *temp = (char*)malloc((sizeof(char))*2*(max_buffer_size));
*buffer=(*buffer)-data_file_size;
memcpy(temp, *buffer, data_file_size);
free(*buffer);
*buffer=temp+data_file_size;
max_buffer_size=2*(max_buffer_size);
}
strncpy(*buffer, data, data_size);
*buffer=*buffer+data_size;
data_file_size+=data_size;
}
//set first byte if value is present,
// if present set second byte if right node is present
void serialize_t(tnode *root, void **data){
int value,rvalue, cvalue;
value=rvalue=cvalue=0;
if(root==NULL){
write_t((void*)&value, sizeof(value), data);
return;
}
else{
value=1;
write_t((void*)&value, sizeof(value), data);
write_t((void*)&(root->value), sizeof(root->value), data);
write_t((void*)&(root->index), sizeof(root->index), data);
if(root->right){
rvalue=1;
write_t((void*)&rvalue, sizeof(rvalue), data);
serialize_t(root->right, data);
}
else {
rvalue=0;
write_t((void*)&rvalue, sizeof(rvalue), data);
}
if(root->child){
cvalue=1;
write_t((void*)&cvalue, sizeof(cvalue), data);
serialize_t(root->child, data);
}
else {
cvalue=0;
write_t((void*)&cvalue, sizeof(cvalue), data);
}
}
}
void deserialize_t(tnode *root, void **data);
void read_from_file(FILE *data_file, tnode **root){
void *data;
data=(char*)malloc(sizeof(char)*data_file_size);
assert(data_file!=NULL);
*root=(tnode*)malloc(sizeof(tnode));
(*root)->child=(*root)->right=NULL;
fread(data, data_file_size, 1, data_file);
if(ferror(data_file)){
perror("Error while reading from the file");
}
deserialize_t(*root, &data);
}
void read_t(void* data, int data_size, void** buffer){
strncpy(data, *buffer, data_size);
*buffer=*buffer+data_size;
}
void deserialize_t(tnode *root, void **data){
assert(root!=NULL);
int value, rvalue, cvalue;
char char_data;
value=rvalue=cvalue=0;
unsigned short int indexvalue=0;
read_t((void*)&value, sizeof(value), data);
if(value==0){
return; //nothing to read more
}
else{
read_t((void*)&char_data, sizeof(char_data), data);
root->value=char_data;
read_t((void*)&indexvalue, sizeof(indexvalue), data);
root->index=indexvalue;
read_t((void*)&rvalue, sizeof(rvalue), data);
if(rvalue==1){
tnode* right_node=(tnode*)malloc(sizeof(tnode));
right_node->child=right_node->right=NULL;
root->right=right_node;
deserialize_t(root->right, data);
}
read_t((void*)&cvalue, sizeof(cvalue), data);
if (cvalue==1){
tnode* child_node=(tnode*)malloc(sizeof(tnode));
child_node->child=child_node->right=NULL;
root->child=child_node;
deserialize_t(root->child, data);
}
}
}
//return the index if the data is found else return -1.
int insert_t(tnode *root, char* input){
boolean right=0, found=0;
assert(strcmp(input, "")!=0);
char *data;
int data_len=0;
INITIALIZE_DATA(input, data, data_len);
if (root == NULL){
INITIALIZE_NODE(root, '\0');//let's say root value is always this
root->index = 0;// reinitilaized to zero, let this contain the trie word count
}
tnode* curr_node=root->child;
tnode* prev_node = root;
int i=0;
while(i<data_len){
if(curr_node == NULL){
INITIALIZE_NODE(curr_node, data[i]);
if(right == 1){
prev_node->right = curr_node;
right = 0;
}
else {
prev_node->child = curr_node;
}
if (i == data_len-1){
curr_node->index=++(root->index);
}
prev_node = curr_node;
curr_node=curr_node->child;
i++;
found = 0;
}
else if(curr_node->value == data[i]){
prev_node=curr_node;
curr_node=curr_node->child;
i++;
found = 1;
}
else{
prev_node=curr_node;
curr_node = curr_node->right;
right = 1;
}
}
if (found == 1){
return prev_node->index;
}
return -1;
}
//return the index if the data is found else return -1.
int search_t(tnode *root, char* input){
assert(strcmp(input, "")!=0);
if((root == NULL) || (root->child == NULL)){
return -1;
}
int data_len=0;
char *data;
INITIALIZE_DATA(input, data, data_len);
int i=0;
tnode* curr_node = root->child;
while(i<data_len){
if(curr_node->value == data[i]){
if(curr_node->value == '\0'){
return curr_node->index;
}
curr_node=curr_node->child;
i++;
}
else if(curr_node->right != NULL){
curr_node=curr_node->right;
}
else{
// not found
return -1;
}
}
return -1;
}