-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.c
159 lines (122 loc) · 2.31 KB
/
trie.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "trie.h"
#ifdef DEBUG_ON
#define LOG(...) printf(__VA_ARGS__)
#else
#define LOG(...)
#endif
typedef struct tag_trie {
char c;
unsigned int n;
void *value;
struct tag_trie **next;
} trie;
static TRIE *st;
static trie* trie_make(void) {
trie *p = (trie*)malloc(sizeof(trie));
if (p == NULL) return p;
p->c = 0;
p->n = 0;
p->next = NULL;
p->value = NULL;
return p;
}
TRIE Trie_Make(void) {
return (TRIE)trie_make();
}
static void trie_free(trie *p) {
int i;
for (i=0; i<p->n; i++) {
trie_free(p->next[i]);
}
free(p);
}
void Trie_Free(TRIE vp) {
trie_free((trie*)vp);
}
static trie* trie_put(trie *p, const char *key, void *value) {
trie *match = NULL;
int i;
if (*key == 0) {
p->value = value;
return p;
}
for (i=0; i<p->n; i++) {
if (p->next[i]->c == *key) {
/*! Hit */
match = p->next[i];
break;
}
}
if (!match) {
/*! No Hit */
trie **nodes;
if (!(match = trie_make())) return NULL;
nodes = (trie**)realloc(p->next, (p->n+1) * sizeof(trie*));
if (!nodes) return NULL;
p->next = nodes;
for (i=0; i<p->n; i++) {
trie *dp = p->next[i];
}
p->next[p->n] = match;
for (i=0; i<=p->n; i++) {
//int *dp = (int*)p->next[i];
trie *dp = p->next[i];
}
p->next[p->n]->c = *key;
p->n++;
}
return trie_put(match, key+1, value);
}
TRIE Trie_Put(TRIE vp, const char *key, void *value) {
st = vp;
return (TRIE)trie_put((trie*)vp, key, value);
}
static void* trie_get(trie *p, const char *key) {
int i;
if (*key == 0) {
return p->value;
}
for (i=0; i<p->n; i++) {
if (*key == p->next[i]->c) {
return trie_get(p->next[i], key+1);
}
}
return NULL;
}
void* Trie_Get(TRIE vp, const char *key) {
return (void *)trie_get((trie*)vp, key);
}
static void trie_show(trie *p, int depth, bool *cr) {
int i;
if (*cr) {
for (i=0; i<depth-2; i++) {
LOG(" ");
}
if (depth > 2) {
LOG(" \\_");
}
*cr = false;
}
if (depth != 0) {
LOG("\'%c\', ", p->c);
}
if (p->value != NULL) {
LOG("! \t\t-> \"%s\" \n", (char*)p->value);
*cr = true;
} else if (p->n == 0) {
LOG("\n");
*cr = true;
}
for (i=0; i<p->n; i++) {
trie_show(p->next[i], depth+1, cr);
}
}
void Trie_Show(TRIE vp) {
int i;
trie *t = (trie*)vp;
bool cr = false;
trie_show((trie*)vp, 0, &cr);
}