Ejemplo n.º 1
0
/**
 * trie_destroy_node:
 * @trie: A #Trie.
 * @node: A #TrieNode.
 * @value_destroy: A #GDestroyNotify or %NULL.
 *
 * Removes @node from the #Trie and releases all memory associated with it.
 * If the nodes value is set, @value_destroy will be called to release it.
 *
 * The reclaimation happens as such:
 *
 * 1) the node is unlinked from its parent.
 * 2) each of the children are destroyed, leaving us an empty chain.
 * 3) each of the allocated chain links are freed.
 * 4) the value pointer is freed.
 * 5) the structure itself is freed.
 */
static void
trie_destroy_node (Trie           *trie,
                   TrieNode       *node,
                   GDestroyNotify  value_destroy)
{
   TrieNodeChunk *iter;
   TrieNodeChunk *tmp;

   g_assert(node);

   trie_node_unlink(node);

   while (node->chunk.count) {
      trie_destroy_node(trie, node->chunk.children[0], value_destroy);
   }

   for (iter = node->chunk.next; iter;) {
      tmp = iter;
      iter = iter->next;
      trie_free(trie, tmp);
   }

   if (node->value && value_destroy) {
      value_destroy(node->value);
   }

   trie_free(trie, node);
}
Ejemplo n.º 2
0
int build_graph(const char *depfile, struct node_list **headp)
{
    FILE *f;
    int err;
    unsigned int nnodes = 0;
    struct trie nodes;

    trie_init(&nodes);
    f = fopen(depfile, "r");
    if (!f) {
	perror("failed to open dependency file");
	return -1;
    }
    err = read_depfile(f, &nodes, &nnodes);
    fclose(f);
    if (err) {
	trie_free(&nodes);
	return -1;
    }

    err = trie_iter(&nodes, add_to_list_from_trie, headp);
    if (err) {
	free_node_list(*headp);
	nnodes = -1;
    }
    trie_free(&nodes);

    return nnodes;
}
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
0
void
trie_free(trie_t trie)
{
	if(trie != NULL_TRIE)
	{
		trie_free(trie->left);
		trie_free(trie->right);
		trie_free(trie->children);
		free(trie);
	}
}
Ejemplo n.º 6
0
int
main (void)
{
    Trie             *test_trie;
    DictRec          *dict_p;
    const AlphaChar **nonalpha_key;
    TrieData          trie_data;
    Bool              is_fail;

    msg_step ("Preparing trie");
    test_trie = en_trie_new ();
    if (!test_trie) {
        fprintf (stderr, "Fail to create test trie\n");
        goto err_trie_not_created;
    }

    /* store */
    msg_step ("Adding data to trie");
    for (dict_p = dict_src; dict_p->key; dict_p++) {
        if (!trie_store (test_trie, dict_p->key, dict_p->data)) {
            printf ("Failed to add key '%ls', data %d.\n",
                    (wchar_t *)dict_p->key, dict_p->data);
            goto err_trie_created;
        }
    }

    /* test storing keys with non-alphabet chars */
    is_fail = FALSE;
    for (nonalpha_key = nonalpha_src; *nonalpha_key; nonalpha_key++) {
        if (trie_retrieve (test_trie, *nonalpha_key, &trie_data)) {
            printf ("False duplication on key '%ls', with existing data %d.\n",
                    (wchar_t *)*nonalpha_key, trie_data);
            is_fail = TRUE;
        }
        if (trie_store (test_trie, *nonalpha_key, TRIE_DATA_UNREAD)) {
            printf ("Wrongly added key '%ls' containing non-alphanet char\n",
                    (wchar_t *)*nonalpha_key);
            is_fail = TRUE;
        }
    }

    if (is_fail)
        goto err_trie_created;

    trie_free (test_trie);
    return 0;

err_trie_created:
    trie_free (test_trie);
err_trie_not_created:
    return 1;
}
Ejemplo n.º 7
0
void test_trie_remove(void)
{
	Trie *trie;
	char buf[10];
	int i;
	unsigned int entries;

	trie = generate_trie();

	/* Test remove on non-existent values. */

	assert(trie_remove(trie, "000000000000000") == 0);
	assert(trie_remove(trie, "") == 0);

	entries = trie_num_entries(trie);

	assert(entries == NUM_TEST_VALUES);

	/* Remove all values */

	for (i=0; i<NUM_TEST_VALUES; ++i) {

		sprintf(buf, "%i", i);

		/* Remove value and check counter */

		assert(trie_remove(trie, buf) != 0);
		--entries;
		assert(trie_num_entries(trie) == entries);
	}

	trie_free(trie);
}
Ejemplo n.º 8
0
void test_trie_lookup(void)
{
	Trie *trie;
	char buf[10];
	int *val;
	int i;

	trie = generate_trie();

	/* Test lookup for non-existent values */

	assert(trie_lookup(trie, "000000000000000") == TRIE_NULL);
	assert(trie_lookup(trie, "") == TRIE_NULL);

	/* Look up all values */

	for (i=0; i<NUM_TEST_VALUES; ++i) {

		sprintf(buf, "%i", i);

		val = (int *) trie_lookup(trie, buf);
		
		assert(*val == i);
	}

	trie_free(trie);
}
Ejemplo n.º 9
0
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);
}
Ejemplo n.º 10
0
void
brk_maximal_on_unload ()
{
    if (brk_dict) {
        trie_free (brk_dict);
    }
}
Ejemplo n.º 11
0
void trie_free( Trie *t,
                void (*free_data_callback) (void*) ) {
  int i;
  if ( t == NULL ) {
    return;
  }
  if ( t->children ) {
    Trie *child;
    for ( i = 0; i < TRIE_ALPHABET_SIZE; ++i ) {
      if ( ( child = t->children[i] ) != 0 ) {
        trie_free( child, free_data_callback );
      }
    }
  }
  if ( t->prefix ) {
    free( t->prefix );
  }
  if ( t->children ) {
    free( t->children );
  }
  if ( t->data && free_data_callback ) {
    free_data_callback( t->data );
  }
  free( t );
}
Ejemplo n.º 12
0
void test_trie_insert_binary(void)
{
	Trie *trie;
	char *value;

	trie = generate_binary_trie();

	/* Overwrite a value */

	assert(trie_insert_binary(trie,
	                          bin_key, sizeof(bin_key),
	                          "hi world") != 0);

	/* Insert NULL value doesn't work */

	assert(trie_insert_binary(trie, bin_key3, sizeof(bin_key3), NULL) == 0);

	/* Read them back */

	value = trie_lookup_binary(trie, bin_key, sizeof(bin_key));
	assert(!strcmp(value, "hi world"));

	value = trie_lookup_binary(trie, bin_key2, sizeof(bin_key2));
	assert(!strcmp(value, "goodbye world"));

	trie_free(trie);
}
Ejemplo n.º 13
0
void test_trie_remove_binary(void)
{
	Trie *trie;
	void *value;

	trie = generate_binary_trie();

	/* Test look up and remove of invalid values */

	value = trie_lookup_binary(trie, bin_key3, sizeof(bin_key3));
	assert(value == NULL);

	assert(trie_remove_binary(trie, bin_key3, sizeof(bin_key3)) == 0);

	assert(trie_lookup_binary(trie, bin_key4, sizeof(bin_key4)) == 0);
	assert(trie_remove_binary(trie, bin_key4, sizeof(bin_key4)) == 0);

	/* Remove the two values */

	assert(trie_remove_binary(trie, bin_key2, sizeof(bin_key2)) != 0);
	assert(trie_lookup_binary(trie, bin_key2, sizeof(bin_key2)) == NULL);
	assert(trie_lookup_binary(trie, bin_key, sizeof(bin_key)) != NULL);

	assert(trie_remove_binary(trie, bin_key, sizeof(bin_key)) != 0);
	assert(trie_lookup_binary(trie, bin_key, sizeof(bin_key)) == NULL);

	trie_free(trie);
}
Ejemplo n.º 14
0
Archivo: cache.c Proyecto: nshi/falcon
void falcon_cache_free(falcon_cache_t *cache)
{
	g_return_if_fail(cache);

	trie_free(cache->objects, (trie_free_func)falcon_object_free);
	g_mutex_free(cache->lock);
	g_free(cache);
}
Ejemplo n.º 15
0
void trie_free(trie_t * top){
    for(int i = 0; i < 1<<STRIDE; i++){
        if(top->children[i] != NULL){
            trie_free(top->children[i]);
        }
    }
    free(top);
}
Ejemplo n.º 16
0
void trie_free(trie* t) {
    int i = 0;
    for (i = 0; i < t->count; i++) {
        trie* x = &(t->children[i]);
        trie_free(x);
    }
    free(t->children);
}
Ejemplo n.º 17
0
ATTrie :: ~ATTrie() 
{
    if (data->trie != NULL)
        trie_free(data->trie);
    if (data->amap != NULL)
        alpha_map_free(data->amap);
    delete data;
}
Ejemplo n.º 18
0
static void atms_node_free(atms_node node)
{
    array_free(node->consequences);

    trie_free(node->label);

    free(node);
}
Ejemplo n.º 19
0
static void propagate(atms tms, atms_justification J, atms_node a, trie I)
{
    trie L = weave(tms, a, I, J->antecedents);

    if (!trie_is_empty(L)) {
        update(tms, L, J->consequent);
    }
    trie_free(L);
}
Ejemplo n.º 20
0
void trie_free(trie_t *t) 
{
	int i;
    for (i = /*skip sentinel*/ 1; i < TRIE_SIZE; i++) {
        if (t->chars[i] != NULL) {
            trie_free(t->chars[i]);
        }
    }
    free(t);
}
Ejemplo n.º 21
0
Archivo: cache.c Proyecto: nshi/falcon
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);
}
Ejemplo n.º 22
0
int main(int argc, char **argv, char **env) {
    debug("int main(int argc, char **argv, char **env)");
    unsigned char buffer[PACKET_SIZE];
    #ifndef USE_GLOBAL
        Trie* trie = NULL
    #endif
    trie = trie_try_new(trie);
    for(;;) {
        /**
            Читаем сообщение из буфера,
            до тех пор пока можем их читать,
            и пока не получили неизвестную нам команду
        **/
        if (1 != read_cmd(buffer)){
            trie_free(trie);
            exit(1);
        }
        switch (*buffer) {
            case CMD_VERSION:
                /**
                    Запрос версии
                **/
                cmd_version();
                break;
            case CMD_APPLY_XSL:
                /**
                    Запрос на преобразование 
                **/
                cmd_apply_xsl(trie);
                break;
            default:
                /**
                    Неведомая фигня
                **/
                trie_free(trie);
                fprintf(stderr, "unknown command %c in adapter\n",
                        *buffer);
                exit(1);
        }
    }
}
Ejemplo n.º 23
0
Archivo: trie.c Proyecto: 10sun/DRL-AI
void
trie_free(trie_ptr * r, void (*func) (void *))
{
    trie_ptr p;
    while ((p = *r)) {
	trie_free(&p->more, func);
	*r = p->otherwise;
	if (func)
	    (*func) (p->value);
	free(p);
    }
}
Ejemplo n.º 24
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);
}
Ejemplo n.º 25
0
/* Generates a file containing pairs of deligatured & "real" words,
   one pair per line.  This file can then be used to efficiently load
   the ligatures into memory via the load_ligatures() function.
   Allows ligature generation to be performed as one-time
   preprocessing step. */
