/* Interface. */ int atms_add_node(atms tms, const signed char assumption, const signed char contradiction) { atms_node node = (atms_node)malloc(sizeof(struct str_atms_node)); if (NULL == node) { return -1; } memset(node, 0, sizeof(struct str_atms_node)); node->index = tms->nodes->sz; node->assumption = assumption; node->contradiction = contradiction; node->consequences = array_new(NULL, NULL); node->label = trie_new((trie_node_destroy_func_t)array_free, (trie_node_clone_func_t)array_copy); array_append(tms->nodes, node); if (assumption) { /* The label of this node is a singleton environment with this node only. */ array env = array_append(array_new(NULL, NULL), node); array key = get_environment_key(env); trie_add(node->label, key, env); array_free(key); } return node->index - 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* parse_Towns(FILE* f) { Trie* my_towns = trie_new(); if(f != NULL) { rewind(f); char* line = NULL; size_t n; while(getline(&line, &n, f) != -1) { strtok(line, ":"); char* split = strtok(NULL, ":"); if(split == NULL) { trie_free(my_towns); return NULL; } split[-1] = '\0'; /* Élimination du ':' */ float x, y; int found; found = sscanf(split, " %f; %f!", &x, &y); if(found != 2) { trie_free(my_towns); return NULL; } my_towns = trie_addTown(my_towns, line, x, y); } free(line); } return my_towns; }
falcon_cache_t *falcon_cache_new(void) { falcon_cache_t *cache = g_new0(falcon_cache_t, 1); cache->lock = g_mutex_new(); cache->objects = trie_new(G_DIR_SEPARATOR_S, 1); return cache; }
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; }
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; }
/* * call-seq: * read(filename_base) -> Trie * * Returns a new trie with data as read from disk. */ static VALUE rb_trie_read(VALUE self, VALUE filename_base) { VALUE da_filename = rb_str_dup(filename_base); VALUE tail_filename = rb_str_dup(filename_base); rb_str_concat(da_filename, rb_str_new2(".da")); rb_str_concat(tail_filename, rb_str_new2(".tail")); StringValue(tail_filename); StringValue(da_filename); Trie *trie = trie_new(); VALUE obj; obj = Data_Wrap_Struct(self, 0, trie_free, trie); DArray *old_da = trie->da; Tail *old_tail = trie->tail; FILE *da_file = fopen(RSTRING_PTR(da_filename), "r"); if (da_file == NULL) raise_ioerror("Error reading .da file."); trie->da = da_read(da_file); fclose(da_file); FILE *tail_file = fopen(RSTRING_PTR(tail_filename), "r"); if (tail_file == NULL) raise_ioerror("Error reading .tail file."); trie->tail = tail_read(tail_file); fclose(tail_file); da_free(old_da); tail_free(old_tail); return obj; }
bool address_dictionary_init(void) { if (address_dict != NULL) return false; address_dict = calloc(1, sizeof(address_dictionary_t)); if (address_dict == NULL) return false; address_dict->canonical = cstring_array_new(); if (address_dict->canonical == NULL) { goto exit_destroy_address_dict; } address_dict->values = address_expansion_value_array_new(); if (address_dict->values == NULL) { goto exit_destroy_address_dict; } address_dict->trie = trie_new(); if (address_dict->trie == NULL) { goto exit_destroy_address_dict; } return true; exit_destroy_address_dict: address_dictionary_destroy(address_dict); address_dict = NULL; return false; }
void ide_source_snippets_clear (IdeSourceSnippets *snippets) { g_return_if_fail (IDE_IS_SOURCE_SNIPPETS (snippets)); trie_destroy (snippets->snippets); snippets->snippets = trie_new (g_object_unref); }
trie_t token_find_bow(parse_context_t pda, char* name) { trie_t ret = (trie_t) hash_find(&pda->bows, name); if(!ret) { ret = trie_new(); hash_addelem(&pda->bows, name, ret); } return ret; }
struct trie *trie_create() { struct trie *t; t = trie_new( "", NULL ); assert( t ); t->root = 1; return t; }
int main (void) { AlphaMap *alpha_map; Trie *test_trie; AlphaChar key[3]; TrieData data; msg_step ("Preparing alpha map"); alpha_map = alpha_map_new (); if (!alpha_map) { printf ("Fail to allocate alpha map\n"); goto err_alpha_map_not_created; } if (alpha_map_add_range (alpha_map, 0x00, 0xff) != 0) { printf ("Fail to add full alpha map range\n"); goto err_alpha_map_created; } msg_step ("Preparing trie"); test_trie = trie_new (alpha_map); alpha_map_free (alpha_map); if (!test_trie) { printf ("Fail to create test trie\n"); goto err_alpha_map_created; } msg_step ("Storing key to test trie"); key[0] = 0xff; key[1] = 0xff; key[2] = 0; if (!trie_store (test_trie, key, TEST_DATA)) { printf ("Fail to store key to test trie\n"); goto err_trie_created; } msg_step ("Retrieving data from test trie"); if (!trie_retrieve (test_trie, key, &data)) { printf ("Fail to retrieve key from test trie\n"); goto err_trie_created; } if (TEST_DATA != data) { printf ("Retrieved data = %d, not %d\n", data, TEST_DATA); goto err_trie_created; } msg_step ("Freeing test trie"); trie_free (test_trie); return 0; err_trie_created: trie_free (test_trie); err_alpha_map_created: alpha_map_free (alpha_map); err_alpha_map_not_created: return 1; }
void falcon_cache_clear(falcon_cache_t *cache) { g_return_if_fail(cache); g_mutex_lock(cache->lock); trie_free(cache->objects, (trie_free_func)falcon_object_free); cache->objects = trie_new(G_DIR_SEPARATOR_S, 1); cache->count = 0; g_mutex_unlock(cache->lock); }
/* Creates a new child for the specified trie node. 'prefix' is copied to newly allocated memory. */ static Trie* trie_new_child( Trie *t, unsigned char *prefix ) { int i = _char_to_index( prefix[0] ); if ( i < 0 ) { return NULL; } _trie_alloc_children_array( t ); if ( !t->children[i] ) { t->children[i] = trie_new( prefix, NULL ); // assert t->children[i]->prefix != prefix } return t->children[i]; }
/** Przygotowsuje środowisko testowe @param state Środowisko testowe. @return 0 jeśli się udało, -1 w p.p. */ static int trie_setup(void **state) { Trie *trie = trie_new(); size_t n_words = 3; wchar_t *words[] = {L"wątły", L"wątlejszy", L"łódka"}; for (size_t i = 0; i < n_words; i++) trie_insert_word(trie, words[i]); *state = trie; 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); }
/* Generates a trie containing a mapping from deligatured words back to their "real" worlds. Returns 1 on success, otherwise 0. */ static Trie* find_ligatures( char* dictionary_file ) { FILE *words_file = fopen( dictionary_file, "r" ); if ( words_file == NULL ) { return 0; } char *buf = NULL; size_t n_read, buf_size = 0; Trie *trie = trie_new( "", NULL ); while ( ( n_read = getline( &buf, &buf_size, words_file ) ) != -1 ) { if ( buf[n_read - 1] == '\n' ) --n_read; buf[n_read] = 0; add_deligatured_word( buf, trie ); /* For non-proper names (i.e., normal words) we want to add to our trie the deligaturization of the *capitalized* version of the word. Consider, for example, that "fluffy" and "Fluffy" have different deligaturizations: "uy" and "Fluy", respectively. */ if ( islower( buf[0] ) ) { buf[0] = toupper( buf[0] ); // now find ligatures again, and add it if appropriate add_deligatured_word( buf, trie ); } } free( buf ); fclose( words_file ); printf( "Trie size=%d\n", trie_size( trie ) ); /* trie_display( trie ); */ /* printf( "Trie size=%d\n", trie_size( words ) ); */ /* trie_display( words ); */ /* printf( "aunt=%d\n", trie_contains( words, "aunt" ) ); */ /* char test_buf[100]; */ /* strcpy( test_buf, "eect" ); */ /* religature_word( test_buf, sizeof( test_buf ) ); */ /* strcpy( test_buf, "Eect" ); */ /* religature_word( test_buf, sizeof( test_buf ) ); */ /* strcpy( test_buf, "uy" ); // fluffy */ /* religature_word( test_buf, sizeof( test_buf ) ); */ /* strcpy( test_buf, "Fluy" ); // Fluffy */ /* religature_word( test_buf, sizeof( test_buf ) ); */ /* strcpy( test_buf, "Flanagan" ); */ /* religature_word( test_buf, sizeof( test_buf ) ); */ /* strcpy( test_buf, "anagan" ); // no such word! (flanagan) */ /* religature_word( test_buf, sizeof( test_buf ) ); */ return trie; }
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; }
trie_node *trie_insert(trie_node *root, trie_pair *entry) { int i; trie_node *saveroot; if (!root) root = trie_new(); saveroot = root; for (i = 0; i < TRIE_MAXLENGTH; i++) { if (entry->key[i] == '\0') break; else { if (!root->branch[entry->key[i]-'a']) { /* make a new node on root for key */ root->branch[entry->key[i]-'a'] = trie_new(); // way cool!! } //printf("insert[%d] = %c\n", i, entry->key[i]); root = root->branch[entry->key[i]-'a']; /* move down appropriate branch */ } } /* at this point, we have tested for all characters of key */ if (root->ref != NULL) printf("warning: tried to insert a duplicate key...%s\n", entry->key); else root->ref = entry; return saveroot; }
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; }
static trie_t *create_trie_from_file(const char *file) { trie_t *db; file_map_t map; const char *p, *end; char line[BUFSIZ]; if (!file_map_open(&map, file, false)) { return NULL; } p = map.map; end = map.end; while (end > p && end[-1] != '\n') { --end; } if (end != map.end) { warn("file %s miss a final \\n, ignoring last line", file); } db = trie_new(); while (p < end && p != NULL) { const char *eol = (char *)memchr(p, '\n', end - p); if (eol == NULL) { eol = end; } if (eol - p > BUFSIZ) { p = eol - BUFSIZ; } int i = 0; if (*p != '#' && p != eol) { #if 1 for (const char *s = eol - 1 ; s >= p ; --s) { line[i++] = ascii_tolower(*s); } #else memcpy(line, p, eol - p); i = eol - p; #endif line[i] = '\0'; trie_insert(db, line); } p = eol + 1; } file_map_close(&map); trie_compile(db, false); return db; }
/** Testuje wstawianie do drzewa. @param state Środowisko testowe. */ static void trie_insert_word_test(void** state) { Trie *trie = trie_new(); size_t n_words = 4; wchar_t *words[] = {L"wątły", L"wątły", L"wątlejszy", L"łódka"}; for (size_t i = 0; i < n_words; i++) { if (i == 1) assert_false(trie_insert_word(trie, words[i])); else assert_true(trie_insert_word(trie, words[i])); } for (size_t i = 0; i < n_words; i++) assert_true(trie_has_word(trie, words[i])); trie_done(trie); }
Trie *generate_binary_trie(void) { Trie *trie; trie = trie_new(); /* Insert some values */ assert(trie_insert_binary(trie, bin_key2, sizeof(bin_key2), "goodbye world") != 0); assert(trie_insert_binary(trie, bin_key, sizeof(bin_key), "hello world") != 0); return trie; }
/* Initialize our global 'deligatured_words' trie by loading the pre-generated ligatures from a file; see generate_ligatures(). */ int load_ligatures( char *ligature_file ) { FILE *ligatures_fp = fopen( ligature_file, "r" ); if ( !ligatures_fp ) { return 0; } deligatured_words = trie_new( 0, (void*) strdup( "" ) ); unsigned char buf[MAX_WORD_LEN * 2 + 2]; while ( fgets( buf, MAX_WORD_LEN * 2 + 2, ligatures_fp ) ) { unsigned char *deligatured_word = buf; char *delimiter = index( buf, '\t' ); delimiter[0] = 0; unsigned char *real_word = (unsigned char*) strdup( delimiter + 1 ); real_word[strlen( real_word ) - 1] = 0; // chomp newline trie_add( deligatured_words, deligatured_word, real_word ); } fclose( ligatures_fp ); return 1; }
ATTrie :: ATTrie() : data(NULL) { nodes=database_info.trie_nodes; data = new ATTrieData; data->amap = alpha_map_new(); if (data->amap == NULL) throw "AlphaMapConstructionException"; // add all characters except 0 and FF FF FF FF // AlphaChar is uint32 // this needs to change later to allow only Arabic and Latin alpha_map_add_range(data->amap, 1, 0xFFFFFFFE); data->trie = trie_new(data->amap); if (data->trie == NULL){ alpha_map_free(data->amap); throw "TrieConstructionException"; } }
ATTrie :: ATTrie(const char * path) : data(NULL) { nodes=database_info.trie_nodes; data = new ATTrieData; #if 1 //added by Jad due to an error occuring in desctructor when trie is loaded from file data->amap = alpha_map_new(); if (data->amap == NULL) throw "AlphaMapConstructionException"; // add all characters except 0 and FF FF FF FF // AlphaChar is uint32 // this needs to change later to allow only Arabic and Latin alpha_map_add_range(data->amap, 1, 0xFFFFFFFE); data->trie = trie_new(data->amap); #endif data->trie = trie_new_from_file(path); if (data->trie == NULL) throw "TrieConstructionException"; }
void test_trie_insert_empty(void) { Trie *trie; char buf[10]; trie = trie_new(); /* Test insert on empty string */ assert(trie_insert(trie, "", buf) != 0); assert(trie_num_entries(trie) != 0); assert(trie_lookup(trie, "") == buf); assert(trie_remove(trie, "") != 0); assert(trie_num_entries(trie) == 0); trie_free(trie); }
static void test_trie_free_long(void) { char *long_string; Trie *trie; /* Generate a long string */ long_string = malloc(LONG_STRING_LEN); memset(long_string, 'A', LONG_STRING_LEN); long_string[LONG_STRING_LEN - 1] = '\0'; /* Create a trie and add the string */ trie = trie_new(); trie_insert(trie, long_string, long_string); trie_free(trie); free(long_string); }
static void test_trie_negative_keys(void) { char my_key[] = { 'a', 'b', 'c', -50, -20, '\0' }; Trie *trie; void *value; trie = trie_new(); assert(trie_insert(trie, my_key, "hello world") != 0); value = trie_lookup(trie, my_key); assert(!strcmp(value, "hello world")); assert(trie_remove(trie, my_key) != 0); assert(trie_remove(trie, my_key) == 0); assert(trie_lookup(trie, my_key) == NULL); 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, "HTTP/1.", "HTTP/1."); trie_insert(trie, "HTTP/1.1 ", "HTTP/1.1 "); trie_insert(trie, "HTTP/1.0 ", "HTTP/1.0 "); trie_insert(trie, "HTTP/1.1 20", "HTTP/1.1 20"); trie_insert(trie, "HTTP/1.0 20", "HTTP/1.0 20"); trie_insert(trie, "HTTP/1.0 30", "HTTP/1.0 30"); trie_insert(trie, "HTTP/1.1 30", "HTTP/1.1 30"); trie_insert(trie, "HTTP/1.1 40", "HTTP/1.1 40"); merge_common_prefix_t *mcp = (merge_common_prefix_t *) malloc (sizeof(merge_common_prefix_t)); mcp->alpha_merge = 5; mcp->flag = 0; mcp->merge_substring = NULL; trie_dfs(trie, str_callback, NULL); printf("-------------------------\n"); trie_dfs(trie, trie_node_merge_callback, mcp); trie_dfs(trie, str_callback, NULL); return 0; }