| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace Itinero.Network.Search.Islands; |
| | | 5 | | |
| | | 6 | | internal class Islands |
| | | 7 | | { |
| | | 8 | | private readonly HashSet<uint> _tiles; // holds the tiles that have been processed. |
| | 43 | 9 | | private readonly ReaderWriterLockSlim _tilesLock = new(); |
| | | 10 | | private readonly HashSet<EdgeId> _islandEdges; |
| | 43 | 11 | | private readonly ReaderWriterLockSlim _islandEdgesLock = new(); |
| | | 12 | | private readonly HashSet<EdgeId> _localEdges; |
| | 43 | 13 | | private readonly ReaderWriterLockSlim _localEdgesLock = new(); |
| | | 14 | | |
| | | 15 | | // Transient state used during the NonLocal classification pass: each |
| | | 16 | | // edge the classifier identifies as Island in the N-only subgraph is |
| | | 17 | | // recorded here. Once both passes complete and the coordinator has |
| | | 18 | | // computed _localEdges from the gap (NonLocal-Island ∩ NotIsland-Full |
| | | 19 | | // ∩ non-L), this set can be cleared. Not part of the persistent |
| | | 20 | | // storage contract. |
| | | 21 | | private readonly HashSet<EdgeId> _nonLocalIslandEdges; |
| | 43 | 22 | | private readonly ReaderWriterLockSlim _nonLocalIslandEdgesLock = new(); |
| | | 23 | | |
| | 43 | 24 | | internal Islands() |
| | 43 | 25 | | { |
| | 43 | 26 | | _tiles = []; |
| | 43 | 27 | | _islandEdges = []; |
| | 43 | 28 | | _localEdges = []; |
| | 43 | 29 | | _nonLocalIslandEdges = []; |
| | 43 | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | private Islands(HashSet<uint> tiles, HashSet<EdgeId> islandEdges, HashSet<EdgeId> localEdges) |
| | 0 | 33 | | { |
| | 0 | 34 | | _tiles = tiles; |
| | 0 | 35 | | _islandEdges = islandEdges; |
| | 0 | 36 | | _localEdges = localEdges; |
| | 0 | 37 | | _nonLocalIslandEdges = []; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Sets the tile as done. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="tileId">Sets the tile as done.</param> |
| | | 44 | | /// <returns>True if the tile is done.</returns> |
| | | 45 | | public bool SetTileDone(uint tileId) |
| | 14 | 46 | | { |
| | | 47 | | try |
| | 14 | 48 | | { |
| | 14 | 49 | | _tilesLock.EnterWriteLock(); |
| | | 50 | | |
| | 14 | 51 | | return _tiles.Add(tileId); |
| | | 52 | | } |
| | | 53 | | finally |
| | 14 | 54 | | { |
| | 14 | 55 | | _tilesLock.ExitWriteLock(); |
| | 14 | 56 | | } |
| | 14 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Returns true if the given tile is done. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="tileId"></param> |
| | | 63 | | /// <returns></returns> |
| | | 64 | | public bool GetTileDone(uint tileId) |
| | 13404 | 65 | | { |
| | | 66 | | try |
| | 13404 | 67 | | { |
| | 13404 | 68 | | _tilesLock.EnterReadLock(); |
| | | 69 | | |
| | 13404 | 70 | | return _tiles.Contains(tileId); |
| | | 71 | | } |
| | | 72 | | finally |
| | 13404 | 73 | | { |
| | 13404 | 74 | | _tilesLock.ExitReadLock(); |
| | 13404 | 75 | | } |
| | 13404 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Returns true if the given edge is on an island. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="edge"></param> |
| | | 82 | | /// <returns></returns> |
| | | 83 | | public bool IsEdgeOnIsland(EdgeId edge) |
| | 79791 | 84 | | { |
| | | 85 | | try |
| | 79791 | 86 | | { |
| | 79791 | 87 | | _islandEdgesLock.EnterReadLock(); |
| | | 88 | | |
| | 79791 | 89 | | return _islandEdges.Contains(edge); |
| | | 90 | | } |
| | | 91 | | finally |
| | 79791 | 92 | | { |
| | 79791 | 93 | | _islandEdgesLock.ExitReadLock(); |
| | 79791 | 94 | | } |
| | 79791 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Marks the given edge as on an island. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="edge"></param> |
| | | 101 | | /// <returns></returns> |
| | | 102 | | public bool SetEdgeOnIsland(EdgeId edge) |
| | 25 | 103 | | { |
| | | 104 | | try |
| | 25 | 105 | | { |
| | 25 | 106 | | _islandEdgesLock.EnterWriteLock(); |
| | | 107 | | |
| | 25 | 108 | | return _islandEdges.Add(edge); |
| | | 109 | | } |
| | | 110 | | finally |
| | 25 | 111 | | { |
| | 25 | 112 | | _islandEdgesLock.ExitWriteLock(); |
| | 25 | 113 | | } |
| | 25 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Kind-aware overload. Reads from <c>_islandEdges</c> for Full and from |
| | | 118 | | /// the transient <c>_nonLocalIslandEdges</c> for NonLocal. The latter is |
| | | 119 | | /// only meaningful while a NonLocal classification pass is in progress |
| | | 120 | | /// or hasn't been collapsed into <c>_localEdges</c> yet. |
| | | 121 | | /// </summary> |
| | | 122 | | public bool IsEdgeOnIsland(EdgeId edge, IslandKind kind) |
| | 70470 | 123 | | { |
| | 70470 | 124 | | if (kind == IslandKind.NonLocal) |
| | 70466 | 125 | | { |
| | | 126 | | try |
| | 70466 | 127 | | { |
| | 70466 | 128 | | _nonLocalIslandEdgesLock.EnterReadLock(); |
| | | 129 | | |
| | 70466 | 130 | | return _nonLocalIslandEdges.Contains(edge); |
| | | 131 | | } |
| | | 132 | | finally |
| | 70466 | 133 | | { |
| | 70466 | 134 | | _nonLocalIslandEdgesLock.ExitReadLock(); |
| | 70466 | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 4 | 138 | | return this.IsEdgeOnIsland(edge); |
| | 70470 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Kind-aware overload. Writes to <c>_islandEdges</c> for Full and to the |
| | | 143 | | /// transient <c>_nonLocalIslandEdges</c> for NonLocal. |
| | | 144 | | /// </summary> |
| | | 145 | | public bool SetEdgeOnIsland(EdgeId edge, IslandKind kind) |
| | 33 | 146 | | { |
| | 33 | 147 | | if (kind == IslandKind.NonLocal) |
| | 11 | 148 | | { |
| | | 149 | | try |
| | 11 | 150 | | { |
| | 11 | 151 | | _nonLocalIslandEdgesLock.EnterWriteLock(); |
| | | 152 | | |
| | 11 | 153 | | return _nonLocalIslandEdges.Add(edge); |
| | | 154 | | } |
| | | 155 | | finally |
| | 11 | 156 | | { |
| | 11 | 157 | | _nonLocalIslandEdgesLock.ExitWriteLock(); |
| | 11 | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | 22 | 161 | | return this.SetEdgeOnIsland(edge); |
| | 33 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Discards the transient <c>_nonLocalIslandEdges</c> set used during |
| | | 166 | | /// NonLocal classification. Called by the build coordinator after |
| | | 167 | | /// <c>_localEdges</c> has been computed from the gap. After this, the |
| | | 168 | | /// only persistent state is <c>_islandEdges</c> and <c>_localEdges</c>. |
| | | 169 | | /// </summary> |
| | | 170 | | internal void ClearNonLocalIslandEdges() |
| | 10 | 171 | | { |
| | | 172 | | try |
| | 10 | 173 | | { |
| | 10 | 174 | | _nonLocalIslandEdgesLock.EnterWriteLock(); |
| | | 175 | | |
| | 10 | 176 | | _nonLocalIslandEdges.Clear(); |
| | 10 | 177 | | } |
| | | 178 | | finally |
| | 10 | 179 | | { |
| | 10 | 180 | | _nonLocalIslandEdgesLock.ExitWriteLock(); |
| | 10 | 181 | | } |
| | 10 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Returns true if the edge is reachable but only via local-access edges — |
| | | 186 | | /// i.e. it is not an island, but every route to it from the main network |
| | | 187 | | /// crosses at least one L-tagged edge first. A typical example is an |
| | | 188 | | /// N-tagged road inside a residential subdivision whose only connection |
| | | 189 | | /// to the main road network goes through an <c>access=destination</c> |
| | | 190 | | /// gate road. |
| | | 191 | | /// |
| | | 192 | | /// L-tagged edges themselves are NOT stored here — they are recognised |
| | | 193 | | /// by their tag, which is build-time information on <c>EdgeFactor</c>. |
| | | 194 | | /// </summary> |
| | | 195 | | public bool IsEdgeLocal(EdgeId edge) |
| | 77356 | 196 | | { |
| | | 197 | | try |
| | 77356 | 198 | | { |
| | 77356 | 199 | | _localEdgesLock.EnterReadLock(); |
| | | 200 | | |
| | 77356 | 201 | | return _localEdges.Contains(edge); |
| | | 202 | | } |
| | | 203 | | finally |
| | 77356 | 204 | | { |
| | 77356 | 205 | | _localEdgesLock.ExitReadLock(); |
| | 77356 | 206 | | } |
| | 77356 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Marks the given edge as reachable only via local-access edges. See |
| | | 211 | | /// <see cref="IsEdgeLocal"/> for what that means. |
| | | 212 | | /// </summary> |
| | | 213 | | public bool SetEdgeLocal(EdgeId edge) |
| | 7 | 214 | | { |
| | | 215 | | try |
| | 7 | 216 | | { |
| | 7 | 217 | | _localEdgesLock.EnterWriteLock(); |
| | | 218 | | |
| | 7 | 219 | | return _localEdges.Add(edge); |
| | | 220 | | } |
| | | 221 | | finally |
| | 7 | 222 | | { |
| | 7 | 223 | | _localEdgesLock.ExitWriteLock(); |
| | 7 | 224 | | } |
| | 7 | 225 | | } |
| | | 226 | | |
| | | 227 | | internal Islands Clone() |
| | 0 | 228 | | { |
| | | 229 | | try |
| | 0 | 230 | | { |
| | 0 | 231 | | _tilesLock.EnterWriteLock(); |
| | | 232 | | try |
| | 0 | 233 | | { |
| | 0 | 234 | | _islandEdgesLock.EnterWriteLock(); |
| | | 235 | | try |
| | 0 | 236 | | { |
| | 0 | 237 | | _localEdgesLock.EnterWriteLock(); |
| | | 238 | | |
| | 0 | 239 | | return new Islands([.. _tiles], [.. _islandEdges], [.. _localEdges]); |
| | | 240 | | } |
| | | 241 | | finally |
| | 0 | 242 | | { |
| | 0 | 243 | | _localEdgesLock.ExitWriteLock(); |
| | 0 | 244 | | } |
| | | 245 | | } |
| | | 246 | | finally |
| | 0 | 247 | | { |
| | 0 | 248 | | _islandEdgesLock.ExitWriteLock(); |
| | 0 | 249 | | } |
| | | 250 | | } |
| | | 251 | | finally |
| | 0 | 252 | | { |
| | 0 | 253 | | _tilesLock.ExitWriteLock(); |
| | 0 | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | } |