示例#1
0
/*
 * Actually perform the insert.
 *
 * Return 0 on success and 1 on failure.
 */
static int _trie_insert(struct trie *t, const char *key, void *data,
			unsigned int depth)
{
    unsigned int e;

    if (depth == strlen(key) + 1) {
	assert(strcmp(t->key, key) == 0);
	t->data = data;
	return 0;
    }

    e = key[depth];
    assert(e < NELMS);
    if (!t->kids[e]) {
	struct trie *k = malloc(sizeof(*k));
	if (!k) {
	    perror("malloc failed");
	    return 1;
	}
	trie_init(k);
	k->key = dup_prefix(key, depth);
	if (!k->key) {
	    free(k);
	    return 1;
	}
	t->kids[e] = k;
    }

    return _trie_insert(t->kids[e], key, data, depth + 1);
}
示例#2
0
void
path_parser_configure (struct path_parser_s *self, const char *descr, void *u)
{
	EXTRA_ASSERT (self != NULL);
	EXTRA_ASSERT (descr != NULL);
	gchar **tokens = g_strsplit (descr, "/", -1);
	self->roots = _trie_insert (NULL, self->roots, tokens, descr, u);
	g_strfreev (tokens);
}
示例#3
0
/* 
 * wrapper functions to set things (like strings) up
 */
TrieNode* trie_insert(char *str, Book *v, TrieNode *t)
{
	char *s;
	TrieNode *T;

	s = setup_string(str);
	T =  _trie_insert(s, v, t);
	free(s);
	return T;
}
示例#4
0
/* Add a key to the trie and create nodes as needed (recursively) */
static TrieNode* _trie_insert(char *s, Book *v, TrieNode *t)
{
	int index;

	if (*s == '\0') {
		/* reached the end */
		t->key   = '\0';
		t->value = v;
	} else {
		/* get index in 0..25 */
		index = *s - 'a';
		/* check for existence */
		if(!t->edges[index])
			t->edges[index] = trie_initialize(*s);
		/* key, for later */
		t->key = *s;
		/* one more shift for the next character */
		s++;
		t->edges[index] = _trie_insert(s, v, t->edges[index]);
	}
	return t;
}
示例#5
0
int trie_insert(struct trie *t, const char *key, void *data)
{
    return _trie_insert(t, key, data, 0);
}