void BubbleDown(int idx) { #ifdef BASE_DEBUG std::cout << "BubbleDown: " << idx << std::endl; #endif Comparator comparator; int firstChild = FirstChild(idx); #ifdef BASE_DEBUG //std::cout << "firstChild: " << firstChild << std::endl; #endif int minIdx = idx; for ( int i = 0; i <= 1 && (firstChild + i) < m_data.size(); ++i ) { #ifdef BASE_DEBUG std::cout << "compare: " << m_data[idx] << " and " << m_data[firstChild + i] << std::endl; #endif if ( !comparator(m_data[minIdx], m_data[firstChild + i]) ) { #ifdef BASE_DEBUG std::cout << "minIdx " << minIdx << std::endl; #endif minIdx = firstChild + i; } } if ( minIdx != idx ) { std::swap(m_data[idx], m_data[minIdx]); BubbleDown(minIdx); } }
PtrType RNHeap<PtrType>:: Pop(void) { // Check number of entries if (nentries == 0) return NULL; // Get head entry PtrType result = entries[0]; // Update deleted entry backpointer if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[0] + entry_offset)) = NULL; if (entry_callback) *((PtrType **) (*entry_callback)(entries[0], callback_data)) = NULL; // Remove head entry, by copying tail over it entries[0] = entries[nentries-1]; // Update new entry[0] backpointer if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[0] + entry_offset)) = &entries[0]; if (entry_callback) *((PtrType **) (*entry_callback)(entries[0], callback_data)) = &entries[0]; // Decrement number of entries nentries--; // Bubble the head entry down to its rightful spot BubbleDown(0); // Return original head entry return result; }
T Heap<T>::extract() { T retval = array[0]; array[0] = array[array.size()-1]; array.pop_back(); BubbleDown(0); return retval; }
bool Extract(T& value) { if ( m_data.empty() ) return false; value = m_data[0]; std::swap(m_data[0], m_data[m_data.size() - 1]); m_data.resize(m_data.size() - 1); BubbleDown(0); return true; }
GridNode* MinHeap::Pop() { GridNode* min = this->theVector[0]; min->heapIdx = -1; min->state = 2; this->theVector[0] = this->theVector.back(); this->theVector[0]->heapIdx = 0; this->theVector.pop_back(); this->length--; BubbleDown(0); return min; }
void MinHeap::Update(int i, double value) { if (value > this->theVector[i]->cost) { this->theVector[i]->cost = value; BubbleDown(i); } else { this->theVector[i]->cost = value; BubbleUp(i); } }
void ModifiedMaxHeap::DeleteMax(int newVal){ int length = _vector.size(); if(length == 0) { return; } _vector[0] = newVal; BubbleDown(0); }
void Heap<T>::BubbleDown(unsigned node) { unsigned Li, Ri, min; Li = 2*node + 1; Ri = 2*node + 2; min=node; if(Li < array.size() && array[Li]<array[min]) min = Li; if(Ri < array.size() && array[Ri] < array[min]) min = Ri; if(min != node) { T tmp = array[node]; array[node] = array[min]; array[min] = tmp; BubbleDown(min); } }
void RNHeap<PtrType>:: Remove(PtrType entry) { // Search for entry PtrType *entryp = NULL; if (entry_offset >= 0) entryp = *((PtrType **) ((unsigned char *) entry + entry_offset)); if (entry_callback) entryp = *((PtrType **) (*entry_callback)(entry, callback_data)); else { // Find entry in heap for (int i = 0; i < nentries; i++) { if (entries[i] == entry) { entryp = &entries[i]; break; } } } // Move data into place if (!entryp) return; int index = entryp - entries; assert((0 <= index) && (index < nentries)); // Update deleted entry backpointer if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[index] + entry_offset)) = NULL; if (entry_callback) *((PtrType **) (*entry_callback)(entries[index], callback_data)) = NULL; // Remove entry, by copying tail over it entries[index] = entries[nentries-1]; // Update new entry[0] backpointer if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[index] + entry_offset)) = &entries[index]; if (entry_callback) *((PtrType **) (*entry_callback)(entries[index], callback_data)) = &entries[index]; // Decrement number of entries nentries--; // Bubble the entry down to its rightful spot BubbleDown(index); }
void RNHeap<PtrType>:: Update(PtrType entry) { // Search for entry PtrType *entryp = NULL; if (entry_offset >= 0) entryp = *((PtrType **) ((unsigned char *) entry + entry_offset)); if (entry_callback) entryp = *((PtrType **) (*entry_callback)(entry, callback_data)); else { // Find entry in heap for (int i = 0; i < nentries; i++) { if (entries[i] == entry) { entryp = &entries[i]; break; } } } // Move data into place if (!entryp) return; int index = entryp - entries; assert((0 <= index) && (index < nentries)); BubbleUp(BubbleDown(index)); }