| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Itinero.Profiles; |
| | | 7 | | |
| | | 8 | | namespace Itinero.Network.Search.Islands; |
| | | 9 | | |
| | | 10 | | internal class RoutingNetworkIslandManager |
| | | 11 | | { |
| | 592 | 12 | | private readonly Dictionary<(string profile, uint tile), Task> _tilesInProgress = new(); |
| | 592 | 13 | | private readonly ReaderWriterLockSlim _tilesInProgressLock = new(); |
| | | 14 | | private readonly Dictionary<string, Islands> _islands; |
| | 592 | 15 | | private readonly Dictionary<(string profile, IslandKind kind), IslandDirectedGraph> _directedGraphs = new(); |
| | 592 | 16 | | private readonly ReaderWriterLockSlim _islandsLock = new(); |
| | | 17 | | |
| | 343 | 18 | | internal RoutingNetworkIslandManager(int maxIslandSize) |
| | 343 | 19 | | { |
| | 343 | 20 | | this.MaxIslandSize = maxIslandSize; |
| | 343 | 21 | | _islands = new(); |
| | 343 | 22 | | } |
| | | 23 | | |
| | 249 | 24 | | private RoutingNetworkIslandManager(int maxIslandSize, Dictionary<string, Islands> islands) |
| | 249 | 25 | | { |
| | 249 | 26 | | this.MaxIslandSize = maxIslandSize; |
| | 249 | 27 | | _islands = islands; |
| | 249 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Checks if an edge is on an island using the directed graph. |
| | | 32 | | /// Returns true if island, false if not island, null if inconclusive. |
| | | 33 | | /// </summary> |
| | | 34 | | internal bool? IsEdgeOnIsland(Profile profile, EdgeId edgeId) => |
| | 7 | 35 | | this.IsEdgeOnIsland(profile.Name, edgeId); |
| | | 36 | | |
| | | 37 | | internal bool? IsEdgeOnIsland(string profileName, EdgeId edgeId) |
| | 7 | 38 | | { |
| | | 39 | | try |
| | 7 | 40 | | { |
| | 7 | 41 | | _islandsLock.EnterReadLock(); |
| | | 42 | | |
| | | 43 | | // Snapping is a Full-classification concern, so the existing |
| | | 44 | | // single-DG semantics route through the Full DG. |
| | 7 | 45 | | if (!_directedGraphs.TryGetValue((profileName, IslandKind.Full), out var dg)) |
| | 5 | 46 | | return null; |
| | | 47 | | |
| | 2 | 48 | | if (!_islands.TryGetValue(profileName, out var profileIslands)) |
| | 0 | 49 | | return null; |
| | 2 | 50 | | if (profileIslands.IsEdgeOnIsland(edgeId)) |
| | 0 | 51 | | return true; |
| | | 52 | | |
| | 2 | 53 | | if (dg.IsNotIsland(edgeId)) |
| | 0 | 54 | | return false; |
| | | 55 | | |
| | 2 | 56 | | return null; |
| | | 57 | | } |
| | | 58 | | finally |
| | 7 | 59 | | { |
| | 7 | 60 | | _islandsLock.ExitReadLock(); |
| | 7 | 61 | | } |
| | 7 | 62 | | } |
| | | 63 | | |
| | | 64 | | internal IslandDirectedGraph GetOrCreateDirectedGraph(Profile profile, IslandKind kind = IslandKind.Full) |
| | 3238 | 65 | | { |
| | 3238 | 66 | | var key = (profile.Name, kind); |
| | | 67 | | try |
| | 3238 | 68 | | { |
| | 3238 | 69 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 70 | | |
| | 6399 | 71 | | if (_directedGraphs.TryGetValue(key, out var dg)) return dg; |
| | | 72 | | |
| | | 73 | | try |
| | 77 | 74 | | { |
| | 77 | 75 | | _islandsLock.EnterWriteLock(); |
| | | 76 | | |
| | 77 | 77 | | dg = new IslandDirectedGraph(); |
| | 77 | 78 | | _directedGraphs[key] = dg; |
| | 77 | 79 | | return dg; |
| | | 80 | | } |
| | | 81 | | finally |
| | 77 | 82 | | { |
| | 77 | 83 | | _islandsLock.ExitWriteLock(); |
| | 77 | 84 | | } |
| | | 85 | | } |
| | | 86 | | finally |
| | 3238 | 87 | | { |
| | 3238 | 88 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 3238 | 89 | | } |
| | 3238 | 90 | | } |
| | | 91 | | |
| | 2566 | 92 | | internal int MaxIslandSize { get; } |
| | | 93 | | |
| | | 94 | | internal bool TryGetIslandsFor(string profileName, out Islands islands) |
| | 0 | 95 | | { |
| | | 96 | | try |
| | 0 | 97 | | { |
| | 0 | 98 | | _islandsLock.EnterReadLock(); |
| | | 99 | | |
| | 0 | 100 | | return _islands.TryGetValue(profileName, out islands); |
| | | 101 | | } |
| | | 102 | | finally |
| | 0 | 103 | | { |
| | 0 | 104 | | _islandsLock.ExitReadLock(); |
| | 0 | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal Islands GetIslandsFor(Profile profile) |
| | 2176 | 109 | | { |
| | | 110 | | try |
| | 2176 | 111 | | { |
| | 2176 | 112 | | _islandsLock.EnterUpgradeableReadLock(); |
| | | 113 | | |
| | 4309 | 114 | | if (_islands.TryGetValue(profile.Name, out var islands)) return islands; |
| | | 115 | | |
| | | 116 | | try |
| | 43 | 117 | | { |
| | 43 | 118 | | _islandsLock.EnterWriteLock(); |
| | | 119 | | |
| | 43 | 120 | | islands = new Islands(); |
| | 43 | 121 | | _islands[profile.Name] = islands; |
| | 43 | 122 | | return islands; |
| | | 123 | | } |
| | | 124 | | finally |
| | 43 | 125 | | { |
| | 43 | 126 | | _islandsLock.ExitWriteLock(); |
| | 43 | 127 | | } |
| | | 128 | | } |
| | | 129 | | finally |
| | 2176 | 130 | | { |
| | 2176 | 131 | | _islandsLock.ExitUpgradeableReadLock(); |
| | 2176 | 132 | | } |
| | 2176 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Returns whether the edge is in the profile's main-N component — the |
| | | 137 | | /// dominant SCC of the N-only subgraph, i.e. the "mainland" without |
| | | 138 | | /// L-edges. |
| | | 139 | | /// |
| | | 140 | | /// <list type="bullet"> |
| | | 141 | | /// <item><c>true</c>: edge is in main-N. Default for any edge in a done tile |
| | | 142 | | /// that is neither L-tagged, on an island, nor a non-main-N pocket member.</item> |
| | | 143 | | /// <item><c>false</c>: edge is not in main-N. Either L-tagged (passed in via |
| | | 144 | | /// <paramref name="isLocalAccess"/>), on an island (unreachable in Full), or |
| | | 145 | | /// recorded as a local edge (non-main-N pocket).</item> |
| | | 146 | | /// <item><c>null</c>: classification has not yet produced a verdict for this |
| | | 147 | | /// tile.</item> |
| | | 148 | | /// </list> |
| | | 149 | | /// |
| | | 150 | | /// The L-tag check is tag-driven and resolved by the caller (typically via |
| | | 151 | | /// the cost function's <c>localAccess</c> field on the result of <c>Get</c>), |
| | | 152 | | /// then passed in. The manager itself does not consult any tag storage. |
| | | 153 | | /// </summary> |
| | | 154 | | internal bool? IsMainN(Profile profile, EdgeId edgeId, bool isLocalAccess) |
| | 788377 | 155 | | { |
| | | 156 | | // L-tagged edge — never main-N, no storage lookup needed. |
| | 788379 | 157 | | if (isLocalAccess) return false; |
| | | 158 | | |
| | | 159 | | try |
| | 788375 | 160 | | { |
| | 788375 | 161 | | _islandsLock.EnterReadLock(); |
| | | 162 | | |
| | 1568830 | 163 | | if (!_islands.TryGetValue(profile.Name, out var islands)) return null; |
| | | 164 | | |
| | | 165 | | // Unreachable in the Full classification → not in main-N. |
| | 7921 | 166 | | if (islands.IsEdgeOnIsland(edgeId)) return false; |
| | | 167 | | |
| | | 168 | | // Non-main-N pocket → not in main-N. |
| | 7920 | 169 | | if (islands.IsEdgeLocal(edgeId)) return false; |
| | | 170 | | |
| | | 171 | | // Tile finished classifying and the edge is in neither set → main-N. |
| | | 172 | | // Otherwise we don't yet know. |
| | 7918 | 173 | | return islands.GetTileDone(edgeId.TileId) ? true : null; |
| | | 174 | | } |
| | | 175 | | finally |
| | 788375 | 176 | | { |
| | 788375 | 177 | | _islandsLock.ExitReadLock(); |
| | 788375 | 178 | | } |
| | 788377 | 179 | | } |
| | | 180 | | |
| | | 181 | | internal async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | | 182 | | CancellationToken cancellationToken) |
| | 7 | 183 | | { |
| | | 184 | | // queue task, if not done yet. |
| | | 185 | | Task task; |
| | | 186 | | try |
| | 7 | 187 | | { |
| | 7 | 188 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 189 | | |
| | 7 | 190 | | if (!_tilesInProgress.TryGetValue((profile.Name, tileId), out task)) |
| | 7 | 191 | | { |
| | | 192 | | try |
| | 7 | 193 | | { |
| | 7 | 194 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 195 | | |
| | 7 | 196 | | task = IslandClassifier.BuildForTileAsync(network, profile, tileId, cancellationToken); |
| | 7 | 197 | | _tilesInProgress[(profile.Name, tileId)] = task; |
| | 7 | 198 | | } |
| | | 199 | | finally |
| | 7 | 200 | | { |
| | 7 | 201 | | _tilesInProgressLock.ExitWriteLock(); |
| | 7 | 202 | | } |
| | 7 | 203 | | } |
| | 7 | 204 | | } |
| | | 205 | | finally |
| | 7 | 206 | | { |
| | 7 | 207 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 7 | 208 | | } |
| | | 209 | | |
| | | 210 | | // await the task. |
| | 7 | 211 | | await task; |
| | | 212 | | |
| | | 213 | | // remove from the queue. |
| | | 214 | | try |
| | 7 | 215 | | { |
| | 7 | 216 | | _tilesInProgressLock.EnterUpgradeableReadLock(); |
| | | 217 | | |
| | 7 | 218 | | if (_tilesInProgress.ContainsKey((profile.Name, tileId))) |
| | 7 | 219 | | { |
| | | 220 | | try |
| | 7 | 221 | | { |
| | 7 | 222 | | _tilesInProgressLock.EnterWriteLock(); |
| | | 223 | | |
| | 7 | 224 | | _tilesInProgress.Remove((profile.Name, tileId)); |
| | 7 | 225 | | } |
| | | 226 | | finally |
| | 7 | 227 | | { |
| | 7 | 228 | | _tilesInProgressLock.ExitWriteLock(); |
| | 7 | 229 | | } |
| | 7 | 230 | | } |
| | 7 | 231 | | } |
| | | 232 | | finally |
| | 7 | 233 | | { |
| | 7 | 234 | | _tilesInProgressLock.ExitUpgradeableReadLock(); |
| | 7 | 235 | | } |
| | 7 | 236 | | } |
| | | 237 | | |
| | | 238 | | internal RoutingNetworkIslandManager Clone() |
| | 249 | 239 | | { |
| | | 240 | | try |
| | 249 | 241 | | { |
| | 249 | 242 | | _islandsLock.EnterReadLock(); |
| | | 243 | | |
| | 249 | 244 | | var islands = new Dictionary<string, Islands>(); |
| | 747 | 245 | | foreach (var (profileName, profileIslands) in _islands) |
| | 0 | 246 | | { |
| | 0 | 247 | | islands[profileName] = profileIslands.Clone(); |
| | 0 | 248 | | } |
| | | 249 | | |
| | 249 | 250 | | return new RoutingNetworkIslandManager(this.MaxIslandSize, islands); |
| | | 251 | | } |
| | | 252 | | finally |
| | 249 | 253 | | { |
| | 249 | 254 | | _islandsLock.ExitReadLock(); |
| | 249 | 255 | | } |
| | 249 | 256 | | } |
| | | 257 | | } |