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
42 typedef struct trie_entry_t trie_entry_t;
48 int32_t children_offset;
51 #define TRIE_ENTRY_INIT { 0, 0, 0, 0 }
55 trie_entry_t_array_t entries;
58 int_array_t keys_offset;
63 trie_t *trie_new(void)
65 return p_new(trie_t, 1);
68 static inline void trie_cleanup_build_data(trie_t *trie)
70 array_wipe(trie->keys);
71 array_wipe(trie->keys_offset);
74 void trie_delete(trie_t **trie)
77 trie_cleanup_build_data(*trie);
79 array_wipe((*trie)->entries);
80 array_wipe((*trie)->c);
85 /** Check that the given entry is a prefix for the given key.
87 static inline bool trie_entry_c_match(const trie_t *trie,
88 const trie_entry_t *entry,
89 const char *key, int *pos)
91 const char *c = array_ptr(trie->c, entry->c_offset);
93 for (i = 0 ; i < entry->c_len ; ++i) {
107 static inline bool trie_entry_match(const trie_t *trie,
108 const trie_entry_t *entry, const char *key)
110 return !!(strcmp(array_ptr(trie->c, entry->c_offset), key) == 0);
113 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
115 return entry->children_len == 0;
118 /** Lookup for a child of entry matching the given entry at the given pos.
119 * Only the first character of the children is taken into account in the
120 * lookup. The current entry is assumed to match the key.
122 static inline trie_entry_t* trie_entry_child(const trie_t *trie,
123 const trie_entry_t* entry,
126 int start = entry->children_offset;
127 int end = start + entry->children_len;
130 while (start < end) {
131 int mid = (start + end) / 2;
132 trie_entry_t* child = array_ptr(trie->entries, mid);
133 const char c2 = array_elt(trie->c, child->c_offset);
151 static inline int trie_entry_new(trie_t *trie)
153 const trie_entry_t e = TRIE_ENTRY_INIT;
154 array_add(trie->entries, e);
155 return trie->entries.len;
158 static inline int trie_add_leaf(trie_t *trie, const char *key)
161 int len = m_strlen(key) + 1;
162 entry = array_ptr(trie->entries, trie_entry_new(trie));
163 entry->c_offset = trie->c.len;
165 array_append(trie->c, key, len);
166 return trie->entries.len - 1;
169 static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
172 if (entry->children_len == 0) {
173 entry->children_offset = pchild;
174 entry->children_len = 1;
176 if (entry->children_offset + entry->children_len != pchild) {
177 printf("Inserting child %d while offset is %d[%d]\n",
178 pchild, entry->children_offset, entry->children_len);
181 ++entry->children_len;
185 static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
188 child = array_ptr(trie->entries, trie_entry_new(trie));
190 child->c_offset = entry->c_offset;
191 child->c_len = entry->c_len;
195 child->c_offset = entry->c_offset + pos;
196 child->c_len = entry->c_len - pos;
199 child->children_offset = entry->children_offset;
200 child->children_len = entry->children_len;
201 entry->children_offset = trie->entries.len - 1;
202 entry->children_len = 1;
205 void trie_insert(trie_t *trie, const char* key)
207 assert(trie->entries.len == 0 && "Trie already compiled");
209 int len = m_strlen(key) + 1;
210 array_add(trie->keys_offset, trie->keys.len);
211 array_append(trie->keys, key, len);
215 static inline void trie_compile_aux(trie_t *trie, int id,
216 int first_key, int last_key, int offset,
223 for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
224 current = array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset);
225 for (int i = first_key + 1 ; i < last_key ; ++i) {
226 const char *str = array_ptr(trie->keys, array_elt(trie->keys_offset, i));
227 const char c = str[offset];
229 array_ensure_capacity_delta(trie->entries, 2);
231 trie_entry_split(trie, array_ptr(trie->entries, id), off_diff);
233 trie_entry_insert_child(trie, array_ptr(trie->entries, id),
234 trie_add_leaf(trie, str + offset));
235 forks[fork_pos++] = i;
239 if (fork_pos == 0 && current == '\0') {
243 forks[fork_pos] = last_key;
245 const int children_len = array_elt(trie->entries, id).children_len;
246 for (int i = 0 ; i < children_len ; ++i) {
247 int child = array_elt(trie->entries, id).children_offset + i;
248 if (forks[i] - 1 > first_key) {
249 trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
251 first_key = forks[i];
255 void trie_compile(trie_t *trie, bool memlock)
257 assert(trie->entries.len == 0 && "Trie already compiled");
258 assert(trie->keys.len != 0 && "Trying to compile an empty trie");
260 # define QSORT_TYPE int
261 # define QSORT_BASE trie->keys_offset.data
262 # define QSORT_NELT trie->keys_offset.len
263 # define QSORT_LT(a,b) strcmp(trie->keys.data + *a, trie->keys.data + *b) < 0
267 array_ensure_capacity(trie->entries, trie->keys.len);
268 trie_compile_aux(trie, trie_add_leaf(trie, trie->keys.data),
269 0, trie->keys_offset.len, 0, 0);
270 trie_cleanup_build_data(trie);
271 array_adjust(trie->entries);
272 array_adjust(trie->c);
278 bool trie_lookup(const trie_t *trie, const char *key)
280 assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
281 if (trie->entries.len == 0) {
284 trie_entry_t *current = array_ptr(trie->entries, 0);
287 if (trie_entry_is_leaf(current)) {
288 return trie_entry_match(trie, current, key);
289 } else if (trie_entry_c_match(trie, current, key, &pos)) {
291 current = trie_entry_child(trie, current, key);
292 if (current == NULL) {
302 void trie_lock(trie_t *trie)
307 if (mlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len) != 0) {
311 if (mlock(trie->c.data, trie->c.len) != 0) {
313 munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len);
319 void trie_unlock(trie_t *trie)
324 munlock(trie->entries.data, sizeof(trie_entry_t) * trie->entries.len);
325 munlock(trie->c.data, trie->c.len);
326 trie->locked = false;
332 static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
333 const trie_entry_t *entry, int level)
335 static int max_depth = 0;
336 static int leaves = 0;
337 static int depth_sum = 0;
339 if (trie_entry_is_leaf(entry)) {
340 if (level > max_depth) {
347 for (int i = 0 ; i < level ; ++i) {
350 if (entry->c_len == 0) {
351 fputs("(nil)", stdout);
353 const char *c = array_ptr(trie->c, entry->c_offset);
354 printf("(%d) ", entry->c_len);
355 for (int i = 0 ; i < entry->c_len ; ++i) {
359 fputs("\\0 ", stdout);
365 for (int i = entry->children_offset ;
366 i < entry->children_offset + entry->children_len ; ++i) {
367 trie_entry_inspect(trie, show_content, array_ptr(trie->entries, i), level + 1);
370 printf("Average char per node: %d\n", trie->c.len / trie->entries.len);
371 printf("Number of nodes: %d\n", trie->entries.len);
372 printf("Number of leaves: %d\n", leaves);
373 printf("Max depth: %d\n", max_depth);
374 printf("Average leaf depth: %d\n", depth_sum / leaves);
375 printf("Memory used: %d\n", (trie->entries.size * sizeof(trie_entry_t))
376 + (trie->c.size) + sizeof(trie_t));
380 void trie_inspect(const trie_t *trie, bool show_content)
382 trie_entry_inspect(trie, show_content, trie->entries.data, 0);