void k_way_sort(data_t *begin, data_t *end, size_t k, less_t &less) { //data_t **heap = new data_t[k]; data_t **heap; heap = new data_t*[k]; for(size_t i = 0; i < k; ++i) { heap_insert(heap, i, (begin + i), less); } data_t *iter = begin; data_t *next_mas = begin + 1; size_t heap_size = k; size_t n = std::distance(begin, end); *begin = heap_pop(heap, heap_size, less, &iter); for(size_t i = 1; i < n; ++i) { begin++; iter += k; if((iter) >= end) { // надо что-то делать // heap_rebuild(); // --heap_size --heap_size; iter = next_mas + k; ++next_mas; } heap_insert(heap, heap_size, iter, less); *begin = heap_pop(heap, heap_size, less, &iter); } /* for(size_t i = 0; i < k; ++i) { printf("%d ", *(heap[i])); } */ }
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"); }
// FIXME - we don't cope optimally with the situation where a branch is // created, files deleted, and then the branch tagged (without rtag). We'll // never know that the tag was placed on the branch; instead we'll place the tag // on the trunk. static void branch_graph (database_t * db, tag_t *** tree_order, tag_t *** tree_order_end) { // First, go through each tag, and put it on all the branches. for (tag_t * i = db->tags; i != db->tags_end; ++i) { i->changeset.unready_count = 0; for (version_t ** j = i->tag_files; j != i->tag_files_end; ++j) { if ((*j)->branch) record_branch_tag ((*j)->branch, i); if (*j != (*j)->file->versions && (*j)[-1].implicit_merge && (*j)[-1].used && (*j)[-1].branch) record_branch_tag ((*j)[-1].branch, i); } } // Go through each branch and put record it on the tags. for (tag_t * i = db->tags; i != db->tags_end; ++i) for (branch_tag_t * j = i->tags; j != i->tags_end; ++j) { ARRAY_EXTEND (j->tag->parents); j->tag->parents_end[-1].branch = i; j->tag->parents_end[-1].weight = j->weight; ++j->tag->changeset.unready_count; } // Do a cycle breaking pass of the branches. heap_t heap; heap_init (&heap, offsetof (tag_t, changeset.ready_index), tag_compare); // Release all the tags that are ready right now; also sort the parent // lists. for (tag_t * i = db->tags; i != db->tags_end; ++i) { ARRAY_SORT (i->parents, compare_pb); if (i->changeset.unready_count == 0) { i->is_released = true; heap_insert (&heap, i); } } while (!heap_empty (&heap)) tag_released (&heap, heap_pop (&heap), tree_order, tree_order_end); for (tag_t * i = db->tags; i != db->tags_end; ++i) while (!i->is_released) { break_cycle (&heap, i); while (!heap_empty (&heap)) tag_released (&heap, heap_pop (&heap), tree_order, tree_order_end); } heap_destroy (&heap); }
void test_heap_test_4(void) { size_t memsize = heap_mem_size(1, sizeof(struct cw)); fprintf(stderr, "memsize: %ld\n", memsize); char heap_mem[heap_mem_size(1, sizeof(struct cw))]; struct heap *h = heap_create( heap_mem , sizeof(heap_mem) , sizeof(struct cw) , __cw_leq , __cw_cpy ); fprintf(stderr, "heap: %p\n", h); fprintf(stdout, "# heap_size: %ld\n", heap_size(h)); struct cw cats[] = { { 1, 1 } , { 2, 1 } , { 1, 2 } , { 3, 1 } , { 12, 3 } , { 5, 1 } , { 31, 2 } , { 6, 2 } , { 7, 1 } , { 7, 1 } , { 10, 5 } }; fprintf(stdout, "\n"); size_t i = 0; for(; i < sizeof(cats)/sizeof(cats[0]); i++ ) { fprintf(stdout, "# {%d, %d}\n", cats[i].cat, cats[i].weight); if( heap_full(h) ) { struct cw *min = heap_get(h); if( __cw_leq(min, &cats[i]) ) { heap_pop(h); } } heap_add(h, &cats[i]); } fprintf(stdout, "\nheap_items %ld\n", heap_items(h)); fprintf(stdout, "\n"); while( !heap_empty(h) ) { struct cw *c = heap_pop(h); fprintf(stdout, "# {%d, %d}\n", c->cat, c->weight); } }
void case_heap_push() { struct heap *heap = heap(heap_cmp); int a = 3, b = 1, c = 2; assert(heap_push(heap, (void *)&a) == HEAP_OK); assert(heap_push(heap, (void *)&b) == HEAP_OK); assert(heap_push(heap, (void *)&c) == HEAP_OK); assert(heap_len(heap) == 3); assert(*(int *)heap_pop(heap) == 1); assert(*(int *)heap_pop(heap) == 2); assert(*(int *)heap_pop(heap) == 3); heap_free(heap); }
void generate_test_heap(void) { size_t n = 0, i, j; int sum = 0, *e=NULL; heap_t *h = heap_init(free, (cmp_func_t)intcmp); for (i = 0; i < 1000; i++) { switch (random() % 3) { case 0: case 2: e = zmalloc(sizeof(int)); *e = random() % 123456789; n++; sum += *e; heap_insert(h, e); break; case 1: e = heap_pop(h); if (e) { n--; sum -= *e; free(e); } break; } /* check propper auto resize */ assert(heap_capacity(h) >= heap_size(h)); for (j = 0; j < heap_size(h); j++) { size_t left = heap_child_l(j); size_t right = heap_child_r(j); size_t greatest = heap_greatest_child(h, j); /* check for heap order is preserved and greatest child is correct */ if (heap_size(h) > right) { assert(*(int*)heap_get(h, j) >= *(int*)heap_get(h, right)); assert(*(int*)heap_get(h, greatest) >= *(int*)heap_get(h, right)); } if (heap_size(h) > left) { assert(*(int*)heap_get(h, j) >= *(int*)heap_get(h, left)); assert(*(int*)heap_get(h, greatest) >= *(int*)heap_get(h, left)); } } } assert(heap_size(h) == n); int ctrlsum = 0, maxctrl; if (heap_size(h)) maxctrl = *(int*)heap_get(h, 0); while (heap_size(h)) { e = heap_pop(h); ctrlsum += *e; /* check that elements are in heap order */ assert(*e <= maxctrl); maxctrl = *e; free(e); } assert(ctrlsum == sum); heap_destroy(h); }
glist_t vithist_sort (glist_t vithist_list) { heap_t heap; gnode_t *gn; vithist_t *h; glist_t vithist_new; int32 ret, score; vithist_new = NULL; heap = heap_new(); for (gn = vithist_list; gn; gn = gnode_next(gn)) { h = (vithist_t *) gnode_ptr(gn); if (heap_insert (heap, (void *) h, h->scr) < 0) { E_ERROR("Panic: heap_insert() failed\n"); return NULL; } } /* * Note: The heap returns nodes with ASCENDING values; and glist_add adds new nodes to the * HEAD of the list. So we get a glist in the desired descending score order. */ while ((ret = heap_pop (heap, (void **)(&h), &score)) > 0) vithist_new = glist_add_ptr (vithist_new, (void *)h); if (ret < 0) { E_ERROR("Panic: heap_pop() failed\n"); return NULL; } heap_destroy (heap); return vithist_new; }
void test_heap_test_1(void) { char heap_mem[heap_mem_size(16, sizeof(uint32_t))]; struct heap *h = heap_create( heap_mem , sizeof(heap_mem) , sizeof(uint32_t) , u32_leq , u32_cpy ); fprintf(stdout, "# heap_size: %ld\n", heap_size(h)); fprintf(stdout, "# heap_empty: %s\n", heap_empty(h) ? "yes" : "no"); uint32_t vs[] = {9,100,7,2,5,200,1,20,8,8,8,150}; size_t i = 0; for(; i < sizeof(vs)/sizeof(vs[0]); i++ ) { if( !heap_add(h, &vs[i]) ) { break; } } fprintf(stdout, "# heap_empty: %s\n", heap_empty(h) ? "yes" : "no"); while( !heap_empty(h) ) { uint32_t *mp = heap_pop(h); if( !mp ) break; fprintf(stdout, "# %d\n", *mp); } }
int my_heapsort (int ** arrayptr, int size) { int retval, counter; int *element, *element2; heap *heap = heap_new(0); retval = 0; /*lets sort that array heap style */ for (counter = 0; counter < size; counter++) { element = malloc(sizeof *element); *element = arrayptr[0][counter]; heap_add(heap, element, *element); } /*overwrite the original values with the sorted ones */ for (counter = 0; counter < size; counter++) { element = (int *)heap_peek(heap); element2 = (int *)heap_pop(heap); if (*element != *element2) { fprintf(stderr, "on iteration: %c, peek returned : %d at %p. pop returned %d at %p.\n", counter, *element, element, *element2, element2); retval++; } arrayptr[0][counter] = *element2; } heap_free(heap); return retval; }
void heap_cpu() { int a = 0; void* p = 0; int i = 0; int to = 0; unsigned int old = getnowtime(); struct heap_s* heap = heap_new(100000, sizeof(int), compare_int, swap_int, NULL); old = getnowtime(); printf("开始添加堆元素:%d\n", old); for(; i < 1000000; ++i) { a = rand() % 10000; heap_insert(heap, &a); } old = getnowtime(); printf("添加完毕,开始pop:%d\n", old); while( p =heap_pop(heap)) { to++; //int temp = *(int*)p; //printf("%d\n", temp); ; } old = getnowtime(); printf("pop完毕:%d, to:%d\n", old, to); }
void case_heap_repalce() { struct heap *heap = heap(heap_cmp); int a = 3, b = 2, c = 5, d = 7, e = 4, f = 6, g = 1, h = 4; assert(heap_push(heap, (void *)&a) == HEAP_OK); assert(heap_push(heap, (void *)&b) == HEAP_OK); assert(heap_push(heap, (void *)&c) == HEAP_OK); assert(heap_push(heap, (void *)&d) == HEAP_OK); assert(heap_push(heap, (void *)&e) == HEAP_OK); assert(heap_push(heap, (void *)&f) == HEAP_OK); assert(heap_push(heap, (void *)&g) == HEAP_OK); assert(1 == *(int *)heap_replace(heap, (void *)&h)); assert(2 == *(int *)heap_pop(heap)); assert(3 == *(int *)heap_pop(heap)); assert(4 == *(int *)heap_pop(heap)); heap_free(heap); }
void test_heapify(void) { size_t i; /* initialize data */ int *array = zmalloc(sizeof(int) * LOTS_OF_INTS); int sum = 0; for (i = 0; i < LOTS_OF_INTS; i++) { array[i] = random() % 123456789; sum += array[i]; } /* construct a heap from our random array */ heap_t *h = heap_heapify(array, LOTS_OF_INTS, sizeof(int), NULL, (cmp_func_t)intcmp); assert(heap_size(h) == LOTS_OF_INTS); int ctrlsum = 0, maxctrl, *e; /* peek head */ if (heap_size(h)) maxctrl = *(int*)heap_get(h, 0); while (heap_size(h)) { e = heap_pop(h); ctrlsum += *e; /* check that elements are in heap order */ assert(*e <= maxctrl); maxctrl = *e; } assert(ctrlsum == sum); heap_destroy(h); free(array); }
void vithist_prune (vithist_t *vh, dict_t *dict, int32 frm, int32 maxwpf, int32 maxhist, int32 beam) { int32 se, fe, filler_done, th; vithist_entry_t *ve; heap_t h; s3wid_t *wid; int32 i; assert (frm >= 0); se = vh->frame_start[frm]; fe = vh->n_entry - 1; th = vh->bestscore[frm] + beam; h = heap_new (); wid = (s3wid_t *) ckd_calloc (maxwpf+1, sizeof(s3wid_t)); wid[0] = BAD_S3WID; for (i = se; i <= fe; i++) { ve = vh->entry[VITHIST_ID2BLK(i)] + VITHIST_ID2BLKOFFSET(i); heap_insert (h, (void *)ve, -(ve->score)); ve->valid = 0; } /* Mark invalid entries: beyond maxwpf words and below threshold */ filler_done = 0; while ((heap_pop (h, (void **)(&ve), &i) > 0) && (ve->score >= th) && (maxhist > 0)) { if (dict_filler_word (dict, ve->wid)) { /* Major HACK!! Keep only one best filler word entry per frame */ if (filler_done) continue; filler_done = 1; } /* Check if this word already valid (e.g., under a different history) */ for (i = 0; IS_S3WID(wid[i]) && (wid[i] != ve->wid); i++); if (NOT_S3WID(wid[i])) { /* New word; keep only if <maxwpf words already entered, even if >= thresh */ if (maxwpf > 0) { wid[i] = ve->wid; wid[i+1] = BAD_S3WID; --maxwpf; --maxhist; ve->valid = 1; } } else if (! vh->bghist) { --maxhist; ve->valid = 1; } } ckd_free ((void *) wid); heap_destroy (h); /* Garbage collect invalid entries */ vithist_frame_gc (vh, frm); }
huf_tree huf_build_tree(symbol *data, size_t length){ huf_node *trees = (huf_node *)malloc_or_die(2*HUF_SYMBOLS*sizeof(huf_node)); huf_node *newTree = trees; heap_node heapNodes[HUF_SYMBOLS]; heap_node *newHeapNode = heapNodes; heap_node *_heap[HUF_SYMBOLS]; heap_node **heap = _heap-1; size_t heapSize = 0; int freqs[HUF_SYMBOLS]; symbol s; int i; for(s=0; s<HUF_SYMBOLS; s++){ freqs[s] = 0; } for(i=0; i<length; i++){ freqs[data[i]]++; } freqs[HUF_EOF] = 1; for(s=0; s<HUF_SYMBOLS; s++){ if(freqs[s]>0){ newTree->symbol = s; newTree->left = NULL; newTree->right = NULL; newHeapNode->tree = newTree++; newHeapNode->freq = freqs[s]; heap_push(heap, heapSize++, newHeapNode++); } } for(; heapSize>=2; heapSize--){ heap_node *n1 = heap_pop(heap, heapSize); heap_node *n2 = heap_pop(heap, heapSize-1); newTree->left = n1->tree; newTree->right = n2->tree; n2->tree = newTree++; // reuse the old heap node n2->freq += n1->freq; heap_push(heap, heapSize-2, n2); } huf_tree t = {heap_pop(heap, heapSize)->tree, trees}; return t; }
void * priqueue_extract(struct PriQueue *priqueue) { assert(priqueue != NULL); void *ret = heap_top(priqueue->heap); heap_pop(priqueue->heap); return ret; }
static huff_node_t * huff_code_build_tree(heap_t *q) { huff_node_t *root = 0; int32 rf; while (heap_size(q) > 1) { huff_node_t *l, *r, *p; int32 lf, rf; heap_pop(q, (void**)(void *)&l, &lf); heap_pop(q, (void**)(void *)&r, &rf); p = huff_node_new_parent(l, r); heap_insert(q, p, lf + rf); } heap_pop(q, (void **)&root, &rf); return root; }
void *i_request_memory_block() { U32 *block = NULL; if (!heap_empty(&p_heap)) { block = heap_pop(&p_heap); } return block; }
void my_heapsort(int* numbers, int size) { int i; int* heap = malloc(sizeof(int) * size); for (i = 0; i < size; i++) { heap_push(heap, i, numbers[i]); } for (i = 0; i < size; i++) { numbers[i] = heap_pop(heap, size - i); } }
static void test_enumerate (heap_t * heap) { heap_t * clone; clone = heap_clone (heap); while (clone->length > 0) { fprintf (stderr, "% 5.2f\t%s\n", HEAP_PEEK_KEY (clone), (char*) HEAP_PEEK_VALUE (clone)); heap_pop (clone); } }
static void MY_dequeue(struct run_queue *rq, struct proc_struct *proc) { assert(!heap_empty(heap) && proc->rq == rq); rq->proc_num --; // heap part heap_entry_t elm; if (!heap_empty(heap)) elm = heap_pop(heap); }
void heap_sort(T *array, int size) { for(int heap_size = 0; heap_size < size; heap_size++) { heap_push(array, heap_size, array[heap_size]); } for(int heap_size = size; heap_size > 0; heap_size--) { array[heap_size-1] = heap_pop(array, heap_size); } }
// pop next flow, dequeue next packet, push flow back to heap Packet* removePacketFromBuffer(bool virtual_f) { FHeap* heap = virtual_f ? virtual_flows : flows; Flow* flow = heap_front(heap); heap_pop(heap, flow); Packet* pkt = flow_dequeue(flow); heap_push(heap, flow); return pkt; }
void solve_maze() { int i,j,x,y,bx,by,tx,ty,goal,nx,ny,nbx,nby,cost,dist; for(i=0;i<MAXDIM*MAXDIM*MAXDIM*MAXDIM;i++) { st[i].dist = MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM+1; st[i].heap = st[i].last = 0; } heap_init(); for(i=0;i<r;i++) for(j=0;j<c;j++) switch(m[i][j]) { case 'S': x = j; y = i; m[i][j] = '.'; break; case 'B': bx = j; by = i; m[i][j] = '.'; break; case 'T': tx = j; ty = i; m[i][j] = '.'; break; } heap_push(x,y,bx,by,0,0); while(heapsize != 0) { heap_pop(&x,&y,&bx,&by,&dist); for(i=0;i<4;i++) { nx = x+dx[i]; ny = y+dy[i]; cost = 1; if(nx == bx && ny == by) { nbx = bx+dx[i]; nby = by+dy[i]; cost += MAXDIM*MAXDIM*MAXDIM*MAXDIM; } else { nbx = bx; nby = by; } if(nx >= 0 && nx < c && ny >= 0 && ny < r && nbx >= 0 && nbx < c && nby >= 0 && nby < r && m[ny][nx] == '.' && m[nby][nbx] == '.') heap_push(nx,ny,nbx,nby,dist+cost,cost==1?walk[i]:push[i]); } } x = y = 0; for(i=0;i<r;i++) for(j=0;j<c;j++) if(st[j+i*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM].dist < st[x+y*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM].dist) { x = j; y = i; } goal = x+y*MAXDIM+tx*MAXDIM*MAXDIM+ty*MAXDIM*MAXDIM*MAXDIM; if(st[goal].dist > MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM*MAXDIM) printf("Impossible."); else rec_print(x,y,tx,ty); printf("\n\n"); }
void heapsort(int *arr, int num_elems) { int i; heap *h=heap_new(); for(i=0; i<num_elems; i++) { heap_push(h, arr[i]); } for(i=0; i<num_elems; i++) { arr[i]=heap_pop(h); } heap_free(h); }
static ssize_t print_heap(struct heap * h, char __user * out, size_t size, size_t readed){ if ( h->cur_size == 0 || size == 0){ return readed; } size_t copy_len = min(strlen(h->data[0]),size); if(copy_to_user(&out[readed],h->data[0],copy_len)) return -EFAULT; heap_pop(h); return print_heap(h,out,size-copy_len,readed+copy_len); }
static void __test_head_add_gt(struct heap *h, uint32_t v) { if( !heap_full(h) ) { heap_add(h, &v); } else { uint32_t *min = heap_get(h); if( *min < v ) { heap_pop(h); heap_add(h, &v); } } }
int32 heap_destroy(heap_t heap) { void *data; int32 val; /* Empty the heap and free it */ while (heap_pop(heap, &data, &val) > 0); ckd_free((char *) heap); return 0; }
int main (int argc, char * argv[]) { long int array[] = { 10, 3, 4, 8, 2, 9, 7, 1, 2, 6, 5 }; Heap * heap = heap_new_with_data ((void**)array, 11, 0, NULL, NULL); heap_remove(heap, (void *)2); while (heap_size(heap) > 0) { printf ("%li\n", (long int)heap_pop(heap)); } return 0; }
void heap_sort(data_t *begin, data_t *end, less_t &less) { size_t n = 0; for(data_t *it = begin; it != end; ++it, ++n) {} data_t *heap = new data_t[n]; size_t heap_real_size = 0; for(data_t *it = begin; it != end; ++it, ++heap_real_size) { heap_insert(&heap[0], heap_real_size, *it, less); } for(data_t *it = end; it != begin; --it) { *(it - 1) = heap_pop(&heap[0], heap_real_size, less); } delete[] heap; }
void test_heap_test_2(void) { char heap_mem[heap_mem_size(10, sizeof(uint32_t))]; struct heap *h = heap_create( heap_mem , sizeof(heap_mem) , sizeof(uint32_t) , u32_leq , u32_cpy ); fprintf(stdout, "# heap_size: %ld\n", heap_size(h)); size_t N = 3000; size_t n = 0; for(n = N; n; n-- ) { __test_head_add_gt(h, n); } fprintf(stdout, "\n"); while( !heap_empty(h) ) { uint32_t *mp = heap_pop(h); if( !mp ) break; fprintf(stdout, "# %d\n", *mp); } for(n = 0; n <= N; n++ ) { __test_head_add_gt(h, n); } fprintf(stdout, "\n"); while( !heap_empty(h) ) { uint32_t *mp = heap_pop(h); if( !mp ) break; fprintf(stdout, "# %d\n", *mp); } }