-
Notifications
You must be signed in to change notification settings - Fork 1
/
RadixTree.c
270 lines (226 loc) · 7.48 KB
/
RadixTree.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
#include "RadixTree.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
RadixNode * create(){
RadixNode * res = malloc(sizeof(RadixNode));
res->num_edges = 0;
res->edge_len = 0;
return res;
}
/**
* Insert edge into the specific nodes, edge array
*
*/
void insert_in_array(RadixNode * node, RadixEdge * edge){
//If true, we have to increase array space.
if ( node->num_edges + 1 > node->edge_len ) {
node->edge_len += ARRAY_G_FACTOR;
RadixEdge ** tmp = realloc (node->edges, node->edge_len * sizeof(RadixEdge*));
if(tmp == NULL ){
free(tmp);
exit(1);
}
node->edges = tmp;
}
//Add the array.
node->edges[node->num_edges] = edge;
node->num_edges++;
}
/**
* Helps setting the value of edge, only used for making code prettier.
*/
void set_label(RadixEdge * edge, char * value, int len){
if(edge == NULL ){
return;
}
edge->label = malloc(sizeof(char)*len);
memcpy(edge->label, value,sizeof(char)*len);
edge->label_len = strlen(edge->label); //Since otherwise we would have to do this alot!
}
/**
* Returns the lenght longest prefix where matched indicates at which index the c
* comparison should start for argument 1.
* returns the match length.
*/
int max_prefix_len(char * str1, char * prefix, int matched){
int s1_len = strlen(str1);
int s2_len = strlen(prefix);
int i = 0;
while( ( i < s1_len && i < s2_len ) && str1[i+matched] == prefix[i]){
i++;
}
return i;
}
int is_prefix(char * str, char * prefix, int matched){
return (0==strncmp(&str[matched],prefix,strlen(prefix)) ? true: false);
}
typedef struct Pair{
int m_len;
int p_len;
RadixEdge * edge;
}Pair;
Pair * find_node(RadixNode * current, RadixEdge * current_edge, char * name, int match, int max_prefix){
//~ if(current->num_edges <= 0 ){
//~ Pair * p = malloc(sizeof(Pair));
//~ p->edge = current_edge;
//~ p->m_len = match;
//~ p->p_len = max_prefix;
//~ return p;
//~ }
int i=0;
RadixEdge * best_edge = NULL;
int longest_prefix = -1;
for(;i<current->num_edges; i++){
int prefix_len = max_prefix_len(name,current->edges[i]->label,match);
if(prefix_len > 0 ) {
return find_node(current->edges[i]->target, current->edges[i], name, match + prefix_len, prefix_len);
}
}
Pair * p = malloc(sizeof(Pair));
p->edge = current_edge;
p->m_len = match;
p->p_len = max_prefix;
return p;
}
void insert(RadixNode * root, char * name, void * value){
//~ //Create the actual new node.
RadixNode * new_node = malloc(sizeof(RadixNode));
int name_len = strlen(name);
new_node->name = malloc(sizeof(char)*name_len);
memcpy(new_node->name, name,sizeof(char)*name_len);
new_node -> value = value;
//New empty tree, insert immidiately.
if(root->num_edges == 0 ){
RadixEdge * edge = malloc(sizeof(RadixEdge));
set_label(edge,name,strlen(name));
edge->from = root;
edge->target = new_node;
insert_in_array(root, edge);
return;
}
Pair * edge_pair = find_node(root, NULL, name, 0,0);
RadixEdge * best_edge = edge_pair -> edge;
//If true, we have to add new edge to root.
if(NULL == best_edge){
RadixEdge * edge = malloc(sizeof(RadixEdge));
set_label(edge,name,strlen(name));
edge->from = root;
edge->target = new_node;
insert_in_array(root, edge);
return;
}
//~ fprintf(stderr,"Kanten i fann heter : %s matchning: %d på namnet :%s\n", best_edge->label, edge_pair->m_len, name);
RadixEdge * new_edge = malloc(sizeof(RadixEdge));
set_label(new_edge,&name[edge_pair->m_len],strlen(name)-edge_pair->m_len);
//New edge point to new node (the node continag the value)
new_edge->target = new_node;
//in this we only have to add our new edge.
if(strlen(best_edge->label) == edge_pair -> p_len ) {
insert_in_array(best_edge->target, new_edge);
}
else{
RadixEdge * old_new_edge = malloc(sizeof(RadixEdge));
set_label(old_new_edge, &best_edge->label[edge_pair->p_len], strlen(best_edge->label) - edge_pair->p_len);
old_new_edge->target = best_edge -> target;
//empty intermediate node
RadixNode * inter_mediate = malloc(sizeof(RadixNode));
best_edge -> target = inter_mediate;
//Add both new edges to intermediate node.
insert_in_array(inter_mediate, old_new_edge);
insert_in_array(inter_mediate, new_edge);
//Update old label (the parent label)
set_label(best_edge, best_edge->label, edge_pair->p_len);
}
//~ new_edge->from = best_edge->;
//~ new_edge->target = new_node;
//~ best_edge->from
//Uppdatera kanten.
//Flytta ner det som blir över.
//Stoppa in nya värdet.
//~ fprintf(stderr,"broke that shit\n");
//~ assert(current_node != NULL);
//Special case, no previous
//~ if(max_prefix <= 0 ){
//~ RadixEdge * edge = malloc(sizeof(RadixEdge));
//~ set_label(edge,name,strlen(name));
//~
//~ edge->from = current_node;
//~ edge->target = new_node;
//~ insert_in_array(current_node, edge);
//~ return;
//~ }
//~ assert(current_edge != NULL);
//~ RadixEdge * new_edge_old = malloc(sizeof(RadixEdge));
//~ RadixEdge * new_edge_new = malloc(sizeof(RadixEdge));
//~
//~ fprintf(stderr,"Den kanten vi valde var: %s\n", current_edge->label);
//~ RadixNode * new_sub_node = malloc(sizeof(RadixNode));
//~
//~ new_edge_old->from = new_sub_node;
//~ new_edge_old->target = current_edge->target;
//~ set_label(new_edge_old, ¤t_edge->label[max_prefix], strlen(current_edge->label));
//~ fprintf(stderr,"First label = %s\n", new_edge_old->label);
//~ new_edge_new ->target = new_node;
//~ new_edge_new->from = new_sub_node;
//~ set_label(new_edge_new, &name[max_prefix], strlen(name)-max_prefix);
//~ fprintf(stderr,"Second label = %s\n", new_edge_new->label);
//~ current_edge->target = new_sub_node;
//~
//~ //Set label to the maximal matched prefix.
//~ set_label(current_edge,current_edge->label,max_prefix);
//~
//~ //Set correct label.
//~ insert_in_array(new_sub_node, new_edge_old);
//~ insert_in_array(new_sub_node, new_edge_new);
}
void delete();
RadixNode * find(RadixNode * root, char * name){
RadixNode * current_node = root;
RadixEdge * current_edge = NULL;
int matched = 0;
int name_len = strlen(name);
bool prefix_match = false;
int i;
while(root != NULL && matched < name_len){
for(i = 0; i<current_node->num_edges; i++){
if( true == is_prefix(name, current_node->edges[i]->label, matched) ){
//~ fprintf(stderr," det i matchad: %s == %s med match %d\n", name, current_node->edges[i]->label, matched);
current_edge = current_node->edges[i];
prefix_match = true;
//~ fprintf(stderr,"prefix = %s mat = %d\n", current_node->edges[i]->label,matched);
break;
}
}
if(!prefix_match){ //If no prefix is matched, value does not exist.
break;
}
else{
//~ fprintf(stderr,"inana e matched %d s: %s\n", matched,current_edge->label);
matched += strlen(current_edge->label);
current_node = current_edge->target;
//~ fprintf(stderr, "vad böor denna %d %d\n", is_prefix("bassning", "pet", 0),true);
return NULL;
}
}
//~ fprintf(stderr,"matched = %d\n", matched);
return (matched==name_len && 0 == current_edge->target->num_edges ? current_edge->target : NULL);
}
void * find_value(RadixNode * root,char * name){
return find(root,name)->value;
}
void print_tabs(int tabs){
while(tabs--){
printf("\t");
}
}
void pretty_print(RadixNode * root, int tabs){
RadixNode * current = root;
int i = 0;
for( i = 0; i< current->num_edges; i++){
print_tabs(tabs);
printf("'%s'\n",current->edges[i]->label);
if(current->edges[i]->target->num_edges > 0 ){
pretty_print(current->edges[i]->target,tabs+1);
}
}
}