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