void heap_insert(heap_t* h, double key, void* data) { int i; if(h->len == h->size) { h->size *= 2; h->A = realloc(h->A, h->size * sizeof(double)); assert(h->A); h->data = realloc(h->data, h->size * sizeof(void*)); assert(h->data); } h->len++; i = h->len - 1; /*@ loop variant i; */ while((i > 0) && (h->A[HEAP_PARENT(i)] < key)) { h->A[i] = h->A[HEAP_PARENT(i)]; h->data[i] = h->data[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } h->A[i] = key; h->data[i] = data; }
static void max_heap_fix_up(int *t, int p) { while (p && t[p] > t[HEAP_PARENT(p)]) { swap_places(t, p, HEAP_PARENT(p)); p = HEAP_PARENT(p); } }
void heap_increase_key(struct vector *v, int i, int key) { if (key < v->v[i]) return; v->v[i] = key; while (i > 1 && v->v[HEAP_PARENT(i)] < v->v[i]) { swap(v->v, i, HEAP_PARENT(i)); i = HEAP_PARENT(i); } }
void heap_push_up(struct heap* heap,int pos){ if (heap == NULL) return; if (pos > heap->heap_cursize) return; while (pos){ if (heap->heap_arr[pos] < heap->heap_arr[HEAP_PARENT(pos)]){ swap(&(heap->heap_arr[pos]),&(heap->heap_arr[HEAP_PARENT(pos)])); pos = HEAP_PARENT(pos); } else break; } }
void heap_heapify_up(heap* h, unsigned int key) { unsigned int value = h->nodes[key], temp; while(key != 0 && (*h->compare)(h->g, value, h->nodes[HEAP_PARENT(key)]) < 0) { temp = h->nodes[HEAP_PARENT(key)]; h->nodes[HEAP_PARENT(key)] = value; h->g->nodes[temp].heap_index = key; h->nodes[key] = temp; h->g->nodes[value].heap_index = HEAP_PARENT(key); key = HEAP_PARENT(key); value = h->nodes[key]; } }
void heap_build(struct heap* heap, int *arr,int size){ if (heap == NULL || arr == NULL) return; if (heap->heap_arr == NULL) heap->heap_arr = (int *)malloc(sizeof(int)*(heap->heap_size)); if (heap->heap_size < size) return; memcpy((void*)heap->heap_arr,(void*)arr,size*sizeof(int)); heap->heap_cursize = size; int i=0; for (i=size-1;i>0;i--){ if (heap->heap_arr[i] < heap->heap_arr[HEAP_PARENT(i)]){ swap(&heap->heap_arr[i],&heap->heap_arr[HEAP_PARENT(i)]); } } }
static void make_heap(plane_t *h, uint32 size) { int32 i; for (i = HEAP_PARENT(size); i >= 0; i--) heapify(h, (uint32)i, size); }
/**************************************************************************************** * Function name - filter_up * * Description - Heap structure restoration to the up direction * * Input - *h - pointer to an initialized heap * index - position, from which to start * Return Code/Output - none ****************************************************************************************/ void filter_up (heap*const h, size_t index) { int curr_pos = index; int parent_pos = HEAP_PARENT(index); hnode* target = h->heap [index]; /* Traverse path of parents up to the root */ while (curr_pos > 0) { /* Compare target and parent value */ if ((*h->fcomp) (h->heap[parent_pos], target)) /* less < */ { break; } else { /* Move data from parent position to current position.*/ heap_put_node_to_slot (h, curr_pos, h->heap[parent_pos]); /* Update current position pointing to parent */ curr_pos = parent_pos; /* Next parent */ parent_pos = (curr_pos - 1)/2; } } heap_put_node_to_slot (h, curr_pos, target); }
// return >= 0, success, return key which used to erase data // return < 0, fail int heap_insert(heap_t* heap, void* data) { int pos, pos_up, res; heap_node_t* node; if (!heap || !data) return -1; if (0 == _heap_full(heap)) { if (_heap_realloc(heap) < 0) { return -1; } } // insert data node = &heap->array[heap->count ++]; node->data = data; node->heap_key = heap->next_key; res = node->heap_key; // set key flag heap->key_table[node->heap_key] = heap->count - 1; _heap_set_next_key(heap); // rotate up pos = heap->count - 1; while (pos > 0) { pos_up = HEAP_PARENT(pos); if (heap->cmp_func(heap->array[pos].data, heap->array[pos_up].data) >= 0) { break; } _heap_swap(heap, pos, pos_up); pos = pos_up; } return res; }
static void insert_node(pj_timer_heap_t *ht, pj_timer_entry *new_node) { if (ht->cur_size + 2 >= ht->max_size) grow_heap(ht); reheap_up( ht, new_node, ht->cur_size, HEAP_PARENT(ht->cur_size)); ht->cur_size++; }
//下移pos位置节点. //如果pos位置的节点 > 其子节点,则将pos位置元素和其子节点中较小的节点交换. //并重复此过程,直到不能再下移. //这样可以尽可能的移动少点 void shiftUp( minHeap & min_h , int pos ) { while ( pos > 0) { int parent = HEAP_PARENT(pos); if ( min_h.vec_min_h.at(parent)->dis <= min_h.vec_min_h.at(pos)->dis) break; //不上移 //交换 minHeap_elem * temp = min_h.vec_min_h.at(pos); min_h.vec_min_h.at(pos) = min_h.vec_min_h.at(parent); min_h.vec_min_h.at(parent) = temp; pos = parent; } }
int heap_push(struct heap_list *h, TYPE *data) { int i; void **p; if (h->heap_keys == h->heap_size) if (heap_inc(h) < 0) return -1; i = h->heap_keys; p = h->heap_data; while (i > 0 && h->heap_comp_fun(p[HEAP_PARENT(i)], data) < 0) { p[i] = p[HEAP_PARENT(i)]; i = HEAP_PARENT(i); } p[i] = data; h->heap_keys++; return 0; }
/**************************************************************************************** * Function name - heap_remove_node * * Description - Removes hnode from a certain slot position, reheapefies the heap * and marks the slot in the ids array as available (-1) * * Input - *h - pointer to an initialized heap * slot - index of the heap-array (slot), where to remove hnode * reserve_slot - true -means to reserve the slot * Return Code/Output - On success - a valid hnode, on error - 0 ****************************************************************************************/ hnode* heap_remove_node (heap*const h, const size_t slot, int reserve_slot) { hnode* mved_end_node = 0; size_t parent_slot = 0; hnode* removed_node = h->heap[slot]; size_t removed_node_id; if (!removed_node) { fprintf(stderr, "%s - error: null removed node.\n", __func__); return 0; } removed_node_id = removed_node->node_id; /* Decrement the heap size */ h->curr_heap_size--; /* Reheapify only, if we're not deleting the last entry. */ if (slot < h->curr_heap_size) { mved_end_node = h->heap[h->curr_heap_size]; /* Move the end node to the location being removed. Update slot in the parallel <ids> array. */ heap_put_node_to_slot (h, slot, mved_end_node); /* If the mved_end_node "node-value" < than the value its parent, we move it up the heap. */ parent_slot = HEAP_PARENT (slot); if ((*h->fcomp) (mved_end_node, h->heap[parent_slot])) // < { filter_up (h, slot); } else { filter_down (h, slot); } } /* Mark the node-id entry as free. */ if (! reserve_slot) { release_node_id (h, removed_node_id); } return removed_node; }
/** * @brief increase the key value of a heap element * * HEAP-INCREASE-KEY(A, i, key) * 1 if key < A[i] * 2 then error "new key is smaller than current key" * 3 A[i] <- key * 4 while i > 1 and A[PARENT(i)] < A[i] * 5 do exchange A[i] <-> A[PARENT(i)] * 6 i <- PARENT(i) * * @param[in] g The heap to operate on * @param[in] data The application object attached with the key * @param[in] key The application object key * @return HEAP_ERR_E */ HEAP_ERR_E heap_increase_key (HEAP_T* h, unsigned long i, unsigned long key) { if (DS_HEAP_MAX != h->type) return HEAP_ERR_WRONG_TYPE; if (key == HEAP_NIL_KEY) return HEAP_ERR_INVALID_KEY; if (HEAP_KEY(h, i) != HEAP_NIL_KEY && key < HEAP_KEY(h, i)) return HEAP_ERR_SMALLER_KEY; HEAP_KEY(h, i) = key; while (i > 0 && (HEAP_KEY(h, HEAP_PARENT(i)) < HEAP_KEY(h, i))) { HEAP_SWAP_NODES(i,HEAP_PARENT(i)); i = HEAP_PARENT(i); } return HEAP_ERR_OK; }
static void BLI_heap_up(Heap *heap, int i) { while (i > 0) { int p = HEAP_PARENT(i); if (HEAP_COMPARE(heap->tree[p], heap->tree[i])) break; HEAP_SWAP(heap, p, i); i = p; } }
void BLI_heap_remove(Heap *heap, HeapNode *node) { int i = node->index; while (i > 0) { int p = HEAP_PARENT(i); HEAP_SWAP(heap, p, i); i = p; } BLI_heap_popmin(heap); }
int heap_valid(heap_t* h) { int i; /*@ loop invariant (h->len > 0 ==> (0 <= i <= h->len)); @ loop invariant (h->len == 0 ==> i == 1); @ loop invariant \valid(h->A+(0..h->len-1)); @ loop assigns i; @*/ for(i=1; i<h->len; i++) { if(h->A[HEAP_PARENT(i)] < h->A[i]) return(0); } return(1); }
void CTaskHeap::SiftUp(int child, SCHCMP *pfCompare) { while (child) { int parent = HEAP_PARENT(child); if (pfCompare(m_pHeap[parent], m_pHeap[child]) <= 0) break; PTASK_RECORD Tmp; Tmp = m_pHeap[child]; m_pHeap[child] = m_pHeap[parent]; m_pHeap[parent] = Tmp; child = parent; } }
void CQueuePriority::Heap_SiftUp( void ) { int child = m_cSize - 1; while( child ) { int parent = HEAP_PARENT( child ); if( m_heap[ parent ].Priority <= m_heap[ child ].Priority ) break; struct tag_HEAP_NODE Tmp; Tmp = m_heap[ child ]; m_heap[ child ] = m_heap[ parent ]; m_heap[ parent ] = Tmp; child = parent; } }
static void reheap_up( pj_timer_heap_t *ht, pj_timer_entry *moved_node, size_t slot, size_t parent) { // Restore the heap property after an insertion. while (slot > 0) { // If the parent node is greater than the <moved_node> we need // to copy it down. if (PJ_TIME_VAL_LT(moved_node->_timer_value, ht->heap[parent]->_timer_value)) { copy_node(ht, slot, ht->heap[parent]); slot = parent; parent = HEAP_PARENT(slot); } else break; } // Insert the new node into its proper resting place in the heap and // update the corresponding slot in the parallel <timer_ids> array. copy_node(ht, slot, moved_node); }
static pj_timer_entry * remove_node( pj_timer_heap_t *ht, size_t slot) { pj_timer_entry *removed_node = ht->heap[slot]; // Return this timer id to the freelist. push_freelist( ht, removed_node->_timer_id ); // Decrement the size of the heap by one since we're removing the // "slot"th node. ht->cur_size--; // Set the ID removed_node->_timer_id = -1; // Only try to reheapify if we're not deleting the last entry. if (slot < ht->cur_size) { int parent; pj_timer_entry *moved_node = ht->heap[ht->cur_size]; // Move the end node to the location being removed and update // the corresponding slot in the parallel <timer_ids> array. copy_node( ht, slot, moved_node); // If the <moved_node->time_value_> is great than or equal its // parent it needs be moved down the heap. parent = HEAP_PARENT (slot); if (PJ_TIME_VAL_GTE(moved_node->_timer_value, ht->heap[parent]->_timer_value)) reheap_down( ht, moved_node, slot, HEAP_LEFT(slot)); else reheap_up( ht, moved_node, slot, parent); } return removed_node; }
static int fr_heap_bubble(fr_heap_t *hp, int child) { /* * Bubble up the element. */ while (child > 0) { int parent = HEAP_PARENT(child); /* * Parent is smaller than the child. We're done. */ if (hp->cmp(hp->p[parent], hp->p[child]) < 0) break; /* * Child is smaller than the parent, repeat. */ HEAP_SWAP(hp->p[child], hp->p[parent]); SET_OFFSET(hp, child); child = parent; } SET_OFFSET(hp, child); return 1; }