void reheap_down(unsigned index) { unsigned child1_index = child1(index); unsigned child2_index = child2(index); if( child1_index != 0 ) { if( child2_index != 0 ) { if( !(*heap[index] < *heap[child1_index]) || !(*heap[index] < *heap[child2_index]) ) { if( *heap[child1_index] < *heap[child2_index] ) { swap(child1_index, index); reheap_down(child1_index); } else { swap(child2_index, index); reheap_down(child2_index); } } } else { if( *heap[child1_index] < *heap[index] ) { swap(index, child1_index); reheap_down(child1_index); } } } }
void update( astar_node_t* element, astar_node_t* new_element ) { PRX_ASSERT(finder.find(element) != finder.end()); unsigned index = finder[element]; finder[new_element] = index; finder.erase(element); heap[index] = new_element; reheap_up(index); reheap_down(index); delete element; }
astar_node_t* remove_min() { astar_node_t* return_val = NULL; if( heap_size > 1 ) { return_val = heap[1]; // heap[1] = heap.back(); heap[1] = heap[heap_size - 1]; finder[heap[1]] = 1; finder.erase(return_val); // heap.pop_back(); heap_size--; reheap_down(1); } return return_val; }
int ZCE_Timer_Heap::remove_nodeid(int timer_node_id) { assert(size_heap_ > 0); if (size_heap_ == 0) { //看你妹呀,瞎填参数 return -1; } size_t delete_heap_id = note_to_heapid_[timer_node_id]; // note_to_heapid_[timer_node_heap_[delete_heap_id]] = INVALID_TIMER_ID; timer_node_heap_[delete_heap_id] = INVALID_TIMER_ID; --size_heap_; //数量为0不旋转 if (size_heap_ == 0) { return 0; } //将最后一个堆放到删除的位置,进行调整 timer_node_heap_[delete_heap_id] = timer_node_heap_[size_heap_]; note_to_heapid_[timer_node_heap_[delete_heap_id]] = static_cast<int>( delete_heap_id); timer_node_heap_[size_heap_] = INVALID_TIMER_ID; //看是否需要向上调整 bool alread_up = reheap_up(delete_heap_id); //如果不需要向上调整,看是否需要向下调整 if ( !alread_up ) { reheap_down(delete_heap_id); } return 0; }
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; }