void insert_element(int i) { int max_top, min_top; if(max_heap.size == min_heap.size) { min_top = *((int*)heap_top(&min_heap)); if(i <= min_top) { heap_insert(&max_heap,&i); } else { free(heap_erase(&min_heap)); heap_insert(&min_heap,&i); heap_insert(&max_heap,&min_top); } free(min_top); } else { //max_heap.size is greater max_top = *((int*)heap_top(&max_heap)); if(i >= max_top) { heap_insert(&min_heap,&i); } else { free(heap_erase(&max_heap)); heap_insert(&max_heap,&i); heap_insert(&min_heap,&max_top); } free(max_top); } }
static void router_test2(void) { int i; struct node_t* node; struct node_t* result[8]; static struct node_t s_nodes[100000]; uint8_t id[N_NODEID] = { 0xAB, 0xCD, 0xEF, 0x89, }; heap_t* heap, *heap2; struct router_t* router; router = router_create(id); heap = heap_create(node_compare_less, (void*)id); heap_reserve(heap, 8 + 1); for (i = 0; i < sizeof(s_nodes) / sizeof(s_nodes[0]); i++) { int v = rand(); memset(&s_nodes[i], 0, sizeof(s_nodes[i])); memcpy(s_nodes[i].id, &v, sizeof(v)); s_nodes[i].ref = 1; if (0 == router_add(router, s_nodes[i].id, &s_nodes[i].addr, &node)) { heap_push(heap, node); if (heap_size(heap) > 8) { node_release((struct node_t*)heap_top(heap)); heap_pop(heap); } } } assert(8 == heap_size(heap)); assert(8 == router_nearest(router, id, result, 8)); heap2 = heap_create(node_compare_less, (void*)id); heap_reserve(heap2, 8); for (i = 0; i < 8; i++) { heap_push(heap2, result[i]); } assert(heap_size(heap) == heap_size(heap2)); for (i = 0; i < 8; i++) { assert(heap_top(heap2) == heap_top(heap)); heap_pop(heap); heap_pop(heap2); } router_destroy(router); heap_destroy(heap); heap_destroy(heap2); printf("router test ok!\n"); }
void case_heap_top() { struct heap *heap = heap(heap_cmp); int a = 3, b = 2, c = 4, d = 1; assert(heap_push(heap, (void *)&a) == HEAP_OK); assert(*(int *)(heap_top(heap)) == 3); assert(heap_push(heap, (void *)&b) == HEAP_OK); assert(*(int *)(heap_top(heap)) == 2); assert(heap_push(heap, (void *)&c) == HEAP_OK); assert(*(int *)(heap_top(heap)) == 2); assert(heap_push(heap, (void *)&d) == HEAP_OK); assert(*(int *)(heap_top(heap)) == 1); heap_free(heap); }
int main(void) { struct AATree aatree; struct CBTree *cbtree; struct md5_ctx md5; struct Heap *heap; char buf[128]; static_assert(sizeof(int) >= 4, "unsupported int size"); heap = heap_create(heap_is_better, NULL, NULL); heap_top(heap); aatree_init(&aatree, NULL, NULL); cbtree = cbtree_create(NULL, NULL, NULL, NULL); cbtree_destroy(cbtree); daemonize(NULL, false); hash_lookup3("foo", 3); if (!event_init()) log_debug("test"); if (!parse_ini_file("foo", NULL, NULL)) log_debug("test"); log_stats("1"); file_size("foo"); md5_reset(&md5); strlcpy(buf, "foo", sizeof(buf)); printf("xmalloc: %p\n", xmalloc(128)); if (0) die("0"); csrandom(); tls_init(); return 0; }
void * priqueue_extract(struct PriQueue *priqueue) { assert(priqueue != NULL); void *ret = heap_top(priqueue->heap); heap_pop(priqueue->heap); return ret; }
void case_heap_top(struct bench_ctx *ctx) { struct heap *heap = heap(&heap_bench_cmp); int i; for (i = 0; i < ctx->n; i++) { heap_push(heap, &i); } bench_ctx_reset_start_at(ctx); for (i = 0; i < ctx->n; i++) { heap_top(heap); } bench_ctx_reset_end_at(ctx); heap_free(heap); }
void test1(void) { static const char * values[] = { "hello", "world", "helloworld", "hello, world!", NULL }; int i; heap_t * heap; char * rv; heap = heap_new(strcmp); for (i = 0; values[i]; i++) heap_add(heap, values[i]); while (rv = (char*)heap_top(heap)) { fprintf(stderr, rv); heap_pop(heap); } heap_free(heap); }
static inline suptimer_t * timers_top() { return timer_from(heap_top(&timers)); }
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; }
int router_nearest(struct router_t* router, const uint8_t id[N_NODEID], struct node_t* nodes[], size_t count) { int i, min, diff; uint8_t xor[N_NODEID]; heap_t* heap; struct rbitem_t* item; struct rbtree_node_t* node; const struct rbtree_node_t* prev; const struct rbtree_node_t* next; heap = heap_create(node_compare_less, (void*)id); heap_reserve(heap, count + 1); min = N_BITS; locker_lock(&router->locker); rbtree_find(&router->rbtree, id, &node); if (NULL == node) { locker_unlock(&router->locker); return 0; } item = rbtree_entry(node, struct rbitem_t, link); bitmap_xor(xor, id, item->node->id, N_BITS); diff = bitmap_count_leading_zero(xor, N_BITS); min = min < diff ? min : diff; heap_push(heap, item->node); prev = rbtree_prev(node); next = rbtree_next(node); do { while (prev) { item = rbtree_entry(prev, struct rbitem_t, link); bitmap_xor(xor, id, item->node->id, N_BITS); diff = bitmap_count_leading_zero(xor, N_BITS); heap_push(heap, item->node); if (heap_size(heap) > (int)count) heap_pop(heap); prev = rbtree_prev(prev); if (diff < min) { min = diff; break; // try right } } while (next) { item = rbtree_entry(next, struct rbitem_t, link); bitmap_xor(xor, id, item->node->id, N_BITS); diff = bitmap_count_leading_zero(xor, N_BITS); heap_push(heap, item->node); if (heap_size(heap) > (int)count) heap_pop(heap); next = rbtree_next(next); if (diff < min) { min = diff; break; // try left } } } while (heap_size(heap) < (int)count && (prev || next)); for (i = 0; i < (int)count && !heap_empty(heap); i++) { nodes[i] = heap_top(heap); node_addref(nodes[i]); heap_pop(heap); } locker_unlock(&router->locker); heap_destroy(heap); return i; }
int main() { // int j; int func; int value; char cmd[1024]; printf("Please input the heap size : "); scanf("%d",&size); printf("\n"); heap = build_heap(size); maxheap(heap); do{ printf("1) Insert 2) Delete 3) Extract max 4) Top Value 5) Print Heap 6)Free:"); for(func = 0; func<=0 || func > 7; sscanf(cmd,"%d",&func)){ scanf("%s",cmd); printf("\n"); } switch(func){ case 1: printf("please input new value for insert:"); scanf("%d ",&value); printf("\n"); heap_insert(heap,value); break; case 2: printf("please input heap-index to delete:"); scanf("%d ",&value); printf("\n"); heap_delete(value); break; case 3: heap_extract(); break; case 4: heap_top(heap); break; case 5: heap_print(); break; case 6: free(heap->array); //free(heap); size = 0; break; } }while(1); return 0; }