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
40 typedef struct trie_entry_t trie_entry_t;
44 uint32_t children_offset;
47 uint16_t children_len;
49 #define TRIE_ENTRY_INIT { 0, 0, 0, 0 }
53 A(trie_entry_t) entries;
61 trie_t *trie_new(void)
63 return p_new(trie_t, 1);
66 static inline void trie_cleanup_build_data(trie_t *trie)
68 array_wipe(trie->keys);
69 array_wipe(trie->keys_offset);
72 void trie_delete(trie_t **trie)
75 trie_cleanup_build_data(*trie);
77 array_wipe((*trie)->entries);
78 array_wipe((*trie)->c);
83 /** Check that the given entry is a prefix for the given key.
85 static inline bool trie_entry_c_match(const trie_t *trie,
86 const trie_entry_t *entry,
89 const char *c = array_ptr(trie->c, entry->c_offset);
91 for (i = 0 ; i < entry->c_len ; ++i) {
99 static inline bool trie_entry_match(const trie_t *trie,
100 const trie_entry_t *entry, const char *key)
102 return !!(strcmp(array_ptr(trie->c, entry->c_offset), key) == 0);
105 static inline bool trie_entry_prefix(const trie_t *trie,
106 const trie_entry_t *entry, const char *key)
108 int len = entry->c_len;
109 if (len > 0 && array_elt(trie->c, entry->c_offset + len - 1) == '\0') {
112 return !!(strncmp(array_ptr(trie->c, entry->c_offset), key, len) == 0);
115 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
117 return entry->children_len == 0;
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.
124 static inline const trie_entry_t* trie_entry_child(const trie_t *trie,
125 const trie_entry_t* entry,
128 uint32_t start = entry->children_offset;
129 uint32_t end = start + entry->children_len;
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);
149 static inline uint32_t trie_entry_new(trie_t *trie)
151 const trie_entry_t e = TRIE_ENTRY_INIT;
152 array_add(trie->entries, e);
153 return trie->entries.len - 1;
156 static inline uint32_t trie_add_leaf(trie_t *trie, const char *key)
159 int len = m_strlen(key) + 1;
160 entry = array_ptr(trie->entries, trie_entry_new(trie));
161 entry->c_offset = trie->c.len;
163 array_append(trie->c, key, len);
164 return trie->entries.len - 1;
167 static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
170 if (entry->children_len == 0) {
171 entry->children_offset = pchild;
172 entry->children_len = 1;
174 if (entry->children_offset + entry->children_len != pchild) {
175 printf("Inserting child %d while offset is %d[%d]\n",
176 pchild, entry->children_offset, entry->children_len);
179 ++entry->children_len;
183 static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, uint16_t pos)
186 child = array_ptr(trie->entries, trie_entry_new(trie));
188 child->c_offset = entry->c_offset;
189 child->c_len = entry->c_len;
193 child->c_offset = entry->c_offset + pos;
194 child->c_len = entry->c_len - pos;
197 child->children_offset = entry->children_offset;
198 child->children_len = entry->children_len;
199 entry->children_offset = trie->entries.len - 1;
200 entry->children_len = 1;
203 void trie_insert(trie_t *trie, const char* key)
205 assert(trie->entries.len == 0 && "Trie already compiled");
207 int len = m_strlen(key) + 1;
208 array_add(trie->keys_offset, trie->keys.len);
209 array_append(trie->keys, key, len);
213 static inline void trie_compile_aux(trie_t *trie, uint32_t id,
214 uint32_t first_key, uint32_t last_key,
215 int offset, int initial_diff)
218 uint32_t fork_pos = 0;
221 for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
222 current = array_elt(trie->keys, array_elt(trie->keys_offset, first_key) + offset);
223 for (uint32_t i = first_key + 1 ; i < last_key ; ++i) {
224 const char *str = array_ptr(trie->keys, array_elt(trie->keys_offset, i));
225 const char c = str[offset];
227 array_ensure_capacity_delta(trie->entries, 2);
229 trie_entry_split(trie, array_ptr(trie->entries, id), off_diff);
231 trie_entry_insert_child(trie, array_ptr(trie->entries, id),
232 trie_add_leaf(trie, str + offset));
233 forks[fork_pos++] = i;
237 if (fork_pos == 0 && current == '\0') {
241 forks[fork_pos] = last_key;
243 const uint8_t children_len = array_elt(trie->entries, id).children_len;
244 for (uint16_t i = 0 ; i < children_len ; ++i) {
245 int child = array_elt(trie->entries, id).children_offset + i;
246 if (forks[i] - 1 > first_key) {
247 trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
249 first_key = forks[i];
253 void trie_compile(trie_t *trie, bool memlock)
255 assert(trie->entries.len == 0 && "Trie already compiled");
256 assert(trie->keys.len != 0 && "Trying to compile an empty trie");
258 # define QSORT_TYPE int
259 # define QSORT_BASE trie->keys_offset.data
260 # define QSORT_NELT trie->keys_offset.len
261 # define QSORT_LT(a,b) strcmp(trie->keys.data + *a, trie->keys.data + *b) < 0
265 array_ensure_capacity(trie->entries, trie->keys.len);
266 trie_compile_aux(trie, trie_add_leaf(trie, trie->keys.data),
267 0, trie->keys_offset.len, 0, 0);
268 trie_cleanup_build_data(trie);
269 array_adjust(trie->entries);
270 array_adjust(trie->c);
276 bool trie_lookup(const trie_t *trie, const char *key)
278 assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
279 if (trie->entries.len == 0) {
282 const trie_entry_t *current = array_ptr(trie->entries, 0);
284 if (trie_entry_is_leaf(current)) {
285 return trie_entry_match(trie, current, key);
286 } else if (trie_entry_c_match(trie, current, key)) {
287 key += current->c_len;
288 current = trie_entry_child(trie, current, key);
289 if (current == NULL) {
299 bool trie_prefix(const trie_t *trie, const char *key)
301 assert(trie->keys.len == 0L && "Can't lookup: trie not compiled");
302 if (trie->entries.len == 0) {
305 const trie_entry_t *current = array_ptr(trie->entries, 0);
307 if (trie_entry_is_leaf(current)) {
308 return trie_entry_prefix(trie, current, key);
309 } else if (trie_entry_c_match(trie, current, key)) {
310 key += current->c_len;
311 current = trie_entry_child(trie, current, key);
312 if (current == NULL) {
322 void trie_lock(trie_t *trie)
327 if (!array_lock(trie->entries)) {
330 if (!array_lock(trie->c)) {
333 if (mlock(trie, sizeof(trie_t)) != 0) {
340 void trie_unlock(trie_t *trie)
345 array_unlock(trie->entries);
346 array_unlock(trie->c);
347 munlock(trie, sizeof(trie_t));
348 trie->locked = false;
354 static inline void trie_entry_inspect(const trie_t *trie, bool show_content,
355 const trie_entry_t *entry, int level)
357 static int max_depth = 0;
358 static int leaves = 0;
359 static int depth_sum = 0;
361 if (entry == array_ptr(trie->entries, 0)) {
366 if (trie_entry_is_leaf(entry)) {
367 if (level > max_depth) {
374 for (int i = 0 ; i < level ; ++i) {
377 if (entry->c_len == 0) {
378 fputs("(nil)", stdout);
380 const char *c = array_ptr(trie->c, entry->c_offset);
381 printf("(%d) ", entry->c_len);
382 for (int i = 0 ; i < entry->c_len ; ++i) {
386 fputs("\\0 ", stdout);
392 for (uint32_t i = entry->children_offset ;
393 i < entry->children_offset + entry->children_len ; ++i) {
394 trie_entry_inspect(trie, show_content, array_ptr(trie->entries, i), level + 1);
397 printf("Average char per node: %d\n", trie->c.len / trie->entries.len);
398 printf("Number of nodes: %d\n", trie->entries.len);
399 printf("Number of leaves: %d\n", leaves);
400 printf("Max depth: %d\n", max_depth);
401 printf("Average leaf depth: %d\n", depth_sum / leaves);
402 printf("Memory used: %zd\n", (trie->entries.size * sizeof(trie_entry_t))
403 + (trie->c.size) + sizeof(trie_t));
407 void trie_inspect(const trie_t *trie, bool show_content)
409 trie_entry_inspect(trie, show_content, trie->entries.data, 0);