int main() { struct heap heap; int result = heap_init(&heap, int_compare); assert(result == 0); assert(heap_empty(&heap)); assert(heap_size(&heap) == 0); static const intptr_t VALUES[] = {12, 2955, 7, 99, 51, 1, 691050}; static const intptr_t EXPECTED[] = {1, 7, 12, 51, 99, 2955, 691050}; int count = sizeof(VALUES) / sizeof(VALUES[0]); for (int i = 0; i < count; i++) { result = heap_push(&heap, (void*)VALUES[i]); assert(result == 0); } assert(heap_size(&heap) == count); assert(!heap_empty(&heap)); assert((intptr_t)heap_min(&heap) == (intptr_t)1); assert(heap_size(&heap) == count); for (int i = 0; i < count; i++) { intptr_t value = (intptr_t)heap_pop_min(&heap); assert(value == EXPECTED[i]); } assert(heap_empty(&heap)); assert(heap_size(&heap) == 0); assert(heap_min(&heap) == NULL); assert(heap_pop_min(&heap) == NULL); // Repeat reordered static const intptr_t VALUES2[] = {2955, 12, 691050, 99, 51, 1, 7}; for (int i = 0; i < count; i++) { result = heap_push(&heap, (void*)VALUES2[i]); assert(result == 0); } for (int i = 0; i < count; i++) { intptr_t value = (intptr_t)heap_pop_min(&heap); assert(value == EXPECTED[i]); } assert(heap_empty(&heap)); assert(heap_size(&heap) == 0); assert(heap_min(&heap) == NULL); assert(heap_pop_min(&heap) == NULL); heap_free(&heap); return 0; }
int search(struct arg_struct *arg) { // printf("# Thread working: %u\n", (int)pthread_self()); int n = arg->topic; int start = arg->startidx; int end = arg->endidx; heap* h = arg->h; heap_create(h,0,NULL); int i=0, j=0; int base = arg->base; float score; int t; float* min_key; int* min_val; for (i=start; i<end; i++) { if (tweetids[i] > topics2011_time[n]) { base += doclengths[i]; continue; } score = 0; int tf[topics2011[n][1]]; memset(tf,0,sizeof(tf)); for (j=0; j<doclengths[i]; j++) { for (t=2; t<2+topics2011[n][1]; t++) { if (collection_pos[base+j] == topics2011[n][t]) { tf[t - 2] ++; } } } for (t = 0; t < topics2011[n][1]; t++) { if (tf[t] > 0) { score += log(1 + tf[t]/(MU * (cf[topics2011[n][t+2]] + 1) / (TOTAL_TERMS + 1))) + log(MU / (doclengths[i] + MU)); } } if (score > 0) { int size = heap_size(h); if ( size < TOP_K ) { int *docid = malloc(sizeof(int)); *docid = i; float *scorez = malloc(sizeof(float)); *scorez = score; heap_insert(h, scorez, docid); } else { heap_min(h, (void**)&min_key, (void**)&min_val); if (score > *min_key) { heap_delmin(h, (void**)&min_key, (void**)&min_val); int *docid = malloc(sizeof(int)); *docid = i; float *scorez = malloc(sizeof(float)); *scorez = score; heap_insert(h, scorez, docid); } } } base += doclengths[i]; } return 0; }
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; }
heap* heap_union(heap* H1, heap* H2){ if(!H1) return H2; if(!H2) return H1; if (heap_min(H2).key < heap_min(H1).key){ return heap_union(H2, H1); } node* H1first = H1; node* H1last = H1first->left; node* H2first = H2; node* H2last = H2first->left; H1last->right = H2first; H2first->left = H1last; H2last->right = H1first; H1first->left = H2last; return H1first; }
void* heap_extract_min(Heap* heap) { void* min = heap_min(heap); if(heap_size(heap) > 0) { heap_set(heap, 0, heap_last(heap)); heap_bubble_down(heap, 0); } heap->size--; return min; }
static int next_timeout(const evLoop *loop) { const struct heap_node *heap_node; const evTimer *handle; uint64_t diff; heap_node = heap_min((const struct heap*) &loop->timer_heap); if (heap_node == NULL) return -1; /* block indefinitely */ handle = container_of(heap_node, const evTimer, heap_node); if (handle->timeout <= loop->time) return 0; diff = handle->timeout - loop->time; if (diff > INT_MAX) diff = INT_MAX; return diff; }
static list_t * etimer_extract_ready(etimer_t *timer) { list_t *ready; struct timeval now; etimer_event_t *event; ready = list_create(); gettimeofday(&now, NULL); while( (event = heap_min(timer->queue)) ) { if( timercmp(&event->deadline, &now, <) ) { heap_shift(timer->queue); list_append(ready, event); } else { return ready; } }
void uv__run_timers(uv_loop_t* loop) { struct heap_node* heap_node; uv_timer_t* handle; for (;;) { heap_node = heap_min(timer_heap(loop)); if (heap_node == NULL) break; handle = container_of(heap_node, uv_timer_t, heap_node); if (handle->timeout > loop->time) break; uv_timer_stop(handle); uv_timer_again(handle); handle->timer_cb(handle); } }
int uv__next_timeout(const uv_loop_t* loop) { const struct heap_node* heap_node; const uv_timer_t* handle; uint64_t diff; heap_node = heap_min(timer_heap(loop)); if (heap_node == NULL) return -1; /* block indefinitely */ handle = container_of(heap_node, uv_timer_t, heap_node); if (handle->timeout <= loop->time) return 0; diff = handle->timeout - loop->time; if (diff > INT_MAX) diff = INT_MAX; return (int) diff; }
static void loop_run_timers(evLoop *loop) { struct heap_node *heap_node; evTimer *timer; for (;;) { loop_run_immediate(loop); heap_node = heap_min((struct heap*) &loop->timer_heap); if (heap_node == NULL) break; timer = container_of(heap_node, evTimer, heap_node); if (timer->timeout > loop->time) { break; } timer_again(timer->handle); timer->handle->cb(timer->handle); if (!timer->repeat || timer->repeat == 0) { timer_close(timer->handle); } } }
static int etimer_calc_timeout(etimer_t *timer) { etimer_event_t *event; struct timeval now, sub; int timeout; if( !(event = heap_min(timer->queue)) ) return -1; gettimeofday(&now, NULL); if( timercmp(&event->deadline, &now, <) ) return 0; timersub(&event->deadline, &now, &sub); timeout = sub.tv_sec*1000 + sub.tv_usec/1000 + (sub.tv_usec % 1000 ? 1 : 0); if( timeout < 0 ) timeout = 0; return timeout; }
void * heap_min_key(heap* h) { void *key = NULL; heap_min(h, &key, NULL); return key; }
int main(int argc, const char* argv[]) { if (argc <= 1) { printf("PLEASE ENTER THREAD NUMBER!\n"); return 0; } int nthreads=atoi(argv[1]); printf("Number of threads: %d\n", nthreads); init_pos(); double total = 0; int N = 3; int count; for (count = 1; count <= N; count ++) { struct timeval begin, end; double time_spent; gettimeofday(&begin, NULL); int n; for (n=0; n<NUM_TOPICS; n++) { // printf("Processing topic %d...\n", topics2011[n][0]); heap h_array[nthreads]; memset(h_array,0,sizeof(h_array)); struct threadpool *pool; pool = threadpool_init(nthreads); int i = 0; for (i=0; i<nthreads; i++) { struct arg_struct *args = malloc(sizeof *args); args->topic = n; args->startidx = i*(int)(ceil((double)NUM_DOCS / nthreads)); if ((i+1)*(int)(ceil((double)NUM_DOCS / nthreads)) > NUM_DOCS) { args->endidx = NUM_DOCS; } else { args->endidx = (i+1)*(int)(ceil((double)NUM_DOCS / nthreads)); } args->base = termindexes[nthreads-1][i]; heap h; h_array[i] = h; args->h = &h_array[i]; threadpool_add_task(pool,search,args,0); } threadpool_free(pool,1); heap h_merge; heap_create(&h_merge,0,NULL); float* min_key_merge; int* min_val_merge; for (i=0; i<nthreads; i++) { float* min_key; int* min_val; while(heap_delmin(&h_array[i], (void**)&min_key, (void**)&min_val)) { int size = heap_size(&h_merge); if ( size < TOP_K ) { heap_insert(&h_merge, min_key, min_val); } else { heap_min(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge); if (*min_key_merge < *min_key) { heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge); heap_insert(&h_merge, min_key, min_val); } } } heap_destroy(&h_array[i]); } int rank = TOP_K; while (heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge)) { printf("MB%02d Q0 %ld %d %f Scan1_pos_multithread_intraquery\n", (n+1), tweetids[*min_val_merge], rank, *min_key_merge); rank--; } heap_destroy(&h_merge); } gettimeofday(&end, NULL); time_spent = (double)((end.tv_sec * 1000000 + end.tv_usec) - (begin.tv_sec * 1000000 + begin.tv_usec)); total = total + time_spent / 1000.0; } printf("Total time = %f ms\n", total/N); printf("Time per query = %f ms\n", (total/N)/NUM_TOPICS); }
void Astar() { int i,v1,v2,j=0; node tmp,now; memset(close,0,sizeof(close)); memset(open,0,sizeof(open)); heaplen=1; for(i=0;i<strlen(s);++i){ if(s[i]=='x'){ heap[1].p=j; heap[1].s[j++]='0'; } if(s[i]>'0'&&s[i]<='9') heap[1].s[j++]=s[i]; } for(i=0;i<=8;i++) if(heap[1].s[i]=='x'){ heap[1].p=i; heap[1].s[i]='0'; } if(inverse(heap[1].s)%2){ printf("unsolvable\n"); return; } heap[1].h=figureh(heap[1]); heap[1].g=0; figuref(heap[1]); dir[cantor_expansion(heap[1])]=-1; while(heaplen){ now=heap_min(); v1=cantor_expansion(now); open[v1]=0; close[v1]=1; closef[v1]=now.f; //printf("%s**",now.s);print(v1);printf("\n"); if(strcmp(now.s,"123456780")==0){ print(v1); printf("\n"); return; } tmp.g=now.g+1; for(i=0;i<4;i++){ tmp.p=now.p+x[i]; if(tmp.p<0||tmp.p>8) continue; if((i==0||i==1)&&tmp.p/3!=now.p/3) continue; strcpy(tmp.s,now.s); tmp.s[now.p]=tmp.s[tmp.p]; tmp.s[tmp.p]='0'; //printf("%s\n",tmp.s); tmp.h=figureh(tmp); figuref(tmp); v2=cantor_expansion(tmp); if(open[v2]==0&&close[v2]==0){ open[v2]=1; dir[v2]=i; heap_insert(tmp); pre[v2]=v1; } else{ if(open[v2]&&heap_update(tmp)){ dir[v2]=i; pre[v2]=v1; } else if(tmp.f<closef[v2]){ open[v2]=1; close[v2]=0; dir[v2]=i; pre[v2]=v1; heap_insert(tmp); } } } } }
uint64_t pq_first( rtems_task_argument tid ) { return heap_min(tid); }