Fix build of the trie.
[apps/pfixtools.git] / common / trie.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
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     */
17 /*     permission.                                                            */
18 /*                                                                            */
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 /******************************************************************************/
31
32 /*
33  * Copyright © 2008 Florent Bruneau
34  */
35
36 #include "array.h"
37 #include "str.h"
38 #include "trie.h"
39
40 typedef struct trie_entry_t trie_entry_t;
41
42 struct trie_entry_t {
43     uint32_t c_offset;
44     uint32_t children_offset;
45
46     uint16_t c_len;
47     uint16_t children_len;
48 };
49 #define TRIE_ENTRY_INIT { 0, 0, 0, 0 }
50 ARRAY(trie_entry_t)
51
52 struct trie_t {
53     A(trie_entry_t) entries;
54     A(char)         c;
55     A(char)         keys;
56     A(int)          keys_offset;
57
58     bool locked;
59 };
60
61 trie_t *trie_new(void)
62 {
63     return p_new(trie_t, 1);
64 }
65
66 static inline void trie_cleanup_build_data(trie_t *trie)
67 {
68     array_wipe(trie->keys);
69     array_wipe(trie->keys_offset);
70 }
71
72 void trie_delete(trie_t **trie)
73 {
74     if (*trie) {
75         trie_cleanup_build_data(*trie);
76         trie_unlock(*trie);
77         array_wipe((*trie)->entries);
78         array_wipe((*trie)->c);
79         p_delete(trie);
80     }
81 }
82
83 /** Check that the given entry is a prefix for the given key.
84  */
85 static inline bool trie_entry_c_match(const trie_t *trie,
86                                       const trie_entry_t *entry,
87                                       const char *key)
88 {
89     const char *c = array_ptr(trie->c, entry->c_offset);
90     int i = 0;
91     for (i = 0 ; i < entry->c_len ; ++i) {
92         if (key[i] != c[i]) {
93             return false;
94         }
95     }
96     return true;
97 }
98
99 static inline bool trie_entry_match(const trie_t *trie,
100                                     const trie_entry_t *entry, const char *key)
101 {
102     return !!(strcmp(array_ptr(trie->c, entry->c_offset), key) == 0);
103 }
104
105 static inline bool trie_entry_prefix(const trie_t *trie,
106                                      const trie_entry_t *entry, const char *key)
107 {
108     int len = entry->c_len;
109     if (len > 0 && array_elt(trie->c, entry->c_offset + len - 1) == '\0') {
110         --len;
111     }
112     return !!(strncmp(array_ptr(trie->c, entry->c_offset), key, len) == 0);
113 }
114
115 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
116 {
117     return entry->children_len == 0;
118 }
119
120 /** Lookup for a child of entry matching the given entry at the given pos.
121  * Only the first character of the children is taken into account in the
122  * lookup. The current entry is assumed to match the key.
123  */
124 static inline const trie_entry_t* trie_entry_child(const trie_t *trie,
125                                                    const trie_entry_t* entry,
126                                                    const char *key)
127 {
128     uint32_t start = entry->children_offset;
129     uint32_t end   = start + entry->children_len;
130     const char c = *key;
131
132     while (start < end) {
133         uint32_t mid = (start + end) >> 1;
134         const trie_entry_t* child = array_ptr(trie->entries, mid);
135         const char c2 = array_elt(trie->c, child->c_offset);
136
137         if (c2 == c) {
138           return child;
139         }
140         if (c < c2) {
141           end = mid;
142         } else {
143           start = mid + 1;
144         }
145     }
146     return NULL;
147 }
148
149 static inline uint32_t trie_entry_new(trie_t *trie)
150 {
151     const trie_entry_t e = TRIE_ENTRY_INIT;
152     array_add(trie->entries, e);
153     return trie->entries.len - 1;
154 }
155
156 static inline uint32_t trie_add_leaf(trie_t *trie, const char *key)
157 {
158     trie_entry_t *entry;
159     int len = m_strlen(key) + 1;
160     int id  = trie_entry_new(trie);
161     entry = array_ptr(trie->entries, id);
162     entry->c_offset = trie->c.len;
163     entry->c_len    = len;
164 #ifdef CHECK_INTEGRITY
165     for (int i = 0 ; i < len - 1 ; ++i) {
166         if (key[i] == '\0') {
167             printf("Found a '\\0' in the string of the leaf\n");
168             abort();
169         }
170     }
171     if (key[len - 1] != '\0') {
172       printf("Key does not end with a '\\0'");
173       abort();
174     }
175 #endif
176     array_append(trie->c, key, len);
177     return trie->entries.len - 1;
178 }
179
180 static inline void trie_entry_insert_child(trie_t *trie, uint32_t id, uint32_t pchild)
181 {
182     trie_entry_t *entry = array_ptr(trie->entries, id);
183     if (entry->children_len == 0) {
184         entry->children_offset = pchild;
185         entry->children_len    = 1;
186     } else {
187         if (entry->children_offset + entry->children_len != pchild) {
188             printf("Inserting child %d while offset is %d[%d]\n",
189                    pchild, entry->children_offset, entry->children_len);
190             abort();
191         }
192         ++entry->children_len;
193     }
194 }
195
196 static inline void trie_entry_split(trie_t *trie, uint32_t id, uint16_t pos)
197 {
198     trie_entry_t *child;
199     trie_entry_t *entry;
200     child    = array_ptr(trie->entries, trie_entry_new(trie));
201     entry    = array_ptr(trie->entries, id);
202     if (pos == 0) {
203         child->c_offset = entry->c_offset;
204         child->c_len    = entry->c_len;
205         entry->c_offset = 0;
206         entry->c_len    = 0;
207     } else {
208         assert(pos <= entry->c_len);
209         child->c_offset = entry->c_offset + pos;
210         child->c_len    = entry->c_len - pos;
211         entry->c_len    = pos;
212     }
213     child->children_offset = entry->children_offset;
214     child->children_len    = entry->children_len;
215     entry->children_offset = trie->entries.len - 1;
216     entry->children_len    = 1;
217 }
218
219 void trie_insert(trie_t *trie, const char* key)
220 {
221     assert(trie->entries.len == 0 && "Trie already compiled");
222
223     int len = m_strlen(key) + 1;
224     array_add(trie->keys_offset, trie->keys.len);
225     array_append(trie->keys, key, len);
226 }
227
228
229 static inline void trie_compile_aux(trie_t *trie, uint32_t id,
230                                     uint32_t first_key, uint32_t last_key,
231                                     int offset)
232 {
233     uint32_t forks[256];
234     uint32_t fork_pos = 0;
235     char current = '\0';
236
237 #ifdef CHECK_INTEGRITY
238     assert(strcmp(array_ptr(trie->keys, array_elt(trie->keys_offset, first_key) + offset),
239                   array_ptr(trie->c, array_elt(trie->entries, id).c_offset)) == 0);
240 #endif
241
242     for (int off_diff = 0 ; fork_pos == 0 ; ++off_diff, ++offset) {
243         current = array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset);
244         for (uint32_t i = first_key + 1 ; i < last_key ; ++i) {
245             const char *str = array_ptr(trie->keys, array_elt(trie->keys_offset, i));
246             const char c = str[offset];
247             if (c != current) {
248                 array_ensure_capacity_delta(trie->entries, 2);
249                 if (fork_pos == 0) {
250                     trie_entry_split(trie, id, off_diff);
251                 }
252                 trie_entry_insert_child(trie, id, trie_add_leaf(trie, str + offset));
253                 forks[fork_pos++] = i;
254                 current = c;
255             }
256         }
257         if (fork_pos == 0 && current == '\0') {
258             return;
259         }
260     }
261     forks[fork_pos] = last_key;
262
263     const uint8_t children_len = array_elt(trie->entries, id).children_len;
264     for (uint16_t i = 0 ; i < children_len ; ++i) {
265         int child = array_elt(trie->entries, id).children_offset + i;
266         if (forks[i] - 1 > first_key) {
267             trie_compile_aux(trie, child, first_key, forks[i], offset - 1);
268         }
269         first_key = forks[i];
270     }
271 }
272
273 void trie_compile(trie_t *trie, bool memlock)
274 {
275     assert(trie->entries.len == 0 && "Trie already compiled");
276     assert(trie->keys.len != 0 && "Trying to compile an empty trie");
277     {
278 #       define QSORT_TYPE int
279 #       define QSORT_BASE trie->keys_offset.data
280 #       define QSORT_NELT trie->keys_offset.len
281 #       define QSORT_LT(a,b) strcmp(trie->keys.data + *a, trie->keys.data + *b) < 0
282 #       include "qsort.c"
283     }
284
285     array_ensure_capacity(trie->entries, trie->keys_offset.len);
286     trie_compile_aux(trie, trie_add_leaf(trie, array_ptr(trie->keys, array_elt(trie->keys_offset, 0))),
287                      0, trie->keys_offset.len, 0);
288     trie_cleanup_build_data(trie);
289     array_adjust(trie->entries);
290     array_adjust(trie->c);
291     if (memlock) {
292         trie_lock(trie);
293     }
294 }
295
296 bool trie_lookup(const trie_t *trie, const char *key)
297 {
298     assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
299     if (trie->entries.len == 0) {
300         return false;
301     } else {
302         const trie_entry_t *current = array_ptr(trie->entries, 0);
303         while (true) {
304             if (trie_entry_is_leaf(current)) {
305                 return trie_entry_match(trie, current, key);
306             } else if (trie_entry_c_match(trie, current, key)) {
307                 key += current->c_len;
308                 current = trie_entry_child(trie, current, key);
309                 if (current == NULL) {
310                     return false;
311                 }
312             } else {
313                 return false;
314             }
315         }
316     }
317 }
318
319 bool trie_prefix(const trie_t *trie, const char *key)
320 {
321     assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
322     if (trie->entries.len == 0) {
323         return false;
324     } else {
325         const trie_entry_t *current = array_ptr(trie->entries, 0);
326         while (true) {
327             if (trie_entry_is_leaf(current)) {
328                 return trie_entry_prefix(trie, current, key);
329             } else if (trie_entry_c_match(trie, current, key)) {
330                 key += current->c_len;
331                 current = trie_entry_child(trie, current, key);
332                 if (current == NULL) {
333                     return false;
334                 }
335             } else {
336                 return false;
337             }
338         }
339     }
340 }
341
342 void trie_lock(trie_t *trie)
343 {
344     if (trie->locked) {
345         return;
346     }
347     if (!array_lock(trie->entries)) {
348         UNIXERR("mlock");
349     }
350     if (!array_lock(trie->c)) {
351         UNIXERR("mlock");
352     }
353     if (mlock(trie, sizeof(trie_t)) != 0) {
354         UNIXERR("mlock");
355         return;
356     }
357     trie->locked = true;
358 }
359
360 void trie_unlock(trie_t *trie)
361 {
362     if (!trie->locked) {
363         return;
364     }
365     array_unlock(trie->entries);
366     array_unlock(trie->c);
367     munlock(trie, sizeof(trie_t));
368     trie->locked = false;
369 }
370
371 /* Debug {{{1
372  */
373
374 static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
375                                       const trie_entry_t *entry, int level)
376 {
377     static int max_depth = 0;
378     static int leaves    = 0;
379     static int depth_sum = 0;
380
381     if (entry == array_ptr(trie->entries, 0)) {
382       max_depth = 0;
383       leaves    = 0;
384       depth_sum = 0;
385     }
386     if (trie_entry_is_leaf(entry)) {
387         if (level > max_depth) {
388             max_depth = level;
389         }
390         ++leaves;
391         depth_sum += level;
392     }
393     if (show_content) {
394         for (int i = 0 ; i < level ; ++i) {
395             fputs("  ", stdout);
396         }
397         if (entry->c_len == 0) {
398             fputs("(0)", stdout);
399         } else {
400             const char *c = array_ptr(trie->c, entry->c_offset);
401             printf("(%d) ", entry->c_len);
402             for (int i = 0 ; i < entry->c_len ; ++i) {
403                 if (c[i]) {
404                     printf("%c ", c[i]);
405                 } else {
406                     fputs("\\0 ", stdout);
407                 }
408             }
409         }
410         fputs("\n", stdout);
411     }
412     for (uint32_t i = entry->children_offset ;
413           i < entry->children_offset + entry->children_len ; ++i) {
414         trie_entry_inspect(trie, show_content, array_ptr(trie->entries, i), level + 1);
415     }
416     if (level == 0) {
417         printf("Average char per node: %d\n", trie->c.len / trie->entries.len);
418         printf("Number of nodes: %d\n", trie->entries.len);
419         printf("Number of leaves: %d\n", leaves);
420         printf("Max depth: %d\n", max_depth);
421         printf("Average leaf depth: %d\n", depth_sum / leaves);
422         printf("Memory used: %zd\n", (trie->entries.size * sizeof(trie_entry_t))
423                                   + (trie->c.size) + sizeof(trie_t));
424     }
425 }
426
427 void trie_inspect(const trie_t *trie, bool show_content)
428 {
429     trie_entry_inspect(trie, show_content, trie->entries.data, 0);
430 }