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(); }
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(); }
inline void TimerBase::heapInsert() { ASSERT(!inHeap()); timerHeap().append(this); m_heapIndex = timerHeap().size() - 1; heapDecreaseKey(); }
void dijkstra(vertex *vertices, heap *root){ int min_vertex, old_cost, new_cost, adjacent; //printGraph(vertices, n_ver); node *min_node; while(root->root_node){ min_node = heapExtractMin(root); min_vertex = min_node->vertex; vertices[min_vertex].heap_node.key = -1; //printf("\nmin_vertex: %d\n", min_vertex); edge *aux = vertices[min_vertex].adjacent; while(aux){ adjacent = aux->head_vertex; new_cost = vertices[min_vertex].cost + aux->cost; //printf("new_cost: %d\tadjacent: %d\n", new_cost, adjacent); if(new_cost < vertices[adjacent].cost){ if(vertices[adjacent].heap_node.key == -1){ vertices[adjacent].heap_node.key = new_cost; heapInsert(root, &(vertices[adjacent].heap_node)); } else{ heapDecreaseKey(root, &(vertices[adjacent].heap_node), new_cost); } vertices[adjacent].predecessor = min_vertex; vertices[adjacent].cost = new_cost; //printGraph(vertices, n_ver); } aux = aux->next; } } }
void minHeapInsert(int key) { if (heap.length < MAX -1) heap.length ++; heap.Arr[heap.length - 1] = INT_MAX; heapDecreaseKey(heap.length-1,key); }
void dijkstra(vertex *vertices, heap *root){ int min_vertex, old_cost, new_cost, adjacent; node *min_node; while(root->root_node){ min_node = heapExtractMin(root); min_vertex = min_node->vertex; free(min_node); vertices[min_vertex].heap_node = NULL; edge *aux = vertices[min_vertex].adjacent; while(aux){ adjacent = aux->head_vertex; new_cost = vertices[min_vertex].cost + aux->cost; if(new_cost < vertices[adjacent].cost){ if(!vertices[adjacent].heap_node){ vertices[adjacent].heap_node = heapInsert(root, new_cost, adjacent); } else{ heapDecreaseKey(root, vertices[adjacent].heap_node, new_cost); } vertices[adjacent].predecessor = min_vertex; vertices[adjacent].cost = new_cost; } aux = aux->next; } } }
// insert a new node into heap void VertexHeap::minHeapInsert(int reference, int key, int parent) { heapSize = heapSize + 1; Vertices[heapSize].setReference(reference); Vertices[heapSize].setKey(key); Vertices[heapSize].setParent(parent); heapDecreaseKey(heapSize); }
inline void TimerBase::heapPop() { // Temporarily force this timer to have the minimum key so we can pop it. double fireTime = m_nextFireTime; m_nextFireTime = -numeric_limits<double>::infinity(); heapDecreaseKey(); heapPopMin(); m_nextFireTime = fireTime; }
inline void TimerBase::heapInsert() { ASSERT(!inHeap()); if (!timerHeap) timerHeap = new Vector<TimerBase*>; timerHeap->append(this); m_heapIndex = timerHeap->size() - 1; heapDecreaseKey(); }
nodeT * MinPriority::extractMin(vector<nodeT *> & pqueue) { for(int j=((pqueue.size()) / 2)-1; j>=0 ;j--) minHeapify(j,pqueue.size(),pqueue); nodeT * extractMinNode= pqueue[0]; heapDecreaseKey(pqueue); return extractMinNode; }
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()); }
node* heapInsert(heap *root, node *new_node){ node *parent = NULL; new_node->parent = new_node->left = new_node->right = NULL; if(!root->count){ root->root_node = root->last_node = new_node; root->count++; } else{ parent = heapFindParentInsertNode(root); if(parent->left){ parent->right = new_node; } else{ parent->left = new_node; } new_node->parent = parent; root->count++; root->last_node = new_node; heapDecreaseKey(root, new_node, new_node->key); } return new_node; }
inline void TimerBase::heapIncreaseKey() { ASSERT(m_nextFireTime != 0); heapPop(); heapDecreaseKey(); }