X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Ftrie.c;h=124c072a068993c089dfed1125c60a0f1dd108c4;hb=8de978eb48332de45a5ad254b0475e74613fcfbd;hp=3cc093a3036c2f0df88c99a04f63a4da87d78c3b;hpb=fb519b0c3a78183d3fad3778d7d57bc2693598fc;p=apps%2Fpfixtools.git diff --git a/common/trie.c b/common/trie.c index 3cc093a..124c072 100644 --- a/common/trie.c +++ b/common/trie.c @@ -33,30 +33,37 @@ * Copyright © 2008 Florent Bruneau */ +#include + #include "str.h" #include "trie.h" typedef struct trie_entry_t trie_entry_t; struct trie_entry_t { - int c_offset; - int c_len; - - int* children; - int children_len; - int children_size; + int32_t c_offset; + int32_t c_len; - bool locked; + int32_t children_offset; + int32_t children_len; }; struct trie_t { trie_entry_t *entries; - int entries_len; - int entries_size; + int32_t entries_len; + int32_t entries_size; char *c; - int c_len; - int c_size; + int32_t c_len; + int32_t c_size; + + char *keys; + int32_t keys_len; + int32_t keys_size; + + int *keys_offset; + int32_t keys_offset_len; + int32_t keys_offset_size; bool locked; }; @@ -66,13 +73,19 @@ trie_t *trie_new(void) return p_new(trie_t, 1); } +static inline void trie_cleanup_build_data(trie_t *trie) +{ + p_delete(&trie->keys); + p_delete(&trie->keys_offset); + trie->keys_len = trie->keys_size = 0; + trie->keys_offset_len = trie->keys_offset_size = 0; +} + void trie_delete(trie_t **trie) { if (*trie) { - for (int i = 0 ; i < (*trie)->entries_len ; ++i) { - trie_entry_t *entry = &(*trie)->entries[i]; - p_delete(&(entry->children)); - } + trie_cleanup_build_data(*trie); + trie_unlock(*trie); p_delete(&(*trie)->entries); p_delete(&(*trie)->c); p_delete(trie); @@ -120,13 +133,13 @@ static inline trie_entry_t* trie_entry_child(const trie_t *trie, const trie_entry_t* entry, const char *key) { - int start = 0; - int end = entry->children_len; + int start = entry->children_offset; + int end = start + entry->children_len; const char c = *key; while (start < end) { int mid = (start + end) / 2; - trie_entry_t* child = &trie->entries[entry->children[mid]]; + trie_entry_t* child = &trie->entries[mid]; const char c2 = trie->c[child->c_offset]; if (child->c_len) { @@ -185,34 +198,17 @@ static inline int trie_add_leaf(trie_t *trie, const char *key) static inline void trie_entry_insert_child(trie_t *trie, trie_entry_t *entry, int pchild) { - const char c = trie->c[trie->entries[pchild].c_offset]; - int start = 0; - int end = entry->children_len; - - p_allocgrow(&entry->children, entry->children_len + 1, &entry->children_size); - while (start < end) { - int mid = (start + end) / 2; - const trie_entry_t* child = &trie->entries[entry->children[mid]]; - const char c2 = trie->c[child->c_offset]; - - if (child->c_len) { - if (c2 == c) { - abort(); - } - if (c < c2) { - end = mid; - } else { - start = mid + 1; - } - } else { + if (entry->children_len == 0) { + entry->children_offset = pchild; + entry->children_len = 1; + } else { + if (entry->children_offset + entry->children_len != pchild) { + printf("Inserting child %d while offset is %d[%d]\n", + pchild, entry->children_offset, entry->children_len); abort(); } + ++entry->children_len; } - memmove(entry->children + start + 1, - entry->children + start, - sizeof(int) * (entry->children_len - start)); - entry->children[start] = pchild; - ++entry->children_len; } static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos) @@ -229,50 +225,92 @@ static inline void trie_entry_split(trie_t *trie, trie_entry_t *entry, int pos) child->c_len = entry->c_len - pos; entry->c_len = pos; } - child->children = entry->children; - child->children_len = entry->children_len; - child->children_size = entry->children_size; - entry->children = NULL; - entry->children_len = 0; - entry->children_size = 0; - trie_entry_insert_child(trie, entry, trie->entries_len - 1); + child->children_offset = entry->children_offset; + child->children_len = entry->children_len; + entry->children_offset = trie->entries_len - 1; + entry->children_len = 1; } void trie_insert(trie_t *trie, const char* key) { - trie_grow(trie, 2); - if (trie->entries_len == 0) { - (void)trie_add_leaf(trie, key); - } else { - trie_entry_t *current = trie->entries; - while (true) { - int pos = 0; - if (trie_entry_c_match(trie, current, key, &pos)) { - if (trie_entry_is_leaf(current)) { - return; - } - trie_entry_t *next = NULL; - key += pos; - next = trie_entry_child(trie, current, key); - if (next == NULL) { - trie_entry_insert_child(trie, current, - trie_add_leaf(trie, key)); - return; - } else { - current = next; + assert(trie->entries == NULL && "Trie already compiled"); + + int len = m_strlen(key) + 1; + GROW(trie->keys, len, trie->keys_len, trie->keys_size); + memcpy(trie->keys + trie->keys_len, key, len); + + GROW(trie->keys_offset, 1, trie->keys_offset_len, trie->keys_offset_size); + trie->keys_offset[trie->keys_offset_len++] = trie->keys_len; + trie->keys_len += len; +} + + +static inline void trie_compile_aux(trie_t *trie, int id, + int first_key, int last_key, int offset, + int initial_diff) +{ + int forks[256]; + int fork_pos = 0; + char current = '\0'; + + for (int off_diff = initial_diff ; fork_pos == 0 ; ++off_diff, ++offset) { + current = trie->keys[trie->keys_offset[first_key] + offset]; + for (int i = first_key + 1 ; i < last_key ; ++i) { + const char *str = trie->keys + trie->keys_offset[i]; + const char c = str[offset]; + if (c != current) { + trie_grow(trie, 2); + if (fork_pos == 0) { + trie_entry_split(trie, &trie->entries[id], off_diff); } - } else { - trie_entry_split(trie, current, pos); - trie_entry_insert_child(trie, current, - trie_add_leaf(trie, key + pos)); - return; + trie_entry_insert_child(trie, &trie->entries[id], + trie_add_leaf(trie, str + offset)); + forks[fork_pos++] = i; + current = c; } } + if (fork_pos == 0 && current == '\0') { + return; + } + } + forks[fork_pos] = last_key; + + const int children_len = trie->entries[id].children_len; + for (int i = 0 ; i < children_len ; ++i) { + int child = trie->entries[id].children_offset + i; + if (forks[i] - 1 > first_key) { + trie_compile_aux(trie, child, first_key, forks[i], offset, 1); + } + first_key = forks[i]; + } +} + +void trie_compile(trie_t *trie, bool memlock) +{ + assert(trie->entries == NULL && "Trie already compiled"); + assert(trie->keys != NULL && "Trying to compile an empty trie"); + { +# define QSORT_TYPE int +# define QSORT_BASE trie->keys_offset +# define QSORT_NELT trie->keys_offset_len +# define QSORT_LT(a,b) strcmp(trie->keys + *a, trie->keys + *b) < 0 +# include "qsort.c" + } + + trie_grow(trie, trie->keys_len); + trie_compile_aux(trie, trie_add_leaf(trie, trie->keys), + 0, trie->keys_offset_len, 0, 0); + trie_cleanup_build_data(trie); + p_shrink(&trie->entries, trie->entries_len, &trie->entries_size); + p_shrink(&trie->c, trie->c_len, &trie->c_size); + if (memlock) { + trie_lock(trie); } } bool trie_lookup(const trie_t *trie, const char *key) { + assert(trie->keys == NULL && "Can't lookup: trie not compiled"); if (trie->entries_len == 0) { return false; } else { @@ -294,43 +332,85 @@ bool trie_lookup(const trie_t *trie, const char *key) } } +void trie_lock(trie_t *trie) +{ + if (trie->locked) { + return; + } + if (mlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len) != 0) { + UNIXERR("mlock"); + return; + } + if (mlock(trie->c, trie->c_len) != 0) { + UNIXERR("mlock"); + munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len); + return; + } + trie->locked = true; +} + +void trie_unlock(trie_t *trie) +{ + if (!trie->locked) { + return; + } + munlock(trie->entries, sizeof(trie_entry_t) * trie->entries_len); + munlock(trie->c, trie->c_len); + trie->locked = false; +} /* Debug {{{1 */ -static inline void trie_entry_inspect(const trie_t *trie, +static inline void trie_entry_inspect(const trie_t *trie, bool show_content, const trie_entry_t *entry, int level) { - static int c_sum = 0; - static int nodes = 0; + static int max_depth = 0; + static int leaves = 0; + static int depth_sum = 0; - ++nodes; - c_sum += entry->c_len; - for (int i = 0 ; i < level ; ++i) { - fputs(" ", stdout); + if (trie_entry_is_leaf(entry)) { + if (level > max_depth) { + max_depth = level; + } + ++leaves; + depth_sum += level; } - if (entry->c_len == 0) { - fputs("(nil)", stdout); - } else { - const char *c = trie->c + entry->c_offset; - for (int i = 0 ; i < entry->c_len ; ++i) { - if (c[i]) { - printf("%c ", c[i]); - } else { - fputs("\\0 ", stdout); + if (show_content) { + for (int i = 0 ; i < level ; ++i) { + fputs(" ", stdout); + } + if (entry->c_len == 0) { + fputs("(nil)", stdout); + } else { + const char *c = trie->c + entry->c_offset; + printf("(%d) ", entry->c_len); + for (int i = 0 ; i < entry->c_len ; ++i) { + if (c[i]) { + printf("%c ", c[i]); + } else { + fputs("\\0 ", stdout); + } } } + fputs("\n", stdout); } - fputs("\n", stdout); - for (int i = 0 ; i < entry->children_len ; ++i) { - trie_entry_inspect(trie, &trie->entries[entry->children[i]], level + 1); + for (int i = entry->children_offset ; + i < entry->children_offset + entry->children_len ; ++i) { + trie_entry_inspect(trie, show_content, &trie->entries[i], level + 1); } if (level == 0) { - printf("Mean char per node: %d\n", c_sum / nodes); + printf("Average char per node: %d\n", trie->c_len / trie->entries_len); + printf("Number of nodes: %d\n", trie->entries_len); + printf("Number of leaves: %d\n", leaves); + printf("Max depth: %d\n", max_depth); + printf("Average leaf depth: %d\n", depth_sum / leaves); + printf("Memory used: %d\n", (trie->entries_size * sizeof(trie_entry_t)) + + (trie->c_size) + sizeof(trie_t)); } } -void trie_inspect(const trie_t *trie) +void trie_inspect(const trie_t *trie, bool show_content) { - trie_entry_inspect(trie, trie->entries, 0); + trie_entry_inspect(trie, show_content, trie->entries, 0); }