From 3d95875a1106a82ab7b812a3820083291bad610b Mon Sep 17 00:00:00 2001 From: Al Date: Thu, 4 Jun 2015 02:41:48 -0400 Subject: [PATCH] [phrases] trie_add_len --- src/trie.c | 24 +++++++++++++++--------- src/trie.h | 3 ++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/trie.c b/src/trie.c index 1f497b2a..01cb6367 100644 --- a/src/trie.c +++ b/src/trie.c @@ -616,8 +616,7 @@ void trie_print(trie_t *self) { } -bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, uint32_t data) { - int num_chars = strlen(key); +bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, size_t len, uint32_t data) { unsigned char *ptr = (unsigned char *)key; uint32_t last_node_id = node_id; trie_node_t last_node = trie_get_node(self, node_id); @@ -630,7 +629,7 @@ bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, uint32_t data) // Walks node until prefix reached, including the trailing \0 - for (int i = 0; i < num_chars + 1; ptr++, i++, last_node_id = node_id, last_node = node) { + for (int i = 0; i < len; ptr++, i++, last_node_id = node_id, last_node = node) { log_debug("--- char=%d\n", *ptr); node_id = trie_get_transition_index(self, last_node, *ptr); @@ -658,13 +657,19 @@ 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) { - if (strlen(key) == 0) return false; - return trie_add_at_index(self, ROOT_NODE_ID, key, data); +inline bool trie_add(trie_t *self, char *key, uint32_t data) { + size_t len = strlen(key); + if (len == 0) return false; + return trie_add_at_index(self, ROOT_NODE_ID, key, len + 1, data); +} + +inline bool trie_add_len(trie_t *self, char *key, size_t len, uint32_t data) { + return trie_add_at_index(self, ROOT_NODE_ID, key, len, data); } 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; + size_t len = strlen(key); + if (start_node_id == NULL_NODE_ID || len == 0) return false; trie_node_t start_node = trie_get_node(self, start_node_id); @@ -674,8 +679,9 @@ bool trie_add_suffix_at_index(trie_t *self, char *key, uint32_t start_node_id, u 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); + char *suffix = utf8_reversed_string(key); + + bool success = trie_add_at_index(self, node_id, suffix, len, data); free(suffix); return success; diff --git a/src/trie.h b/src/trie.h index 172c6d4c..b6e94272 100644 --- a/src/trie.h +++ b/src/trie.h @@ -97,8 +97,9 @@ void trie_set_tail(trie_t *self, unsigned char *tail, int32_t tail_pos); int32_t trie_separate_tail(trie_t *self, uint32_t from_index, unsigned char *tail, uint32_t data); void trie_tail_merge(trie_t *self, uint32_t old_node_id, unsigned char *suffix, uint32_t data); -bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, uint32_t data); +bool trie_add_at_index(trie_t *self, uint32_t node_id, char *key, size_t len, uint32_t data); bool trie_add(trie_t *self, char *key, uint32_t data); +bool trie_add_len(trie_t *self, char *key, size_t len, 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);