int pq_insert(PQ* pq, int id, double priority) { if(id < 0 || id >= pq->capacity) { printf("ID out of range.\n"); return 0; } if(pq->id_ptrs[id] != NULL) { printf("ID in use.\n"); return 0; } if(pq->size >= pq->capacity) { printf("Full is priority queue.\n"); return 0; } int index = ++pq->size; pq->heap[index].id = id; pq->heap[index].heap_index = index; pq->heap[index].priority = priority; // insert data at the end of the array pq->id_ptrs[id] = &(pq->heap[index]); // label the id of the data as IDs percolate_up(pq, index); return 1; }
int heap_insert(heap_t heap, int key, point_t* elem) { if (heap->locked) { return -EBUSY; } if (heap->elements == heap->max_size - 1) { size_t max_size = heap->max_size * 2; elem_t* new_heap = (elem_t*) realloc((void*) heap->heap, sizeof(elem_t) * max_size); if (new_heap == NULL) { heap->locked = 0; return -ENOMEM; } heap->heap = new_heap; heap->max_size = max_size; } ++heap->elements; size_t hole = percolate_up(heap, key); heap->heap[hole].key = key; heap->heap[hole].point = elem; heap->locked = 0; return 1; }
//Insert element into heap void insert(int data){ if(h->count==h->capacity) h=resize_heap(); //Increase heap size h->count++; //Place data at last h->a[h->count-1]=data; percolate_up(h->count-1); }
void BinHeap::update(int positionInHeap) { int parent=(positionInHeap-1)>>1; if(positionInHeap>0 && rtimes[positionInHeap]<rtimes[parent]){ //std::cout << "percolate up" << std::endl; percolate_up(positionInHeap); } else percolate_down(positionInHeap); }
long long int insert(char first_name[],char last_name[]) { long long int index; top++; strcpy(array[top].first_name,first_name); strcpy(array[top].last_name,last_name); index=percolate_up(top); return index; }
//note: percolates up to either 1 or 2 as a root static void percolate_up(struct block_pool *bp, size_t node) { size_t parent = P(node); struct block tmp; if (node<3 || V(parent) >= V(node)) return; tmp = bp->block[node]; bp->block[node] = bp->block[parent]; bp->block[parent] = tmp; percolate_up(bp, parent); }
int pq_remove_by_id(PQ* pq, int id) { if(id < 0 || id >= pq->capacity) { printf("ID out of range.\n"); return 0; } if(pq->id_ptrs[id] == NULL) { printf("ID not in use.\n"); return 0; } int pq_size = pq->size; if(pq_size == 0) { printf("Empty priority queue.\n"); return 0; } if(pq_size == 1 || pq->id_ptrs[id]->heap_index == pq_size) { pq->id_ptrs[id] = NULL; // remove the location label pq->size--; return 1; } int new_id; double new_priority; new_id = pq->heap[pq_size].id; // copy data from the last data set new_priority = pq->heap[pq_size].priority; pq->id_ptrs[id]->id = new_id; // replace the data from the id specified pq->id_ptrs[id]->priority = new_priority; pq->id_ptrs[new_id] = pq->id_ptrs[id]; // update the label location of the copied data pq->id_ptrs[id] = NULL; // remove the location label pq->size--; int index = pq->id_ptrs[new_id]->heap_index; percolate_up(pq, index); percolate_down(pq, index); return 1; }
int pq_change_priority(PQ* pq, int id, double new_priority) { if(id < 0 || id >= pq->capacity) { printf("ID out of range.\n"); return 0; } if(pq->id_ptrs[id] == NULL) { printf("ID not in use.\n"); return 0; } pq->id_ptrs[id]->priority = new_priority; int index = pq->id_ptrs[id]->heap_index; percolate_up(pq, index); percolate_down(pq, index); return 1; }
/*---------------------------------------------------------------------------*/ void tw_pq_enqueue(tw_pq *h, ELEMENT_TYPE e ) { if( h->nelems >= h->curr_max ) { const unsigned int i = 50000; const unsigned int u = h->curr_max; h->curr_max += i; h->elems = tw_unsafe_realloc( TW_LOC, "heap queue elements", h->elems, sizeof(*h->elems) * h->curr_max); memset(&h->elems[u], 0, sizeof(*h->elems) * i); } e->heap_index = h->nelems; h->elems[h->nelems++] = e; percolate_up( h, h->nelems-1 ); e->state.owner = TW_pe_pq; e->next = NULL; e->prev = NULL; }
/*---------------------------------------------------------------------------*/ void tw_pq_delete_any(tw_pq *h, tw_event * victim) { int i = victim->heap_index; if( !(0 <= i && i < h->nelems) || (h->elems[i]->heap_index != i) ) { fprintf( stderr, "Fatal: Bad node in FEL!\n" ); exit(2); } else { h->nelems--; victim->state.owner = 0; if( h->nelems > 0 ) { ELEMENT_TYPE successor = h->elems[h->nelems]; h->elems[i] = successor; successor->heap_index = i; if( KEY(successor) <= KEY(victim) ) percolate_up( h, i ); else sift_down( h, i ); } } }
void *block_pool_alloc_align(struct block_pool *bp, size_t size, size_t align) { void *ret; if (align) align--; //if there aren't any blocks, make a new one if (!bp->count) { bp->count = 1; return new_block_tiny(bp->block, size); } //try the root block ret = try_block(bp->block, size, align); if (ret) return ret; //root block is filled, percolate down and try the biggest one percolate_down(bp, 0); ret = try_block(bp->block, size, align); if (ret) return ret; //the biggest wasn't big enough; we need a new block if (bp->count >= bp->alloc) { //make room for another block bp->alloc += bp->alloc; bp->alloc++; bp->block = realloc(bp->block, bp->alloc * sizeof(struct block)); } ret = new_block(bp->block+(bp->count++), size); //fix the heap after adding the new block percolate_up(bp, bp->count-1); return ret; }
binheap_node_t *binheap_insert(binheap_t *h, void *v) { binheap_node_t **tmp; binheap_node_t *retval; if (h->size == h->array_size) { h->array_size *= 2; tmp = realloc(h->array, h->array_size * sizeof (*h->array)); if (!tmp) { /* Error */ } else { h->array = tmp; } } h->array[h->size] = retval = malloc(sizeof (*h->array[h->size])); h->array[h->size]->datum = v; h->array[h->size]->index = h->size; percolate_up(h, h->size); h->size++; return retval; }
void Heap<T>::adjust_to_heap_up(){ int last = nodes.size()-1; for(int i = last; i > last/2; i-=1){//the step of i is not in clarity percolate_up(i); } }
void binheap_decrease_key(binheap_t *h, binheap_node_t *n) { percolate_up(h, n->index); }