int main() { int exists_string = 0; Trie root = NULL; root = trie_init(); trie_insert(root, "daijinwei"); trie_insert(root, "hello"); trie_insert(root, "wangsan"); trie_insert(root, "ccnp"); trie_insert(root, "linux"); exists_string = trie_search(root,"daijinwei"); if(0 != exists_string){ fprintf(stdout, "The string is exists the trie\n"); }else{ fprintf(stdout, "The string is not exists the trie\n"); } exists_string = trie_search(root,"daijinw"); if(0 != exists_string){ fprintf(stdout, "The string is exists the trie\n"); }else{ fprintf(stdout, "The string is not exists the trie\n"); } trie_destroy(root); }
void test_trie_insert(void) { Trie *trie; unsigned int entries; size_t allocated; trie = generate_trie(); /* Test insert of NULL value has no effect */ entries = trie_num_entries(trie); assert(trie_insert(trie, "hello world", NULL) == 0); assert(trie_num_entries(trie) == entries); /* Test out of memory scenario */ allocated = alloc_test_get_allocated(); alloc_test_set_limit(0); assert(trie_insert(trie, "a", "test value") == 0); assert(trie_num_entries(trie) == entries); /* Test rollback */ alloc_test_set_limit(5); assert(trie_insert(trie, "hello world", "test value") == 0); assert(alloc_test_get_allocated() == allocated); assert(trie_num_entries(trie) == entries); trie_free(trie); }
int main(int argc, char** argv) { /* Trie *trie = generate_trie(); lookup_trie(trie); */ Trie * trie = trie_new(); /* trie_insert(trie, "aa", "aa"); trie_insert(trie, "ab", "ab"); trie_insert(trie, "ac", "ac"); trie_insert(trie, "ba", "ba"); trie_insert(trie, "bb", "bb"); trie_insert(trie, "bc", "bc"); */ trie_insert(trie, "welcom", "welcom"); trie_insert(trie, "welcome", "welcome"); trie_insert(trie, "elcome", "elcome"); printf("-------------------------\n"); void *extension = NULL; trie_dfs(trie, str_callback, extension); return 0; }
/* internal function to resize the db and add a book */ int add_book(Book* A) { long size = (db.numberOfBooks + 1) * sizeof(Book); char *name; char *title; /* the new array */ Book* D; /* make it bigger */ D = realloc(db.arr, size); if (D) { db.arr = D; /* now add the book at the end */ db.arr[db.numberOfBooks] = *A; db.numberOfBooks++; /* trees */ title = db.arr[db.numberOfBooks].title; name = db.arr[db.numberOfBooks].authors[0].last; avl = avl_insert(&db.arr[db.numberOfBooks], avl); trie_title = trie_insert(title, &db.arr[db.numberOfBooks], trie_title); trie_name = trie_insert(name, &db.arr[db.numberOfBooks], trie_name); /* finally */ sorted = 0; free(A); return 0; } else return -1; }
int main() { trie_node *root = NULL; trie_pair *entry; root = trie_insert(root, pair_new("dog\0", 2)); root = trie_insert(root, pair_new("cat\0", 3)); root = trie_insert(root, pair_new("top\0", 4)); root = trie_insert(root, pair_new("rat\0", 5)); trie_show(root, 0); entry = trie_search(root, "cat"); trie_delete(root, "dog"); entry = trie_search(root, "dog"); }
int main(void) { static struct word word1 = { .data = (uint8_t *)"aaa", .size = 4, .value = (void *)0xA}; static struct word word2 = { .data = (uint8_t *)"aba", .size = 4, .value = (void *)0xB}; struct trie *obj = trie_new(NULL, dealloc); assert(obj); void *data; bool ret; ret = trie_insert(obj, word1.data, word1.size, word1.value, &data); assert(ret); assert(data == NULL); // check the first insertion ret = trie_at(obj, word1.data, word1.size, &data); assert(ret); assert(data == word1.value); ret = trie_insert(obj, word2.data, word2.size, word2.value, &data); assert(ret); assert(data == NULL); // check all after all insertions ret = trie_at(obj, word1.data, word1.size, &data); assert(ret); assert(data == word1.value); ret = trie_at(obj, word2.data, word2.size, &data); assert(ret); assert(data == word2.value); for (struct trie_node *i = trie_begin(obj); i; i = trie_next_delete(obj, i)) { char *str = NULL; if (trie_data(i, (void *)&str)) printf("trie next: 0x%x\n", (unsigned int)str); else printf("trie next failed!\n"); } trie_delete(&obj); assert(obj == NULL); return 0; }
int main(void) { trie *t; t = new_trie(); printf("Added %s %d\n","texta",trie_insert(t,"texta")); printf("Added %s %d\n","textb",trie_insert(t,"textb")); printf("Added %s %d\n","texta",trie_insert(t,"texta")); printf("Searched %s %d\n","texta",trie_search(t,"texta")); printf("Searched %s %d\n","textc",trie_search(t,"textc")); free_trie(t); return 0; }
static void addpat(const char *pat) { char letter[TOKLEN + 1] = {0}, *pletter = letter; uint8_t wght[TOKLEN + 1] = {0}, nwght = 0; for (const char *s = pat; *s; ++s) if ('0' <= *s && *s <= '9') wght[nwght] = *s - '0'; else { *pletter++ = *s; ++nwght; } void *tofree = trie_insert( pattern, letter, memcpy( (uint8_t *)malloc(sizeof wght), wght, sizeof wght ) ); assert(!tofree); if (tofree) free(tofree); }
int main(int argc, char **argv) { TrieNode *root = new_trie(); trie_insert(root, "/foo/bar", "baz"); trie_insert(root, "/foo/blubb", "bla"); trie_insert(root, "/asdf/dassf/fdas", "blubb"); trie_insert(root, "/asdd/f/dsa/s", "fasd"); //dump_tree(root); iterate_trie(root, print_line, NULL); char *result = (char*)trie_search(root, "/asdf/dassf/fdas"); fprintf(stderr, "RESULT: %s\n", result); return 0; }
void trie_insert(Trie * node, char *tema) { //a palavra nao contem letras ou o no e invalido assert(tema != NULL && *tema != '\0' && node != NULL && POS(tema) >= 0); if(node->table[POS(tema)] == NULL ) { //criar um novo node para a letra actual node->table[POS(tema)] = trie_newnode(); //posicao seguinte e o fim da palavra, parar o avanço e marcar o //fim da palavra. if(*(tema+1) == '\0') { node->table[POS(tema)]->word = 1; return; } } else { if(*(tema+1) == '\0') { node->table[POS(tema)]->word = 1; return; } } //letra seguinte trie_insert(node->table[POS(tema)], tema+1); }
Trie *generate_trie(void) { Trie *trie; int i; unsigned int entries; /* Create a trie and fill it with a large number of values */ trie = trie_new(); entries = 0; for(i = 0; i < NUM_TEST_VALUES; ++i) { test_array[i] = i; sprintf(test_strings[i], "%i", i); trie_insert(trie, test_strings[i], &test_array[i]); ++entries; assert(trie_num_entries(trie) == entries); } return trie; }
Trie *generate_trie(void) { Trie *trie; int i; unsigned int entries; /* Create a trie and fill it with a large number of values */ trie = trie_new(); entries = 0; for (i=0; i<NUM_TEST_VALUES; ++i) { /* Create a string containing a text version of i, and use * it as a key for the value */ test_array[i] = i; sprintf(test_strings[i], "%i", i); assert(trie_insert(trie, test_strings[i], &test_array[i]) != 0); ++entries; assert(trie_num_entries(trie) == entries); } return trie; }
static void enter_phones() { int i; char *s; for (i = 1; (s = ph_name[i]); i++) trie_insert(&phones, s, (void *) i); }
int build_trie() { int i; for (i = 0; i < global_srecord_count; ++i) { trie_insert(global_srecord[i].sorder, global_srecord[i].chareter); } return 0; }
int main() { char str_hash[MAX_LEN]; int n; scanf ("%d", &n); getchar(); int i; for (i = 0; i < n; i++) gets (words[i]); // for (i = 0; i < n; i++) // quick_sort (words[i], 0, strlen (words[i]) - 1); struct trie_node* root = create_trie_node (); for (i = 0; i < n; i++) { strcpy (str_hash, words[i]); quick_sort (str_hash, 0, strlen (str_hash) - 1); trie_insert (root, str_hash, i); } int q; scanf ("%d", &q); getchar(); for (i = 0; i < q; i++) { char query[MAX_LEN]; gets (query); strcpy (str_hash, query); quick_sort (str_hash, 0, strlen (str_hash) - 1); struct trie_node* node = trie_search (root, str_hash); if (node == NULL) { answer[i] = 0; } else { // printf ("p->size: %d\n", node->size); answer[i] = node->size; int j; for (j = 0; j < node->size; j++) { // printf ("ori: %s\n", words[node->index[j]]); // printf ("current: %s\n", query); if (strcmp (words[node->index[j]], query) == 0) answer[i] -= 1; } } } for (i = 0; i < q; i++) printf ("%d\n", answer[i]); return 0; }
int main(int argc, char* argv[]) { int *a, *b, *c, *d; a = malloc(sizeof(int)); *a = 1; b = malloc(sizeof(int)); *b = 2; c = malloc(sizeof(int)); *c = 3; d = malloc(sizeof(int)); *d = 4; struct trie* test_trie = trie_create(); printf("created new trie at %p\n", test_trie); void* inserted = trie_insert(test_trie, "aa", a); printf("inserted %d at %p\n", *a, inserted); int *trie_value = trie_lookup(test_trie, "aa"); printf("lookup got %d\n", *trie_value); inserted = trie_insert(test_trie, "ab", b); printf("inserted %d at %p\n", *a, inserted); trie_value = trie_lookup(test_trie, "ab"); printf("lookup got %d\n", *trie_value); inserted = trie_insert(test_trie, "ccz", c); printf("inserted %d at %p\n", *a, inserted); trie_value = trie_lookup(test_trie, "ccz"); printf("lookup got %d\n", *trie_value); trie_value = trie_lookup_prefix(test_trie, "c", NULL); printf("prefix lookup got %d\n", *trie_value); trie_value = trie_lookup_prefix(test_trie, "a", d); printf("ambiguous prefix lookup got %d\n", *trie_value); trie_destroy(test_trie); printf("freed trie\n"); return 0; }
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; }
/* insert word if it doesn't exists, returns word counter */ int trie_insert(trie * t, char *s) { if(*s) { if(t->next[*s-'a'] == NULL) t->next[*s-'a'] = new_trie(); return trie_insert(t->next[*s-'a'], s+1); } return ++t->val; }
/** \fn memo_xsl **/ inline xsp_t memo_xsl(Trie* trie, unsigned char *xslfile){ debug("xsp_t memo_xsl(Trie* trie, unsigned char *xslfile)"); xsp_t xsl = (xsp_t)trie_lookup(trie, (char*)xslfile); if(NULL == xsl) { xsl = parse_xslt((const xc_t*) xslfile); trie_insert(trie, (char*)xslfile, xsl); } return xsl; }
/* Inserts a word to a dictionary and returns true if query is correct * and word doesn't exist in a dictionary; otherwise query is ignored and * function returns false. */ bool insert(char *params) { bool query_result = false; char *word = calloc(LINE_LENGTH, sizeof(char)); int insert_result = -1; if (get_word_from_string(¶ms, word) && is_string_blank(¶ms) && (insert_result = trie_insert(word)) != -1) { printf("word number: %d\n", insert_result); query_result = true; } free(word); return query_result; }
/* Insert a string, s, in a trie, t, and return the updated trie */ trie trie_insert(trie t, string s) { if(t == NULL) t = trie_init(); if(s[0] == '\0') /* We've found the end */ t->number++; else t->next[char2index(s[0])] = trie_insert(t->next[char2index(s[0])], s+1); return t; }
void ide_source_snippets_add (IdeSourceSnippets *snippets, IdeSourceSnippet *snippet) { const gchar *trigger; g_return_if_fail (IDE_IS_SOURCE_SNIPPETS (snippets)); g_return_if_fail (IDE_IS_SOURCE_SNIPPET (snippet)); trigger = ide_source_snippet_get_trigger (snippet); trie_insert (snippets->snippets, trigger, g_object_ref (snippet)); }
int main() { scanf("%d", &n); for (i=0; i<n; ++i) scanf("%d", &arr[i]); init(); trie_insert(arr[0]); maxor = 0; for (i=1; i<n; ++i) { x = trie_max_xor(arr[i]); if (maxor < x) { maxor = x; } trie_insert(arr[i]); } /* printf("trie : "); for (i=0; i<trie_size; ++i) printf("[%d %d] ", trie[i].lef, trie[i].rit); printf("\n\n"); */ printf("%d\n", maxor); return 0; }
void test_trie_new_free(void) { Trie *trie; /* Allocate and free an empty trie */ trie = trie_new(); assert(trie != NULL); trie_free(trie); /* Add some values before freeing */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_insert(trie, "hell", "testing") != 0); assert(trie_insert(trie, "testing", "testing") != 0); assert(trie_insert(trie, "", "asfasf") != 0); trie_free(trie); /* Add a value, remove it and then free */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_remove(trie, "hello") != 0); trie_free(trie); /* Test out of memory scenario */ alloc_test_set_limit(0); trie = trie_new(); assert(trie == NULL); }
int mime_table_init() { mime_trie = trie_new(); /* now load the table with mime types */ mime_pair *it = &mime_data[0]; while(it->key != NULL && it->value != NULL) { if(trie_insert(mime_trie, it->key, (void*)it->value) != 0) { return -1; } it++; } return 0; }
int trie_find_or_insert(trie_t *trie, char *key, void *value, size_t vsize, void **prev_value, size_t *prev_vsize, int copy) { trie_node_t *node = trie_find_internal(trie, key); if (!node) return trie_insert(trie, key, value, vsize, copy); if (prev_value) *prev_value = node->value; if (prev_vsize) *prev_vsize = node->vsize; return 0; }
void archive(struct file_info_t *info, struct file_entry_t *entry) { Set *hash_set = trie_lookup(info->hash_trie, entry->hash); if (hash_set == TRIE_NULL) { /* Otherwise, the value needs a new list */ hash_set = set_new(&pointer_hash, &pointer_equal); slist_prepend(&info->duplicates, hash_set); trie_insert(info->hash_trie, entry->hash, hash_set); } if (!set_insert(hash_set, entry)) { #ifndef NDEBUG fprintf(stderr, "[DEBUG] '%s' (extra file)\n", entry->path); #endif } }
static gboolean copy_into (Trie *trie, const gchar *key, gpointer value, gpointer user_data) { IdeSourceSnippet *snippet = value; Trie *dest = user_data; g_assert (dest); g_assert (IDE_IS_SOURCE_SNIPPET (snippet)); trie_insert (dest, key, g_object_ref (snippet)); return FALSE; }
int trie_find_and_insert(trie_t *trie, char *key, void *value, size_t vsize, void **prev_value, size_t *prev_vsize, int copy) { trie_node_t *node = trie_find_internal(trie, key); if (node) { if (prev_value) *prev_value = node->value; if (prev_vsize) *prev_vsize = node->vsize; trie_node_set_value(trie, node, value, vsize, copy); return 0; } return trie_insert(trie, key, value, vsize, copy); }
bvm_cache *bvm_new(bvm_cache *this_bvm){ // bvm_new# #ifdef BABEL_RESET_TRACE _trace; #endif mword *self = tptr_detag(this_bvm, tptr_detag(this_bvm, this_bvm->self)); lci(bvm_dstack_ptr(this_bvm),0) = stack_new(this_bvm); #define Y(a,b,c) \ if(!_exha(this_bvm, self, c)){ \ _insha( this_bvm, \ self, \ c, \ nil, \ hash_new_entry( \ this_bvm, \ c, \ nil, \ nil)); \ } #define X(a,b,c) \ if(!trie_exists(this_bvm, self, c, nil)){ \ trie_insert(this_bvm, self, c, nil, nil); \ } CACHED_FIELDS #undef X mword *local_root = _ith( this_bvm, trie_lookup_hash(this_bvm, self, BABEL_SYM_LOCAL_ROOT, nil), 2 ); if(is_nil(local_root)){ trie_insert(this_bvm, self, BABEL_SYM_LOCAL_ROOT, nil, _ith( this_bvm, trie_lookup_hash(this_bvm, self, BABEL_SYM_SOFT_ROOT, nil), 2)); } cache_update(this_bvm); this_bvm->flags->BVM_CACHE_DIRTY = FLAG_CLR; this_bvm->flags->BVM_CACHE_INVALID = FLAG_CLR; return this_bvm; }