示例#1
0
文件: encode.c 项目: tiwanari/soft2
/**
 *  符号の木を作る関数
 *  @return Node* 符号の木の根
 */
Node* build_tree()
{
    int i, n = 0;
    Node *nodes[NSYMBOLS];
    
    // 葉を作成する
    for (i = 0; i < NSYMBOLS; i++)
    {
        if (count[i] == 0) continue;    // 一度も出現していないとき
        nodes[n] = (Node*)malloc(sizeof(Node));
        nodes[n]->symbol = i;
        nodes[n]->count  = count[i];
        nodes[n]->left   = NULL;
        nodes[n]->right  = NULL;
        n++;    // 要素数をカウント
    }
    
    // 符号の木を作る
    while (n >= 2)
    {
        Node *node1 = pop_min(&n, nodes);   // 出現回数最小のノード
        Node *node2 = pop_min(&n, nodes);   // 出現回数最小のノード
        
        // 節を作成
        Node *new_node = (Node *)malloc(sizeof(Node));
        new_node->count = node1->count + node2->count;  // 出現回数を合計する
        new_node->left  = node1;
        new_node->right = node2;
        
        // 新しく作ったノードをリストに追加
        nodes[n++] = new_node;
    }
    
    return nodes[0];    // 根を返す
}
示例#2
0
void test_inserting_vertex_with_3_dist_then_2_dist()
{
	init();

	insert(17, 3);
	insert(20, 2);

	assert(pop_min() == 20);
	assert(pop_min() == 17);

	assert_finished();
}
示例#3
0
void test_insert_pop_min()
{
	init();

	insert(0, 0);
	insert(1, 0);

	int v1 = pop_min();
	int v2 = pop_min();
	assert(EQUAL(v1, v2, 0, 1));

	assert_finished();
}
示例#4
0
void run_djkstra(char *file, char *dir, lint source){
	t_csr *gs = (t_csr*)malloc(sizeof(t_csr));
	read_csr_bin(gs, file,  dir, SEND, 0);
	lint *precedent = (lint*)malloc(sizeof(lint)*gs->v_size);
	int i;
	gs->vet_info[source].weight = 0.0;
	p_list Q = (p_list)malloc(sizeof(t_list));
	add_list_two(Q, source, 0.0);	
	lint iter=0;
	while(Q->next != NULL){
		p_list elem = pop_min(Q); 
		lint from = elem->int_val;
		double from_val = elem->double_val;
		lint start = from == 0 ? 0:gs->vet_idx[from-1];
		lint end = gs->vet_idx[from];
		for(i=start; i<end; i++){
			lint to = gs->edge_idx[i];
			double edge_weight = gs->edge_info[i].edge_weight;	
			double to_val = gs->vet_info[to].weight; 
			if((from_val+edge_weight)<to_val){
				//DPRINTF(1, "%f %f %f\n", from_val, edge_weight, to_val);
				gs->vet_info[to].weight = from_val+edge_weight;	
				precedent[to] = from;
				add_list_two(Q, to, gs->vet_info[to].weight);
			}
		}
		//DPRINTF(1, "this is iter %lld\n", iter++);
	}
	for(i=0;i<gs->v_size;i++)
		DPRINTF(1, "%f ", gs->vet_info[i].weight);
	DPRINTF(1, "\n");
}
int
toposort()
{
    int *s;
    int m = 0;
    int i, j;
    int t;
    int ll = 0;

    NEW(s, n);
    for (i = 0; i < n; ++i) {
        if (a[i] < 0)
            continue;
        if (d[i] == 0) {
            s[m++] = i;
        }
    }
    make_minheap(s, m);
    while (m > 0) {
        t = pop_min(s, &m);
        l[ll++] = t;
        for (i = 0; i < n; ++i) {
            if (map[t][i]) {
                map[t][i] = 0;
                if (--(d[i]) == 0) {
                    insert(s, i, &m);
                }
            }
        }
    }
    free(s);

    return ll;
}
示例#6
0
void test_inserting_two_vertices_with_zero_distance()
{
	init();

	insert(0, 0);
	assert(pop_min() == 0);

	assert_finished();
}
示例#7
0
void test_insert_max_distance()
{
	init();

	// This is done for faster TESTING ONLY, obviously not part of the API;
	// Do not use this technique yourself unless you know what you're doing
	q->min_distance_candidate = DISTANCE__MAX - 10;
	q->max_distance_ever_seen = DISTANCE__MAX - 20;

	insert(34, DISTANCE__MAX);
	assert(pop_min() == 34);
	assert(graph->vertices[34].distance == DISTANCE__MAX);

	assert_finished();
}
示例#8
0
文件: heap.c 项目: sabraham/ds
int test_heap_arr_pop_min () {
  size_t arr_len = 10;
  size_t heap_size = 0;
  int *arr = init_heap(arr_len);
  heap_arr_insert(arr, 7, &heap_size, &arr_len);
  heap_arr_insert(arr, 7, &heap_size, &arr_len);
  heap_arr_insert(arr, 2, &heap_size, &arr_len);
  print_heap_arr(arr, arr_len);
  int popped = pop_min(arr, &heap_size);
  print_heap_arr(arr, arr_len);
  printf("popped: %d\n", popped);
  int expected[] = {7, 7};
  int ret = 0;
  ret += arrs_equal(arr, expected, heap_size);
  free(arr);
  ret += popped == 2 ? 1 : 0;
  return ret;
}