From 11a9881988c62ec7fb9ba379861111bcc62e4908 Mon Sep 17 00:00:00 2001 From: Al Date: Sun, 9 Aug 2015 03:38:28 -0400 Subject: [PATCH] [phrases] adding _from_index_get_prefix_char/_from_index_get_suffix_char methods --- src/trie_search.c | 38 +++++++++++++++++++++++++------------- src/trie_search.h | 6 +++++- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/trie_search.c b/src/trie_search.c index fe5afa98..e6547eaf 100644 --- a/src/trie_search.c +++ b/src/trie_search.c @@ -505,18 +505,25 @@ phrase_t trie_search_suffixes_from_index(trie_t *self, char *word, size_t len, u return (phrase_t) {phrase_start, phrase_len, value}; } -inline phrase_t trie_search_suffixes(trie_t *self, char *word, size_t len) { - trie_node_t root_node = trie_get_root(self); - uint32_t node_id = trie_get_transition_index(self, root_node, TRIE_SUFFIX_CHAR); - trie_node_t node = trie_get_node(self, node_id); +inline phrase_t trie_search_suffixes_from_index_get_suffix_char(trie_t *self, char *word, size_t len, uint32_t start_node_id) { + if (word == NULL || len == 0) return NULL_PHRASE; + trie_node_t node = trie_get_node(self, start_node_id); + uint32_t node_id = trie_get_transition_index(self, node, TRIE_SUFFIX_CHAR); + node = trie_get_node(self, node_id); - if (node.check != ROOT_NODE_ID) { - return (phrase_t){0, 0, 0}; + if (node.check != start_node_id) { + return NULL_PHRASE; } return trie_search_suffixes_from_index(self, word, len, node_id); } +inline phrase_t trie_search_suffixes(trie_t *self, char *word, size_t len) { + if (word == NULL || len == 0) return NULL_PHRASE; + return trie_search_suffixes_from_index_get_suffix_char(self, word, len, ROOT_NODE_ID); +} + + phrase_t trie_search_prefixes_from_index(trie_t *self, char *word, size_t len, uint32_t start_node_id) { log_debug("Call to trie_search_prefixes_from_index\n"); uint32_t node_id = start_node_id, last_node_id = node_id; @@ -625,21 +632,26 @@ phrase_t trie_search_prefixes_from_index(trie_t *self, char *word, size_t len, u ptr++; } - if (phrase_len == 0) return (phrase_t){0, 0, 0}; + if (phrase_len == 0) return NULL_PHRASE; return (phrase_t) {phrase_start, phrase_len, value}; } -inline phrase_t trie_search_prefixes(trie_t *self, char *word, size_t len) { - trie_node_t root_node = trie_get_root(self); - uint32_t node_id = trie_get_transition_index(self, root_node, TRIE_PREFIX_CHAR); - trie_node_t node = trie_get_node(self, node_id); +inline phrase_t trie_search_prefixes_from_index_get_prefix_char(trie_t *self, char *word, size_t len, uint32_t start_node_id) { + trie_node_t node = trie_get_node(self, start_node_id); + uint32_t node_id = trie_get_transition_index(self, node, TRIE_PREFIX_CHAR); + node = trie_get_node(self, node_id); - if (node.check != ROOT_NODE_ID) { - return (phrase_t){0, 0, 0}; + if (node.check != start_node_id) { + return NULL_PHRASE; } return trie_search_prefixes_from_index(self, word, len, node_id); } +inline phrase_t trie_search_prefixes(trie_t *self, char *word, size_t len) { + if (word == NULL || len == 0) return NULL_PHRASE; + return trie_search_prefixes_from_index_get_prefix_char(self, word, len, ROOT_NODE_ID); +} + diff --git a/src/trie_search.h b/src/trie_search.h index a3cf1be0..bb24ac3d 100644 --- a/src/trie_search.h +++ b/src/trie_search.h @@ -25,15 +25,19 @@ typedef struct phrase { VECTOR_INIT(phrase_array, phrase_t) +#define NULL_PHRASE (phrase_t){0, 0, 0}; + phrase_array *trie_search(trie_t *self, char *text); phrase_array *trie_search_from_index(trie_t *self, char *text, uint32_t start_node_id); phrase_array *trie_search_tokens(trie_t *self, char *str, token_array *tokens); phrase_array *trie_search_tokens_from_index(trie_t *self, char *str, token_array *tokens, uint32_t start_node_id); phrase_t trie_search_suffixes_from_index(trie_t *self, char *word, size_t len, uint32_t start_node_id); +phrase_t trie_search_suffixes_from_index_get_suffix_char(trie_t *self, char *word, size_t len, uint32_t start_node_id); phrase_t trie_search_suffixes(trie_t *self, char *word, size_t len); phrase_t trie_search_prefixes_from_index(trie_t *self, char *word, size_t len, uint32_t start_node_id); +phrase_t trie_search_prefixes_from_index_get_prefix_char(trie_t *self, char *word, size_t len, uint32_t start_node_id); phrase_t trie_search_prefixes(trie_t *self, char *word, size_t len); -#endif \ No newline at end of file +#endif