From fa784677f2036694966777eca0cd79b6ad0e47e9 Mon Sep 17 00:00:00 2001 From: Al Date: Thu, 4 Jun 2015 02:30:53 -0400 Subject: [PATCH] [phrases] trie_add_suffix_at_index method --- src/trie.c | 19 +++++++++++++------ src/trie.h | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/trie.c b/src/trie.c index 970862ee..1f497b2a 100644 --- a/src/trie.c +++ b/src/trie.c @@ -663,20 +663,27 @@ bool trie_add(trie_t *self, char *key, uint32_t data) { return trie_add_at_index(self, ROOT_NODE_ID, key, data); } -bool trie_add_suffix(trie_t *self, char *key, uint32_t data) { - if (strlen(key) == 0) return false; - trie_node_t root = trie_get_root(self); +bool trie_add_suffix_at_index(trie_t *self, char *key, uint32_t start_node_id, uint32_t data) { + if (start_node_id == NULL_NODE_ID || strlen(key) == 0) return false; - uint32_t node_id = trie_get_transition_index(self, root, '\0'); + trie_node_t start_node = trie_get_node(self, start_node_id); + + uint32_t node_id = trie_get_transition_index(self, start_node, '\0'); trie_node_t node = trie_get_node(self, node_id); - if (node.check != ROOT_NODE_ID) { - node_id = trie_add_transition(self, ROOT_NODE_ID, '\0'); + if (node.check != start_node_id) { + node_id = trie_add_transition(self, start_node_id, '\0'); } char *suffix = utf8_reversed_string(key); bool success = trie_add_at_index(self, node_id, suffix, data); + free(suffix); return success; + +} + +inline bool trie_add_suffix(trie_t *self, char *key, uint32_t data) { + return trie_add_suffix_at_index(self, key, ROOT_NODE_ID, data); } bool trie_compare_tail(trie_t *self, char *str, size_t len, size_t tail_index) { diff --git a/src/trie.h b/src/trie.h index ef38c3ca..172c6d4c 100644 --- a/src/trie.h +++ b/src/trie.h @@ -100,6 +100,7 @@ void trie_tail_merge(trie_t *self, uint32_t old_node_id, unsigned char *suffix, bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, uint32_t data); bool trie_add(trie_t *self, char *key, uint32_t data); bool trie_add_suffix(trie_t *self, char *key, uint32_t data); +bool trie_add_suffix_at_index(trie_t *self, char *key, uint32_t start_node_id, uint32_t data); uint32_t trie_get_from_index(trie_t *self, char *word, size_t len, uint32_t i); uint32_t trie_get_len(trie_t *self, char *word, size_t len);