private BinaryHeapPriorityQueue.Entry <E> RightChild(BinaryHeapPriorityQueue.Entry <E> entry) { int index = entry.index; int rightIndex = index * 2 + 2; return(rightIndex < Count ? GetEntry(rightIndex) : null); }
/// <summary> /// On the assumption that /// leftChild(entry) and rightChild(entry) satisfy the heap property, /// make sure that the heap at entry satisfies this property by possibly /// percolating the element entry downwards. /// </summary> /// <remarks> /// On the assumption that /// leftChild(entry) and rightChild(entry) satisfy the heap property, /// make sure that the heap at entry satisfies this property by possibly /// percolating the element entry downwards. I've replaced the obvious /// recursive formulation with an iterative one to gain (marginal) speed /// </remarks> private void HeapifyDown(BinaryHeapPriorityQueue.Entry <E> entry) { BinaryHeapPriorityQueue.Entry <E> bestEntry; do { // initialized below bestEntry = entry; BinaryHeapPriorityQueue.Entry <E> leftEntry = LeftChild(entry); if (leftEntry != null) { if (Compare(bestEntry, leftEntry) < 0) { bestEntry = leftEntry; } } BinaryHeapPriorityQueue.Entry <E> rightEntry = RightChild(entry); if (rightEntry != null) { if (Compare(bestEntry, rightEntry) < 0) { bestEntry = rightEntry; } } if (bestEntry != entry) { // Swap min and current Swap(bestEntry, entry); } }while (bestEntry != entry); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual double GetPriority(E key) { BinaryHeapPriorityQueue.Entry <E> entry = GetEntry(key); if (entry == null) { return(double.NegativeInfinity); } return(entry.priority); }
/// <summary>Searches for the object in the queue and returns it.</summary> /// <remarks> /// Searches for the object in the queue and returns it. May be useful if /// you can create a new object that is .equals() to an object in the queue /// but is not actually identical, or if you want to modify an object that is /// in the queue. /// </remarks> /// <returns> /// null if the object is not in the queue, otherwise returns the /// object. /// </returns> public virtual E GetObject(E key) { if (!Contains(key)) { return(null); } BinaryHeapPriorityQueue.Entry <E> e = GetEntry(key); return(e.key); }
private BinaryHeapPriorityQueue.Entry <E> MakeEntry(E key) { BinaryHeapPriorityQueue.Entry <E> entry = new BinaryHeapPriorityQueue.Entry <E>(); entry.index = Count; entry.key = key; entry.priority = double.NegativeInfinity; indexToEntry.Add(entry); keyToEntry[key] = entry; return(entry); }
/// <summary>Structural swap of two entries.</summary> private void Swap(BinaryHeapPriorityQueue.Entry <E> entryA, BinaryHeapPriorityQueue.Entry <E> entryB) { int indexA = entryA.index; int indexB = entryB.index; entryA.index = indexB; entryB.index = indexA; indexToEntry.Set(indexA, entryB); indexToEntry.Set(indexB, entryA); }
public override bool Remove(object key) { E eKey = (E)key; BinaryHeapPriorityQueue.Entry <E> entry = GetEntry(eKey); if (entry == null) { return(false); } RemoveEntry(entry); return(true); }
private void RemoveEntry(BinaryHeapPriorityQueue.Entry <E> entry) { BinaryHeapPriorityQueue.Entry <E> lastEntry = GetLastEntry(); if (entry != lastEntry) { Swap(entry, lastEntry); RemoveLastEntry(); Heapify(lastEntry); } else { RemoveLastEntry(); } }
/// <summary>Changes a priority, either up or down, adding the key it if it wasn't there already.</summary> /// <param name="key">an <code>Object</code> value</param> /// <returns>whether the priority actually changed.</returns> public virtual bool ChangePriority(E key, double priority) { BinaryHeapPriorityQueue.Entry <E> entry = GetEntry(key); if (entry == null) { entry = MakeEntry(key); } if (Compare(priority, entry.priority) == 0) { return(false); } entry.priority = priority; Heapify(entry); return(true); }
private int Compare(BinaryHeapPriorityQueue.Entry <E> entryA, BinaryHeapPriorityQueue.Entry <E> entryB) { int result = Compare(entryA.priority, entryB.priority); if (result != 0) { return(result); } if ((entryA.key is IComparable) && (entryB.key is IComparable)) { IComparable <E> key = ErasureUtils.UncheckedCast(entryA.key); return(key.CompareTo(entryB.key)); } return(result); }
/// <summary>iterative heapify up: move item o at index up until correctly placed</summary> private void HeapifyUp(BinaryHeapPriorityQueue.Entry <E> entry) { while (true) { if (entry.index == 0) { break; } BinaryHeapPriorityQueue.Entry <E> parentEntry = Parent(entry); if (Compare(entry, parentEntry) <= 0) { break; } Swap(entry, parentEntry); } }
private BinaryHeapPriorityQueue.Entry <E> LeftChild(BinaryHeapPriorityQueue.Entry <E> entry) { int leftIndex = entry.index * 2 + 1; return(leftIndex < Count ? GetEntry(leftIndex) : null); }
private BinaryHeapPriorityQueue.Entry <E> Parent(BinaryHeapPriorityQueue.Entry <E> entry) { int index = entry.index; return(index > 0 ? GetEntry((index - 1) / 2) : null); }
// at start of next loop, we set currentIndex to largestIndex // this indexation now holds current, so it is unchanged // log.info("Done with heapify down"); // verify(); private void Heapify(BinaryHeapPriorityQueue.Entry <E> entry) { HeapifyUp(entry); HeapifyDown(entry); }
/// <summary>Get entry by index, exception if none.</summary> private BinaryHeapPriorityQueue.Entry <E> GetEntry(int index) { BinaryHeapPriorityQueue.Entry <E> entry = indexToEntry[index]; return(entry); }
/// <summary>Remove the last element of the heap (last in the index array).</summary> public virtual void RemoveLastEntry() { BinaryHeapPriorityQueue.Entry <E> entry = indexToEntry.Remove(Count - 1); Sharpen.Collections.Remove(keyToEntry, entry.key); }