| | 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() |
| 192 | 20 | | : this(2) { } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Creates a new binary heap. |
| | 24 | | /// </summary> |
| 64 | 25 | | public BinaryHeap(uint initialSize) |
| 64 | 26 | | { |
| 64 | 27 | | _heap = new T[initialSize]; |
| 64 | 28 | | _priorities = new double[initialSize]; |
| | 29 | |
|
| 64 | 30 | | _count = 0; |
| 64 | 31 | | _latestIndex = 1; |
| 64 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Returns the number of items in this queue. |
| | 36 | | /// </summary> |
| 204 | 37 | | public int Count => _count; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Enqueues a given item. |
| | 41 | | /// </summary> |
| | 42 | | public void Push(T item, double priority) |
| 156 | 43 | | { |
| 156 | 44 | | _count++; // another item was added! |
| | 45 | |
|
| | 46 | | // increase size if needed. |
| 156 | 47 | | if (_latestIndex == _priorities.Length - 1) |
| 64 | 48 | | { |
| | 49 | | // time to increase size! |
| 64 | 50 | | Array.Resize(ref _heap, _heap.Length + 100); |
| 64 | 51 | | Array.Resize(ref _priorities, _priorities.Length + 100); |
| 64 | 52 | | } |
| | 53 | |
|
| | 54 | | // add the item at the first free point |
| 156 | 55 | | _priorities[_latestIndex] = priority; |
| 156 | 56 | | _heap[_latestIndex] = item; |
| | 57 | |
|
| | 58 | | // ... and let it 'bubble' up. |
| 156 | 59 | | var bubbleIndex = _latestIndex; |
| 156 | 60 | | _latestIndex++; |
| 189 | 61 | | while (bubbleIndex != 1) |
| 69 | 62 | | { |
| | 63 | | // bubble until the index is one. |
| 69 | 64 | | var parentIdx = bubbleIndex / 2; |
| 69 | 65 | | if (_priorities[bubbleIndex] < _priorities[parentIdx]) |
| 33 | 66 | | { |
| | 67 | | // the parent priority is higher; do the swap. |
| 33 | 68 | | var tempPriority = _priorities[parentIdx]; |
| 33 | 69 | | var tempItem = _heap[parentIdx]; |
| 33 | 70 | | _priorities[parentIdx] = _priorities[bubbleIndex]; |
| 33 | 71 | | _heap[parentIdx] = _heap[bubbleIndex]; |
| 33 | 72 | | _priorities[bubbleIndex] = tempPriority; |
| 33 | 73 | | _heap[bubbleIndex] = tempItem; |
| | 74 | |
|
| 33 | 75 | | bubbleIndex = parentIdx; |
| 33 | 76 | | } |
| | 77 | | else |
| 36 | 78 | | { |
| | 79 | | // the parent priority is lower or equal; the item will not bubble up more. |
| 36 | 80 | | break; |
| | 81 | | } |
| 33 | 82 | | } |
| 156 | 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) |
| 138 | 105 | | { |
| 138 | 106 | | priority = 0; |
| 138 | 107 | | if (_count <= 0) |
| 0 | 108 | | { |
| 0 | 109 | | return default; |
| | 110 | | } |
| | 111 | |
|
| 138 | 112 | | var item = _heap[1]; // get the first item. |
| 138 | 113 | | priority = _priorities[1]; |
| | 114 | |
|
| 138 | 115 | | _count--; // reduce the element count. |
| 138 | 116 | | _latestIndex--; // reduce the latest index. |
| | 117 | |
|
| 138 | 118 | | var swapItem = 1; |
| 138 | 119 | | var parentPriority = _priorities[_latestIndex]; |
| 138 | 120 | | var parentItem = _heap[_latestIndex]; |
| 138 | 121 | | _heap[1] = parentItem; // place the last element on top. |
| 138 | 122 | | _priorities[1] = parentPriority; // place the last element on top. |
| | 123 | | do |
| 205 | 124 | | { |
| 205 | 125 | | var parent = swapItem; |
| 205 | 126 | | var swapItemPriority = 0d; |
| 205 | 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 | | } |
| 204 | 150 | | else if (2 * parent <= _latestIndex) |
| 66 | 151 | | { |
| | 152 | | // Only one child exists |
| 66 | 153 | | swapItemPriority = _priorities[2 * parent]; |
| 66 | 154 | | if (parentPriority >= swapItemPriority) |
| 66 | 155 | | { |
| 66 | 156 | | swapItem = 2 * parent; |
| 66 | 157 | | } |
| | 158 | | else |
| 0 | 159 | | { |
| 0 | 160 | | break; |
| | 161 | | } |
| 66 | 162 | | } |
| | 163 | | else |
| 138 | 164 | | { |
| 138 | 165 | | break; |
| | 166 | | } |
| | 167 | |
|
| 67 | 168 | | _priorities[parent] = swapItemPriority; |
| 67 | 169 | | _priorities[swapItem] = parentPriority; |
| 67 | 170 | | _heap[parent] = _heap[swapItem]; |
| 67 | 171 | | _heap[swapItem] = parentItem; |
| 134 | 172 | | } while (true); |
| | 173 | |
|
| 138 | 174 | | return item; |
| 138 | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Clears this priority queue. |
| | 179 | | /// </summary> |
| | 180 | | public void Clear() |
| 30 | 181 | | { |
| 30 | 182 | | _count = 0; |
| 30 | 183 | | _latestIndex = 1; |
| 30 | 184 | | } |
| | 185 | | } |