| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationDependencySortWork.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Canonically sorts unique graph dependencies with bounded comparisons.</summary> |
| | | 13 | | internal struct NavigationDependencySortWork |
| | | 14 | | { |
| | | 15 | | private enum Collection : byte |
| | | 16 | | { |
| | | 17 | | Components = 0, |
| | | 18 | | Pages = 1, |
| | | 19 | | Complete = 2 |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | private enum SiftStage : byte |
| | | 23 | | { |
| | | 24 | | None = 0, |
| | | 25 | | ChooseChild = 1, |
| | | 26 | | CompareRoot = 2 |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | private readonly NavigationSurfaceComponentKey[] _components; |
| | | 30 | | private readonly int _componentCount; |
| | | 31 | | private readonly GraphPageDependencyAddress[] _pages; |
| | | 32 | | private readonly int _pageCount; |
| | | 33 | | private Collection _collection; |
| | | 34 | | private SiftStage _siftStage; |
| | | 35 | | private int _heapSize; |
| | | 36 | | private int _buildIndex; |
| | | 37 | | private int _sortEnd; |
| | | 38 | | private int _siftRoot; |
| | | 39 | | private int _siftCandidate; |
| | | 40 | | private bool _building; |
| | | 41 | | |
| | | 42 | | internal NavigationDependencySortWork(NavigationAStarWorkspace workspace) |
| | 215 | 43 | | : this( |
| | 215 | 44 | | workspace.EndpointComponents, |
| | 215 | 45 | | workspace.EndpointComponentCount, |
| | 215 | 46 | | workspace.EndpointPages, |
| | 215 | 47 | | workspace.EndpointPageCount) |
| | | 48 | | { |
| | 215 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal NavigationDependencySortWork( |
| | | 52 | | NavigationSurfaceComponentKey[] components, |
| | | 53 | | int componentCount, |
| | | 54 | | GraphPageDependencyAddress[] pages, |
| | | 55 | | int pageCount) |
| | | 56 | | { |
| | 920 | 57 | | _components = components; |
| | 920 | 58 | | _componentCount = componentCount; |
| | 920 | 59 | | _pages = pages; |
| | 920 | 60 | | _pageCount = pageCount; |
| | 920 | 61 | | _collection = Collection.Components; |
| | 920 | 62 | | _siftStage = SiftStage.None; |
| | 920 | 63 | | _heapSize = 0; |
| | 920 | 64 | | _buildIndex = 0; |
| | 920 | 65 | | _sortEnd = 0; |
| | 920 | 66 | | _siftRoot = 0; |
| | 920 | 67 | | _siftCandidate = 0; |
| | 920 | 68 | | _building = false; |
| | 920 | 69 | | InitializeCollection(); |
| | 920 | 70 | | } |
| | | 71 | | |
| | 8143 | 72 | | internal bool IsComplete => _collection == Collection.Complete; |
| | | 73 | | |
| | | 74 | | internal static int GetMaximumComparisonCount( |
| | | 75 | | int componentCount, |
| | | 76 | | int pageCount) |
| | | 77 | | { |
| | 119 | 78 | | SwiftThrowHelper.ThrowIfNegative(componentCount, nameof(componentCount)); |
| | 119 | 79 | | SwiftThrowHelper.ThrowIfNegative(pageCount, nameof(pageCount)); |
| | 119 | 80 | | return checked( |
| | 119 | 81 | | GetCollectionMaximumComparisonCount(componentCount) |
| | 119 | 82 | | + GetCollectionMaximumComparisonCount(pageCount)); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | internal bool Advance(NavigationWorkMeter meter, int lookupStepLimit) |
| | | 86 | | { |
| | 941 | 87 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 941 | 88 | | int remaining = Math.Min(lookupStepLimit, meter.RemainingLookupProbes); |
| | 5386 | 89 | | while (!IsComplete) |
| | | 90 | | { |
| | 4468 | 91 | | if (_siftStage == SiftStage.None) |
| | | 92 | | { |
| | 3916 | 93 | | if (_building) |
| | | 94 | | { |
| | 1950 | 95 | | if (_buildIndex < 0) |
| | | 96 | | { |
| | 1837 | 97 | | _building = false; |
| | 1837 | 98 | | continue; |
| | | 99 | | } |
| | 113 | 100 | | BeginSift(_buildIndex--); |
| | 113 | 101 | | continue; |
| | | 102 | | } |
| | 1966 | 103 | | if (_sortEnd <= 0) |
| | | 104 | | { |
| | 1837 | 105 | | _collection = _collection == Collection.Components |
| | 1837 | 106 | | ? Collection.Pages |
| | 1837 | 107 | | : Collection.Complete; |
| | 1837 | 108 | | InitializeCollection(); |
| | 1837 | 109 | | continue; |
| | | 110 | | } |
| | 129 | 111 | | Swap(0, _sortEnd); |
| | 129 | 112 | | _heapSize = _sortEnd--; |
| | 129 | 113 | | BeginSift(0); |
| | 129 | 114 | | continue; |
| | | 115 | | } |
| | | 116 | | |
| | 552 | 117 | | if (_siftStage == SiftStage.ChooseChild) |
| | | 118 | | { |
| | 383 | 119 | | int left = checked((_siftRoot * 2) + 1); |
| | 383 | 120 | | if (left >= _heapSize) |
| | | 121 | | { |
| | 212 | 122 | | _siftStage = SiftStage.None; |
| | 212 | 123 | | continue; |
| | | 124 | | } |
| | 171 | 125 | | _siftCandidate = left; |
| | 171 | 126 | | int right = left + 1; |
| | 171 | 127 | | if (right < _heapSize) |
| | | 128 | | { |
| | 58 | 129 | | if (!TryConsumeComparison(meter, ref remaining)) |
| | 12 | 130 | | return false; |
| | 46 | 131 | | if (Compare(left, right) < 0) |
| | 20 | 132 | | _siftCandidate = right; |
| | | 133 | | } |
| | 159 | 134 | | _siftStage = SiftStage.CompareRoot; |
| | 159 | 135 | | continue; |
| | | 136 | | } |
| | | 137 | | |
| | 169 | 138 | | if (!TryConsumeComparison(meter, ref remaining)) |
| | 11 | 139 | | return false; |
| | 158 | 140 | | if (Compare(_siftRoot, _siftCandidate) >= 0) |
| | | 141 | | { |
| | 29 | 142 | | _siftStage = SiftStage.None; |
| | 29 | 143 | | continue; |
| | | 144 | | } |
| | 129 | 145 | | Swap(_siftRoot, _siftCandidate); |
| | 129 | 146 | | _siftRoot = _siftCandidate; |
| | 129 | 147 | | _siftStage = SiftStage.ChooseChild; |
| | | 148 | | } |
| | 918 | 149 | | return true; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private void InitializeCollection() |
| | | 153 | | { |
| | 2757 | 154 | | if (IsComplete) |
| | 918 | 155 | | return; |
| | 1839 | 156 | | int count = _collection == Collection.Components |
| | 1839 | 157 | | ? _componentCount |
| | 1839 | 158 | | : _pageCount; |
| | 1839 | 159 | | _heapSize = count; |
| | 1839 | 160 | | _buildIndex = (count / 2) - 1; |
| | 1839 | 161 | | _sortEnd = count - 1; |
| | 1839 | 162 | | _building = true; |
| | 1839 | 163 | | _siftStage = SiftStage.None; |
| | 1839 | 164 | | } |
| | | 165 | | |
| | | 166 | | private void BeginSift(int root) |
| | | 167 | | { |
| | 242 | 168 | | _siftRoot = root; |
| | 242 | 169 | | _siftStage = SiftStage.ChooseChild; |
| | 242 | 170 | | } |
| | | 171 | | |
| | | 172 | | private int Compare(int left, int right) |
| | | 173 | | { |
| | 204 | 174 | | if (_collection == Collection.Components) |
| | | 175 | | { |
| | 72 | 176 | | return _components[left].CompareTo(_components[right]); |
| | | 177 | | } |
| | 132 | 178 | | GraphPageDependencyAddress leftPage = _pages[left]; |
| | 132 | 179 | | GraphPageDependencyAddress rightPage = _pages[right]; |
| | 132 | 180 | | int mapComparison = string.CompareOrdinal(leftPage.MapId, rightPage.MapId); |
| | 132 | 181 | | return mapComparison != 0 |
| | 132 | 182 | | ? mapComparison |
| | 132 | 183 | | : leftPage.PageIndex.CompareTo(rightPage.PageIndex); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private void Swap(int left, int right) |
| | | 187 | | { |
| | 258 | 188 | | if (_collection == Collection.Components) |
| | | 189 | | { |
| | 134 | 190 | | (_components[left], _components[right]) = |
| | 134 | 191 | | (_components[right], _components[left]); |
| | 134 | 192 | | return; |
| | | 193 | | } |
| | 124 | 194 | | (_pages[left], _pages[right]) = (_pages[right], _pages[left]); |
| | 124 | 195 | | } |
| | | 196 | | |
| | | 197 | | private static bool TryConsumeComparison( |
| | | 198 | | NavigationWorkMeter meter, |
| | | 199 | | ref int remaining) |
| | | 200 | | { |
| | 227 | 201 | | if (remaining == 0 || !meter.TryConsumeLookupProbes(1)) |
| | 23 | 202 | | return false; |
| | 204 | 203 | | remaining--; |
| | 204 | 204 | | return true; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | private static int GetCollectionMaximumComparisonCount(int count) |
| | | 208 | | { |
| | 236 | 209 | | int comparisons = 0; |
| | 580 | 210 | | for (int root = (count / 2) - 1; root >= 0; root--) |
| | 57 | 211 | | comparisons = checked(comparisons + GetSiftMaximumComparisonCount(root, count)); |
| | 584 | 212 | | for (int heapSize = count - 1; heapSize > 0; heapSize--) |
| | 59 | 213 | | comparisons = checked(comparisons + GetSiftMaximumComparisonCount(0, heapSize)); |
| | 233 | 214 | | return comparisons; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private static int GetSiftMaximumComparisonCount(int root, int heapSize) |
| | | 218 | | { |
| | 116 | 219 | | int comparisons = 0; |
| | 62 | 220 | | while (true) |
| | | 221 | | { |
| | 178 | 222 | | int left = checked((root * 2) + 1); |
| | 175 | 223 | | if (left >= heapSize) |
| | 113 | 224 | | return comparisons; |
| | 62 | 225 | | comparisons = checked(comparisons + 1); |
| | 62 | 226 | | if (left + 1 < heapSize) |
| | 8 | 227 | | comparisons = checked(comparisons + 1); |
| | 62 | 228 | | root = left; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | } |