-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrie.c
More file actions
211 lines (166 loc) · 3.86 KB
/
Trie.c
File metadata and controls
211 lines (166 loc) · 3.86 KB
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
#include "Trie.h"
Trie * Tr_create(unsigned int tokensize, BTCompare token_comparator)
{
Trie * toreturn;
toreturn=alloc(sizeof(Trie));
toreturn->count=0;
toreturn->root=alloc(sizeof(TrieNode));
toreturn->root->children=BT_create(token_comparator);
toreturn->root->data=0;
toreturn->tokensize=tokensize;
toreturn->comparator=token_comparator;
#ifdef SYNCHRONIZED
toreturn->TrSync=(LPCRITICAL_SECTION)alloc(sizeof(CRITICAL_SECTION));
InitializeCriticalSection(toreturn->TrSync);
#endif
return toreturn;
}
int IsNullToken(void * token, unsigned int tokensize)
{
unsigned int idx;
char * curptr=(char *)token;
for(idx=0;idx<tokensize;idx++)
{
if((*curptr))
return 0;
curptr+=tokensize;
}
return 1;
}
void Tr_add(Trie * pThis,void * key, void * data)
{
TrieNode * curnode, * nextnode;
void * pCurToken;
unsigned int i;
#ifdef SYNCHRONIZED
EnterCriticalSection(pThis->TrSync);
#endif
i=0;
pCurToken=key;
curnode=pThis->root;
while(!IsNullToken(pCurToken,pThis->tokensize))
{
if(!(nextnode=BT_find(curnode->children,pCurToken)))
{
nextnode=alloc(sizeof(TrieNode));
nextnode->children=BT_create(pThis->comparator);
nextnode->data=0;
BT_insert(curnode->children,pCurToken,nextnode);
}
curnode=nextnode;
i++;
pCurToken=(char *)key+i*(pThis->tokensize);
}
curnode->data=data;
pThis->count++;
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return;
}
void Tr_insert(Trie * pThis,void * key, unsigned int len, void * data)
{
TrieNode * curnode, * nextnode;
void * pCurToken;
unsigned int i;
#ifdef SYNCHRONIZED
EnterCriticalSection(pThis->TrSync);
#endif
curnode=pThis->root;
for(i=0;i<len;i++)
{
pCurToken=(char *)key+i*(pThis->tokensize);
if(!(nextnode=BT_find(curnode->children,pCurToken)))
{
nextnode=alloc(sizeof(TrieNode));
nextnode->children=BT_create(pThis->comparator);
nextnode->data=0;
BT_insert(curnode->children,pCurToken,nextnode);
}
curnode=nextnode;
}
curnode->data=data;
pThis->count++;
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return;
}
void * Tr_lookup(Trie * pThis,void * tofind)
{
TrieNode * curnode;
void * pCurToken;
unsigned int i;
void * toreturn;
#ifdef SYNCHRONIZED
EnterCriticalSection(pThis->TrSync);
#endif
i=0;
pCurToken=tofind;
curnode=pThis->root;
while(!IsNullToken(pCurToken,pThis->tokensize))
{
if(!(curnode=BT_find(curnode->children,pCurToken)))
{
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return 0;
}
i++;
pCurToken=(char *)tofind+i*(pThis->tokensize);
}
toreturn=curnode->data;
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return toreturn;
}
void * Tr_find(Trie * pThis,void * tofind, unsigned int len)
{
TrieNode * curnode;
void * pCurToken;
unsigned int i;
void * toreturn;
#ifdef SYNCHRONIZED
EnterCriticalSection(pThis->TrSync);
#endif
curnode=pThis->root;
for(i=0;i<len;i++)
{
pCurToken=(char *)tofind+i*(pThis->tokensize);
if(!(curnode=BT_find(curnode->children,pCurToken)))
{
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return 0;
}
}
toreturn=curnode->data;
#ifdef SYNCHRONIZED
LeaveCriticalSection(pThis->TrSync);
#endif
return toreturn;
}
void DeleteTrieNode(TrieNode * todelete)
{
BinaryTreeEnum * btenum;
TrieNode * curchild;
btenum=BT_newenum(todelete->children);
while(curchild=(TrieNode *)BT_next(btenum))
DeleteTrieNode(curchild);
BT_delete(todelete->children);
clean(todelete);
return;
}
void Tr_delete(Trie * todelete)
{
DeleteTrieNode(todelete->root);
#ifdef SYNCHRONIZED
DeleteCriticalSection(todelete->TrSync);
clean(todelete->TrSync);
#endif
clean(todelete);
return;
}