Exemplo n.º 1
0
int main( void ){
    // Initialization
    int i,j;
    bst_t* tree = NULL;
    
    SystemInit();
    init_scroll();
    GLCD_Clear( Blue );
    
    tree = malloc(sizeof(bst_t));
    bst_init(tree);
    
    // Insertion
    for (i=0; i<100; i++) bst_insert(tree, value_array[i]);
    printf("0 | %d,  %d\n",bst_min(tree), bst_max(tree));
            
    // Deletion            
    for (i=0; i<5; i++){
            for (j=0; j<20; j++) bst_erase(tree, erase_array[i][j]);
            printf("%d | %d, %d\n", i+1, bst_min(tree), bst_max(tree));
    }

    // Destruction
    bst_destroy(tree);
    while ( 1 ) {
        /* An emebedded system does not terminate... */
    }
}
Exemplo n.º 2
0
void
init_child_index ()
{
    /* Init the child index data structure */
    pthread_mutex_init (&index.lock, NULL);
    bst_init (&index.tree, &child_compare);

    /* Initialize the run commands index */
    runcommand[NOTHING] = &nothing_command;
    runcommand[SEND_B] = &send_b_command;
    runcommand[SEND_NB] = &send_nb_command;
    runcommand[SEND_WAIT] = &send_wait_command;
    runcommand[RECV_B] = &recv_b_command;
    runcommand[RECV_NB] = &recv_nb_command;
    runcommand[RECV_WAIT] = &recv_wait_command;
    runcommand[SEMA_INIT] = &sema_init_command;
    runcommand[SEMA_POST] = &sema_post_command;
    runcommand[SEMA_WAIT] = &sema_wait_command;
    runcommand[SEMA_TRY_WAIT] = &sema_try_wait_command;
    runcommand[LOCK_INIT] = &lock_init_command;
    runcommand[LOCK_ACQIRE] = &lock_acquire_command;
    runcommand[LOCK_RELEASE] = &lock_release_command;
    runcommand[LOCK_TRY_ACQUIRE] = &lock_try_acquire_command;
    runcommand[MONITOR_INIT] = &monitor_init_command;
    runcommand[MONITOR_WAIT] = &monitor_wait_command;
    runcommand[MONITOR_SIGNAL] = &monitor_signal_command;
    runcommand[MONITOR_BCAST] = &monitor_bcast_command;
};
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
        if (2 != argc) {
                errx(EXIT_FAILURE, "Usage: %s [CONFIGURATION FILE]", argv[0]);
        }

        pw_client_bst = bst_init();

        if (0 != atexit(clean_up)) {
                bst_destroy(&pw_client_bst, (enum bst_type)PW_CLIENT_BST);
                errx(EXIT_FAILURE, "atexit() : failed to register clean_up()");
        }

        configname = argv[1];
        struct sigaction sa = {
                .sa_handler = signal_handle,
                /*.sa_flags = SA_RESTART,*/
        };
        sigemptyset_or_die(&sa.sa_mask);
        sigaction_or_die(SIGINT, &sa, NULL);
        sigaction_or_die(SIGHUP, &sa, NULL);
        setbuf(stdout, NULL); /*Make stdout unbuffered.*/
        procclean((enum pw_clean_type)PW_SERVER_CLEAN);
        procserver();

        return EXIT_SUCCESS;
}
Exemplo n.º 4
0
extern int bbst_init(bbst *bb, size_t elem_size, comp_fn_t comp) {
	bst *b = &bb->b;
	sarray *sa = &bb->sa;

	bst_init(b, elem_size, comp);
	sarray_init(sa, elem_size, comp);

	return 0;
}
Exemplo n.º 5
0
void test_bst_traverse_breadth_first() {
	bst b;
	int ints[] = {5, 3, 7, 4, 2};
	int ints_size = ARRAY_SIZE(ints);

	bst_init(&b, sizeof(int), comp_int_member);
	insert_ints(&b, (add_fn_t)bst_insert, ints, ints_size);
	traverse_int_assert(&b, bst_traverse_breadth_first,
			    5,  5, 3, 7, 2, 4);
}
Exemplo n.º 6
0
void test_bst_traverse_preorder() {
	bst b;
	int ints[] = {5, 3, 7, 4, 2};
	int ints_size = ARRAY_SIZE(ints);

	bst_init(&b, sizeof(int), comp_int_member);
	insert_ints(&b, (add_fn_t)bst_insert, ints, ints_size);
	traverse_int_assert(&b, bst_traverse_preorder,
			    5,  5, 3, 2, 4, 7);
}
Exemplo n.º 7
0
void test_bst_count() {
	bst b;
	int count;
	int ints[] = {5, 3, 7, 4, 2, 3};
	int ints_size = ARRAY_SIZE(ints);

	bst_init(&b, sizeof(int), comp_int_member);
	insert_ints(&b, (add_fn_t)bst_insert, ints, ints_size);
	count = bst_count(&b);
	TEST_ASSERT_EQUAL_INT(count, 6);
}
Exemplo n.º 8
0
int graph_init(Graph *g, int (*cmp_v) (const void *va, const void *vb),
                            int (*cmp_e) (const void *ea, const void *eb),
                            void (*destroy_vertex) (void *vertex),
                            void (*destroy_vertex_data) (void *vData),
                            void (*destroy_edge) (void *edge),
                            void (*destroy_edge_data) (void *eData))
{
    
    if (g == 0 || cmp_v == 0 || cmp_e == 0) {
        return -1;
    }
    
    bst_init(&g->treeVertexData, cmp_v, destroy_vertex, destroy_vertex_data);
    bst_init(&g->treeVertexUndirectedEdge, cmp_v, 0, 0);
    
    bst_init(&g->treeUndirectedEdgeData, cmp_e, destroy_edge, destroy_edge_data);
    bst_init(&g->treeUndirectedEdgeSource1, cmp_e, 0, 0);
    bst_init(&g->treeUndirectedEdgeSource2, cmp_e, 0, 0);
    
    g->cmp_vertex = cmp_v;
    g->cmp_edge = cmp_e;
    
    return 0;
}
Exemplo n.º 9
0
int main(void)
{
    size_t i;
    int A[] = {'E', 'A', 'S', 'Y', 'Q', 'U', 'E', 'S', 'T', 'I', 'O', 'N'};
    bst_t bst1;
    bst1 = bst_init();

    for (i = 0; i < SIZE(A); i++)  {
        bst_insert(bst1, A[i]);
    }

    bst_sort(bst1, print);
    bst_finalize(&bst1);
    return 0;
}
Exemplo n.º 10
0
void test_bst_min_max() {
	bst b;
	city *c;

	bst_init(&b, sizeof(city), comp_city_byname);

	// Insert some cities.
	city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab);

	c = bst_min(&b);
	TEST_ASSERT_EQUAL_STRING(c->name, "alba");
        
	c = bst_max(&b);
	TEST_ASSERT_EQUAL_STRING(c->name, "sibiu");       
}
Exemplo n.º 11
0
void
setup()
{
	bst_init(tree, sizeof(elem_t), sizeof(int), int_cmp, updator);
	ASSERT(tree != NULL);

	if (scanf("%i", &idatalen) != 1) {
		printf("No input\n");
		exit(1);
	}

	idata = malloc(idatalen * sizeof(int));
	for (int i = 0; i < idatalen; i++) {
		scanf("%i", &idata[i]);
		bst_insert(tree, &(elem_t){.v = idata[i]});
	}
Exemplo n.º 12
0
void test_bst_search() {
	bst b;
	bst_node *node, *left, *right;
	city *c;

	bst_init(&b, sizeof(city), comp_city_byname);

	// Insert some cities.
	city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab);

	// All cities must be included in the tree.
	for(int i = 0; i < CITIES_SB_CJ_AB_SIZE; i++)
		city_search_byname(&b, (adt_search_fn_t)bst_search,
				   &cities_sb_cj_ab[i],
				   cities_sb_cj_ab[i].size);

}
Exemplo n.º 13
0
void test_bst_insert() {
	bst b;
	bst_node *node, *left, *right;
	city *c;

	bst_init(&b, sizeof(city), comp_city_byname);

	/* Insert some cities.
	          sb
                 /
		cj
	       /
	      ab
	*/
	city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab);

	// sb is the root.
	node = bst_getroot(&b);
	left = bst_node_left(node);
	right = bst_node_right(node);
	c = bst_node_value(node);
	TEST_ASSERT_EQUAL_STRING(c->name, "sibiu");
	// sb right node is empty
	TEST_ASSERT_NULL(right);

	// Descend down the tree.
	node = left;
	left = bst_node_left(node);
	right = bst_node_right(node);
	c = bst_node_value(node);
	TEST_ASSERT_EQUAL_STRING(c->name, "cluj");
	// cj right node is empty
	TEST_ASSERT_NULL(right);

	// Descend down the tree.
	node = left;
	left = bst_node_left(node);
	right = bst_node_right(node);
	c = bst_node_value(node);
	TEST_ASSERT_EQUAL_STRING(c->name, "alba");

	// ab has no children
	TEST_ASSERT_NULL(left);
	TEST_ASSERT_NULL(right);
}
Exemplo n.º 14
0
/* Put a new key/value pair into a table. */
int ht_put(hashtbl_t * tbl, char *key, void *data) {
  unsigned long h;
  bst_node_t *treenode;
  ht_elem_t *elem, key_elem;

  key_elem.key = key;
  elem = mempool_alloc(tbl->ht_elem_pool, sizeof(ht_elem_t));
  if (! elem)
    return -1;
  elem->key = mempool_alloc(tbl->key_pool,
                            sizeof(char) * strlen(key) + 1);
  if (! elem->key) {
    /* elem leaks here, but we cannot free it from the mempool. */
    return -1;
  }
  strcpy(elem->key, key);
  elem->data = data;

  h = tbl->hash((unsigned char *) elem->key) % tbl->arrsz;

  if (!tbl->arr[h]) {
    tbl->arr[h] = xmalloc(sizeof(bstree_t));
    /* No free() fn for the bst, since its elements are in a mempool. */
    bst_init(tbl->arr[h], ht_key_cmp, NULL);
    bst_insert(tbl->arr[h], elem);
    tbl->nelems++;
    return 0;
  }

  treenode = bst_find(tbl->arr[h], &key_elem);

  /* If no match is found, insert the new element and increase the counter.
   * Otherwise, replace the old data with the new. */
  if (!treenode) {
    bst_insert(tbl->arr[h], elem);
    tbl->nelems++;
  } else {
    if (tbl->free)
      tbl->free(((ht_elem_t *) treenode->data)->data);
    treenode->data = elem;
  }
  return 0;
}
Exemplo n.º 15
0
void test_bst_fill() {
	bst b;
	int fillval;
	int ints[] = {5, 3, 7, 4, 2};
	int ints_size = ARRAY_SIZE(ints);

	bst_init(&b, sizeof(int), comp_int_member);

	/* Build the tree with five nodes:
	           5
		  / \
                 3   7
                / \
               2   4
	*/
	insert_ints(&b, (add_fn_t)bst_insert, ints, ints_size);

	/* Fill at 3.
	          5
                 / \
                2   7
                 \   \
                  4  10
	*/
	fillval = 7;
	bst_fill(&b, test_bst_check_fill, (bst_fill_fn_t)h_bst_fill, &fillval);
	traverse_int_assert(&b, bst_traverse_inorder,
			    5,  2, 4, 5, 7, 10);

	/* Fill at 4.
	          5
                 / \
                2   7
                     \
                     10(2)
	*/
	fillval = 6;
	bst_fill(&b, test_bst_check_fill, (bst_fill_fn_t)h_bst_fill, &fillval);
	traverse_int_assert(&b, bst_traverse_inorder,
			    4,  2, 5, 7, 10);
}
Exemplo n.º 16
0
void test_bst_search_count() {
	bst b;
	int search_elem, search_res;
	int ints[] = {5, 3, 7, 4, 2, 7, 7, 4};
	int ints_size = ARRAY_SIZE(ints);

	bst_init(&b, sizeof(int), comp_int_member);
	insert_ints(&b, (add_fn_t)bst_insert, ints, ints_size);

	search_elem = 7;
	search_res = bst_search_count(&b, &search_elem);
	TEST_ASSERT_EQUAL_INT(search_res, 3); // We've added three nodes with value 7.

	search_elem = 4;
	search_res = bst_search_count(&b, &search_elem);
	TEST_ASSERT_EQUAL_INT(search_res, 2); // We've added three nodes with value 4.

	search_elem = 5;
	search_res = bst_search_count(&b, &search_elem);
	TEST_ASSERT_EQUAL_INT(search_res, 1); // We've added one node with value 5.
}
Exemplo n.º 17
0
int main(void)
{
    size_t i;
    size_t n = 1000;
    bst_t bst1;

    for (; n < 1000000; n *= 10) {
        bst1 = bst_init();

        for (i = 0; i < n; i++)  {
            bst_insert(bst1, rand() % MAX);
        }

        printf("for n = %u: The max comparision of the tree is: %u\n",
               n, bst_maxcompare(bst1));
        printf("for n = %u: The average search hit of the tree is:%.2f\n",
               n, bst_avg_hit(bst1));
        printf("for n = %u: The average search miss of the tree is: %.2f\n\n",
               n, bst_avg_miss(bst1));
        bst_finalize(&bst1);
    }

    return 0;
}
Exemplo n.º 18
0
bstree_t * make_tree(test_balanced_t kind, int do_init_asserts) {
    bstree_t *tree = malloc(sizeof(bstree_t));
    bst_node_t *node;
    bst_init(tree, strcmp, free);
    if (do_init_asserts) {
        ASSERT_TRUE(tree->root == NULL, "bts_init: set root to NULL");
        ASSERT_TRUE(tree->cmp == strcmp, "bts_init: set compare fn");
        ASSERT_TRUE(tree->free == free, "bts_init: set free fn");
    }
    if (kind == BALANCED) {
        node = bst_insert(tree, strdup("BBB"));
        node = bst_insert(tree, strdup("AAA"));
        node = bst_insert(tree, strdup("CCC"));
    } else if (kind == LEFT_HEAVY) {
        node = bst_insert(tree, strdup("CCC"));
        node = bst_insert(tree, strdup("BBB"));
        node = bst_insert(tree, strdup("AAA"));
    } else if (kind == RIGHT_HEAVY) {
        node = bst_insert(tree, strdup("AAA"));
        node = bst_insert(tree, strdup("BBB"));
        node = bst_insert(tree, strdup("CCC"));
    }
    return tree;
}