コード例 #1
0
ファイル: hyphen.c プロジェクト: dongyx/hyphen
static char *hyphen(char *dst, const char *src, char delim) {
	const char *exp = expchck(src, delim);
	if (exp) return strcpy(dst, exp);

	char txt[TOKLEN + 1];
	sprintf(txt, ".%s.", src);
	char *txtend = txt + strlen(txt);

	uint8_t wght[TOKLEN + 1] = {0};
	for (char *s = txt; *s; ++s) {
		char *p = s;
		trie_t trie = pattern;
		while (p <= txtend && trie) {
			if (trie_child(trie, 0))
				for (char *i = s; i <= p; ++i)
					wght[i - txt] = MAX(
						wght[i - txt],
						((uint8_t *)trie_find(trie, ""))[i - s]
					);
			trie = trie_child(trie, tolower(*p++));
		}
	}

	wght[1] = wght[2] = 0;
	wght[txtend - txt - 1] = wght[txtend - txt - 2] = 0;

	char *pdst = dst;
	for (char *s = txt + 1; s < txtend - 1; *pdst++ = *s++)
		if (wght[s - txt] & 1)
			*pdst++ = delim;
	*pdst = 0;
	return dst;
}
コード例 #2
0
ファイル: path.c プロジェクト: git-for-windows/git
static void update_common_dir(struct strbuf *buf, int git_dir_len,
			      const char *common_dir)
{
	char *base = buf->buf + git_dir_len;
	init_common_trie();
	if (trie_find(&common_trie, base, check_common, NULL) > 0)
		replace_dir(buf, git_dir_len, common_dir);
}
コード例 #3
0
ファイル: trie.c プロジェクト: johnliu1/HW7FINAL
bool trie_remove(trie_t trie, const char * word, void ** data)
{
    int len = strlen(word);
    bool dataflag = true;
    trie_t temp = trie;

    if (!trie_find(trie, word))
        return false;

    //move to the end of work pos
    for (int i = 0; i < len; i++)
    {
        temp = temp->next[ (word[i]) - ' '];
    }

    if (temp->endNode != true)
    {
        if (!temp->nodePtr->value)
            return false;

        if (data !=NULL)
            *data = temp->nodePtr->value;
        temp->nodePtr->value=NULL;
        return true;
    }

    if (temp->endNode == true)
    {
        trie_t temp2;
        for (int i = len-1; i >= 0; i--)
        {
            temp2 = temp->pre;
            if (data != NULL&&dataflag)
            {
                *data = temp->nodePtr->value;
                dataflag = false;
            }
            trie_destroy(temp, NULL);
            temp2->next[ (word[i]) - ' '] = NULL;

            for (int i = 0; i < SUBTREEPOSSIBLENUM; i++)
            {
                if (temp2->next[i] != NULL)
                    return true;
            }
            temp = temp2;
            if (temp->nodePtr->value)
            {
                temp->endNode = true;
                return true;
            }
        }
    }
    return true;
}
コード例 #4
0
ファイル: dictionary.c プロジェクト: Pand9/IPP-2015-1
int parse_and_print_to_stdout(const char * operation)
{
    if (NULL == operation)
    {
        skip_line(stdin);
        return -1;
    }
    if (0 == strcmp(OP_INSERT, operation))
    {
        if (0 != parse_insert(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int word_num = trie_insert(word_buffer);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_PREV, operation))
    {
        int number, start, end;
        if (0 != parse_prev(stdin, &number, &start, &end))
            return -1;
        int word_num = trie_prev(number, start, end);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_DELETE, operation))
    {
        int number;
        if (0 != parse_delete(stdin, &number))
            return -1;
        int word_num = trie_delete(number);
        if (word_num < 0)
            return -1;
        printf("deleted: %d\n", number);
    }
    if (0 == strcmp(OP_FIND, operation))
    {
        if (0 != parse_find(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int find_result = trie_find(word_buffer, strlen(word_buffer));
        if (find_result < 0)
            return -1;
        puts(find_result == 0 ? "YES" : "NO");
    }
    if (0 == strcmp(OP_CLEAR, operation))
    {
        if (0 != parse_clear(stdin))
            return -1;
        if (0 != trie_clear())
            return -1;
        puts("cleared");
    }
    return 0;
}
コード例 #5
0
ファイル: cache.c プロジェクト: nshi/falcon
falcon_object_t *falcon_cache_get(falcon_cache_t *cache, const gchar *name)
{
	trie_node_t *node = NULL;

	g_return_val_if_fail(cache, NULL);
	g_return_val_if_fail(name, NULL);

	g_mutex_lock(cache->lock);
	node = trie_find(cache->objects, name);
	g_mutex_unlock(cache->lock);
	return trie_data(node);
}
コード例 #6
0
/*
 * Search a trie for some key.  Find the longest /-or-\0-terminated
 * prefix of the key for which the trie contains a value.  Call fn
 * with the unmatched portion of the key and the found value, and
 * return its return value.  If there is no such prefix, return -1.
 *
 * The key is partially normalized: consecutive slashes are skipped.
 *
 * For example, consider the trie containing only [refs,
 * refs/worktree] (both with values).
 *
 * | key             | unmatched  | val from node | return value |
 * |-----------------|------------|---------------|--------------|
 * | a               | not called | n/a           | -1           |
 * | refs            | \0         | refs          | as per fn    |
 * | refs/           | /          | refs          | as per fn    |
 * | refs/w          | /w         | refs          | as per fn    |
 * | refs/worktree   | \0         | refs/worktree | as per fn    |
 * | refs/worktree/  | /          | refs/worktree | as per fn    |
 * | refs/worktree/a | /a         | refs/worktree | as per fn    |
 * |-----------------|------------|---------------|--------------|
 *
 */
static int trie_find(struct trie *root, const char *key, match_fn fn,
		     void *baton)
{
	int i;
	int result;
	struct trie *child;

	if (!*key) {
		/* we have reached the end of the key */
		if (root->value && !root->len)
			return fn(key, root->value, baton);
		else
			return -1;
	}

	for (i = 0; i < root->len; i++) {
		/* Partial path normalization: skip consecutive slashes. */
		if (key[i] == '/' && key[i+1] == '/') {
			key++;
			continue;
		}
		if (root->contents[i] != key[i])
			return -1;
	}

	/* Matched the entire compressed section */
	key += i;
	if (!*key)
		/* End of key */
		return fn(key, root->value, baton);

	/* Partial path normalization: skip consecutive slashes */
	while (key[0] == '/' && key[1] == '/')
		key++;

	child = root->children[(unsigned char)*key];
	if (child)
		result = trie_find(child, key + 1, fn, baton);
	else
		result = -1;

	if (result >= 0 || (*key != '/' && *key != 0))
		return result;
	if (root->value)
		return fn(key, root->value, baton);
	else
		return -1;
}
コード例 #7
0
ファイル: cache.c プロジェクト: nshi/falcon
void falcon_cache_foreach_descendant(falcon_cache_t *cache, const gchar *name,
                                     GFunc func, gpointer userdata)
{
	trie_node_t *node = NULL;
	falcon_object_t *data = NULL;

	g_return_if_fail(cache);
	g_return_if_fail(func);

	g_mutex_lock(cache->lock);
	node = trie_find(cache->objects, name);
	falcon_cache_recursive_foreach_descendant(trie_child(node), func, userdata);
	if ((data = trie_data(node)))
		func(data, userdata);
	g_mutex_unlock(cache->lock);
}
コード例 #8
0
ファイル: Automaton.c プロジェクト: biocad/au-summer-2013
static int
automaton_contains(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
	ssize_t wordlen;
	TRIE_LETTER_TYPE* word;
	PyObject* py_word;

	py_word = pymod_get_string(args, &word, &wordlen);
	if (py_word == NULL)
		return -1;

	TrieNode* node = trie_find(automaton->root, word, wordlen);
	Py_DECREF(py_word);

	return (node and node->eow);
#undef automaton
}
コード例 #9
0
ファイル: tries.c プロジェクト: Hiperzone/University
bool trie_find(Trie  * node, char *tema)
{
    assert(tema != NULL && *tema != '\0' && node != NULL && POS(tema) >= 0);

    //apenas procurar se o proximo char nao for o fim da palavra
    if(*(tema+1) != '\0' && node->table[POS(tema)] != NULL)
    {
        return trie_find(node->table[POS(tema)], tema+1);
    }
    //e a ultima letra da palavra, verificar se esta marcado como uma palavra.
    else if( node->table[POS(tema)] != NULL && node->table[POS(tema)]->word == 1 )
    {
        return true;
    }

    //nao encontrou
    return false;
}
コード例 #10
0
ファイル: Automaton.c プロジェクト: biocad/au-summer-2013
static PyObject*
automaton_get(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
	ssize_t wordlen;
	TRIE_LETTER_TYPE* word;
	PyObject* py_word;
	PyObject* py_def;

	py_word = pymod_get_string_from_tuple(args, 0, &word, &wordlen);
	if (py_word == NULL)
		return NULL;

	TrieNode* node = trie_find(automaton->root, word, wordlen);

	if (node and node->eow) {
		switch (automaton->store) {
			case STORE_INTS:
			case STORE_LENGTH:
				return Py_BuildValue("i", node->output.integer);

			case STORE_ANY:
				Py_INCREF(node->output.object);
				return node->output.object;

			default:
				PyErr_SetNone(PyExc_ValueError);
				return NULL;
		}
	}
	else {
		py_def = PyTuple_GetItem(args, 1);
		if (py_def) {
			Py_INCREF(py_def);
			return py_def;
		}
		else {
			PyErr_Clear();
			PyErr_SetNone(PyExc_KeyError);
			return NULL;
		}
	}
#undef automaton
}
コード例 #11
0
ファイル: Automaton.c プロジェクト: biocad/au-summer-2013
static PyObject*
automaton_match(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
	ssize_t wordlen;
	TRIE_LETTER_TYPE* word;
	PyObject* py_word;

	py_word = pymod_get_string_from_tuple(args, 0, &word, &wordlen);
	if (py_word == NULL)
		return NULL;

	TrieNode* node = trie_find(automaton->root, word, wordlen);;

	Py_DECREF(py_word);
	if (node)
		Py_RETURN_TRUE;
	else
		Py_RETURN_FALSE;
#undef automaton
}
コード例 #12
0
ファイル: cache.c プロジェクト: nshi/falcon
void falcon_cache_foreach_child(falcon_cache_t *cache, const gchar *name,
                                GFunc func, gpointer userdata)
{
	trie_node_t *node = NULL;
	trie_node_t *next = NULL;
	falcon_object_t *data = NULL;

	g_return_if_fail(cache);
	g_return_if_fail(func);

	g_mutex_lock(cache->lock);
	node = trie_find(cache->objects, name);
	next = trie_child(node);
	while (next) {
		if ((data = trie_data(next)))
			func(data, userdata);
		next = trie_next(next);
	}
	if ((data = trie_data(node)))
		func(data, userdata);
	g_mutex_unlock(cache->lock);
}
コード例 #13
0
ファイル: cache.c プロジェクト: nshi/falcon
gboolean falcon_cache_delete(falcon_cache_t *cache, const gchar *name)
{
	trie_node_t *node = NULL;

	g_return_val_if_fail(cache, FALSE);
	g_return_val_if_fail(name, FALSE);

	g_mutex_lock(cache->lock);
	node = trie_find(cache->objects, name);
	if (!node) {
		g_mutex_unlock(cache->lock);
		g_warning(_("Failed to delete \"%s\", it does not exist in the cache."),
		          name);
		return FALSE;
	}

	trie_delete(cache->objects, name, (trie_free_func)falcon_object_free);
	cache->count--;
	g_mutex_unlock(cache->lock);

	return TRUE;
}
コード例 #14
0
ファイル: cache.c プロジェクト: nshi/falcon
gboolean falcon_cache_add(falcon_cache_t *cache, falcon_object_t *object)
{
	trie_node_t *old_node = NULL;
	falcon_object_t *old = NULL;
	falcon_object_t *dup = falcon_object_copy(object);

	g_return_val_if_fail(cache, FALSE);
	g_return_val_if_fail(object, FALSE);

	g_mutex_lock(cache->lock);
	old_node = trie_find(cache->objects, falcon_object_get_name(dup));

	if (old_node && (old = trie_data(old_node))) {
		falcon_object_free(old);
		trie_set_data(old_node, dup);
	} else {
		trie_add(cache->objects, falcon_object_get_name(dup), dup);
		cache->count++;
	}

	g_mutex_unlock(cache->lock);

	return TRUE;
}
コード例 #15
0
ファイル: dictionary.c プロジェクト: Pand9/IPP-2015
bool dictionary_find(const struct dictionary *dict, const wchar_t* word)
{
    return trie_find(dict->tree, word);
}