Example #1
0
int find_path(AStar_t astar,int x,int y,int x1,int y1,kn_dlist *path){
	AStarNode *from = get_node(astar,x,y);
	AStarNode *to = get_node(astar,x1,y1);
	if(!from || !to || from == to || to->block)		
		return 0;
	minheap_insert(astar->open_list,&from->heap);	
	AStarNode *current_node = NULL;	
	while(1){	
		struct heapele *e = minheap_popmin(astar->open_list);
		if(!e){ 
			reset(astar);
			return 0;
		}		
		current_node = (AStarNode*)((int8_t*)e-sizeof(kn_dlist_node));
		if(current_node == to){ 
			while(current_node)
			{
				kn_dlist_remove((kn_dlist_node*)current_node);//可能在close_list中,需要删除
				if(current_node != from)//当前点无需加入到路径点中
					kn_dlist_push_front(path,(kn_dlist_node*)current_node);
				AStarNode *t = current_node;
				current_node = current_node->parent;
				t->parent = NULL;
				t->F = t->G = t->H = 0;
				t->heap.index = 0;
			}	
			reset(astar);
			return 1;
		}
		//current插入到close表
		kn_dlist_push(&astar->close_list,(kn_dlist_node*)current_node);
		//获取current的相邻节点
		kn_dlist *neighbors =  get_neighbors(astar,current_node);
		if(neighbors)
		{
			AStarNode *n;
			while((n = (AStarNode*)kn_dlist_pop(neighbors))){
				if(n->heap.index)//在openlist中
				{
					float new_G = current_node->G + cost_2_neighbor(current_node,n);
					if(new_G < n->G)
					{
						//经过当前neighbor路径更佳,更新路径
						n->G = new_G;
						n->F = n->G + n->H;
						n->parent = current_node;
						minheap_change(astar->open_list,&n->heap);
					}
					continue;
				}
				n->parent = current_node;
				n->G = current_node->G + cost_2_neighbor(current_node,n);
				n->H = cost_2_goal(n,to);
				n->F = n->G + n->H;
				minheap_insert(astar->open_list,&n->heap);
			}
			neighbors = NULL;
		}	
	}	
}
Example #2
0
static inline void sche_add_timeout(sche_t s,coro_t co)
{
	co->status = CORO_SLEEP;
	struct heapele *hele = &(co->_heapele);
	minheap_insert(s->_minheap,hele);
	_sche_next(s,co);
}
Example #3
0
struct timer_item* register_timer(struct timer *t,struct timer_item *item,timer_callback cb,void *ud,uint64_t timeout)
{
	if(!item){
		item = calloc(1,sizeof(*item));
		item->_ud = ud;
		item->_callback = cb;
	}
	item->_timeout = GetSystemMs64()+timeout;
	minheap_insert(t->_minheap,(struct heapele*)item);
	return item;
}
Example #4
0
void minheap_remove(minheap_t m,struct heapele *e)
{
	struct heapele **back = calloc(1,sizeof(struct heapele*)*(m->size-1));
	int32_t i = 1;
	int32_t c = 1;
	for( ; i <= m->size;++i)
	{
		m->data[i]->index = 0;
		if(m->data[i] != e)
			back[c++] = m->data[i];
	}
	memset(&(m->data[0]),0,sizeof(struct heapele*)*m->max_size);
	m->size = 0;
	i = 1;
	for(; i < c; ++i)
		minheap_insert(m,back[i]);
	free(back);
}
Example #5
0
File: mheap.c Project: ttxie/comm
int main()
{
        int32_t i; 
        u_char* ptr=NULL; 
        const char *a="a", *b="b", *c="c", *d="d", *e="e", *f="f", *g="g";
        minheap_t* heap = minheap_create(256);
        if (heap == NULL) {
                printf("malloc minheap memory error!\n");
        }
        
        minheap_insert(heap, 34, (u_char *)a);
        printf("index:%d\n", (int)minheap_insert(heap, 25, (u_char*)b));
        minheap_insert(heap, 89, (u_char *)c);
        minheap_insert(heap, 4,  (u_char *)d);
        minheap_insert(heap, 12, (u_char *)e);
        minheap_insert(heap, 25, (u_char *)b);
        minheap_insert(heap, 10, (u_char *)g);
     
         
        for(i=0; i<minheap_count(heap); i++) {
                printf("index:%d, weight:%d, ptr:%s\n", i,
                        heap->mh_nodes[i].mn_weight, (char *)heap->mh_nodes[i].mn_ptr);
        } 
       
        ptr = minheap_pop(heap);
        printf("minheap pop ptr:%p\n", ptr);
        printf("minheap pop ptr:%s\n", ptr);

        for(i=0; i<minheap_count(heap); i++) {
                printf("index:%d, weight:%d, ptr:%s\n", i,
                        heap->mh_nodes[i].mn_weight, (char *)heap->mh_nodes[i].mn_ptr);
        } 
        
        minheap_free(heap);
        
}
main()
{
    int i,ch,H[20],ps,ky;
    
    printf("Input array length: ");
    scanf("%d",&len);
	
    printf("\nInput array -\n");
    for(i=0;i<len;i++)
        scanf("%d",&H[i]);

    heapsize=len;
    
    //build_maxheap(H);
    build_minheap(H);
    
    printf("\nDisplaying heap -\n");
    display_heap(H);
    
	//printf("\n---------PRIORITY QUEUE-----------\n\n1.) Print maximum value in queue\n2.) Print and extract maximum value in queue\n3.) Increase key for an element in the queue\n4.) Insert key into the queue\n");
	printf("\n---------PRIORITY QUEUE-----------\n\n1.) Print minimum value in queue\n2.) Print and extract minimum value in queue\n3.) Decrease key for an element in the queue\n4.) Insert key into the queue\n");
	
	printf("\nEnter choice (1-4): ");
	scanf("%d",&ch);
	printf("\n");

	switch(ch)
	{
		case 1:
		//printf("%d\n",heap_maximum(H)); 
		printf("%d\n",heap_minimum(H));
		
		printf("Displaying PriorityQ: ");
		display_heap(H);
		break;

		case 2:
		//printf("%d\n",heap_max_extract(H));
		printf("%d\n",heap_min_extract(H));
		printf("Displaying PriorityQ: ");
		display_heap(H);
		break;

		case 3: 
		//printf("Enter position of the queue element whose key you want to increase (1-%d): ",len);
		printf("Enter position of the queue element whose key you want to decrease (1-%d): ",len);
		
		scanf("%d",&ps);
		printf("Enter new key: ");
		scanf("%d",&ky);
		
		//heap_increase_key(H,ps,ky);
		heap_decrease_key(H,ps,ky);
		
		printf("Displaying PriorityQ: ");
		display_heap(H);
		break;

		case 4: printf("Enter the new key you want to insert: ");
		scanf("%d",&ky);
		
		//maxheap_insert(H,ky);
		minheap_insert(H,ky);
		printf("Displaying PriorityQ: ");
		display_heap(H);
		break;

		default: break;
	}
	
	printf("\n");   
}
int main(void)
{
	uint32_t cnt, i, data[] =
	                    {14, 2, 22, 13, 23, 10, 90, 36, 108, 12,
	                      9, 91, 1, 51, 11, 3, 15, 80, 3, 78, 53,
	                      5, 12, 21, 65, 70, 4};
	const uint32_t data_len = sizeof(data)/sizeof(uint32_t);
	struct heap heap = heap_create(data_len + 5);

	printf(COL_BEG "push data...\n" COL_END);
	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf("data len = %u, heap size = %u.\n", data_len,
	       heap_size(&heap));

	printf(COL_BEG "heap tree:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_set_callbk(&heap, &test_less_than);

	printf(COL_BEG "after heapify:\n" COL_END);
	minheap_heapify(&heap);
	heap_print_tr(&heap, &test_print);

	cnt = 0;
	printf(COL_BEG "ranking emulation...:\n" COL_END);
	while (cnt < 100) {
		i = (i + 1) % data_len;
		if (!heap_full(&heap)) {
			printf("insert %d\n", data[i]);
			minheap_insert(&heap, &data[i]);
		} else {
			void *top = heap_top(&heap);
			if (test_less_than(top, &data[i])) {
				printf("replace with %d\n", data[i]);
				minheap_delete(&heap, 0);
				minheap_insert(&heap, &data[i]);
			}
		}
		cnt ++;
	}

	printf(COL_BEG "a heavy heap tree now:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	minheap_sort(&heap);
	printf(COL_BEG "heap array after min-heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_destory(&heap);

	printf(COL_BEG "a new heap...\n" COL_END);
	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);

	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_sort_desc(&heap);
	printf(COL_BEG "heap array after heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_print_tr(&heap, &test_print);
	heap_destory(&heap);

	printf(COL_BEG "sort a heap with one element...\n" COL_END);

	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);
	heap_push(&heap, &data[0]);
	heap_print_tr(&heap, &test_print);
	heap_sort_desc(&heap);

	heap_destory(&heap);
	return 0;
}