-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdict.c
211 lines (196 loc) · 5.29 KB
/
dict.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
/*
* this trie-based dict is optimized for alphabetic lookup.
* it has exactly the same API as the dict module and can be
* used in its place
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dict.h"
#include "mem.h"
#undef DICT_DEBUG
/* create a new dictionary entry (key + value) */
DICT_ENTRY *dictEntry_new(char *key, void *value) {
DICT_ENTRY *nde = (DICT_ENTRY *) malloc(sizeof(DICT_ENTRY));
nde->key = strdup(key);
nde->value = value;
return nde;
}
void dictEntry_free(DICT_ENTRY *entry) {
free(entry->key);
free(entry);
}
void dictEntry_freeData(DICT_ENTRY *entry, Destructor d) {
d(entry->value);
free(entry->key);
free(entry);
}
DICT *dict_new() {
DICT *nd = calloc(1, sizeof(DICT));
nd->entries = list_new();
return nd;
}
void dict_free(DICT *d) {
/* recursively free a DICT */
int i;
long il, len;
for (i = 0; i < 26; i++) {
if (d->children[i]) {
dict_free(d->children[i]);
}
}
len = list_length(d->entries);
for (il = 0; il < len; il++) {
DICT_ENTRY *e = (DICT_ENTRY *) list_get(d->entries, il);
dictEntry_free(e);
}
list_free(d->entries);
free(d);
}
void dict_freeValues(DICT *d, Destructor destructo) {
/* recursively free a DICT */
int i;
long il, len;
for (i = 0; i < 26; i++) {
if (d->children[i]) {
dict_freeValues(d->children[i], destructo);
}
}
len = list_length(d->entries);
for (il = 0; il < len; il++) {
DICT_ENTRY *e = (DICT_ENTRY *) list_get(d->entries, il);
dictEntry_freeData(e, destructo);
}
list_free(d->entries);
free(d);
}
void _dict_put(DICT *d, char *key, char *orig_key, void *data) {
char *kp = key;
int ix;
#ifdef DICT_DEBUG
printf("putting %s\n", key); /* debug */
#endif
/* if the key is empty, we've found or created the appropriate
leaf node and now just need to append an entry to it */
if (!*kp) {
DICT_ENTRY *newEntry;
newEntry = dictEntry_new(orig_key, data);
#ifdef DICT_DEBUG
printf("adding new entry %s (%p)\n", orig_key, newEntry);
fflush(stdout);
#endif
list_add(d->entries, newEntry);
return;
}
/* otherwise we need to skip non-alpha chars */
while (!isalpha(*kp)) {
kp++;
if (!*kp) { /* last char is non-alpha */
#ifdef DICT_DEBUG
printf("last char of %s is non-alpha\n",orig_key);
#endif
/* add to this node and quit */
_dict_put(d, kp, orig_key, data);
return;
}
}
ix = tolower(*kp) - 'a';
/* if the appropriate subtrie does not exist, create it */
if (!d->children[ix]) {
#ifdef DICT_DEBUG
printf("creating subtree for %c\n",*kp); /* debug */
#endif
d->children[ix] = dict_new();
}
/* recurse */
_dict_put(d->children[ix], kp + 1, orig_key, data);
}
void dict_put(DICT *d, char *key, void *data) {
_dict_put(d, key, key, data);
}
void *_dict_get(DICT *d, char *key, char *orig_key) {
char *kp = key;
int ix;
/* skip nonalphas */
while (*kp && !isalpha(*kp)) {
kp++;
}
if (!*kp) {
/* do a linear search of this node's entries */
int i, len;
len = list_length(d->entries);
#ifdef DICT_DEBUG
printf("searching %d entries for %s\n",len,orig_key); /* debug */
#endif
/* search from the end, so that new values shadow old */
for (i = len - 1; i >= 0; i--) {
DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i);
if (!strcmp(orig_key, de->key)) {
return de->value;
}
}
return NULL;
}
#ifdef DICT_DEBUG
printf("getting %s\n",kp); /* debug */
#endif
ix = tolower(*kp) - 'a';
if (!d->children[ix])
return NULL;
return _dict_get(d->children[ix], kp + 1, orig_key);
}
LIST *_dict_getAll(DICT *d, char *key, char *orig_key) {
char *kp = key;
int ix;
/* skip nonalphas */
while (*kp && !isalpha(*kp)) {
kp++;
}
#ifdef DICT_DEBUG
printf("getting %s\n",kp); /* debug */
#endif
if (!*kp) {
LIST *all;
int i, len;
all = list_new();
len = list_length(d->entries);
for (i = 0; i < len; i++) {
DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i);
list_add(all, de->value);
}
return all;
}
ix = tolower(*kp) - 'a';
if (!d->children[ix])
return NULL;
return _dict_get(d->children[ix], kp + 1, orig_key);
}
void *dict_get(DICT *d, char *key) {
return _dict_get(d, key, key);
}
LIST *dict_getAll(DICT *d, char *key) {
return _dict_getAll(d, key, key);
}
void dict_traverse(DICT *d, TraversalAction action, void *action_arg) {
int i;
if (!d)
return;
if (d->entries) {
int len;
len = list_length(d->entries);
#ifdef DICT_DEBUG
printf("%d entries\n", len);
#endif
for (i = 0; i < len; i++) {
DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i);
#ifdef DICT_DEBUG
printf("entry %d = %p\n", i, de);
#endif
action(de->key, de->value, action_arg);
}
}
for (i = 0; i < 26; i++)
if (d->children[i])
dict_traverse(d->children[i], action, action_arg);
}