Encapsulates algorithms for a max-heap, i.e. a priority queue that always knows the largest item and can remove it in O(log Count) time, or add a new item in O(log Count) time.
More...
Encapsulates algorithms for a max-heap, i.e. a priority queue that always knows the largest item and can remove it in O(log Count) time, or add a new item in O(log Count) time.
- Template Parameters
-
| T | Item type |
| TList | List type that the heap object is a wrapper for |
| TComparer | Type used to compare items; an instance of this type must be provided to the constructor. |
Call Push(T) to add a new item, Peek() to get the largest item, and Pop() or TryPop(out bool) to remove the largest item.
Most users will want to use the derived classes MaxHeap{T} or MaxHeapInList{T} to avoid dealing with three type parameters. Ideally this type would be a struct in order to maximize performance. It is a class in order to allow derived classes including MinHeap{T}.
- See also
- MaxHeap{T}
- Type Constraints
-
| TList | : | IList<T> | |
| TComparer | : | IComparer<T> | |
|
| | MaxHeap (TList list, TComparer comparer, Action< T, int >?onItemMoved=null) |
| | Initializes the heap wrapper with the list and comparer to use. Both parameters must not be null. More...
|
| |
| MaxHeap< T, TList, TComparer > | Heapify () |
| | Rearranges items to ensure that the underlying list has the heap property. Takes O(Count) time. More...
|
| |
| void | Add (T item) |
| | Adds an item to the heap (synonym of Push()). Complexity: O(Count). More...
|
| |
| void | Push (T item) |
| | Adds an item to the heap. Complexity: O(Count). More...
|
| |
| void | PriorityChanged (int index) |
| | Notifies the heap that the priority of the item at the specified index has changed. The item is bubbled up or down as appropriate. More...
|
| |
| T | PopAndPush (T item) |
| | Combines a pop followed by a push into one operation that is more efficient than a separate Pop nad Push(T). More...
|
| |
| T | TryPop (out bool isEmpty) |
| | Removes the largest item from the heap (or smallest item, if this is a MinHeap). More...
|
| |
| T | Pop () |
| | Removes the largest item from the heap (or smallest item, if this is a MinHeap). More...
|
| |
| T | TryPeek (out bool isEmpty) |
| | Gets the largest item from the heap if it is not empty (or the smallest item, if this is a MinHeap). More...
|
| |
| T | Peek () |
| | Gets the largest item from the heap (or the smallest item, if this is a MinHeap). More...
|
| |
| Action<T, int> Loyc.Collections.MaxHeap< T, TList, TComparer >.OnItemMoved |
This optional callback is called whenever an item is placed into the heap, removed from the heap, or moved within the List that holds the contents of the heap. The callback is useful for certain algorithms, such as the Dijkstra algorithm, in which an object's priority may need to change while it is inside the heap.
In order to change an object's priority, the object must contain mutable state that represents its priority. After changing the priority, you must call PriorityChanged to inform the heap that the priority has changed. That method takes the current position of the object in the List as an argument. In order to keep track of the current position you must handle the OnItemMoved event and store the new position somewhere, such as inside the object itself.
The parameters to OnItemMoved are the object that has moved and the new index of the item within the List. If the item has been removed from the list, the index is -1.