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;
54 trie_entry_t* entries;
64 return p_new(trie_t, 1);
67 void trie_delete(trie_t **trie)
70 for (int i = 0 ; i < (*trie)->entries_len ; ++i) {
71 trie_entry_t *entry = &(*trie)->entries[i];
77 p_delete(&(entry->children));
83 /** Check that the given entry is a prefix for the given key.
85 static inline bool trie_entry_c_match(const trie_entry_t* entry,
86 const char *key, int *pos)
89 for (i = 0 ; i < entry->c_len ; ++i) {
90 if (key[i] != entry->c[i]) {
103 /** Lookup for a child of entry matching the given entry at the given pos.
104 * Only the first character of the children is taken into account in the
105 * lookup. The current entry is assumed to match the key.
107 static inline trie_entry_t* trie_entry_child(const trie_t *trie,
108 const trie_entry_t* entry,
112 int end = entry->children_len;
115 while (start < end) {
116 int mid = (start + end) / 2;
117 trie_entry_t* child = &trie->entries[entry->children[mid]];
120 if (child->c[0] == c) {
123 if (c < child->c[0]) {
135 static void trie_grow(trie_t *trie, int delta)
137 p_allocgrow(&trie->entries, trie->entries_len + delta, &trie->entries_size);
140 static int trie_add_leaf(trie_t *trie, const char *key)
144 entry = &trie->entries[trie->entries_len++];
145 entry->c = m_strdup(key);
146 entry->c_len = m_strlen(key) + 1;
148 return trie->entries_len - 1;
151 static void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry, int pchild)
153 const char c = trie->entries[pchild].c[0];
155 int end = entry->children_len;
157 p_allocgrow(&entry->children, entry->children_len + 1, &entry->children_size);
158 while (start < end) {
159 int mid = (start + end) / 2;
160 trie_entry_t* child = &trie->entries[entry->children[mid]];
163 if (child->c[0] == c) {
166 if (c < child->c[0]) {
175 memmove(entry->children + start + 1,
176 entry->children + start,
177 sizeof(int) * (entry->children_len - start));
178 entry->children[start] = pchild;
181 static void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos)
185 child = &trie->entries[trie->entries_len++];
188 child->c_len = entry->c_len;
189 child->c_own = entry->c_own;
192 entry->c_own = false;
194 child->c = entry->c + pos;
195 child->c_len = entry->c_len - pos;
196 child->c_own = false;
199 child->children = entry->children;
200 child->children_len = entry->children_len;
201 child->children_size = entry->children_size;
202 entry->children = NULL;
203 entry->children_len = 0;
204 entry->children_size = 0;
205 trie_entry_insert_child(trie, entry, trie->entries_len - 1);
208 void trie_insert(trie_t *trie, const char* key)
210 if (trie->entries_len == 0) {
211 (void)trie_add_leaf(trie, key);
213 trie_entry_t *current = trie->entries;
216 if (trie_entry_c_match(current, key, &pos)) {
217 trie_entry_t *next = NULL;
219 next = trie_entry_child(trie, current, key);
221 trie_entry_insert_child(trie, current,
222 trie_add_leaf(trie, key));
228 trie_entry_split(trie, current, pos);
229 trie_entry_insert_child(trie, current,
230 trie_add_leaf(trie, key + pos));
237 bool trie_lookup(const trie_t *trie, const char *key)
239 if (trie->entries_len == 0) {
242 trie_entry_t *current = trie->entries;
245 if (trie_entry_c_match(current, key, &pos)) {
246 if (current->c_len && current->c[current->c_len - 1] == '\0') {
250 current = trie_entry_child(trie, current, key);
251 if (current == NULL) {