1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
41 typedef struct trie_entry_t trie_entry_t;
47 int32_t children_offset;
52 trie_entry_t *entries;
65 int32_t keys_offset_len;
66 int32_t keys_offset_size;
71 trie_t *trie_new(void)
73 return p_new(trie_t, 1);
76 static inline void trie_cleanup_build_data(trie_t *trie)
78 p_delete(&trie->keys);
79 p_delete(&trie->keys_offset);
80 trie->keys_len = trie->keys_size = 0;
81 trie->keys_offset_len = trie->keys_offset_size = 0;
84 void trie_delete(trie_t **trie)
87 trie_cleanup_build_data(*trie);
89 p_delete(&(*trie)->entries);
90 p_delete(&(*trie)->c);
95 /** Check that the given entry is a prefix for the given key.
97 static inline bool trie_entry_c_match(const trie_t *trie,
98 const trie_entry_t *entry,
99 const char *key, int *pos)
101 const char *c = trie->c + entry->c_offset;
103 for (i = 0 ; i < entry->c_len ; ++i) {
104 if (key[i] != c[i]) {
117 static inline bool trie_entry_match(const trie_t *trie,
118 const trie_entry_t *entry, const char *key)
120 return !!(strcmp(trie->c + entry->c_offset, key) == 0);
123 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
125 return entry->children_len == 0;
128 /** Lookup for a child of entry matching the given entry at the given pos.
129 * Only the first character of the children is taken into account in the
130 * lookup. The current entry is assumed to match the key.
132 static inline trie_entry_t* trie_entry_child(const trie_t *trie,
133 const trie_entry_t* entry,
136 int start = entry->children_offset;
137 int end = start + entry->children_len;
140 while (start < end) {
141 int mid = (start + end) / 2;
142 trie_entry_t* child = &trie->entries[mid];
143 const char c2 = trie->c[child->c_offset];
161 #define GROW(Buffer, Added, Len, Size) \
163 const int required_size = (Len) + (Added); \
164 int next_size = (Size); \
165 if (next_size >= required_size) { \
169 next_size = p_alloc_nr(next_size); \
170 } while (next_size < required_size); \
171 p_allocgrow(&(Buffer), next_size, &(Size)); \
174 static inline void trie_grow(trie_t *trie, int delta)
176 GROW(trie->entries, delta, trie->entries_len, trie->entries_size);
179 static inline int trie_entry_new(trie_t *trie)
181 memset(trie->entries + trie->entries_len, 0, sizeof(trie_entry_t));
182 return trie->entries_len++;
185 static inline int trie_add_leaf(trie_t *trie, const char *key)
188 int len = m_strlen(key) + 1;
189 entry = &trie->entries[trie_entry_new(trie)];
190 GROW(trie->c, len, trie->c_len, trie->c_size);
191 memcpy(trie->c + trie->c_len, key, len);
192 entry->c_offset = trie->c_len;
195 return trie->entries_len - 1;
198 static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
201 if (entry->children_len == 0) {
202 entry->children_offset = pchild;
203 entry->children_len = 1;
205 if (entry->children_offset + entry->children_len != pchild) {
206 printf("Inserting child %d while offset is %d[%d]\n",
207 pchild, entry->children_offset, entry->children_len);
210 ++entry->children_len;
214 static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
217 child = &trie->entries[trie_entry_new(trie)];
219 child->c_offset = entry->c_offset;
220 child->c_len = entry->c_len;
224 child->c_offset = entry->c_offset + pos;
225 child->c_len = entry->c_len - pos;
228 child->children_offset = entry->children_offset;
229 child->children_len = entry->children_len;
230 entry->children_offset = trie->entries_len - 1;
231 entry->children_len = 1;
234 void trie_insert(trie_t *trie, const char* key)
236 assert(trie->entries == NULL && "Trie already compiled");
238 int len = m_strlen(key) + 1;
239 GROW(trie->keys, len, trie->keys_len, trie->keys_size);
240 memcpy(trie->keys + trie->keys_len, key, len);
242 GROW(trie->keys_offset, 1, trie->keys_offset_len, trie->keys_offset_size);
243 trie->keys_offset[trie->keys_offset_len++] = trie->keys_len;
244 trie->keys_len += len;
248 static inline void trie_compile_aux(trie_t *trie, int id,
249 int first_key, int last_key, int offset,
256 for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
257 current = trie->keys[trie->keys_offset[first_key] + offset];
258 for (int i = first_key + 1 ; i < last_key ; ++i) {
259 const char *str = trie->keys + trie->keys_offset[i];
260 const char c = str[offset];
264 trie_entry_split(trie, &trie->entries[id], off_diff);
266 trie_entry_insert_child(trie, &trie->entries[id],
267 trie_add_leaf(trie, str + offset));
268 forks[fork_pos++] = i;
272 if (fork_pos == 0 && current == '\0') {
276 forks[fork_pos] = last_key;
278 const int children_len = trie->entries[id].children_len;
279 for (int i = 0 ; i < children_len ; ++i) {
280 int child = trie->entries[id].children_offset + i;
281 if (forks[i] - 1 > first_key) {
282 trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
284 first_key = forks[i];
288 void trie_compile(trie_t *trie, bool memlock)
290 assert(trie->entries == NULL && "Trie already compiled");
291 assert(trie->keys != NULL && "Trying to compile an empty trie");
293 # define QSORT_TYPE int
294 # define QSORT_BASE trie->keys_offset
295 # define QSORT_NELT trie->keys_offset_len
296 # define QSORT_LT(a,b) strcmp(trie->keys + *a, trie->keys + *b) < 0
300 trie_grow(trie, trie->keys_len);
301 trie_compile_aux(trie, trie_add_leaf(trie, trie->keys),
302 0, trie->keys_offset_len, 0, 0);
303 trie_cleanup_build_data(trie);
304 p_shrink(&trie->entries, trie->entries_len, &trie->entries_size);
305 p_shrink(&trie->c, trie->c_len, &trie->c_size);
311 bool trie_lookup(const trie_t *trie, const char *key)
313 assert(trie->keys == NULL && "Can't lookup: trie not compiled");
314 if (trie->entries_len == 0) {
317 trie_entry_t *current = trie->entries;
320 if (trie_entry_is_leaf(current)) {
321 return trie_entry_match(trie, current, key);
322 } else if (trie_entry_c_match(trie, current, key, &pos)) {
324 current = trie_entry_child(trie, current, key);
325 if (current == NULL) {
335 void trie_lock(trie_t *trie)
340 if (mlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len) != 0) {
344 if (mlock(trie->c, trie->c_len) != 0) {
346 munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len);
352 void trie_unlock(trie_t *trie)
357 munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len);
358 munlock(trie->c, trie->c_len);
359 trie->locked = false;
365 static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
366 const trie_entry_t *entry, int level)
368 static int max_depth = 0;
369 static int leaves = 0;
370 static int depth_sum = 0;
372 if (trie_entry_is_leaf(entry)) {
373 if (level > max_depth) {
380 for (int i = 0 ; i < level ; ++i) {
383 if (entry->c_len == 0) {
384 fputs("(nil)", stdout);
386 const char *c = trie->c + entry->c_offset;
387 printf("(%d) ", entry->c_len);
388 for (int i = 0 ; i < entry->c_len ; ++i) {
392 fputs("\\0 ", stdout);
398 for (int i = entry->children_offset ;
399 i < entry->children_offset + entry->children_len ; ++i) {
400 trie_entry_inspect(trie, show_content, &trie->entries[i], level + 1);
403 printf("Average char per node: %d\n", trie->c_len / trie->entries_len);
404 printf("Number of nodes: %d\n", trie->entries_len);
405 printf("Number of leaves: %d\n", leaves);
406 printf("Max depth: %d\n", max_depth);
407 printf("Average leaf depth: %d\n", depth_sum / leaves);
408 printf("Memory used: %d\n", (trie->entries_size * sizeof(trie_entry_t))
409 + (trie->c_size) + sizeof(trie_t));
413 void trie_inspect(const trie_t *trie, bool show_content)
415 trie_entry_inspect(trie, show_content, trie->entries, 0);