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); } } }
void heap_decrease_key(heap** H, elem* x, int newKey){ assert(H && *H); assert(x && x->key >= newKey); x->key = newKey; if(x->parent && x->parent->key > newKey){ if (x->left == x){ assert(x->parent->degree == 2); x->parent->kid = NULL; }else{ assert(x->parent->degree > 2); x->left->right = x->right; x->right->left = x->left; x->parent->kid = x->left; } x->parent->degree--; heap_add(H, x); if (! x->parent->hasLostKid){ x->parent->hasLostKid = 1; }else{ heap_decrease_key(H, x->parent, x->parent->key); } }else{ if (newKey < (*H)->key){ assert(!x->parent); *H = x; } } }
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() { int N = 0; int i = 0; int cmd = 0; int cur = 0; freopen("heap.in", "rt", stdin); freopen("heap.out", "wt", stdout); scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &cmd); switch (cmd) { case 0: scanf("%d", &cur); heap_add(cur); break; case 1: assert(size > 0); printf("%d\n", heap_extract_max()); break; } } //fcloseall(); return 0; }
inline void heapAdjustOrAdd ( Heap &heap, HeapKey<K, W, L> k) { typename Heap::iterator heapLoc = k.location(); if (heapLoc) heapAdjustUp(heap.begin(), heapLoc); else heap_add(heap, k); }
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 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; }
HEAP_ERR_E heap_min_insert (HEAP_T* h, unsigned long key, void* data, unsigned long* i) { if (h->type != DS_HEAP_MIN) return HEAP_ERR_WRONG_TYPE; heap_add (h, HEAP_NIL_KEY, data, i); heap_decrease_key (h, h->heap_size-1, key); return HEAP_ERR_OK; }
void OctreeQuant(RGB* src_pixels, int pixels_count, u8* dst_pixels, RGB dst_palette[256]) { node_heap heap = { 0, 0, 0 }; oct_node* root = node_new(0, 0, 0); RGB* pix = src_pixels; for (int i = 0; i < pixels_count; i++) { heap_add(&heap, node_insert(root, (u8*)pix)); pix++; } while (heap.n > 256 /* palette size */ + 1) { heap_add(&heap, node_fold(pop_heap(&heap))); } for (int i = 1; i < heap.n; i++) { oct_node* node = heap.buf[i]; double c = node->count; node->r = (u32)(node->r / c + .5); node->g = (u32)(node->g / c + .5); node->b = (u32)(node->b / c + .5); RGB* plt_entry = dst_palette + (i - 1); plt_entry->red = node->r; plt_entry->green = node->g; plt_entry->blue = node->b; } RGB* sptr = src_pixels; u8* dptr = dst_pixels; for (int i = 0; i < pixels_count; i++) { color_replace(root, (u8*)sptr, dptr); sptr++; dptr++; } node_free(); free(heap.buf); }
int main(int argc, char * argv[]) { int length, num, c; int * array; heap h; if (argc != 3) { fprintf(stderr,"Usage: %s <heap-length> <num-tests>\n", argv[0]); return 1; } length = atoi(argv[1]); num = atoi(argv[2]); if (length <= 0) { fprintf(stderr,"Error: length <= 0\n"); return 1; } h = heap_create(); array = ALLOC(int,length); nusmv_assert(array); for (c = 1; c <= num; c++) { int i, i1, i2; printf("Test %2d:", c); for (i = 0; i < length; i++) { array[i] = i+1; } for (i = 0; i < 4 * length; i++) { i1 = utils_random() % length; i2 = utils_random() % length; if (i1 == i2) continue; array[i1] = array[i1] + array[i2]; array[i2] = array[i1] - array[i2]; array[i1] = array[i1] - array[i2]; } for (i = 0; i < length; i++) { printf(" %d", array[i]); heap_add(h, -array[i], (void *)array[i]); } printf("\n------->"); for (i = 0; i < length; i++) { int val = (int)heap_getmax(h); printf(" %d", val); nusmv_assert(val == i+1); } printf("\n"); assert(heap_isempty(h)); } heap_destroy(h); return 0; }
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); } }
static void invalidate_buckets_lru(struct cache *ca) { struct bucket *b; ssize_t i; ca->heap.used = 0; for_each_bucket(b, ca) { /* * If we fill up the unused list, if we then return before * adding anything to the free_inc list we'll skip writing * prios/gens and just go back to allocating from the unused * list: */ if (fifo_full(&ca->unused)) return; if (!can_invalidate_bucket(ca, b)) continue; if (!GC_SECTORS_USED(b) && bch_bucket_add_unused(ca, b)) continue; if (!heap_full(&ca->heap)) heap_add(&ca->heap, b, bucket_max_cmp); else if (bucket_max_cmp(b, heap_peek(&ca->heap))) { ca->heap.data[0] = b; heap_sift(&ca->heap, 0, bucket_max_cmp); } } for (i = ca->heap.used / 2 - 1; i >= 0; --i) heap_sift(&ca->heap, i, bucket_min_cmp); while (!fifo_full(&ca->free_inc)) { if (!heap_pop(&ca->heap, b, bucket_min_cmp)) { /* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything */ ca->invalidate_needs_gc = 1; wake_up_gc(ca->set); return; } invalidate_one_bucket(ca, b); } }
// create a new heap with the size given ramaddr_t* heap_init(size_t heapsize) { ramaddr_t* rv = malloc(sizeof(ramaddr_t)); memset(rv,0,sizeof(ramaddr_t)); int i=0; ramaddr_t* top = rv; while(i < heapsize) { heap_add(rv); rv->value = 0xDEAD; // initial ram value should be something we can easily recognise // move the list right after adding rv = (ramaddr_t*)rv->next; i++; } rv = top; // fast-rewind the list before returning it return rv; }
void heap_sort(int *num, int length) { int tmp, i; // 最初のヒープを作る for (i=1; i<length; i++) { heap_add(num, length, i); } print_array(num, length); // ヒープから最小値を抜き配列の後ろから詰めていく for (i=0; i<length; i++) { tmp=num[length-1-i]; num[length-1-i]=num[0]; num[0]=tmp; heap_del(num, length-1-i, 0); } // 逆順にする for (i=0; i<length/2; i++) { tmp = num[i]; num[i] = num[length-i-1]; num[length-i-1] = tmp; } }
void heap_consolidate(heap** H){ node* x = *H; if (!x) return; node** A = calloc(100, sizeof(node)); memset(A, '\0', 100); assert(x->degree >= 0); node* last = x->left; while(x != last){ node* next = x->right; heap_match_degrees(H, A, x); x = next; } heap_match_degrees(H, A, last); *H = heap_init(); for (int i=0; i<100; i++){ if (A[i]){ heap_add(H, A[i]); } } free(A); }
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); }
static void invalidate_buckets_lru(struct cache *ca) { struct bucket *b; ssize_t i; ca->heap.used = 0; for_each_bucket(b, ca) { if (!bch_can_invalidate_bucket(ca, b)) continue; if (!heap_full(&ca->heap)) heap_add(&ca->heap, b, bucket_max_cmp); else if (bucket_max_cmp(b, heap_peek(&ca->heap))) { ca->heap.data[0] = b; heap_sift(&ca->heap, 0, bucket_max_cmp); } } for (i = ca->heap.used / 2 - 1; i >= 0; --i) heap_sift(&ca->heap, i, bucket_min_cmp); while (!fifo_full(&ca->free_inc)) { if (!heap_pop(&ca->heap, b, bucket_min_cmp)) { /* * We don't want to be calling invalidate_buckets() * multiple times when it can't do anything */ ca->invalidate_needs_gc = 1; wake_up_gc(ca->set); return; } bch_invalidate_one_bucket(ca, b); } }
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); }
void worker_create(void (*func)(Worker *, void *), void *arg) { if (null(worker_thread_pool)) { pthread_t newthread; pthread_attr_t attr; Worker *t = GC_NEW(Worker); assert(t != NULL); t->sleep = cond_new(); t->func = func; t->arg = arg; t->priority = worker_next_priority++; t->blocking = NULL; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, max(PTHREAD_STACK_MIN, 1024 * 1024)); pthread_create(&newthread, &attr, (void *(*)(void *)) worker_loop, (void *) t); pthread_detach(newthread); } else { Worker *t = car(worker_thread_pool); worker_thread_pool = cdr(worker_thread_pool); t->func = func; t->arg = arg; t->priority = worker_next_priority++; t->blocking = NULL; if (!heap_isempty(worker_ready_to_run)) { /* wait for older threads that are ready to run */ worker_wake_up_next(); heap_add(worker_ready_to_run, t); } else { cond_signal(t->sleep); } } }
void minHeapInsert(KEY_TYPE key ,VALUE_TYPE value, Heap *heap){ heap_add(key, value, heap); minHeapDecreaseKey(heap->size-1, heap); }
/* add a new unvisited cells to the cells-to-treat list * the list is in fact a min_heap. Cell at index i has its sons at 2*i+1 and 2*i+2 */ static void TCOD_path_push_cell(TCOD_path_data_t *path, int x, int y) { heap_add(path,path->heap,x,y); }
/* add new element at the end of the heap and then enforces heap property */ void heap_insert(heap* h, int x) { heap_add(h, x); heap_swap_up(h, h->len-1); }
int init_search(int depr_fd) { int r, c, r_nbr, c_nbr, ct_dir; CELL *depr_buf, ele_value; int nextdr[8] = { 1, -1, 0, 0, -1, 1, 1, -1 }; int nextdc[8] = { 0, 0, -1, 1, 1, -1, 1, -1 }; char asp_value, is_null; WAT_ALT wa; ASP_FLAG af, af_nbr; GW_LARGE_INT n_depr_cells = 0; nxt_avail_pt = heap_size = 0; /* load edge cells and real depressions to A* heap */ if (depr_fd >= 0) depr_buf = Rast_allocate_buf(CELL_TYPE); else depr_buf = NULL; G_message(_("Initializing A* search...")); for (r = 0; r < nrows; r++) { G_percent(r, nrows, 2); if (depr_fd >= 0) { Rast_get_row(depr_fd, depr_buf, r, CELL_TYPE); } for (c = 0; c < ncols; c++) { seg_get(&aspflag, (char *)&af, r, c); is_null = FLAG_GET(af.flag, NULLFLAG); if (is_null) continue; asp_value = 0; if (r == 0 || r == nrows - 1 || c == 0 || c == ncols - 1) { if (r == 0 && c == 0) asp_value = -7; else if (r == 0 && c == ncols - 1) asp_value = -5; else if (r == nrows - 1 && c == 0) asp_value = -1; else if (r == nrows - 1 && c == ncols - 1) asp_value = -3; else if (r == 0) asp_value = -2; else if (c == 0) asp_value = -4; else if (r == nrows - 1) asp_value = -6; else if (c == ncols - 1) asp_value = -8; seg_get(&watalt, (char *)&wa, r, c); ele_value = wa.ele; heap_add(r, c, ele_value); FLAG_SET(af.flag, INLISTFLAG); FLAG_SET(af.flag, EDGEFLAG); af.asp = asp_value; seg_put(&aspflag, (char *)&af, r, c); continue; } /* any neighbour NULL ? */ for (ct_dir = 0; ct_dir < sides; ct_dir++) { /* get r, c (r_nbr, c_nbr) for neighbours */ r_nbr = r + nextdr[ct_dir]; c_nbr = c + nextdc[ct_dir]; seg_get(&aspflag, (char *)&af_nbr, r_nbr, c_nbr); is_null = FLAG_GET(af_nbr.flag, NULLFLAG); if (is_null) { asp_value = -1 * drain[r - r_nbr + 1][c - c_nbr + 1]; seg_get(&watalt, (char *)&wa, r, c); ele_value = wa.ele; heap_add(r, c, ele_value); FLAG_SET(af.flag, INLISTFLAG); FLAG_SET(af.flag, EDGEFLAG); af.asp = asp_value; seg_put(&aspflag, (char *)&af, r, c); break; } } if (asp_value) /* some neighbour was NULL, point added to list */ continue; /* real depression ? */ if (depr_fd >= 0) { if (!Rast_is_c_null_value(&depr_buf[c]) && depr_buf[c] != 0) { seg_get(&watalt, (char *)&wa, r, c); ele_value = wa.ele; heap_add(r, c, ele_value); FLAG_SET(af.flag, INLISTFLAG); FLAG_SET(af.flag, DEPRFLAG); af.asp = asp_value; seg_put(&aspflag, (char *)&af, r, c); n_depr_cells++; } } } } G_percent(nrows, nrows, 2); /* finish it */ if (depr_fd >= 0) { Rast_close(depr_fd); G_free(depr_buf); } G_debug(1, "%lld edge cells", heap_size - n_depr_cells); if (n_depr_cells) G_debug(1, "%lld cells in depressions", n_depr_cells); return 1; }
static void *worker_loop(Worker *t) { int i; enum worker_transaction_states state; lock(); if (!heap_isempty(worker_ready_to_run)) { /* wait for older threads that are ready to run */ worker_wake_up_next(); heap_add(worker_ready_to_run, t); cond_wait(t->sleep); } /* let threads expire after a while so the thread pool can grow and shrink * as needed without hard limits on the number of threads */ for (i = 0; i < THREAD_LIFETIME; i++) { if (i > 0) { /* wait in the pool for a request */ worker_thread_pool = append_elt(worker_thread_pool, t); cond_wait(t->sleep); } worker_active++; /* initialize the exception handler and catch exceptions */ do { state = setjmp(t->jmp); if (state == WORKER_BLOCKED) { /* wait for the blocking transaction to finish, * then start over */ worker_cleanup(t); worker_wake_up_next(); cond_wait(t->sleep); } else if (state == WORKER_MULTISTEP) { /* wait for the next step in this grant/revoke op to wake us up * with more work to do */ if (DEBUG_VERBOSE) printf("WORKER_MULTISTEP sleeping\n"); worker_cleanup(t); t->func = NULL; t->arg = NULL; worker_wake_up_next(); while (t->func == NULL) cond_wait(t->sleep); if (DEBUG_VERBOSE) printf("WORKER_MULTISTEP waking up\n"); } else if (state == WORKER_RETRY) { worker_cleanup(t); } } while (state != WORKER_ZERO); /* process the request */ if (t->func != NULL) t->func(t, t->arg); worker_cleanup(t); t->func = NULL; /* make anyone waiting on this transaction ready to run, then run one */ if (!null(t->blocking)) { for ( ; !null(t->blocking); t->blocking = cdr(t->blocking)) heap_add(worker_ready_to_run, car(t->blocking)); } worker_active--; worker_wake_up_next(); } unlock(); return NULL; }
int do_astar(void) { int r, c, r_nbr, c_nbr, ct_dir; GW_LARGE_INT first_cum, count; int nextdr[8] = { 1, -1, 0, 0, -1, 1, 1, -1 }; int nextdc[8] = { 0, 0, -1, 1, 1, -1, 1, -1 }; CELL ele_val, ele_up, ele_nbr[8]; WAT_ALT wa; ASP_FLAG af; char is_in_list, is_worked; HEAP_PNT heap_p; /* sides * |7|1|4| * |2| |3| * |5|0|6| */ int nbr_ew[8] = { 0, 1, 2, 3, 1, 0, 0, 1 }; int nbr_ns[8] = { 0, 1, 2, 3, 3, 2, 3, 2 }; double dx, dy, dist_to_nbr[8], ew_res, ns_res; double slope[8]; struct Cell_head window; int skip_diag; count = 0; first_cum = n_points; G_message(_("A* Search...")); Rast_get_window(&window); for (ct_dir = 0; ct_dir < sides; ct_dir++) { /* get r, c (r_nbr, c_nbr) for neighbours */ r_nbr = nextdr[ct_dir]; c_nbr = nextdc[ct_dir]; /* account for rare cases when ns_res != ew_res */ dy = abs(r_nbr) * window.ns_res; dx = abs(c_nbr) * window.ew_res; if (ct_dir < 4) dist_to_nbr[ct_dir] = dx + dy; else dist_to_nbr[ct_dir] = sqrt(dx * dx + dy * dy); } ew_res = window.ew_res; ns_res = window.ns_res; while (heap_size > 0) { G_percent(count++, n_points, 1); if (count > n_points) G_fatal_error(_("%lld surplus points"), heap_size); if (heap_size > n_points) G_fatal_error (_("Too many points in heap %lld, should be %lld"), heap_size, n_points); heap_p = heap_drop(); r = heap_p.pnt.r; c = heap_p.pnt.c; ele_val = heap_p.ele; for (ct_dir = 0; ct_dir < sides; ct_dir++) { /* get r, c (r_nbr, c_nbr) for neighbours */ r_nbr = r + nextdr[ct_dir]; c_nbr = c + nextdc[ct_dir]; slope[ct_dir] = ele_nbr[ct_dir] = 0; skip_diag = 0; /* check that neighbour is within region */ if (r_nbr < 0 || r_nbr >= nrows || c_nbr < 0 || c_nbr >= ncols) continue; seg_get(&aspflag, (char *)&af, r_nbr, c_nbr); is_in_list = FLAG_GET(af.flag, INLISTFLAG); is_worked = FLAG_GET(af.flag, WORKEDFLAG); if (!is_worked) { seg_get(&watalt, (char *)&wa, r_nbr, c_nbr); ele_nbr[ct_dir] = wa.ele; slope[ct_dir] = get_slope(ele_val, ele_nbr[ct_dir], dist_to_nbr[ct_dir]); } /* avoid diagonal flow direction bias */ if (!is_in_list) { if (ct_dir > 3 && slope[ct_dir] > 0) { if (slope[nbr_ew[ct_dir]] > 0) { /* slope to ew nbr > slope to center */ if (slope[ct_dir] < get_slope(ele_nbr[nbr_ew[ct_dir]], ele_nbr[ct_dir], ew_res)) skip_diag = 1; } if (!skip_diag && slope[nbr_ns[ct_dir]] > 0) { /* slope to ns nbr > slope to center */ if (slope[ct_dir] < get_slope(ele_nbr[nbr_ns[ct_dir]], ele_nbr[ct_dir], ns_res)) skip_diag = 1; } } } if (!skip_diag) { if (is_in_list == 0) { ele_up = ele_nbr[ct_dir]; af.asp = drain[r_nbr - r + 1][c_nbr - c + 1]; heap_add(r_nbr, c_nbr, ele_up); FLAG_SET(af.flag, INLISTFLAG); seg_put(&aspflag, (char *)&af, r_nbr, c_nbr); } else if (is_in_list && is_worked == 0) { if (FLAG_GET(af.flag, EDGEFLAG)) { /* neighbour is edge in list, not yet worked */ if (af.asp < 0) { /* adjust flow direction for edge cell */ af.asp = drain[r_nbr - r + 1][c_nbr - c + 1]; seg_put(&aspflag, (char *)&af, r_nbr, c_nbr); } } else if (FLAG_GET(af.flag, DEPRFLAG)) { G_debug(3, "real depression"); /* neighbour is inside real depression, not yet worked */ if (af.asp == 0 && ele_val <= ele_nbr[ct_dir]) { af.asp = drain[r_nbr - r + 1][c_nbr - c + 1]; FLAG_UNSET(af.flag, DEPRFLAG); seg_put(&aspflag, (char *)&af, r_nbr, c_nbr); } } } } } /* end neighbours */ /* add astar points to sorted list for flow accumulation and stream extraction */ first_cum--; seg_put(&astar_pts, (char *)&heap_p.pnt, 0, first_cum); seg_get(&aspflag, (char *)&af, r, c); FLAG_SET(af.flag, WORKEDFLAG); seg_put(&aspflag, (char *)&af, r, c); } /* end A* search */ G_percent(n_points, n_points, 1); /* finish it */ return 1; }
void cover_LS() { int remove_v, add_v; int remove_dscore, add_dscore; int e,v1,v2; int i; step = 1; #if one_tabu_remove_mode == 1 tabu_remove_v = 0; #endif #if one_tabu_add_mode == 1 tabu_add_v = 0; #endif while(1) { if (uncov_stack_fill_pointer == 0)//update best solution if needed { update_best_sol(); //if (c_size==optimal_size) return; update_target_size(); continue; } if(step%try_step==0) //check cutoff { times(&finish); double elap_time = (finish.tms_utime + finish.tms_stime - start_time)/sysconf(_SC_CLK_TCK); if(elap_time >= cutoff_time)return; } remove_v = choose_remove_v(); //remove_dscore = dscore[remove_v]; remove(remove_v); #if heap_cover_mode == 1 heap_del(remove_v); #endif e = uncov_stack[rand()%uncov_stack_fill_pointer]; v1 = edge[e].v1; v2 = edge[e].v2; #if one_tabu_add_mode == 1 //********** wodexiugai add" ==1 if(v1 == tabu_add_v) { #ifdef debug_mode cout << "tabu functions" << endl; #endif add_v = v2; } else if(v2 == tabu_add_v) { #ifdef debug_mode cout << "tabu functions" << endl; #endif add_v = v1; } else #endif #if add_time_stamp_mode == 1 if(dscore[v1]>dscore[v2] || (dscore[v1]==dscore[v2] && add_stamp[v1]<add_stamp[v2]) ) #else if(dscore[v1]>dscore[v2] || (dscore[v1]==dscore[v2] && time_stamp[v1]<time_stamp[v2]) ) #endif add_v=v1; else add_v=v2; add(add_v); #if 0 int index = index_in_remove_cand[remove_v]; index_in_remove_cand[remove_v] = 0; remove_cand[index] = add_v; index_in_remove_cand[add_v] = index; #endif time_stamp[add_v]=time_stamp[remove_v]=step; #if add_time_stamp_mode == 1 add_stamp[add_v] = step; #endif #if remove_time_stamp_mode == 1 remove_stamp[remove_v] = step; #endif // danger: heap_add should never be executed before updating the time stamps #if heap_cover_mode == 1 heap_add(add_v); #endif #if one_tabu_remove_mode == 1 tabu_remove_v = add_v; #endif #if one_tabu_add_mode == 1 tabu_add_v = remove_v; #endif //tabu_remove = add_v; //update_edge_weight(); step++; } }
list* search_a_star(void* state, void* state_world, search_is_goal state_goal_func, search_gen_successors state_gen_func, search_link_parent state_link_func, search_goal_backtrace state_back_func, search_trans_cost state_trans_func, search_heuristic state_heur_func, search_set_f_cost state_f_cost_set_func, hash_func state_hash_alg, generic_comp state_comp_func, generic_cpy state_copy_func, generic_op state_free_func, heap_comp state_heap_func) { int* g_cost_ptr, *f_cost_ptr, f_cost, tmp_f, g_cost, found; void* current_state, *successor_state, *heap_memory_location; list* states_overflow, *successor_list, *path; hash_table* states_closed_set, *states_open_set; hash_map* states_g_cost, *states_f_cost, *states_heap_index; heap* states_heap; states_overflow = list_create(NULL, NULL, state_free_func); states_closed_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_open_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_g_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_f_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_heap_index = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, NULL, NULL); states_heap = heap_create(89, state_heap_func, state_comp_func, state_copy_func, state_free_func); current_state = state; f_cost = state_heur_func(current_state, NULL); state_f_cost_set_func(current_state, f_cost); g_cost = 0; g_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; f_cost_ptr = malloc(sizeof(int)); *f_cost_ptr = f_cost; hash_map_insert(states_g_cost, current_state, g_cost_ptr, 0); heap_memory_location = heap_add(states_heap, state_copy_func(current_state)); hash_table_insert(states_open_set, state_copy_func(current_state), 0); hash_map_insert(states_f_cost, state_copy_func(current_state), f_cost_ptr, 0); hash_map_insert(states_heap_index, current_state, heap_memory_location, 1); path = NULL; found = 0; while(!heap_is_empty(states_heap) && !found) { current_state = state_copy_func(heap_peek(states_heap)); heap_remove(states_heap); hash_table_remove(states_open_set, current_state); hash_map_remove(states_heap_index, current_state); if(state_goal_func(current_state, state_world)) { path = state_back_func(current_state); found = 1; } else { if(!hash_table_insert(states_closed_set, current_state, 0)) { list_push_front(states_overflow, current_state); } successor_list = state_gen_func(current_state, state_world); while(!list_is_empty(successor_list)) { successor_state = list_front(successor_list); g_cost = *(int*)hash_map_get(states_g_cost, current_state) + state_trans_func(current_state, successor_state, state_world); f_cost = g_cost + state_heur_func(successor_state, state_world); tmp_f = hash_map_contains_key(states_f_cost, successor_state) ? *(int*)hash_map_get(states_f_cost, successor_state) : UINT_MAX; if(hash_table_contains(states_closed_set, successor_state) && f_cost > tmp_f) { list_remove_front(successor_list); continue; } if(!hash_table_contains(states_open_set, successor_state) || f_cost < tmp_f) { state_f_cost_set_func(successor_state, f_cost); state_link_func(successor_state, current_state); g_cost_ptr = malloc(sizeof(int)); f_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; *f_cost_ptr = f_cost; if(!hash_table_contains(states_open_set, successor_state)) { hash_table_insert(states_open_set, successor_state, 0); heap_memory_location = heap_add(states_heap, state_copy_func(successor_state)); hash_map_insert(states_heap_index, successor_state, heap_memory_location, 1); } else { heap_memory_location = hash_map_get(states_heap_index, successor_state); heap_up_mod_data(states_heap, heap_memory_location, successor_state); } if(!hash_map_set(states_g_cost, successor_state, g_cost_ptr)) { hash_map_insert(states_g_cost, state_copy_func(successor_state), g_cost_ptr, 0); } if(!hash_map_set(states_f_cost, successor_state, f_cost_ptr)) { hash_map_insert(states_f_cost, state_copy_func(successor_state), f_cost_ptr, 0); } list_pop(successor_list); } else { list_remove_front(successor_list); } } list_kill(successor_list); } } heap_kill(states_heap); list_kill(states_overflow); hash_map_kill(states_g_cost); hash_map_kill(states_f_cost); hash_table_kill(states_open_set); hash_table_kill(states_closed_set); hash_map_dissolve(states_heap_index); return path; }
elem* heap_insert(heap** H, int key, void* value){ node* newNode = node_init(key, value); return heap_add(H, newNode); }