示例#1
0
 void PushBottom() {
   int index = s;
   while (index > 1) {
     index /= 2;
     if (HeapSwap(index) == 0) { break; }
   }
 }
示例#2
0
 void HeapBubbleUp(int pos) {
   int parent = HeapParent(pos);
   if (HeapDominates(pos, parent)) {
     HeapSwap(pos, parent);
     HeapBubbleUp(parent);
   }
 }
示例#3
0
 void PushTop() {
   int index = 1;
   while (index * 2 <= s) {
     int child = HeapSwap(index);
     if (child == 0) { break; }
     if (child == 1) { index = index * 2; }
     if (child == 2) { index = index * 2 + 1; }
   }
 }
示例#4
0
 void Pop()
 {
   // For now: Keep State in hash
   if (heap_size) {
     heap[0]->heap_index = -1;
     --heap_size;
     HeapSwap(0, heap_size);
     HeapBubbleDown(0);
   }
 }
示例#5
0
 void HeapBubbleDown(int pos) {
   int dominant = pos;
   for (auto child : { HeapLeftChild(pos), HeapRightChild(pos) }) {
     if (child < heap_size && HeapDominates(child, dominant)) {
       dominant = child;
     }
   }
   if (pos != dominant) {
     HeapSwap(pos, dominant);
     HeapBubbleDown(dominant);
   }
 }
示例#6
0
文件: heap.cpp 项目: jlglearn/Code
int HEAP::Pop(void)
{
    ASSERT(nElements > 0, "Pop() on empty Heap");
    if (nElements > 0)
    {
        int k = pE[0].key;
        HeapSwap(pE, 1, nElements);
        nElements--;
        HeapifyDown(1);
        return k;
    }
    return 0;
}
void ValueTableOpLogMeta::HeapMaxHeapify(int32_t index) {
  int32_t left = HeapLeft(index);
  int32_t right = HeapRight(index);

  int32_t largest = index;
  if (left < heap_size_ && CompRowOpLogMeta(heap_[left], heap_[index])) {
    largest = left;
  }

  if (right < heap_size_ && CompRowOpLogMeta(heap_[right], heap_[largest])) {
    largest = right;
  }

  if (largest != index) {
    HeapSwap(index, largest);
    HeapMaxHeapify(largest);
  }
}
void ValueTableOpLogMeta::HeapIncrease(
    int32_t index, const RowOpLogMeta &row_oplog_meta) {
  heap_[index].importance += row_oplog_meta.get_importance();
  if (heap_[index].clock == -1 || heap_[index].clock > row_oplog_meta.get_clock()) {
    heap_[index].clock = row_oplog_meta.get_clock();
  }

  int32_t check_index = index;
  while (check_index != 0) {
    int32_t parent_index = HeapParent(check_index);
    if (CompRowOpLogMeta(heap_[check_index], heap_[parent_index])) {
      HeapSwap(check_index, parent_index);
      check_index = parent_index;
    } else {
      break;
    }
  }
}
示例#9
0
文件: heap.cpp 项目: jlglearn/Code
void HEAP::HeapifyUp(int i)
{
    while (i > 1)
    {
        int p = HEAPPARENT(i);
        
        if (HEAPELEMENT(p).key <= HEAPELEMENT(i).key)
        {
            // if parent <= than child (i), heap holds, done
            break;
        }
        else
        {
            // if parent greater than child, swap and continue checking upward
            HeapSwap(pE, p, i);
        }
        
        i = p;
    }
}
示例#10
0
文件: heap.cpp 项目: jlglearn/Code
void HEAP::HeapifyDown(int i)
{
    if (i >= nElements)
        return;
        
    int l = HEAPLEFT(i);
    int r = HEAPRIGHT(i);
    int m = i;
    
    if ((l <= nElements) && (HEAPELEMENT(l).key < HEAPELEMENT(i).key))
        m = l;
        
    if ((r <= nElements) && (HEAPELEMENT(r).key < HEAPELEMENT(m).key))
        m = r;
        
    if (m == i)
        return;
        
    HeapSwap(pE, m, i);
    HeapifyDown(m);
}