| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Itinero.MapMatching.Model; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Implements a priority queue in the form of a binary heap. |
| | | 7 | | /// </summary> |
| | | 8 | | internal class BinaryHeap<T> |
| | | 9 | | where T : struct |
| | | 10 | | { |
| | | 11 | | private T[] _heap; // The objects per priority. |
| | | 12 | | private double[] _priorities; // Holds the priorities of this heap. |
| | | 13 | | private int _count; // The current count of elements. |
| | | 14 | | private uint _latestIndex; // The latest unused index |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates a new binary heap. |
| | | 18 | | /// </summary> |
| | | 19 | | public BinaryHeap() |
| | 29 | 20 | | : this(2) |
| | 29 | 21 | | { |
| | | 22 | | |
| | 29 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Creates a new binary heap. |
| | | 27 | | /// </summary> |
| | 29 | 28 | | public BinaryHeap(uint initialSize) |
| | 29 | 29 | | { |
| | 29 | 30 | | _heap = new T[initialSize]; |
| | 29 | 31 | | _priorities = new double[initialSize]; |
| | | 32 | | |
| | 29 | 33 | | _count = 0; |
| | 29 | 34 | | _latestIndex = 1; |
| | 29 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Returns the number of items in this queue. |
| | | 39 | | /// </summary> |
| | 95508 | 40 | | public int Count => _count; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Enqueues a given item. |
| | | 44 | | /// </summary> |
| | | 45 | | public void Push(T item, double priority) |
| | 114491 | 46 | | { |
| | 114491 | 47 | | _count++; // another item was added! |
| | | 48 | | |
| | | 49 | | // increase size if needed. |
| | 114491 | 50 | | if (_latestIndex == _priorities.Length - 1) |
| | 285 | 51 | | { |
| | | 52 | | // time to increase size! |
| | 285 | 53 | | Array.Resize(ref _heap, _heap.Length + 100); |
| | 285 | 54 | | Array.Resize(ref _priorities, _priorities.Length + 100); |
| | 285 | 55 | | } |
| | | 56 | | |
| | | 57 | | // add the item at the first free point |
| | 114491 | 58 | | _priorities[_latestIndex] = priority; |
| | 114491 | 59 | | _heap[_latestIndex] = item; |
| | | 60 | | |
| | | 61 | | // ... and let it 'bubble' up. |
| | 114491 | 62 | | var bubbleIndex = _latestIndex; |
| | 114491 | 63 | | _latestIndex++; |
| | 189982 | 64 | | while (bubbleIndex != 1) |
| | 189288 | 65 | | { |
| | | 66 | | // bubble until the index is one. |
| | 189288 | 67 | | var parentIdx = bubbleIndex / 2; |
| | 189288 | 68 | | if (_priorities[bubbleIndex] < _priorities[parentIdx]) |
| | 75491 | 69 | | { |
| | | 70 | | // the parent priority is higher; do the swap. |
| | 75491 | 71 | | var tempPriority = _priorities[parentIdx]; |
| | 75491 | 72 | | var tempItem = _heap[parentIdx]; |
| | 75491 | 73 | | _priorities[parentIdx] = _priorities[bubbleIndex]; |
| | 75491 | 74 | | _heap[parentIdx] = _heap[bubbleIndex]; |
| | 75491 | 75 | | _priorities[bubbleIndex] = tempPriority; |
| | 75491 | 76 | | _heap[bubbleIndex] = tempItem; |
| | | 77 | | |
| | 75491 | 78 | | bubbleIndex = parentIdx; |
| | 75491 | 79 | | } |
| | | 80 | | else |
| | 113797 | 81 | | { |
| | | 82 | | // the parent priority is lower or equal; the item will not bubble up more. |
| | 113797 | 83 | | break; |
| | | 84 | | } |
| | 75491 | 85 | | } |
| | 114491 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Returns the smallest weight in the queue. |
| | | 90 | | /// </summary> |
| | | 91 | | public double PeekWeight() |
| | 0 | 92 | | { |
| | 0 | 93 | | return _priorities[1]; |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Returns the object with the smallest weight. |
| | | 98 | | /// </summary> |
| | | 99 | | public T Peek() |
| | 0 | 100 | | { |
| | 0 | 101 | | return _heap[1]; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Returns the object with the smallest weight and removes it. |
| | | 106 | | /// </summary> |
| | | 107 | | public T Pop(out double priority) |
| | 95508 | 108 | | { |
| | 95508 | 109 | | priority = 0; |
| | 95508 | 110 | | if (_count <= 0) return default(T); |
| | | 111 | | |
| | 95508 | 112 | | var item = _heap[1]; // get the first item. |
| | 95508 | 113 | | priority = _priorities[1]; |
| | | 114 | | |
| | 95508 | 115 | | _count--; // reduce the element count. |
| | 95508 | 116 | | _latestIndex--; // reduce the latest index. |
| | | 117 | | |
| | 95508 | 118 | | var swapItem = 1; |
| | 95508 | 119 | | var parentPriority = _priorities[_latestIndex]; |
| | 95508 | 120 | | var parentItem = _heap[_latestIndex]; |
| | 95508 | 121 | | _heap[1] = parentItem; // place the last element on top. |
| | 95508 | 122 | | _priorities[1] = parentPriority; // place the last element on top. |
| | | 123 | | do |
| | 1126803 | 124 | | { |
| | 1126803 | 125 | | var parent = swapItem; |
| | 1126803 | 126 | | var swapItemPriority = 0d; |
| | 1126803 | 127 | | if ((2 * parent + 1) <= _latestIndex) |
| | 1054435 | 128 | | { |
| | 1054435 | 129 | | swapItemPriority = _priorities[2 * parent]; |
| | 1054435 | 130 | | var potentialSwapItem = _priorities[2 * parent + 1]; |
| | 1054435 | 131 | | if (parentPriority >= swapItemPriority) |
| | 1006863 | 132 | | { |
| | 1006863 | 133 | | swapItem = 2 * parent; |
| | 1006863 | 134 | | if (_priorities[swapItem] >= potentialSwapItem) |
| | 481590 | 135 | | { |
| | 481590 | 136 | | swapItemPriority = potentialSwapItem; |
| | 481590 | 137 | | swapItem = 2 * parent + 1; |
| | 481590 | 138 | | } |
| | 1006863 | 139 | | } |
| | 47572 | 140 | | else if (parentPriority >= potentialSwapItem) |
| | 23715 | 141 | | { |
| | 23715 | 142 | | swapItemPriority = potentialSwapItem; |
| | 23715 | 143 | | swapItem = 2 * parent + 1; |
| | 23715 | 144 | | } |
| | | 145 | | else |
| | 23857 | 146 | | { |
| | 23857 | 147 | | break; |
| | | 148 | | } |
| | 1030578 | 149 | | } |
| | 72368 | 150 | | else if ((2 * parent) <= _latestIndex) |
| | 717 | 151 | | { |
| | | 152 | | // Only one child exists |
| | 717 | 153 | | swapItemPriority = _priorities[2 * parent]; |
| | 717 | 154 | | if (parentPriority >= swapItemPriority) |
| | 717 | 155 | | { |
| | 717 | 156 | | swapItem = 2 * parent; |
| | 717 | 157 | | } |
| | | 158 | | else |
| | 0 | 159 | | { |
| | 0 | 160 | | break; |
| | | 161 | | } |
| | 717 | 162 | | } |
| | | 163 | | else |
| | 71651 | 164 | | { |
| | 71651 | 165 | | break; |
| | | 166 | | } |
| | | 167 | | |
| | 1031295 | 168 | | _priorities[parent] = swapItemPriority; |
| | 1031295 | 169 | | _priorities[swapItem] = parentPriority; |
| | 1031295 | 170 | | _heap[parent] = _heap[swapItem]; |
| | 1031295 | 171 | | _heap[swapItem] = parentItem; |
| | | 172 | | |
| | 2062590 | 173 | | } while (true); |
| | | 174 | | |
| | 95508 | 175 | | return item; |
| | 95508 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Clears this priority queue. |
| | | 180 | | /// </summary> |
| | | 181 | | public void Clear() |
| | 0 | 182 | | { |
| | 0 | 183 | | _count = 0; |
| | 0 | 184 | | _latestIndex = 1; |
| | 0 | 185 | | } |
| | | 186 | | } |