static void new_heapSize_twoCount(void **state){ heap_t * newHeap = heap_new(228,30); assert_int_equal(heap_getSize(newHeap), 228); heap_t * newSecondHeap = heap_new(300,30); assert_int_equal(heap_getSize(newSecondHeap), 300); heap_delete(newHeap); heap_delete(newSecondHeap); }
static void new_viewMemory_twoData(void **state){ heap_t * newHeap = heap_new(100,30); memory_t * newMemory = heap_interactionMemory(newHeap, 50); memory_check(newMemory, "Hello World!"); assert_int_equal(strcmp(memory_view(newMemory), "Hello World!"), 0); heap_t * newSecondHeap = heap_new(100,31); memory_t * newSecondMemory = heap_interactionMemory(newSecondHeap, 30); memory_check(newSecondMemory, "Hello C\C++!"); assert_int_equal(strcmp(memory_view(newSecondMemory), "Hello C\C++!"), 0); heap_delete(newHeap); heap_delete(newSecondHeap); }
int bmon_init(batch_monitor_t* batch, int batch_period, int (*item_cmp)( void* a, void* b, void* udata), int (*commit)(batch_monitor_t* m, batch_queue_t* bq)) { uv_mutex_init(&batch->lock); uv_cond_init(&batch->done); batch->queues[0].queue = heap_new((void*)item_cmp, NULL); batch->queues[1].queue = heap_new((void*)item_cmp, NULL); batch->nanos = batch_period; batch->commit = commit; return 0; }
static void new_viewMemory_invalidData(void **state){ heap_t * newHeap = heap_new(100,30); memory_t * newMemory = heap_interactionMemory(newHeap, 20); memory_check(newMemory, "Hello World!"); assert_int_equal(strcmp(memory_view(newMemory), "Zdrastvuyte"), -1); heap_delete(newHeap); }
static void new_checkMemory_normal(void **state){ heap_t * newHeap = heap_new(100,30); memory_t * newMemory = heap_interactionMemory(newHeap, 50); memory_check(newMemory, "I am normal string!"); assert_int_equal(memory_status(newMemory), M_OK); heap_delete(newHeap); }
static void new_checkMemory_empty(void **state){ heap_t * newHeap = heap_new(100,30); memory_t * newMemory = heap_interactionMemory(newHeap, 50); memory_check(newMemory, ""); assert_int_equal(memory_status(newMemory), M_EMPTY); heap_delete(newHeap); }
static void new_memory_fullSize(void **state){ heap_t * newHeap = heap_new(50,30); memory_t * memory = heap_interactionMemory(newHeap, 100); assert_int_equal(heap_status(newHeap), H_FULL); assert_int_equal(memory_status(memory), M_EMPTY); heap_delete(newHeap); }
int main() { heap_node* n[5]; heap *ph = heap_new(); n[4] = heap_insert(ph, 4); n[1] = heap_insert(ph, 1); n[0] = heap_insert(ph, 0); n[3] = heap_insert(ph, 3); n[2] = heap_insert(ph, 2); int i; for(i=0; i<5; i++) { n[i]->value.v_in_mst = -i; n[i]->value.v_not_in_mst = i; } heap_decrease_key(ph, n[3], -1); for(i=0; i<5; i++) { int v_not_in_mst = heap_min(ph); heap_value* val = &n[v_not_in_mst]->value; printf("%d %d %f\n", val->v_in_mst, v_not_in_mst, val->weight); heap_delete_min(ph); } return 0; }
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); }
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; }
static void freeMemory_incorrectIndex_status(void ** state){ heap_t * heap1 = heap_new(5); heap_freeMemory(heap1, 0); heap_freeMemory(heap1, 4); assert_int_equal(heap_getStatus(heap1), MEMORY_INCORRECTDATA); heap_free(heap1); }
static void areadBytesfromMemory_hasNoData_status(void **state){ heap_t * heap1 = heap_new(5); memory_t * mem; mem = heap_addMemory(heap1); assert_null(memory_readBytesfromMemory(mem)); heap_free(heap1); }
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; }
int main() { heap H = heap_new(7, &higher_priority); int *a = malloc(sizeof(int)); int *b = malloc(sizeof(int)); int *c = malloc(sizeof(int)); int *d = malloc(sizeof(int)); int *e = malloc(sizeof(int)); int *f = malloc(sizeof(int)); int *g = malloc(sizeof(int)); *a = 2; *b = 6; *c = 9; *d = 10; *e = 8; *f = 15; *g = 11; heap_add(H, (void*)a); heap_add(H, (void*)b); heap_add(H, (void*)c); heap_add(H, (void*)d); heap_add(H, (void*)e); heap_add(H, (void*)f); heap_add(H, (void*)g); heap_rem_elem(H, (void*)b); print_heap(H); heap_rem_elem(H, (void*)e); print_heap(H); free_heap(H); return 0; }
static void freeMemory_addAndRemoveSameMemory_statusEmpty(void ** state){ heap_t * heap1 = heap_new(5); heap_addMemory(heap1); heap_freeMemory(heap1, 0); assert_int_equal(heap_getStatus(heap1), HEAP_EMPTY); heap_free(heap1); }
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); }
static void addMemory_overflowMemory_status(void ** state){ heap_t * heap1 = heap_new(5); for (int i = 0; i < MAX_AMOUNT_OF_OBJECTS; i++){ heap_addMemory(heap1); } assert_true(heap_amountOfObjects(heap1) == MAX_AMOUNT_OF_OBJECTS); heap_free(heap1); }
static void addMemory_hasPointerToStructure_countOne(void ** state){ heap_t * heap1 = heap_new(5); heap_addMemory(heap1); assert_int_equal(heap_amountOfObjects(heap1), 1); assert_int_equal(heap_getStatus(heap1), HEAP_OK); heap_freeMemory(heap1, 0); heap_free(heap1); }
static void writeBytesInMemory_overflowMemory_status(void ** state){ heap_t * heap1 = heap_new(5); memory_t * mem; mem = heap_addMemory(heap1); memory_writeBytesInMemory(mem, "0123456789"); assert_true(strlen("0123456789") + memory_getSize(mem) < MAX_AMOUNT_OF_BYTES); heap_free(heap1); }
void worker_state_init(void) { worker_next_priority = 0; worker_ready_to_run = heap_new(WORKER_READY_QUEUE_SIZE, (Cmpfunc) worker_priority_cmp); worker_thread_pool = NULL; worker_biglock = GC_NEW(pthread_mutex_t); assert(worker_biglock != NULL); pthread_mutex_init(worker_biglock, NULL); worker_active = 0; }
void *match_thread(void *thread_args) { long i; float score; heap_t *heap = NULL; thread_args_t *args = (thread_args_t *)thread_args; if (args->limit) { // Reserve one extra slot so that we can do an insert-then-extract even // when "full" (effectively allows use of min-heap to maintain a // top-"limit" list of items). heap = heap_new(args->limit + 1, cmp_score); } for ( i = args->thread_index; i < args->path_count; i += args->thread_count ) { args->matches[i].path = RARRAY_PTR(args->haystacks)[i]; if (args->needle_bitmask == UNSET_BITMASK) { args->matches[i].bitmask = UNSET_BITMASK; } if (!NIL_P(args->last_needle) && args->matches[i].score == 0.0) { // Skip over this candidate because it didn't match last // time and it can't match this time either. continue; } args->matches[i].score = calculate_match( args->matches[i].path, args->needle, args->case_sensitive, args->always_show_dot_files, args->never_show_dot_files, args->recurse, args->needle_bitmask, &args->matches[i].bitmask ); if (args->matches[i].score == 0.0) { continue; } if (heap) { if (heap->count == args->limit) { score = ((match_t *)HEAP_PEEK(heap))->score; if (args->matches[i].score >= score) { heap_insert(heap, &args->matches[i]); (void)heap_extract(heap); } } else { heap_insert(heap, &args->matches[i]); } } } return heap; }
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); }
int bt_rarestfirst_selector_poll_best_piece( void *r, const void *peer ) { rarestfirst_t *rf = r; heap_t *hp; peer_t *pr; void *p; int piece_idx; hashmap_iterator_t iter; piece_t *pce; if (!(pr = hashmap_get(rf->peers, peer))) { return -1; } /* if we have no pieces, fail */ if (0 == hashmap_count(pr->have_pieces)) { return -1; } hp = heap_new(__cmp_piece, rf); /* add to priority queue */ for (hashmap_iterator(rf->pieces, &iter); (p = hashmap_iterator_next(rf->pieces, &iter));) { /* only add if peer has it */ if (hashmap_get(pr->have_pieces, p)) { pce = hashmap_get(rf->pieces, p); heap_offer(hp, pce); } } /* queue best from priority queue */ if ((pce = heap_poll(hp))) { piece_idx = pce->idx; hashmap_remove(rf->pieces, (void *) (long) pce->idx); hashmap_put(rf->pieces_polled, (void *) (long) pce->idx, pce); } else { piece_idx = -1; } heap_free(hp); return piece_idx; }
int ropa_init (unsigned num_cb, unsigned num_cli, unsigned num_cc, unsigned hashsz_cb, unsigned hashsz_cli, unsigned hashsz_cc) { ht_callbacks = hashtabnew (hashsz_cb, callbacks_cmp, callbacks_hash); if (ht_callbacks == NULL) errx (1, "ropa_init: failed to create hashtable for callbacks"); ht_clients_ip = hashtabnew (hashsz_cli, clients_cmp_ip, clients_hash_ip); if (ht_clients_ip == NULL) errx (1, "ropa_init: failed to create hashtable for clients_ip"); ht_clients_uuid = hashtabnew (hashsz_cli, clients_cmp_uuid, clients_hash_uuid); if (ht_clients_uuid == NULL) errx (1, "ropa_init: failed to create hashtable for clients_uuid"); ht_ccpairs = hashtabnew (hashsz_cc, ccpairs_cmp, ccpairs_hash); if (ht_ccpairs == NULL) errx (1, "ropa_init: failed to create hashtable for ccpairs"); lru_clients = listnew (); if (lru_clients == NULL) errx (1, "ropa_init: failed to create lru for clients"); lru_ccpair = listnew (); if (lru_ccpair == NULL) errx (1, "ropa_init: failed to create list for ccpairs"); lru_callback = listnew (); if (lru_callback == NULL) errx (1, "ropa_init: failed to create list for callback"); heap_ccpairs = heap_new (num_cc, ccpair_cmp_time); if (heap_ccpairs == NULL) errx (1, "ropa_init: failed to create heap for ccpairs"); create_clients(num_cli); create_callbacks(num_cb); create_ccpairs(num_cc); uuid_init_simple (&server_uuid, 0x82ED305E); if (LWP_CreateProcess (heapcleaner, ROPA_STACKSIZE, 1, NULL, "heap-invalidator", &cleaner_pid)) errx (1, "ropa_init: LWP_CreateProcess failed"); debuglevel = mlog_log_get_level_num(); /* XXX */ return 0; }
sim_event_queue_t* sim_new_event_queue(void) { sim_event_queue_t *queue = smalloc(sizeof(sim_event_queue_t)); queue->freeEvents = smalloc(sizeof(sim_event_t) * OO_EVENT_QUEUE_INIT_LEN); queue->activeEventHeap = heap_new(8, (compute_rank_f)EventRank); for (int i = 0 ; i < OO_EVENT_QUEUE_INIT_LEN; i ++) { queue->freeEvents[i].next = &queue->freeEvents[i+1]; queue->freeEvents[i].data = NULL; queue->freeEvents[i].handler = NULL; queue->freeEvents[i].fireTime = 0; } return queue; }
/** * Create a managed VBO. * @param n_elem number of elements in new VBO * @return VBO ID*/ unsigned int ren_vbom_init( const int n_elem ) { if (!__vbos) { __vbos = arraylistf_new(); } vbo_t *vb; vb = calloc(1, sizeof(vbo_t)); vb->n_elem = n_elem; vb->heap = heap_new(__freeslot_compare, NULL); /* we are wrapping a VBO */ vb->vbo = ren_vbo_init(n_elem); return arraylistf_add(__vbos, vb) + 1; }
/** * Create a managed VBO. * @param n_elem number of elements in new VBO * @return VBO ID*/ unsigned int ren_vbosquare_init(const int n_elem) { if (!__vbos) { __vbos = arraylistf_new(); } vbo_t *vb; /* create vbo */ vb = calloc(1, sizeof(vbo_t)); vb->n_elem = n_elem; vb->heap = heap_new(__freeslot_compare, NULL); ren_vertex_tc_t verts[n_elem]; ren_vertex_position_t verts_pos[n_elem]; unsigned int size; /* vertex coords */ size = sizeof(ren_vertex_tc_t) * n_elem; memset(verts, 0, size); glGenBuffers(1, &vb->vtxcoord); glBindBuffer(GL_ARRAY_BUFFER_ARB, vb->vtxcoord); glBufferData(GL_ARRAY_BUFFER_ARB, size, verts, GL_DYNAMIC_DRAW); //GL_STATIC_DRAW); #if 0 /* position vertex attribute */ size = sizeof(ren_vertex_position_t) * n_elem; memset(verts_pos, 0, size); glGenBuffers(1, &vb->vtxpos); glBindBuffer(GL_ARRAY_BUFFER, vb->vtxpos); glBufferData(GL_ARRAY_BUFFER, size, verts_pos, GL_DYNAMIC_DRAW); #endif /* clean up */ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); return arraylistf_add(__vbos, vb) + 1; }
huff_code_t * huff_code_build_int(int32 const *values, int32 const *frequencies, int nvals) { huff_code_t *hc; huff_node_t *root; heap_t *q; int i; hc = ckd_calloc(1, sizeof(*hc)); hc->refcount = 1; hc->type = HUFF_CODE_INT; /* Initialize the heap with nodes for each symbol. */ q = heap_new(); for (i = 0; i < nvals; ++i) { heap_insert(q, huff_node_new_int(values[i]), frequencies[i]); } /* Now build the tree, which gives us codeword lengths. */ root = huff_code_build_tree(q); heap_destroy(q); if (root == NULL || root->nbits > 64) { E_ERROR("Huffman trees currently limited to 32 bits\n"); huff_node_free_int(root); huff_code_free(hc); return NULL; } /* Build a canonical codebook. */ hc->maxbits = root->nbits; huff_code_canonicalize(hc, root); /* Tree no longer needed. */ huff_node_free_int(root); return hc; }
huff_code_t * huff_code_build_str(char * const *values, int32 const *frequencies, int nvals) { huff_code_t *hc; huff_node_t *root; heap_t *q; int i; hc = (huff_code_t*)ckd_calloc(1, sizeof(*hc)); hc->refcount = 1; hc->type = HUFF_CODE_STR; /* Initialize the heap with nodes for each symbol. */ q = heap_new(); for (i = 0; i < nvals; ++i) { heap_insert(q, huff_node_new_str(values[i]), frequencies[i]); } /* Now build the tree, which gives us codeword lengths. */ root = huff_code_build_tree(q); heap_destroy(q); if (root == 0 || root->nbits > 32) { E_ERROR("Huffman trees currently limited to 32 bits\n"); huff_node_free_str(root, TRUE); huff_code_free(hc); return 0; } /* Build a canonical codebook. */ hc->maxbits = root->nbits; huff_code_canonicalize(hc, root); /* Tree no longer needed (note we retain pointers to its strings). */ huff_node_free_str(root, FALSE); return hc; }
void my_heapsort (int ** arrayptr, int size) { int counter = 0; int *array = *arrayptr; heap_element *element; struct heap *heap = heap_new(); /*lets sort that array heap style */ for (counter = 0; counter < size; counter++) { element = malloc(sizeof *element); element->value = counter; element->element = NULL; heap_add(heap, element); } for (counter = 0; counter < size; counter++) { element = heap_pop(heap); array[counter] = element->value; free(element); } heap_free(heap); }