int generate_ligatures( char *dictionary_file,
                        char *ligature_file ) {
  Trie *trie;
  if ( trie = find_ligatures( dictionary_file ) ) {
    FILE *ligatures_fp = fopen( ligature_file, "w" );
    if ( ligatures_fp ) {
      trie_iterate_dfs( trie, 
                        write_ligature_callback,
                        ligatures_fp );
    }
    fclose( ligatures_fp );
  }
  trie_free( trie, _free_ligature_data_callback2 );
}
Ejemplo n.º 26
0
void trie_free(struct trie *t)
{
    unsigned int i;

    if (!t)
	return;

    for (i = 0; i < NELMS; i += 1) {
	if (t->kids[i]) {
	    trie_free(t->kids[i]);
	    free((void *) t->kids[i]->key);
	    free(t->kids[i]);
	}
    }
}
Ejemplo n.º 27
0
void test_trie_insert_out_of_memory(void)
{
	Trie *trie;

	trie = generate_binary_trie();

	alloc_test_set_limit(3);

	assert(trie_insert_binary(trie,
	                          bin_key4, sizeof(bin_key4), 
	                          "test value") == 0);

	assert(trie_lookup_binary(trie, bin_key4, sizeof(bin_key4)) == NULL);
	assert(trie_num_entries(trie) == 2);

	trie_free(trie);
}
Ejemplo n.º 28
0
void test_trie_replace(void)
{
	Trie *trie;
	int *val;

	trie = generate_trie();

	/* Test replacing values */

	val = malloc(sizeof(int));
	*val = 999;
	assert(trie_insert(trie, "999", val) != 0);
	assert(trie_num_entries(trie) == NUM_TEST_VALUES);

	assert(trie_lookup(trie, "999") == val);
	free(val);
	trie_free(trie);
}
Ejemplo n.º 29
0
void
rsynth_term(rsynth_t * rsynth)
{
    if (rsynth) {
	rsynth_flush(rsynth, 0);
	trie_free(&phtoelm, &free);
#ifdef DO_RANGE_CHECKS
	fprintf(stderr, "Max range %g @ %s:%d\n", range_max, __FILE__,
		range_ln);
#endif
	if (rsynth->voice_file)
	    fclose(rsynth->voice_file);
	if (rsynth->parm_file)
	    fclose(rsynth->parm_file);
	free(rsynth->pvt);
	free(rsynth);
    }
}
Ejemplo n.º 30
0
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);
}