示例#1
0
int main()
{
	void *prm;
	char *v;
	cp_trie *t = cp_trie_create(0);

	if (t == NULL)
	{
		printf("can\'t create trie\n");
		exit(1);
	}
	
	cp_trie_add(t, "ferguson", "xxx");
	cp_trie_add(t, "fermat", "yyy");

	cp_trie_prefix_match(t, "ferry", &prm);
	v = prm;
	printf("prefix match for ferry: %s\n", v ? v : "none");

	v = cp_trie_exact_match(t, "ferry");
	printf("exact match for ferry: %s\n", v ? v : "none");

	cp_trie_prefix_match(t, "fergusonst", &prm);
	v = prm;
	printf("prefix match for fergusonst: %s\n", v ? v : "none");

	v = cp_trie_exact_match(t, "fergusonst");
	printf("exact match for fergusonst: %s\n", v ? v : "none");

	cp_trie_destroy(t);

	return 0;
}
static int trie_nkncnt_name_to_index(const char *name, int *cindex)
{
    int n;
    int rv;
    int ret;
    nkn_shmdef_t cur_shmdef;
    glob_item_t *item;

    pthread_mutex_lock(&nkncnt_lock);

    cur_shmdef = *pshmdef;
    while (cur_shmdef.revision != shmdef.revision) {
    	if (nkn_cnt_trie) {
	    cp_trie_destroy(nkn_cnt_trie);
	}
	nkn_cnt_trie = cp_trie_create_trie(
				(COLLECTION_MODE_NOSYNC|COLLECTION_MODE_COPY|
		 	 	COLLECTION_MODE_DEEP), 
				trie_copy_func, trie_destruct_func);
	if (!nkn_cnt_trie) {
	    ret = 1;
	    goto exit;
	}

	for (n = 0; n < pshmdef->tot_cnts; n++) {
	    if (!g_allcnts[n].size) {
	    	continue;
	    }
	    rv = cp_trie_add(nkn_cnt_trie,(varname+g_allcnts[n].name), 
			     &g_allcnts[n]);
	    if (rv) {
	    	ret = 2;
	    	goto exit;
	    }
	}
	if (cur_shmdef.revision == pshmdef->revision) {
	    shmdef = cur_shmdef;
	    break;
	}
    	cur_shmdef = *pshmdef;
    }

    item = (glob_item_t *)cp_trie_exact_match(nkn_cnt_trie, (char *)name);
    if (item) {
    	*cindex = (((uint64_t)item - (uint64_t)g_allcnts)/sizeof(glob_item_t));
	ret = 0;
    } else {
    	*cindex = -1;
    	ret = 0; // Not found
    }

exit:

    pthread_mutex_unlock(&nkncnt_lock);
    return ret;
}
示例#3
0
void globals_free(globals **p) {
	globals *g = *p;

	cp_trie_destroy(g->sorts);

	cp_multimap_destroy(g->terms);

	cp_multimap_destroy(g->ops);

	cp_vector_destroy_custom(g->op_names, free);

	free(*p);
	*p = 0;
}