void TimerBase::setNextFireTime(double newTime) { ASSERT(m_thread == currentThread()); // Keep heap valid while changing the next-fire time. double oldTime = m_nextFireTime; if (oldTime != newTime) { m_nextFireTime = newTime; static unsigned currentHeapInsertionOrder; m_heapInsertionOrder = currentHeapInsertionOrder++; bool wasFirstTimerInHeap = m_heapIndex == 0; if (oldTime == 0) heapInsert(); else if (newTime == 0) heapDelete(); else if (newTime < oldTime) heapDecreaseKey(); else heapIncreaseKey(); bool isFirstTimerInHeap = m_heapIndex == 0; if (wasFirstTimerInHeap || isFirstTimerInHeap) threadGlobalData().threadTimers().updateSharedTimer(); } checkConsistency(); }
void maxHeapInsert(int key) { if (heap.length < MAX -2) heap.length ++; heap.Arr[heap.length] = INT_MIN; heapIncreaseKey(heap.length,key); }
int main(void) { // srand(1); // // //init the data // int i; // for(i = 1; i <= MAX; i++) { // number[i] = rand() % 100; // } //heap_size = MAX; printf("test heapSort\n"); int a[] = {4,1,3,2,16,9,10,14,8,7}; heap_size = sizeof(a)/sizeof(a[0]); std::copy(a, a+heap_size,number+1); // print printf("before sort:"); printHeap(); // sort heapSort(); printf("after sort"); printHeap(); printf("test priority queue\n"); heap_size = sizeof(a)/sizeof(a[0]); std::copy(a, a+heap_size,number+1); buildMaxHeap(); heapIncreaseKey( 9, 15 ); printHeap(); return 0; }
void TimerBase::setNextFireTime(double newTime) { // Keep heap valid while changing the next-fire time. if (timersReadyToFire) timersReadyToFire->remove(this); double oldTime = m_nextFireTime; if (oldTime != newTime) { m_nextFireTime = newTime; bool wasFirstTimerInHeap = m_heapIndex == 0; if (oldTime == 0) heapInsert(); else if (newTime == 0) heapDelete(); else if (newTime < oldTime) heapDecreaseKey(); else heapIncreaseKey(); bool isFirstTimerInHeap = m_heapIndex == 0; if (wasFirstTimerInHeap || isFirstTimerInHeap) updateSharedTimer(); } checkConsistency(); }
int maxHeapInsert(int *S, int key) { S[0] = S[0] + 1; S[S[0]] = MIN; heapIncreaseKey(S, S[0], key); return 0; }
int main() { int max = 0; heap[0] = 10; heap[1] = 1; heap[2] = 2; heap[3] = 3; heap[4] = 4; heap[5] = 7; heap[6] = 8; heap[7] = 9; heap[8] = 10; heap[9] = 14; heap[10] = 16; printf("Original State:\n"); printHeap(heap); buildMaxHeap(heap); printf("\n After Heapifying:\n"); printHeap(heap); heapIncreaseKey(heap, 9, 15); printf("\n After Increasing S[9]'s key to 15:\n"); printHeap(heap); max = heapExtractMax(heap); printf("\n Max key is %d, After Extracting Max Key:\n", max); printHeap(heap); maxHeapInsert(heap, 13); printf("\n After inserting key 13:\n"); printHeap(heap); return 0; }
// max heap 에 key 값을 가진 노드 삽입 함수 void maxHeapInsert (heap_t* heap, int key) { heap->size++; heap->element[heap->size] = INT_MIN; heapIncreaseKey(heap, heap->size, key); }
void TimerBase::updateHeapIfNeeded(double oldTime) { if (m_nextFireTime && hasValidHeapPosition()) return; #if ENABLE(ASSERT) int oldHeapIndex = m_heapIndex; #endif if (!oldTime) heapInsert(); else if (!m_nextFireTime) heapDelete(); else if (m_nextFireTime < oldTime) heapDecreaseKey(); else heapIncreaseKey(); ASSERT(m_heapIndex != oldHeapIndex); ASSERT(!inHeap() || hasValidHeapPosition()); }
void maxHeapInsert(int key) { heap_size ++; number[heap_size] = MINIMUM_INT; heapIncreaseKey( heap_size, key ); }