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; }
int32_t ds_trie_mgmt_add(void* val, char *key, hash_tbl_props_t* htp) { void *existing_obj = NULL; existing_obj = (void *)cp_trie_exact_match(htp->trie, key); if (existing_obj) { return -1; } cp_trie_add(htp->trie, strdup(key), val); return 0; }
int parse_input(char* file, globals* vars) { cfg_opt_t operation_opts[] = { CFG_STR_LIST("in","{}", CFGF_NONE), CFG_STR("out", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR_LIST("sorts", "{}", CFGF_NONE), CFG_SEC("op", operation_opts, CFGF_MULTI | CFGF_TITLE), CFG_FUNC("include", &cfg_include), CFG_END() }; cfg_t *cfg; int i, j, N, err; char *found, *sortname, *opname; op_signature *ops = malloc(sizeof(op_signature)+sizeof(char*)*MAX_OP_INPUTS); assert(ops); cfg = cfg_init(opts, CFGF_NONE); assert(cfg); if (cfg_parse(cfg, file) == CFG_PARSE_ERROR) return 1; /** * add new sort names to the sorts trie */ N = cfg_size(cfg, "sorts"); for (i = 0; i < N; i++) { sortname = cfg_getnstr(cfg, "sorts", i); found = cp_trie_exact_match(vars->sorts, sortname); if (!found) { sortname = strdup(sortname); cp_trie_add(vars->sorts, sortname, sortname); vars->sorts_N += 1; } } /** * the the operations to the operation list */ N = cfg_size(cfg, "op"); for (i = 0; i < N; i++) { cfg_t *op = cfg_getnsec(cfg, "op", i); opname = strdup(cfg_title(op)); cp_vector_add_element(vars->op_names, opname); sortname = cfg_getstr(op,"out"); assert(sortname); found = cp_trie_exact_match(vars->sorts,sortname); if (! found) { fputs("Operation ",stderr); fputs(opname,stderr); fputs(" uses undeclared output sort ",stderr); fputs(sortname,stderr); fputs("\n",stderr); /** fix it */ sortname = strdup(sortname); cp_trie_add(vars->sorts, sortname, sortname); vars->sorts_N += 1; found = sortname; } ops->id = vars->ops_N; ops->out = found; ops->in_N = cfg_size(op, "in"); assert(ops->in_N <= MAX_OP_INPUTS); for (j=0;j<ops->in_N; ++j) { sortname = cfg_getnstr(op,"in",j); assert(sortname); found = cp_trie_exact_match(vars->sorts,sortname); if (! found) { fputs("Operation ",stderr); fputs(opname,stderr); fputs(" uses undeclared input sort ",stderr); fputs(sortname,stderr); fputs("\n",stderr); /** fix it */ sortname = strdup(sortname); cp_trie_add(vars->sorts, sortname, sortname); vars->sorts_N += 1; found = sortname; } ops->in[j] = found; } err = 0; cp_multimap_insert(vars->ops, ops, &err); if (err) { fputs("Operation ",stderr); fputs(opname,stderr); fputs(": cannot add to multimap ",stderr); fprintf(stderr,"%d",err); fputs("\n",stderr); } vars->ops_N += 1; } cfg_free(cfg); free(ops); return 0; }