static int trie_da_enum_func(const char *key, trie_idx_t sep_node, void *user_data) { __walk_data_t walk_data; trie_idx_t t; const char *suffix; char *full_key, *p; int ret; trie_data_t data; size_t keyl, suffixl; walk_data = user_data; t = trie_da_get_tail_index(walk_data->trie->da, sep_node); suffix = tail_get_suffix(walk_data->trie->tail, t); keyl = strlen(key); suffixl = strlen(suffix); full_key = malloc(keyl + suffixl + 1); for (p = full_key; *key; p++, key++) { *p = alpha_map_trie_to_char(*key); } for ( ; *suffix; p++, suffix++) { *p = alpha_map_trie_to_char(*suffix); } *p = 0; data = tail_get_data(walk_data->trie->tail, t); ret = walk_data->walkf(full_key, data, walk_data->user_data); free(full_key); return ret; }
static Bool trie_da_enum_func (const TrieChar *key, TrieIndex sep_node, void *user_data) { _TrieEnumData *enum_data; TrieIndex t; const TrieChar *suffix; AlphaChar *full_key, *p; Bool ret; enum_data = (_TrieEnumData *) user_data; t = trie_da_get_tail_index (enum_data->trie->da, sep_node); suffix = tail_get_suffix (enum_data->trie->tail, t); full_key = (AlphaChar *) malloc ((strlen ((const char *)key) + strlen ((const char *)suffix) + 1) * sizeof (AlphaChar)); for (p = full_key; *key; p++, key++) { *p = alpha_map_trie_to_char (enum_data->trie->alpha_map, *key); } for ( ; *suffix; p++, suffix++) { *p = alpha_map_trie_to_char (enum_data->trie->alpha_map, *suffix); } *p = 0; ret = (*enum_data->enum_func) (full_key, tail_get_data (enum_data->trie->tail, t), enum_data->user_data); free (full_key); return ret; }
/** * @brief Get all walkable characters from state * * @param s : the state to get * @param chars : the storage for the result * @param chars_nelm : the size of @a chars[] in number of elements * * @return total walkable characters * * Get the list of all walkable characters from state @a s. At most * @a chars_nelm walkable characters are stored in @a chars[] on return. * * The function returns the actual number of walkable characters from @a s. * Note that this may not equal the number of characters stored in @a chars[] * if @a chars_nelm is less than the actual number. * * Available since: 0.2.6 */ int trie_state_walkable_chars (const TrieState *s, AlphaChar chars[], int chars_nelm) { int syms_num = 0; if (!s->is_suffix) { Symbols *syms = da_output_symbols (s->trie->da, s->index); int i; syms_num = symbols_num (syms); for (i = 0; i < syms_num && i < chars_nelm; i++) { TrieChar tc = symbols_get (syms, i); chars[i] = alpha_map_trie_to_char (s->trie->alpha_map, tc); } symbols_free (syms); } else { const TrieChar *suffix = tail_get_suffix (s->trie->tail, s->index); chars[0] = alpha_map_trie_to_char (s->trie->alpha_map, suffix[s->suffix_idx]); syms_num = 1; } return syms_num; }
/** * @brief Get key for a trie iterator * * @param iter : an iterator * * @return the allocated key string; NULL on failure * * Get key for the current entry referenced by the trie iterator @a iter. * * The return string must be freed with free(). * * Available since: 0.2.6 */ AlphaChar * trie_iterator_get_key (const TrieIterator *iter) { const TrieState *s; const TrieChar *tail_str; AlphaChar *alpha_key, *alpha_p; s = iter->state; if (!s) return NULL; /* if s is in tail, root == s */ if (s->is_suffix) { tail_str = tail_get_suffix (s->trie->tail, s->index); if (!tail_str) return NULL; tail_str += s->suffix_idx; alpha_key = (AlphaChar *) malloc (sizeof (AlphaChar) * (strlen (tail_str) + 1)); alpha_p = alpha_key; } else { TrieIndex tail_idx; int i, key_len; const TrieChar *key_p; tail_idx = trie_da_get_tail_index (s->trie->da, s->index); tail_str = tail_get_suffix (s->trie->tail, tail_idx); if (!tail_str) return NULL; key_len = trie_string_length (iter->key); key_p = trie_string_get_val (iter->key); alpha_key = (AlphaChar *) malloc ( sizeof (AlphaChar) * (key_len + strlen (tail_str) + 1) ); alpha_p = alpha_key; for (i = key_len; i > 0; i--) { *alpha_p++ = alpha_map_trie_to_char (s->trie->alpha_map, *key_p++); } } while (*tail_str) { *alpha_p++ = alpha_map_trie_to_char (s->trie->alpha_map, *tail_str++); } *alpha_p = 0; return alpha_key; }
static __attribute__((unused)) char* alpha_map_trie_to_char_str(const char *str) { char *alpha_str, *p; alpha_str = malloc(strlen(str) + 1); for (p = alpha_str; *str; p++, str++) { *p = alpha_map_trie_to_char(*str); } *p = 0; return alpha_str; }
AlphaChar * alpha_map_trie_to_char_str (const AlphaMap *alpha_map, const TrieChar *str) { AlphaChar *alpha_str, *p; alpha_str = (AlphaChar *) malloc ((strlen ((const char *)str) + 1) * sizeof (AlphaChar)); for (p = alpha_str; *str; p++, str++) { *p = (AlphaChar) alpha_map_trie_to_char (alpha_map, *str); } *p = 0; return alpha_str; }