Esempio n. 1
0
/**
 * @brief Get data from leaf state
 *
 * @param s    : a leaf state
 *
 * @return the data associated with the leaf state @a s,
 *         or TRIE_DATA_ERROR if @a s is not a leaf state
 *
 * Get value from a leaf state of trie. Getting value from a non-leaf state
 * will result in TRIE_DATA_ERROR.
 */
trie_data_t
trie_state_get_data(const_trie_state_t s)
{
	return s->suffixp
		? tail_get_data(s->trie->tail, s->index)
		: TRIE_DATA_ERROR;
}
Esempio n. 2
0
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;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
Bool
trie_retrieve (Trie *trie, const TrieChar *key, TrieData *o_data)
{
    TrieIndex        s;
    short            suffix_idx;
    const TrieChar  *p;
    size_t           len;

    /* walk through branches */
    s = da_get_root (trie->da);
    for (p = key; !trie_da_is_separate (trie->da, s); p++) {
        if (!da_walk (trie->da, &s, *p))
            return FALSE;
        if ('\0' == *p)
            break;
    }

    /* walk through tail */
    s = trie_da_get_tail_index (trie->da, s);
    if ('\0' != *p) {
        suffix_idx = 0;
        len = strlen ((const char *) p) + 1;    /* including null-terminator */
        if (tail_walk_str (trie->tail, s, &suffix_idx, p, len) != len)
            return FALSE;
    }

    /* found, set the val and return */
    if (o_data)
        *o_data = tail_get_data (trie->tail, s);
    return TRUE;
}
Esempio n. 5
0
File: trie.c Progetto: Rustem/datrie
/**
 * @brief Get data from terminal state
 *
 * @param s    : a terminal state
 *
 * @return the data associated with the terminal state @a s,
 *         or TRIE_DATA_ERROR if @a s is not a terminal state
 *
 */
TrieData
trie_state_get_terminal_data (const TrieState *s)
{
    TrieIndex        tail_index;
    TrieIndex index = s->index;

    if (!s)
        return TRIE_DATA_ERROR;

    if (!s->is_suffix){
        if (!trie_da_is_separate(s->trie->da, index)) {
            /* walk to a terminal char to get the data */
            Bool ret = da_walk (s->trie->da, &index, TRIE_CHAR_TERM);
            if (!ret) {
                return TRIE_DATA_ERROR;
            }
        }
        tail_index = trie_da_get_tail_index (s->trie->da, index);
    }
    else {
        tail_index = s->index;
    }

    return tail_get_data (s->trie->tail, tail_index);
}
Esempio n. 6
0
/**
 * @brief Get data for the entry referenced by an iterator
 *
 * @param iter  : an iterator
 *
 * @return the data associated with the entry referenced by iterator @a iter,
 *         or TRIE_DATA_ERROR if @a iter does not reference to a unique entry
 *
 * Get value for the entry referenced by an iterator. Getting value from an
 * un-iterated (or broken for any reason) iterator will result in
 * TRIE_DATA_ERROR.
 *
 * Available since: 0.2.6
 */
TrieData
trie_iterator_get_data (const TrieIterator *iter)
{
    const TrieState *s = iter->state;
    TrieIndex        tail_index;

    if (!s)
        return TRIE_DATA_ERROR;

    if (!s->is_suffix) {
        if (!trie_da_is_separate (s->trie->da, s->index))
            return TRIE_DATA_ERROR;

        tail_index = trie_da_get_tail_index (s->trie->da, s->index);
    } else {
        tail_index = s->index;
    }

    return tail_get_data (s->trie->tail, tail_index);
}
Esempio n. 7
0
/**
 * @brief Retrieve an entry from trie
 *
 * @param trie   : the trie
 * @param key    : the key for the entry to retrieve
 * @param o_data : the storage for storing the entry data on return
 *
 * @return boolean value indicating the existence of the entry.
 *
 * Retrieve an entry for the given @a key from @a trie. On return,
 * if @a key is found and @a o_data is not NULL, @a *o_data is set
 * to the data associated to @a key.
 */
int
trie_retrieve(const_trie_t trie, const char *key, trie_data_t *o_data)
{
	trie_idx_t s;
	short int suffix_idx;
	const char *p;

	/* walk through branches */
	s = da_get_root(trie->da);
	for (p = key; !trie_da_separate_p(trie->da, s); p++) {
		if (da_walk(trie->da, &s, alpha_map_char_to_trie(*p)) < 0) {
			return -1;
		}
		if (*p == 0) {
			break;
		}
	}
	/* walk through tail */
	s = trie_da_get_tail_index(trie->da, s);
	suffix_idx = 0;
	for ( ; ; p++) {
		if (tail_walk_char(
			    trie->tail, s, &suffix_idx,
			    alpha_map_char_to_trie(*p)) < 0) {
			return -1;
		}
		if (*p == 0) {
			break;
		}
	}

	/* found, set the val and return */
	if (o_data) {
		*o_data = tail_get_data(trie->tail, s);
	}
	return 0;
}
Esempio n. 8
0
/**
 * @brief Retrieve an entry from trie
 *
 * @param trie   : the trie
 * @param key    : the key for the entry to retrieve
 * @param o_data : the storage for storing the entry data on return
 *
 * @return boolean value indicating the existence of the entry.
 *
 * Retrieve an entry for the given @a key from @a trie. On return,
 * if @a key is found and @a o_data is not NULL, @a *o_data is set
 * to the data associated to @a key.
 */
Bool
trie_retrieve (const Trie *trie, const AlphaChar *key, TrieData *o_data)
{
    TrieIndex        s;
    short            suffix_idx;
    const AlphaChar *p;

    /* walk through branches */
    s = da_get_root (trie->da);
    for (p = key; !trie_da_is_separate (trie->da, s); p++) {
        if (!da_walk (trie->da, &s,
                      alpha_map_char_to_trie (trie->alpha_map, *p)))
        {
            return FALSE;
        }
        if (0 == *p)
            break;
    }

    /* walk through tail */
    s = trie_da_get_tail_index (trie->da, s);
    suffix_idx = 0;
    for ( ; ; p++) {
        if (!tail_walk_char (trie->tail, s, &suffix_idx,
                             alpha_map_char_to_trie (trie->alpha_map, *p)))
        {
            return FALSE;
        }
        if (0 == *p)
            break;
    }

    /* found, set the val and return */
    if (o_data)
        *o_data = tail_get_data (trie->tail, s);
    return TRUE;
}
Esempio n. 9
0
/**
 * @brief Get data from leaf state
 *
 * @param s    : a leaf state
 *
 * @return the data associated with the leaf state @a s,
 *         or TRIE_DATA_ERROR if @a s is not a leaf state
 *
 * Get value from a leaf state of trie. Getting value from a non-leaf state
 * will result in TRIE_DATA_ERROR.
 */
TrieData
trie_state_get_data (const TrieState *s)
{
    return s->is_suffix ? tail_get_data (s->trie->tail, s->index)
           : TRIE_DATA_ERROR;
}
Esempio n. 10
0
/**
 * @brief Get data from leaf state
 *
 * @param s    : a leaf state
 *
 * @return the data associated with the leaf state @a s,
 *         or TRIE_DATA_ERROR if @a s is not a leaf state
 *
 * Get value from a leaf state of trie. Getting value from a non-leaf state
 * will result in TRIE_DATA_ERROR.
 */
TrieData
trie_state_get_data (const TrieState *s)
{
    return trie_state_is_leaf (s) ? tail_get_data (s->trie->tail, s->index)
                                  : TRIE_DATA_ERROR;
}