| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace Trailblazer.Pathing; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Low-allocation metadata table for <see cref="PathHeap{TNode}"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class PathHeapMetadata<TNode> where TNode : class |
| | | 12 | | { |
| | | 13 | | private const double LoadFactorThreshold = 0.72; |
| | | 14 | | |
| | 3 | 15 | | private static readonly EqualityComparer<TNode> _comparer = EqualityComparer<TNode>.Default; |
| | | 16 | | |
| | | 17 | | private readonly int _minimumCapacity; |
| | | 18 | | |
| | | 19 | | private TNode?[] _keys = null!; |
| | | 20 | | |
| | | 21 | | private PathHeapMeta[] _values = null!; |
| | | 22 | | |
| | | 23 | | private int[] _hashes = null!; |
| | | 24 | | |
| | | 25 | | private int[] _occupiedSlots = null!; |
| | | 26 | | |
| | | 27 | | private int _mask; |
| | | 28 | | |
| | | 29 | | private int _resizeThreshold; |
| | | 30 | | |
| | | 31 | | private int _count; |
| | | 32 | | |
| | 2909 | 33 | | public PathHeapMetadata(int capacity) |
| | | 34 | | { |
| | 2909 | 35 | | _minimumCapacity = NormalizeCapacity(capacity); |
| | 2909 | 36 | | Initialize(_minimumCapacity); |
| | 2909 | 37 | | } |
| | | 38 | | |
| | 123 | 39 | | public int Count => _count; |
| | | 40 | | |
| | | 41 | | public PathHeapMeta this[TNode key] |
| | | 42 | | { |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 44 | | get |
| | | 45 | | { |
| | 46510 | 46 | | if (TryGetValue(key, out PathHeapMeta value)) |
| | 46509 | 47 | | return value; |
| | | 48 | | |
| | 1 | 49 | | throw new KeyNotFoundException("The requested path heap node is not tracked."); |
| | | 50 | | } |
| | | 51 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 26694 | 52 | | set => Set(key, value); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 56 | | public bool TryGetValue(TNode key, out PathHeapMeta value) |
| | | 57 | | { |
| | 130154 | 58 | | if (key == null) |
| | | 59 | | { |
| | 1 | 60 | | value = default; |
| | 1 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | 130153 | 64 | | int hash = GetStoredHash(key); |
| | 130153 | 65 | | int slot = FindSlot(key, hash); |
| | 130153 | 66 | | if (slot < 0) |
| | | 67 | | { |
| | 28626 | 68 | | value = default; |
| | 28626 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | |
| | 101527 | 72 | | value = _values[slot]; |
| | 101527 | 73 | | return true; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public void Set(TNode key, PathHeapMeta value) |
| | | 77 | | { |
| | 26695 | 78 | | if (key == null) |
| | 1 | 79 | | throw new ArgumentNullException(nameof(key)); |
| | | 80 | | |
| | 26694 | 81 | | int hash = GetStoredHash(key); |
| | 26694 | 82 | | int existingSlot = FindSlot(key, hash); |
| | 26694 | 83 | | if (existingSlot >= 0) |
| | | 84 | | { |
| | 14193 | 85 | | _values[existingSlot] = value; |
| | 14193 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 12501 | 89 | | if (_count >= _resizeThreshold) |
| | 10 | 90 | | Resize(_keys.Length * 2); |
| | | 91 | | |
| | 12501 | 92 | | InsertNew(key, hash, value); |
| | 12501 | 93 | | } |
| | | 94 | | |
| | | 95 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 249 | 96 | | public ClosedEnumerable EnumerateClosed(uint heapVersion) => new(this, heapVersion); |
| | | 97 | | |
| | | 98 | | public void Clear() |
| | | 99 | | { |
| | 19926 | 100 | | for (int i = 0; i < _count; i++) |
| | | 101 | | { |
| | 8250 | 102 | | int slot = _occupiedSlots[i]; |
| | 8250 | 103 | | _keys[slot] = null; |
| | 8250 | 104 | | _values[slot] = default; |
| | 8250 | 105 | | _hashes[slot] = 0; |
| | 8250 | 106 | | _occupiedSlots[i] = 0; |
| | | 107 | | } |
| | | 108 | | |
| | 1713 | 109 | | _count = 0; |
| | 1713 | 110 | | } |
| | | 111 | | |
| | | 112 | | public void EnsureCapacity(int capacity) |
| | | 113 | | { |
| | 7 | 114 | | int normalized = NormalizeCapacity(capacity); |
| | 7 | 115 | | if (normalized > _keys.Length) |
| | 1 | 116 | | Resize(normalized); |
| | 7 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void TrimExcess() |
| | | 120 | | { |
| | 2 | 121 | | int target = NormalizeCapacity(Math.Max(_minimumCapacity, _count)); |
| | 2 | 122 | | if (target < _keys.Length) |
| | 1 | 123 | | Resize(target); |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | private void Initialize(int capacity) |
| | | 127 | | { |
| | 2921 | 128 | | _keys = new TNode?[capacity]; |
| | 2921 | 129 | | _values = new PathHeapMeta[capacity]; |
| | 2921 | 130 | | _hashes = new int[capacity]; |
| | 2921 | 131 | | _occupiedSlots = new int[capacity]; |
| | 2921 | 132 | | _mask = capacity - 1; |
| | 2921 | 133 | | _resizeThreshold = Math.Max(1, (int)(capacity * LoadFactorThreshold)); |
| | 2921 | 134 | | _count = 0; |
| | 2921 | 135 | | } |
| | | 136 | | |
| | | 137 | | private void Resize(int capacity) |
| | | 138 | | { |
| | 12 | 139 | | TNode?[] oldKeys = _keys; |
| | 12 | 140 | | PathHeapMeta[] oldValues = _values; |
| | 12 | 141 | | int[] oldHashes = _hashes; |
| | 12 | 142 | | int[] oldOccupiedSlots = _occupiedSlots; |
| | 12 | 143 | | int oldCount = _count; |
| | | 144 | | |
| | 12 | 145 | | Initialize(NormalizeCapacity(capacity)); |
| | | 146 | | |
| | 12378 | 147 | | for (int i = 0; i < oldCount; i++) |
| | | 148 | | { |
| | 6177 | 149 | | int oldSlot = oldOccupiedSlots[i]; |
| | 6177 | 150 | | TNode? key = oldKeys[oldSlot]; |
| | 6177 | 151 | | if (key != null) |
| | 6177 | 152 | | InsertNew(key, oldHashes[oldSlot], oldValues[oldSlot]); |
| | | 153 | | } |
| | 12 | 154 | | } |
| | | 155 | | |
| | | 156 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 157 | | private void InsertNew(TNode key, int hash, PathHeapMeta value) |
| | | 158 | | { |
| | 18678 | 159 | | int slot = hash & _mask; |
| | 30549 | 160 | | while (_keys[slot] != null) |
| | 11871 | 161 | | slot = (slot + 1) & _mask; |
| | | 162 | | |
| | 18678 | 163 | | _keys[slot] = key; |
| | 18678 | 164 | | _hashes[slot] = hash; |
| | 18678 | 165 | | _values[slot] = value; |
| | 18678 | 166 | | _occupiedSlots[_count++] = slot; |
| | 18678 | 167 | | } |
| | | 168 | | |
| | | 169 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 170 | | private int FindSlot(TNode key, int hash) |
| | | 171 | | { |
| | 156847 | 172 | | int slot = hash & _mask; |
| | | 173 | | |
| | 65768 | 174 | | while (true) |
| | | 175 | | { |
| | 222615 | 176 | | TNode? candidate = _keys[slot]; |
| | 222615 | 177 | | if (candidate == null) |
| | 41127 | 178 | | return -1; |
| | | 179 | | |
| | 181488 | 180 | | if (_hashes[slot] == hash && _comparer.Equals(candidate, key)) |
| | 115720 | 181 | | return slot; |
| | | 182 | | |
| | 65768 | 183 | | slot = (slot + 1) & _mask; |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 188 | | private static int GetStoredHash(TNode key) |
| | | 189 | | { |
| | | 190 | | unchecked |
| | | 191 | | { |
| | 156847 | 192 | | uint hash = (uint)_comparer.GetHashCode(key); |
| | 156847 | 193 | | hash ^= hash >> 16; |
| | 156847 | 194 | | hash *= 0x7feb352d; |
| | 156847 | 195 | | hash ^= hash >> 15; |
| | 156847 | 196 | | hash *= 0x846ca68b; |
| | 156847 | 197 | | hash ^= hash >> 16; |
| | 156847 | 198 | | return (int)(hash & 0x7FFFFFFF); |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private static int NormalizeCapacity(int capacity) |
| | | 203 | | { |
| | 2930 | 204 | | int normalized = 1; |
| | 23454 | 205 | | while (normalized < capacity) |
| | 20524 | 206 | | normalized <<= 1; |
| | | 207 | | |
| | 2930 | 208 | | return Math.Max(2, normalized); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | internal readonly struct ClosedEnumerable : IEnumerable<TNode> |
| | | 212 | | { |
| | | 213 | | private readonly PathHeapMetadata<TNode> _metadata; |
| | | 214 | | |
| | | 215 | | private readonly uint _heapVersion; |
| | | 216 | | |
| | | 217 | | public ClosedEnumerable(PathHeapMetadata<TNode> metadata, uint heapVersion) |
| | | 218 | | { |
| | 249 | 219 | | _metadata = metadata; |
| | 249 | 220 | | _heapVersion = heapVersion; |
| | 249 | 221 | | } |
| | | 222 | | |
| | | 223 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 249 | 224 | | public Enumerator GetEnumerator() => new(_metadata, _heapVersion); |
| | | 225 | | |
| | 3 | 226 | | IEnumerator<TNode> IEnumerable<TNode>.GetEnumerator() => GetEnumerator(); |
| | | 227 | | |
| | 1 | 228 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | internal struct Enumerator : IEnumerator<TNode> |
| | | 232 | | { |
| | | 233 | | private readonly PathHeapMetadata<TNode> _metadata; |
| | | 234 | | |
| | | 235 | | private readonly uint _heapVersion; |
| | | 236 | | |
| | | 237 | | private int _index; |
| | | 238 | | |
| | | 239 | | private TNode? _current; |
| | | 240 | | |
| | | 241 | | public Enumerator(PathHeapMetadata<TNode> metadata, uint heapVersion) |
| | | 242 | | { |
| | 249 | 243 | | _metadata = metadata; |
| | 249 | 244 | | _heapVersion = heapVersion; |
| | 249 | 245 | | _index = 0; |
| | 249 | 246 | | _current = null; |
| | 249 | 247 | | } |
| | | 248 | | |
| | 3639 | 249 | | public readonly TNode Current => _current!; |
| | | 250 | | |
| | 2 | 251 | | readonly object IEnumerator.Current => Current; |
| | | 252 | | |
| | | 253 | | public bool MoveNext() |
| | | 254 | | { |
| | 3895 | 255 | | while (_index < _metadata._count) |
| | | 256 | | { |
| | 3647 | 257 | | int slot = _metadata._occupiedSlots[_index++]; |
| | 3647 | 258 | | if (_metadata._values[slot].ClosedHeapVersion != _heapVersion) |
| | | 259 | | continue; |
| | | 260 | | |
| | 3638 | 261 | | TNode? key = _metadata._keys[slot]; |
| | 3638 | 262 | | if (key == null) |
| | | 263 | | continue; |
| | | 264 | | |
| | 3638 | 265 | | _current = key; |
| | 3638 | 266 | | return true; |
| | | 267 | | } |
| | | 268 | | |
| | 248 | 269 | | _current = null; |
| | 248 | 270 | | return false; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | public void Reset() |
| | | 274 | | { |
| | 1 | 275 | | _index = 0; |
| | 1 | 276 | | _current = null; |
| | 1 | 277 | | } |
| | | 278 | | |
| | | 279 | | public readonly void Dispose() |
| | | 280 | | { |
| | 248 | 281 | | } |
| | | 282 | | } |
| | | 283 | | } |