| | | 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 | | /// <remarks> |
| | | 9 | | /// Priorities and items are separate arrays: sifting is almost all priority comparisons, and a |
| | | 10 | | /// combined (T item, double priority)[] made each one stride over the item to read 8 bytes. |
| | | 11 | | /// </remarks> |
| | | 12 | | internal class BinaryHeap<T> |
| | | 13 | | where T : struct |
| | | 14 | | { |
| | | 15 | | private T[] _items; |
| | | 16 | | private double[] _priorities; |
| | | 17 | | private int _count; |
| | | 18 | | private uint _latestIndex; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates a new binary heap. |
| | | 22 | | /// </summary> |
| | | 23 | | public BinaryHeap() |
| | 1071 | 24 | | : this(1024) { } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates a new binary heap. |
| | | 28 | | /// </summary> |
| | 357 | 29 | | public BinaryHeap(uint initialSize) |
| | 357 | 30 | | { |
| | 357 | 31 | | _items = new T[initialSize]; |
| | 357 | 32 | | _priorities = new double[initialSize]; |
| | 357 | 33 | | _count = 0; |
| | 357 | 34 | | _latestIndex = 1; |
| | 357 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Returns the number of items in this queue. |
| | | 39 | | /// </summary> |
| | 424755 | 40 | | public int Count => _count; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Enqueues a given item. |
| | | 44 | | /// </summary> |
| | | 45 | | public void Push(T item, double priority) |
| | 428721 | 46 | | { |
| | 428721 | 47 | | _count++; |
| | | 48 | | |
| | 428721 | 49 | | if (_latestIndex == _items.Length - 1) |
| | 0 | 50 | | { |
| | 0 | 51 | | Array.Resize(ref _items, _items.Length * 2); |
| | 0 | 52 | | Array.Resize(ref _priorities, _priorities.Length * 2); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | // ... and let it 'bubble' up using hole-sinking: |
| | | 56 | | // instead of swapping at each level, leave a hole and move it up. |
| | 428721 | 57 | | var bubbleIndex = _latestIndex; |
| | 428721 | 58 | | _latestIndex++; |
| | 709273 | 59 | | while (bubbleIndex != 1) |
| | 638604 | 60 | | { |
| | 638604 | 61 | | var parentIdx = bubbleIndex / 2; |
| | 638604 | 62 | | if (priority < _priorities[parentIdx]) |
| | 280552 | 63 | | { |
| | 280552 | 64 | | _items[bubbleIndex] = _items[parentIdx]; |
| | 280552 | 65 | | _priorities[bubbleIndex] = _priorities[parentIdx]; |
| | 280552 | 66 | | bubbleIndex = parentIdx; |
| | 280552 | 67 | | } |
| | | 68 | | else |
| | 358052 | 69 | | { |
| | 358052 | 70 | | break; |
| | | 71 | | } |
| | 280552 | 72 | | } |
| | | 73 | | |
| | 428721 | 74 | | _items[bubbleIndex] = item; |
| | 428721 | 75 | | _priorities[bubbleIndex] = priority; |
| | 428721 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Returns the smallest weight in the queue. |
| | | 80 | | /// </summary> |
| | | 81 | | public double PeekWeight() |
| | 0 | 82 | | { |
| | 0 | 83 | | return _priorities[1]; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Returns the object with the smallest weight. |
| | | 88 | | /// </summary> |
| | | 89 | | public T Peek() |
| | 0 | 90 | | { |
| | 0 | 91 | | return _items[1]; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Returns the object with the smallest weight and removes it. |
| | | 96 | | /// </summary> |
| | | 97 | | public T Pop(out double priority) |
| | 412102 | 98 | | { |
| | 412102 | 99 | | priority = 0; |
| | 412102 | 100 | | if (_count <= 0) |
| | 0 | 101 | | { |
| | 0 | 102 | | return default; |
| | | 103 | | } |
| | | 104 | | |
| | 412102 | 105 | | var result = _items[1]; |
| | 412102 | 106 | | priority = _priorities[1]; |
| | | 107 | | |
| | 412102 | 108 | | _count--; |
| | 412102 | 109 | | _latestIndex--; |
| | | 110 | | |
| | | 111 | | // take the last element and sift it down from the root. |
| | 412102 | 112 | | var lastItem = _items[_latestIndex]; |
| | 412102 | 113 | | var lastPriority = _priorities[_latestIndex]; |
| | | 114 | | |
| | | 115 | | // sift down using hole-sinking: move the smaller child up, |
| | | 116 | | // place the last element at the final hole position. |
| | 412102 | 117 | | uint hole = 1; |
| | 1298716 | 118 | | while (true) |
| | 1298716 | 119 | | { |
| | 1298716 | 120 | | var child = hole * 2; |
| | 1298716 | 121 | | if (child + 1 <= _latestIndex) |
| | 992006 | 122 | | { |
| | | 123 | | // two children exist — pick the smaller one. |
| | 992006 | 124 | | if (_priorities[child + 1] < _priorities[child]) |
| | 457373 | 125 | | { |
| | 457373 | 126 | | child++; |
| | 457373 | 127 | | } |
| | | 128 | | |
| | 992006 | 129 | | if (lastPriority <= _priorities[child]) |
| | 105392 | 130 | | { |
| | 105392 | 131 | | break; |
| | | 132 | | } |
| | | 133 | | |
| | 886614 | 134 | | _items[hole] = _items[child]; |
| | 886614 | 135 | | _priorities[hole] = _priorities[child]; |
| | 886614 | 136 | | hole = child; |
| | 886614 | 137 | | } |
| | 306710 | 138 | | else if (child <= _latestIndex) |
| | 99483 | 139 | | { |
| | | 140 | | // only left child exists. |
| | 99483 | 141 | | if (lastPriority <= _priorities[child]) |
| | 99483 | 142 | | { |
| | 99483 | 143 | | break; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | _items[hole] = _items[child]; |
| | 0 | 147 | | _priorities[hole] = _priorities[child]; |
| | 0 | 148 | | hole = child; |
| | 0 | 149 | | } |
| | | 150 | | else |
| | 207227 | 151 | | { |
| | 207227 | 152 | | break; |
| | | 153 | | } |
| | 886614 | 154 | | } |
| | | 155 | | |
| | 412102 | 156 | | _items[hole] = lastItem; |
| | 412102 | 157 | | _priorities[hole] = lastPriority; |
| | | 158 | | |
| | 412102 | 159 | | return result; |
| | 412102 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Clears this priority queue. |
| | | 164 | | /// </summary> |
| | | 165 | | public void Clear() |
| | 11588 | 166 | | { |
| | 11588 | 167 | | _count = 0; |
| | 11588 | 168 | | _latestIndex = 1; |
| | 11588 | 169 | | } |
| | | 170 | | } |