| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Pathing; |
| | | 6 | | |
| | | 7 | | internal struct PathHeapMeta |
| | | 8 | | { |
| | | 9 | | public uint HeapIndex; |
| | | 10 | | |
| | | 11 | | public uint HeapVersion; |
| | | 12 | | |
| | | 13 | | public uint ClosedHeapVersion; |
| | | 14 | | |
| | | 15 | | public int PathCost; |
| | | 16 | | |
| | | 17 | | public int TieBreakCost; |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Shared binary heap used by the pathing surveyors to track open and closed nodes. |
| | | 22 | | /// Heap ordering cost is owned by the heap metadata instead of the node types themselves. |
| | | 23 | | /// </summary> |
| | | 24 | | internal sealed class PathHeap<TNode> where TNode : class |
| | | 25 | | { |
| | | 26 | | public const int DefaultCapacity = 128; |
| | | 27 | | |
| | | 28 | | private TNode[] _items; |
| | | 29 | | |
| | | 30 | | private readonly PathHeapMetadata<TNode> _meta; |
| | | 31 | | |
| | | 32 | | public uint CurrentHeapVersion { get; private set; } = 1; |
| | | 33 | | |
| | | 34 | | public uint HeapCount { get; private set; } |
| | | 35 | | |
| | 123 | 36 | | public int TrackedCount => _meta.Count; |
| | | 37 | | |
| | 2 | 38 | | public int Capacity => _items.Length; |
| | | 39 | | |
| | 2908 | 40 | | public PathHeap() |
| | | 41 | | { |
| | 2908 | 42 | | _items = new TNode[DefaultCapacity]; |
| | 2908 | 43 | | _meta = new(DefaultCapacity); |
| | 2908 | 44 | | } |
| | | 45 | | |
| | | 46 | | public void Add(TNode item, int pathCost, int tieBreakCost = 0) |
| | | 47 | | { |
| | 12500 | 48 | | if (Contains(item)) |
| | 1 | 49 | | return; |
| | | 50 | | |
| | 12499 | 51 | | if (HeapCount + 1 > _items.Length) |
| | 6 | 52 | | Resize(_items.Length * 2); |
| | | 53 | | |
| | 12499 | 54 | | PathHeapMeta meta = new() |
| | 12499 | 55 | | { |
| | 12499 | 56 | | HeapIndex = HeapCount, |
| | 12499 | 57 | | HeapVersion = CurrentHeapVersion, |
| | 12499 | 58 | | PathCost = pathCost, |
| | 12499 | 59 | | TieBreakCost = tieBreakCost |
| | 12499 | 60 | | }; |
| | | 61 | | |
| | 12499 | 62 | | _meta[item] = meta; |
| | 12499 | 63 | | _items[HeapCount++] = item; |
| | 12499 | 64 | | SortUp(item); |
| | 12499 | 65 | | } |
| | | 66 | | |
| | | 67 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 68 | | public bool TryGetPathCost(TNode item, out int pathCost) |
| | | 69 | | { |
| | 15259 | 70 | | if (!_meta.TryGetValue(item, out PathHeapMeta meta)) |
| | | 71 | | { |
| | 2 | 72 | | pathCost = int.MaxValue; |
| | 2 | 73 | | return false; |
| | | 74 | | } |
| | | 75 | | |
| | 15257 | 76 | | pathCost = meta.PathCost; |
| | 15257 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public void UpdatePathCost(TNode item, int pathCost, int tieBreakCost = 0) |
| | | 82 | | { |
| | 5 | 83 | | if (_meta.TryGetValue(item, out PathHeapMeta meta)) |
| | | 84 | | { |
| | 5 | 85 | | meta.PathCost = pathCost; |
| | 5 | 86 | | meta.TieBreakCost = tieBreakCost; |
| | 5 | 87 | | _meta[item] = meta; |
| | | 88 | | } |
| | 5 | 89 | | } |
| | | 90 | | |
| | | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 92 | | public TNode PeekAt(int index) => _items[index]; |
| | | 93 | | |
| | | 94 | | public bool RemoveFirst([MaybeNullWhen(false)] out TNode result) |
| | | 95 | | { |
| | 4032 | 96 | | if (HeapCount == 0) |
| | | 97 | | { |
| | 564 | 98 | | result = null!; |
| | 564 | 99 | | return false; |
| | | 100 | | } |
| | | 101 | | |
| | 3468 | 102 | | result = _items[0]; |
| | 3468 | 103 | | if (!_meta.TryGetValue(result, out PathHeapMeta meta)) |
| | 0 | 104 | | return false; |
| | | 105 | | |
| | 3468 | 106 | | HeapCount--; |
| | | 107 | | |
| | 3468 | 108 | | if (HeapCount == 0) |
| | | 109 | | { |
| | 1711 | 110 | | _items[0] = null!; |
| | | 111 | | } |
| | | 112 | | else |
| | | 113 | | { |
| | 1757 | 114 | | TNode temp = _items[HeapCount]; |
| | 1757 | 115 | | PathHeapMeta tempMeta = _meta[temp]; |
| | 1757 | 116 | | _items[0] = temp; |
| | 1757 | 117 | | tempMeta.HeapIndex = 0; |
| | 1757 | 118 | | _meta[temp] = tempMeta; |
| | 1757 | 119 | | _items[HeapCount] = null!; |
| | | 120 | | |
| | 1757 | 121 | | if (HeapCount > 1) |
| | 1561 | 122 | | SortDown(temp); |
| | | 123 | | } |
| | | 124 | | |
| | 3468 | 125 | | meta.HeapVersion--; |
| | 3468 | 126 | | _meta[result] = meta; |
| | 3468 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public bool Contains(TNode item) |
| | | 132 | | { |
| | 17307 | 133 | | return _meta.TryGetValue(item, out PathHeapMeta meta) |
| | 17307 | 134 | | && meta.HeapVersion == CurrentHeapVersion; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | public void SetClosed(TNode item) |
| | | 139 | | { |
| | 3179 | 140 | | if (_meta.TryGetValue(item, out PathHeapMeta meta)) |
| | | 141 | | { |
| | 3179 | 142 | | meta.ClosedHeapVersion = CurrentHeapVersion; |
| | 3179 | 143 | | _meta[item] = meta; |
| | | 144 | | } |
| | 3179 | 145 | | } |
| | | 146 | | |
| | | 147 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 148 | | public bool IsClosed(TNode item) |
| | | 149 | | { |
| | 44425 | 150 | | return _meta.TryGetValue(item, out PathHeapMeta meta) |
| | 44425 | 151 | | && meta.ClosedHeapVersion == CurrentHeapVersion; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public void SortUp(TNode item) |
| | | 155 | | { |
| | 12504 | 156 | | PathHeapMeta meta = _meta[item]; |
| | 12504 | 157 | | uint index = meta.HeapIndex; |
| | | 158 | | |
| | 13652 | 159 | | while (index > 0 && index < HeapCount) |
| | | 160 | | { |
| | 11599 | 161 | | uint parentIndex = (index - 1) / 2; |
| | 11599 | 162 | | TNode parent = _items[parentIndex]; |
| | | 163 | | |
| | 11599 | 164 | | if (!HasHigherPriority(meta, _meta[parent])) |
| | | 165 | | break; |
| | | 166 | | |
| | 1148 | 167 | | Swap(item, parent); |
| | 1148 | 168 | | meta = _meta[item]; |
| | 1148 | 169 | | index = meta.HeapIndex; |
| | | 170 | | } |
| | 12504 | 171 | | } |
| | | 172 | | |
| | | 173 | | public void SortDown(TNode item) |
| | | 174 | | { |
| | 1561 | 175 | | PathHeapMeta meta = _meta[item]; |
| | 1561 | 176 | | uint index = meta.HeapIndex; |
| | | 177 | | |
| | 1744 | 178 | | while (true) |
| | | 179 | | { |
| | 3305 | 180 | | uint left = (index * 2) + 1; |
| | 3305 | 181 | | uint right = left + 1; |
| | 3305 | 182 | | uint lowest = index; |
| | | 183 | | |
| | 3305 | 184 | | if (left < HeapCount |
| | 3305 | 185 | | && HasHigherPriority(_meta[_items[left]], _meta[_items[lowest]])) |
| | | 186 | | { |
| | 1126 | 187 | | lowest = left; |
| | | 188 | | } |
| | | 189 | | |
| | 3305 | 190 | | if (right < HeapCount |
| | 3305 | 191 | | && HasHigherPriority(_meta[_items[right]], _meta[_items[lowest]])) |
| | | 192 | | { |
| | 677 | 193 | | lowest = right; |
| | | 194 | | } |
| | | 195 | | |
| | 3305 | 196 | | if (lowest == index) |
| | | 197 | | break; |
| | | 198 | | |
| | 1744 | 199 | | Swap(item, _items[lowest]); |
| | 1744 | 200 | | meta = _meta[item]; |
| | 1744 | 201 | | index = meta.HeapIndex; |
| | | 202 | | } |
| | 1561 | 203 | | } |
| | | 204 | | |
| | | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 206 | | public PathHeapMetadata<TNode>.ClosedEnumerable EnumerateClosed() |
| | 245 | 207 | | => _meta.EnumerateClosed(CurrentHeapVersion); |
| | | 208 | | |
| | | 209 | | public void FastClear() |
| | | 210 | | { |
| | 1712 | 211 | | HeapCount = 0; |
| | 1712 | 212 | | CurrentHeapVersion++; |
| | 1712 | 213 | | _meta.Clear(); |
| | 1712 | 214 | | } |
| | | 215 | | |
| | | 216 | | public void Reset() |
| | | 217 | | { |
| | 1 | 218 | | HeapCount = 0; |
| | 1 | 219 | | CurrentHeapVersion = 1; |
| | 1 | 220 | | _meta.Clear(); |
| | 1 | 221 | | _meta.TrimExcess(); |
| | 1 | 222 | | } |
| | | 223 | | |
| | | 224 | | private void Resize(int newSize) |
| | | 225 | | { |
| | 6 | 226 | | int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize; |
| | | 227 | | |
| | 6 | 228 | | TNode[] newArray = new TNode[newCapacity]; |
| | 6 | 229 | | if (HeapCount > 0) |
| | 6 | 230 | | Array.Copy(_items, 0, newArray, 0, HeapCount); |
| | | 231 | | |
| | 6 | 232 | | _items = newArray; |
| | 6 | 233 | | _meta.EnsureCapacity(newCapacity); |
| | 6 | 234 | | } |
| | | 235 | | |
| | | 236 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 237 | | private static bool HasHigherPriority(PathHeapMeta candidate, PathHeapMeta current) |
| | | 238 | | { |
| | 16805 | 239 | | return candidate.PathCost < current.PathCost |
| | 16805 | 240 | | || (candidate.PathCost == current.PathCost |
| | 16805 | 241 | | && candidate.TieBreakCost < current.TieBreakCost); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 245 | | private void Swap(TNode itemA, TNode itemB) |
| | | 246 | | { |
| | 2892 | 247 | | PathHeapMeta metaA = _meta[itemA]; |
| | 2892 | 248 | | PathHeapMeta metaB = _meta[itemB]; |
| | | 249 | | |
| | 2892 | 250 | | uint indexA = metaA.HeapIndex; |
| | 2892 | 251 | | uint indexB = metaB.HeapIndex; |
| | | 252 | | |
| | 2892 | 253 | | _items[indexA] = itemB; |
| | 2892 | 254 | | _items[indexB] = itemA; |
| | | 255 | | |
| | 2892 | 256 | | metaA.HeapIndex = indexB; |
| | 2892 | 257 | | metaB.HeapIndex = indexA; |
| | 2892 | 258 | | _meta[itemA] = metaA; |
| | 2892 | 259 | | _meta[itemB] = metaB; |
| | 2892 | 260 | | } |
| | | 261 | | } |