void CTaskHeap::SiftDown(int iSubRoot, SCHCMP *pfCompare) { int parent = iSubRoot; int child = HEAP_LEFT_CHILD(parent); PTASK_RECORD Ref = m_pHeap[parent]; while (child < m_nCurrent) { int rightchild = HEAP_RIGHT_CHILD(parent); if (rightchild < m_nCurrent) { if (pfCompare(m_pHeap[rightchild], m_pHeap[child]) < 0) { child = rightchild; } } if (pfCompare(Ref, m_pHeap[child]) <= 0) break; m_pHeap[parent] = m_pHeap[child]; parent = child; child = HEAP_LEFT_CHILD(parent); } m_pHeap[parent] = Ref; }
void CQueuePriority::Heap_SiftDown( int iSubRoot ) { int parent = iSubRoot; int child = HEAP_LEFT_CHILD( parent ); struct tag_HEAP_NODE Ref = m_heap[ parent ]; while( child < m_cSize ) { int rightchild = HEAP_RIGHT_CHILD( parent ); if( rightchild < m_cSize ) { if( m_heap[ rightchild ].Priority < m_heap[ child ].Priority ) { child = rightchild; } } if( Ref.Priority <= m_heap[ child ].Priority ) break; m_heap[ parent ] = m_heap[ child ]; parent = child; child = HEAP_LEFT_CHILD( parent ); } m_heap[ parent ] = Ref; }
void MaxHeapify(int* pBuffer, uint32_t nIndex, uint32_t nBufferSize) { while (true) { uint32_t nLeft = HEAP_LEFT_CHILD(nIndex); uint32_t nRight = HEAP_RIGHT_CHILD(nIndex); uint32_t nLargest = nIndex; if ((nLeft < nBufferSize) && (pBuffer[nLeft] > pBuffer[nIndex])) { nLargest = nLeft; } if ((nRight < nBufferSize) && (pBuffer[nLeft] < pBuffer[nRight])) { nLargest = nRight; } if (nLargest != nIndex) { std::swap(pBuffer[nLargest], pBuffer[nIndex]); nIndex = nLargest; } else { break; } } return; }
void _heap_rotdown(heap_t* heap, int pos) { int pos_left, pos_right; if (!heap) return; // rotate down while (pos < (int)heap->count) { pos_left = HEAP_LEFT_CHILD(pos); pos_right = HEAP_RIGHT_CHILD(pos); // no left & right if (pos_left >= (int)heap->count) { break; } // no right, left < pos else if (pos_right >= (int)heap->count) { if (heap->cmp_func(heap->array[pos_left].data, heap->array[pos].data) < 0) { _heap_swap(heap, pos, pos_left); pos = pos_left; } else { break; } } // left < right, check to swap with left else if (heap->cmp_func(heap->array[pos_left].data, heap->array[pos_right].data) < 0) { if (heap->cmp_func(heap->array[pos_left].data, heap->array[pos].data) < 0) { _heap_swap(heap, pos, pos_left); pos = pos_left; } else { break; } } // right <= left, check to swap with right else { if (heap->cmp_func(heap->array[pos_right].data, heap->array[pos].data) < 0) { _heap_swap(heap, pos, pos_right); pos = pos_right; } else { break; } } } }