Пример #1
0
static Bool
trie_branch_in_tail   (Trie           *trie,
                       TrieIndex       sep_node,
                       const TrieChar *suffix,
                       TrieData        data)
{
    TrieIndex   old_tail, old_da;
    TrieChar   *old_suffix, *p;

    /* adjust separate point in old path */
    old_tail = trie_da_get_tail_index (trie->da, sep_node);
    old_suffix = tail_get_suffix (trie->tail, old_tail);

    for (p = old_suffix; *p == *suffix; p++, suffix++)
        sep_node = da_insert_branch (trie->da, sep_node, *p);

    old_da = da_insert_branch (trie->da, sep_node, *p);
    if ('\0' != *p)
        ++p;
    tail_set_suffix (trie->tail, old_tail, p);
    trie_da_set_tail_index (trie->da, old_da, old_tail);

    free (old_suffix);

    /* insert the new branch at the new separate point */
    return trie_branch_in_branch (trie, sep_node, suffix, data);
}
Пример #2
0
Bool
trie_store (Trie *trie, const TrieChar *key, TrieData data)
{
    TrieIndex        s, t;
    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 trie_branch_in_branch (trie, s, p, data);
        if ('\0' == *p)
            break;
    }

    /* walk through tail */
    t = 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, t, &suffix_idx, p, len) != len)
            return trie_branch_in_tail (trie, s, p, data);
    }

    /* duplicated key, overwrite val */
    tail_set_data (trie->tail, t, data);
    return TRUE;
}
Пример #3
0
static Bool
trie_store_conditionally (Trie            *trie,
                          const AlphaChar *key,
                          TrieData         data,
                          Bool             is_overwrite)
{
    TrieIndex        s, t;
    short            suffix_idx;
    const AlphaChar *p, *sep;

    /* 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)))
        {
            TrieChar *key_str;
            Bool      res;

            key_str = alpha_map_char_to_trie_str (trie->alpha_map, p);
            res = trie_branch_in_branch (trie, s, key_str, data);
            free (key_str);

            return res;
        }
        if (0 == *p)
            break;
    }

    /* walk through tail */
    sep = p;
    t = trie_da_get_tail_index (trie->da, s);
    suffix_idx = 0;
    for ( ; ; p++) {
        if (!tail_walk_char (trie->tail, t, &suffix_idx,
                             alpha_map_char_to_trie (trie->alpha_map, *p)))
        {
            TrieChar *tail_str;
            Bool      res;

            tail_str = alpha_map_char_to_trie_str (trie->alpha_map, sep);
            res = trie_branch_in_tail (trie, s, tail_str, data);
            free (tail_str);

            return res;
        }
        if (0 == *p)
            break;
    }

    /* duplicated key, overwrite val if flagged */
    if (!is_overwrite) {
        return FALSE;
    }
    tail_set_data (trie->tail, t, data);
    trie->is_dirty = TRUE;
    return TRUE;
}
Пример #4
0
static int
trie_store_maybe(trie_t trie, const char *key, trie_data_t d, int overwritep)
{
	trie_idx_t s, t;
	short int suffix_idx;
	const char *p, *sep;

	/* 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) {
			char *key_str;
			int res;

			key_str = alpha_map_char_to_trie_str(p);
			res = trie_branch_in_branch(trie, s, key_str, d);
			free(key_str);

			return res;
		}
		if (*p == '\0') {
			break;
		}
	}

	/* walk through tail */
	sep = p;
	t = trie_da_get_tail_index(trie->da, s);
	suffix_idx = 0;
	for ( ; ; p++) {
		if (tail_walk_char(
			    trie->tail, t, &suffix_idx,
			    alpha_map_char_to_trie(*p)) < 0) {
			char *tail_str;
			int res;

			tail_str = alpha_map_char_to_trie_str(sep);
			res = trie_branch_in_tail(trie, s, tail_str, d);
			free(tail_str);
			return res;
		}
		if (*p == '\0') {
			break;
		}
	}

	/* duplicated key, overwrite val if flagged */
	if (!overwritep) {
		return -1;
	}
	tail_set_data(trie->tail, t, d);
	trie->dirtyp = 1;
	return 0;
}
Пример #5
0
static int
trie_branch_in_tail(
	trie_t trie, trie_idx_t sep_node,
	const char *suffix, trie_data_t data)
{
	trie_idx_t old_tail, old_da, s;
	const char *old_suffix, *p;

	/* adjust separate point in old path */
	old_tail = trie_da_get_tail_index(trie->da, sep_node);
	if ((old_suffix = tail_get_suffix(trie->tail, old_tail)) == NULL) {
		return -1;
	}

	for (p = old_suffix, s = sep_node; *p == *suffix; p++, suffix++) {
		trie_idx_t t = da_insert_branch(trie->da, s, *p);
		if (t == TRIE_INDEX_ERROR) {
			goto fail;
		}
		s = t;
	}

	old_da = da_insert_branch(trie->da, s, *p);
	if (old_da == TRIE_INDEX_ERROR) {
		goto fail;
	}

	if (*p != '\0') {
		p++;
	}
	tail_set_suffix(trie->tail, old_tail, p);
	trie_da_set_tail_index(trie->da, old_da, old_tail);

	/* insert the new branch at the new separate point */
	return trie_branch_in_branch(trie, s, suffix, data);

fail:
	/* failed, undo previous insertions and return error */
	da_prune_upto(trie->da, sep_node, s);
	trie_da_set_tail_index(trie->da, sep_node, old_tail);
	return -1;
}
Пример #6
0
static Bool
trie_branch_in_tail   (Trie           *trie,
                       TrieIndex       sep_node,
                       const TrieChar *suffix,
                       TrieData        data)
{
    TrieIndex       old_tail, old_da, s;
    const TrieChar *old_suffix, *p;

    /* adjust separate point in old path */
    old_tail = trie_da_get_tail_index (trie->da, sep_node);
    old_suffix = tail_get_suffix (trie->tail, old_tail);
    if (!old_suffix)
        return FALSE;

    for (p = old_suffix, s = sep_node; *p == *suffix; p++, suffix++) {
        TrieIndex t = da_insert_branch (trie->da, s, *p);
        if (TRIE_INDEX_ERROR == t)
            goto fail;
        s = t;
    }

    old_da = da_insert_branch (trie->da, s, *p);
    if (TRIE_INDEX_ERROR == old_da)
        goto fail;

    if ('\0' != *p)
        ++p;
    tail_set_suffix (trie->tail, old_tail, p);
    trie_da_set_tail_index (trie->da, old_da, old_tail);

    /* insert the new branch at the new separate point */
    return trie_branch_in_branch (trie, s, suffix, data);

fail:
    /* failed, undo previous insertions and return error */
    da_prune_upto (trie->da, sep_node, s);
    trie_da_set_tail_index (trie->da, sep_node, old_tail);
    return FALSE;
}