| | | 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 item, double priority)[] _data; |
| | | 12 | | private int _count; |
| | | 13 | | private uint _latestIndex; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a new binary heap. |
| | | 17 | | /// </summary> |
| | | 18 | | public BinaryHeap() |
| | 228 | 19 | | : this(1024) { } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a new binary heap. |
| | | 23 | | /// </summary> |
| | 76 | 24 | | public BinaryHeap(uint initialSize) |
| | 76 | 25 | | { |
| | 76 | 26 | | _data = new (T, double)[initialSize]; |
| | 76 | 27 | | _count = 0; |
| | 76 | 28 | | _latestIndex = 1; |
| | 76 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns the number of items in this queue. |
| | | 33 | | /// </summary> |
| | 400778 | 34 | | public int Count => _count; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Enqueues a given item. |
| | | 38 | | /// </summary> |
| | | 39 | | public void Push(T item, double priority) |
| | 408679 | 40 | | { |
| | 408679 | 41 | | _count++; |
| | | 42 | | |
| | 408679 | 43 | | if (_latestIndex == _data.Length - 1) |
| | 0 | 44 | | { |
| | 0 | 45 | | Array.Resize(ref _data, _data.Length * 2); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | // add the item at the first free point. |
| | 408679 | 49 | | _data[_latestIndex] = (item, priority); |
| | | 50 | | |
| | | 51 | | // ... and let it 'bubble' up using hole-sinking: |
| | | 52 | | // instead of swapping at each level, leave a hole and move it up. |
| | 408679 | 53 | | var bubbleIndex = _latestIndex; |
| | 408679 | 54 | | _latestIndex++; |
| | 685283 | 55 | | while (bubbleIndex != 1) |
| | 616755 | 56 | | { |
| | 616755 | 57 | | var parentIdx = bubbleIndex / 2; |
| | 616755 | 58 | | if (priority < _data[parentIdx].priority) |
| | 276604 | 59 | | { |
| | 276604 | 60 | | _data[bubbleIndex] = _data[parentIdx]; |
| | 276604 | 61 | | bubbleIndex = parentIdx; |
| | 276604 | 62 | | } |
| | | 63 | | else |
| | 340151 | 64 | | { |
| | 340151 | 65 | | break; |
| | | 66 | | } |
| | 276604 | 67 | | } |
| | | 68 | | |
| | 408679 | 69 | | _data[bubbleIndex] = (item, priority); |
| | 408679 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Returns the smallest weight in the queue. |
| | | 74 | | /// </summary> |
| | | 75 | | public double PeekWeight() |
| | 0 | 76 | | { |
| | 0 | 77 | | return _data[1].priority; |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Returns the object with the smallest weight. |
| | | 82 | | /// </summary> |
| | | 83 | | public T Peek() |
| | 0 | 84 | | { |
| | 0 | 85 | | return _data[1].item; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Returns the object with the smallest weight and removes it. |
| | | 90 | | /// </summary> |
| | | 91 | | public T Pop(out double priority) |
| | 392571 | 92 | | { |
| | 392571 | 93 | | priority = 0; |
| | 392571 | 94 | | if (_count <= 0) |
| | 11 | 95 | | { |
| | 11 | 96 | | return default; |
| | | 97 | | } |
| | | 98 | | |
| | 392560 | 99 | | var result = _data[1]; |
| | 392560 | 100 | | priority = result.priority; |
| | | 101 | | |
| | 392560 | 102 | | _count--; |
| | 392560 | 103 | | _latestIndex--; |
| | | 104 | | |
| | | 105 | | // take the last element and sift it down from the root. |
| | 392560 | 106 | | var last = _data[_latestIndex]; |
| | 392560 | 107 | | var lastPriority = last.priority; |
| | | 108 | | |
| | | 109 | | // sift down using hole-sinking: move the smaller child up, |
| | | 110 | | // place the last element at the final hole position. |
| | 392560 | 111 | | uint hole = 1; |
| | 1184452 | 112 | | while (true) |
| | 1184452 | 113 | | { |
| | 1184452 | 114 | | var child = hole * 2; |
| | 1184452 | 115 | | if (child + 1 <= _latestIndex) |
| | 894566 | 116 | | { |
| | | 117 | | // two children exist — pick the smaller one. |
| | 894566 | 118 | | if (_data[child + 1].priority < _data[child].priority) |
| | 412589 | 119 | | { |
| | 412589 | 120 | | child++; |
| | 412589 | 121 | | } |
| | | 122 | | |
| | 894566 | 123 | | if (lastPriority <= _data[child].priority) |
| | 102674 | 124 | | { |
| | 102674 | 125 | | break; |
| | | 126 | | } |
| | | 127 | | |
| | 791892 | 128 | | _data[hole] = _data[child]; |
| | 791892 | 129 | | hole = child; |
| | 791892 | 130 | | } |
| | 289886 | 131 | | else if (child <= _latestIndex) |
| | 97561 | 132 | | { |
| | | 133 | | // only left child exists. |
| | 97561 | 134 | | if (lastPriority <= _data[child].priority) |
| | 97561 | 135 | | { |
| | 97561 | 136 | | break; |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | _data[hole] = _data[child]; |
| | 0 | 140 | | hole = child; |
| | 0 | 141 | | } |
| | | 142 | | else |
| | 192325 | 143 | | { |
| | 192325 | 144 | | break; |
| | | 145 | | } |
| | 791892 | 146 | | } |
| | | 147 | | |
| | 392560 | 148 | | _data[hole] = last; |
| | | 149 | | |
| | 392560 | 150 | | return result.item; |
| | 392571 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Clears this priority queue. |
| | | 155 | | /// </summary> |
| | | 156 | | public void Clear() |
| | 11438 | 157 | | { |
| | 11438 | 158 | | _count = 0; |
| | 11438 | 159 | | _latestIndex = 1; |
| | 11438 | 160 | | } |
| | | 161 | | } |