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
39 typedef struct trie_entry_t trie_entry_t;
50 trie_entry_t *entries;
65 trie_t *trie_new(void)
67 return p_new(trie_t, 1);
70 void trie_delete(trie_t **trie)
73 p_delete(&(*trie)->entries);
74 p_delete(&(*trie)->c);
79 /** Check that the given entry is a prefix for the given key.
81 static inline bool trie_entry_c_match(const trie_t *trie,
82 const trie_entry_t *entry,
83 const char *key, int *pos)
85 const char *c = trie->c + entry->c_offset;
87 for (i = 0 ; i < entry->c_len ; ++i) {
101 static inline bool trie_entry_match(const trie_t *trie,
102 const trie_entry_t *entry, const char *key)
104 return !!(strcmp(trie->c + entry->c_offset, key) == 0);
107 static inline bool trie_entry_is_leaf(const trie_entry_t *entry)
109 return entry->children_len == 0;
112 /** Lookup for a child of entry matching the given entry at the given pos.
113 * Only the first character of the children is taken into account in the
114 * lookup. The current entry is assumed to match the key.
116 static inline trie_entry_t* trie_entry_child(const trie_t *trie,
117 const trie_entry_t* entry,
120 int start = entry->children_offset;
121 int end = start + entry->children_len;
124 while (start < end) {
125 int mid = (start + end) / 2;
126 trie_entry_t* child = &trie->entries[mid];
127 const char c2 = trie->c[child->c_offset];
145 #define GROW(Buffer, Added, Len, Size) \
147 const int required_size = (Len) + (Added); \
148 int next_size = (Size); \
149 if (next_size >= required_size) { \
153 next_size = p_alloc_nr(next_size); \
154 } while (next_size < required_size); \
155 p_allocgrow(&(Buffer), next_size, &(Size)); \
158 static inline void trie_grow(trie_t *trie, int delta)
160 GROW(trie->entries, delta, trie->entries_len, trie->entries_size);
163 static inline int trie_entry_new(trie_t *trie)
165 memset(trie->entries + trie->entries_len, 0, sizeof(trie_entry_t));
166 return trie->entries_len++;
169 static inline int trie_add_leaf(trie_t *trie, const char *key)
172 int len = m_strlen(key) + 1;
173 entry = &trie->entries[trie_entry_new(trie)];
174 GROW(trie->c, len, trie->c_len, trie->c_size);
175 memcpy(trie->c + trie->c_len, key, len);
176 entry->c_offset = trie->c_len;
179 return trie->entries_len - 1;
182 static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry,
185 if (entry->children_len == 0) {
186 entry->children_offset = pchild;
187 entry->children_len = 1;
189 if (entry->children_offset + entry->children_len != pchild) {
191 printf("Inserting child %d while offset is %d[%d]\n",
192 pchild, entry->children_offset, entry->children_len);
195 ++entry->children_len;
199 static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
202 child = &trie->entries[trie_entry_new(trie)];
204 child->c_offset = entry->c_offset;
205 child->c_len = entry->c_len;
209 child->c_offset = entry->c_offset + pos;
210 child->c_len = entry->c_len - pos;
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;
219 void trie_insert(trie_t *trie, const char* key)
221 GROW(trie->keys, 1, trie->keys_len, trie->keys_size);
222 trie->keys[trie->keys_len++] = strdup(key);
226 static inline void trie_compile_aux(trie_t *trie, int id,
227 int first_key, int last_key, int offset,
234 for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) {
235 current = trie->keys[first_key][offset];
236 for (int i = first_key + 1 ; i < last_key ; ++i) {
237 if (trie->keys[i][offset] != current) {
240 trie_entry_split(trie, &trie->entries[id], off_diff);
242 trie_entry_insert_child(trie, &trie->entries[id],
243 trie_add_leaf(trie, trie->keys[i] +
245 forks[fork_pos++] = i;
246 current = trie->keys[i][offset];
250 forks[fork_pos] = last_key;
252 const int children_len = trie->entries[id].children_len;
253 for (int i = 0 ; i < children_len ; ++i) {
254 int child = trie->entries[id].children_offset + i;
255 if (forks[i] - 1 > first_key) {
256 trie_compile_aux(trie, child, first_key, forks[i], offset, 1);
258 first_key = forks[i];
262 static inline void trie_cleanup_build_data(trie_t *trie)
264 for (int i = 0 ; i < trie->keys_len ; ++i) {
265 p_delete(&trie->keys[i]);
267 p_delete(&trie->keys);
270 static inline void trie_shrink(trie_t *trie)
274 static inline void trie_lock(trie_t *trie)
280 void trie_compile(trie_t *trie, bool memlock)
283 # define QSORT_TYPE str_t
284 # define QSORT_BASE trie->keys
285 # define QSORT_NELT trie->keys_len
286 # define QSORT_LT(a,b) strcmp(*a, *b) < 0
290 trie_grow(trie, trie->keys_len);
291 trie_compile_aux(trie, trie_add_leaf(trie, trie->keys[0]),
292 0, trie->keys_len, 0, 0);
293 trie_cleanup_build_data(trie);
300 bool trie_lookup(const trie_t *trie, const char *key)
302 if (trie->entries_len == 0) {
305 trie_entry_t *current = trie->entries;
308 if (trie_entry_is_leaf(current)) {
309 return trie_entry_match(trie, current, key);
310 } else if (trie_entry_c_match(trie, current, key, &pos)) {
312 current = trie_entry_child(trie, current, key);
313 if (current == NULL) {
327 static inline void trie_entry_inspect(const trie_t *trie,
328 const trie_entry_t *entry, int level)
330 static int c_sum = 0;
331 static int nodes = 0;
334 c_sum += entry->c_len;
335 for (int i = 0 ; i < level ; ++i) {
338 if (entry->c_len == 0) {
339 fputs("(nil)", stdout);
341 const char *c = trie->c + entry->c_offset;
342 for (int i = 0 ; i < entry->c_len ; ++i) {
346 fputs("\\0 ", stdout);
351 for (int i = entry->children_offset ;
352 i < entry->children_offset + entry->children_len ; ++i) {
353 trie_entry_inspect(trie, &trie->entries[i], level + 1);
356 printf("Mean char per node: %d\n", c_sum / nodes);
360 void trie_inspect(const trie_t *trie)
362 trie_entry_inspect(trie, trie->entries, 0);