SwapElements(int index1, int index2) { if (__data.Count < 2) { throw new InvalidOperationException("The heap must contain at least 2 elements."); } if (index1 < 0 || index1 >= __data.Count) { throw new ArgumentOutOfRangeException("index1 must be within the range [0,Count-1]."); } if (index2 < 0 || index2 >= __data.Count) { throw new ArgumentOutOfRangeException("index2 must be within the range [0,Count-1]."); } // Lock the thread -- CRITICAL SECTION BEGIN System.Threading.Monitor.Enter(__data); try { // Do a plain old swap of the elements at index1 and index2 in the heap. PriorityValuePair <T> temp = __data[index1]; __data[index1] = __data[index2]; __data[index2] = temp; } finally { System.Threading.Monitor.Exit(__data); // Unlock the thread -- CRITICAL SECTION END } }
Remove(PriorityValuePair <T> element) { if (IsEmpty) { //throw new InvalidOperationException( "The heap is empty." ); return(false); } // Lock the thread -- CRITICAL SECTION BEGIN System.Threading.Monitor.Enter(__data); bool result = false; try { // Find the element within the heap. int index = __data.IndexOf(element); //Console.WriteLine( "index: " + index + ", count: " + __data.Count ); if (index < 0) { // Return false to indicate that the element was not found in the heap. result = false; } else if (index == __data.Count - 1) { // If we're removing the last element, we can simply remove it. There is no // need to heapify following the removal. __data.RemoveAt(index); result = true; } else { // In most cases, we need to swap the element from the index where it was found // with the last element in the set, then remove the last element (since it is // now the one we aimed to get rid of). Then, once this has been done, we need // to heapify up (and potentially also down) to re-sort the heap. SwapElements(index, __data.Count - 1); __data.RemoveAt(__data.Count - 1); int newIndex = HeapifyBottomUp(index); if (newIndex == index) { HeapifyTopDown(index); } result = true; } } catch (Exception e) { throw e; } finally { System.Threading.Monitor.Exit(__data); // Unlock the thread -- CRITICAL SECTION END } return(result); }
Remove(PriorityValuePair <T> element) { bool result = __heap.Remove(element); if (Count <= 0) { // If the queue is empty now, clear the queue to reset the PriorityAdjustment. Clear(); } return(result); }
/// <summary> /// Enqueues an existing PriorityValuePair element into the priority queue. /// </summary> /// <remarks> /// Increments the PriorityAdjustment by one EPSILON value for each queued item to ensure /// FIFO ordering of elements with the same priority. /// </remarks> /// <param name="element">A PriorityValuePair element.</param> public void Enqueue(PriorityValuePair <T> element) { // Apply the priorty adjustment before adding the new element so it orders correctly. element.Priority -= PriorityAdjustment; // Increment the number of queued items to the priority adjustment updates. NumQueuedItems++; // Add the new element to the queue. __heap.Push(element); }
Dequeue() { PriorityValuePair <T> result = __heap.Pop(); if (Count <= 0) { // If the queue is empty now, clear the queue to reset the PriorityAdjustment. Clear(); } return(result); }
/// <summary> /// Insert a new element into the heap and heapify it into its correct position, given /// an existing PriorityValuePair containing a double priority as its key and a value. /// </summary> /// <param name="element">A PriorityValuePair containing a double priority as its key and a /// generically-typed value.</param> public void Push(PriorityValuePair <T> element) { // Lock the thread -- CRITICAL SECTION BEGIN System.Threading.Monitor.Enter(__data); try { // Add a new element to the heap at the end of the data list. __data.Add(element); // Heapify bottom up to sort the element into the correct position in the heap. HeapifyBottomUp(__data.Count - 1); } catch (Exception e) { throw e; } finally { System.Threading.Monitor.Exit(__data); // Unlock the thread -- CRITICAL SECTION END } }
Contains(PriorityValuePair <T> element) { // Lock the thread -- CRITICAL SECTION BEGIN System.Threading.Monitor.Enter(__data); bool result = false; try { result = __data.Contains(element); } catch (Exception e) { throw e; } finally { System.Threading.Monitor.Exit(__data); // Unlock the thread -- CRITICAL SECTION END } return(result); }
public void Enqueue(T item) { PriorityValuePair <T> element = new PriorityValuePair <T>(1000.0, item); Enqueue(element); }
Contains(PriorityValuePair <T> element) { return(__heap.Contains(element)); }
public void Push(T item) { PriorityValuePair <T> element = new PriorityValuePair <T>(1000.0, item); Push(element); }
Add(PriorityValuePair <T> element) { Push(element); }