Ejemplo n.º 1
0
void MinHeap::Heapify()
{
	for (int i = this->length - 1; i > 0; i--)
	{
		BubbleUp(i);
	}
}
Ejemplo n.º 2
0
void RNHeap<PtrType>::
Push(PtrType entry)
{
  // Allocate space for entry
  if (nentries == nallocated) {
    nallocated = (nallocated == 0) ? 1 : 2 * nallocated;
    PtrType *newentries = new PtrType [nallocated];
    for (int i = 0; i < nentries; i++) {
      newentries[i] = entries[i];
      if (entry_offset >= 0) *((PtrType **) ((unsigned char *) newentries[i] + entry_offset)) = &newentries[i];
      if (entry_callback) *((PtrType **) (*entry_callback)(newentries[i], callback_data)) = &newentries[i];
    }
    if (entries) delete [] entries;
    entries = newentries;
  }

  // Put entry into tail
  entries[nentries] = entry;

  // Update entry backpointer
  if (entry_offset >= 0) *((PtrType **) ((unsigned char *) entries[nentries] + entry_offset)) = &entries[nentries];
  if (entry_callback) *((PtrType **) (*entry_callback)(entries[nentries], callback_data)) = &entries[nentries];

  // Increment number of entries
  nentries++;

  // Bubble tail entry up to its rightful spot
  BubbleUp(nentries-1);
}
Ejemplo n.º 3
0
void MinHeap::Push(GridNode* nodePtr)
{
	this->theVector.push_back(nodePtr);
	nodePtr->heapIdx = length++;
	BubbleUp(length - 1);
	nodePtr->state = 1;
}
Ejemplo n.º 4
0
    void Insert(const T& value)
    {
#ifdef BASE_DEBUG
        std::cout << "Inserting: " << value << std::endl;
#endif
        m_data.push_back(value);
        BubbleUp(m_data.size() - 1);
    }
Ejemplo n.º 5
0
void ModifiedMaxHeap::Insert(int newValue){
    int length = _vector.size();

	if(length <max_size){
	    _vector.push_back(newValue);
	    BubbleUp(length);
	}else if(newValue < GetMax()){
		DeleteMax(newValue);
	}
}
Ejemplo n.º 6
0
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);
	}
}
Ejemplo n.º 7
0
void Heap<T>::BubbleUp(unsigned node) {
  if(node==0)
    return;

  unsigned p = parent(node);
  if(array[node] < array[p]) {
    T tmp = array[node];
    array[node] = array[p];
    array[p] = tmp;
    BubbleUp(p);
  }
  return;
}
Ejemplo n.º 8
0
    void BubbleUp(int idx)
    {
        int parent = Parent(idx);
        if ( parent != -1 )
        {
            Comparator comparator;
            if ( comparator(m_data[idx], m_data[parent]) )
            {
#ifdef BASE_DEBUG
                std::cout << "swapping " << m_data[idx] << " with " <<  m_data[parent] << std::endl;
#endif
                std::swap(m_data[parent], m_data[idx]);

                BubbleUp(parent);
            }
            else
            {
#ifdef BASE_DEBUG
                std::cout << "not swapping " << m_data[idx] << " with " <<  m_data[parent] << std::endl;
#endif
            }
        }
    }
Ejemplo n.º 9
0
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));
}
Ejemplo n.º 10
0
void Heap<T>::insert(T item) {
  array.push_back(item);
  BubbleUp(array.size()-1);
}