| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Itinero.Network; |
| | | 8 | | using Itinero.Routes.Paths; |
| | | 9 | | using Itinero.Routing.DataStructures; |
| | | 10 | | using Itinero.Snapping; |
| | | 11 | | |
| | | 12 | | [assembly: InternalsVisibleTo("Itinero.Tests")] |
| | | 13 | | [assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")] |
| | | 14 | | [assembly: InternalsVisibleTo("Itinero.Tests.Functional")] |
| | | 15 | | |
| | | 16 | | namespace Itinero.Routing.Flavours.Dijkstra.EdgeBased; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// An edge-based dijkstra implementation. |
| | | 20 | | /// </summary> |
| | | 21 | | internal class Dijkstra |
| | | 22 | | { |
| | 21 | 23 | | private readonly PathTree _tree = new(); |
| | 21 | 24 | | private readonly HashSet<(EdgeId edgeId, VertexId vertexId)> _visits = new(); |
| | 21 | 25 | | private readonly BinaryHeap<(uint pointer, EdgeId edgeId, VertexId vertexId)> _heap = new(); |
| | | 26 | | |
| | | 27 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, SnapPoint source, |
| | | 28 | | SnapPoint target, |
| | | 29 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 30 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 31 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 32 | | CancellationToken cancellationToken = default) |
| | 0 | 33 | | { |
| | 0 | 34 | | var paths = await this.RunAsync(network, (source, null), new[] { (target, (bool?)null) }, getDijkstraWeight, |
| | 0 | 35 | | settled, queued, cancellationToken); |
| | | 36 | | |
| | 0 | 37 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, |
| | | 41 | | (SnapPoint sp, bool? direction) source, |
| | | 42 | | (SnapPoint sp, bool? direction) target, |
| | | 43 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 44 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 45 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 46 | | CancellationToken cancellationToken = default) |
| | 15 | 47 | | { |
| | 15 | 48 | | var paths = await this.RunAsync(network, source, new[] { target }, getDijkstraWeight, settled, queued, cancellat |
| | | 49 | | |
| | 15 | 50 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| | 15 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, SnapPoint source, |
| | | 54 | | IReadOnlyList<SnapPoint> targets, |
| | | 55 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 56 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 57 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 58 | | CancellationToken cancellationToken = default) |
| | 11293 | 59 | | { |
| | 11293 | 60 | | var directedTargets = new (SnapPoint sp, bool? direction)[targets.Count]; |
| | 397210 | 61 | | for (var i = 0; i < targets.Count; i++) |
| | 187312 | 62 | | { |
| | 187312 | 63 | | directedTargets[i] = (targets[i], null); |
| | 187312 | 64 | | } |
| | | 65 | | |
| | 11293 | 66 | | return await this.RunAsync(network, (source, null), directedTargets, |
| | 11293 | 67 | | getDijkstraWeight, settled, queued, cancellationToken); |
| | 11293 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="network"></param> |
| | | 74 | | /// <param name="source"></param> |
| | | 75 | | /// <param name="targets"></param> |
| | | 76 | | /// <param name="getDijkstraWeight"></param> |
| | | 77 | | /// <param name="settled">This Callback is called for every edge for which the minimal cost is known. If this callba |
| | | 78 | | /// <param name="queued">This callback is called before an edge is loaded. Should not be used to influence route pla |
| | | 79 | | /// <returns></returns> |
| | | 80 | | /// <exception cref="Exception"></exception> |
| | | 81 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, |
| | | 82 | | (SnapPoint sp, bool? direction) source, |
| | | 83 | | IReadOnlyList<(SnapPoint sp, bool? direction)> targets, |
| | | 84 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 85 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 86 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 87 | | CancellationToken cancellationToken = default) |
| | 11310 | 88 | | { |
| | | 89 | | static double GetWorst((uint pointer, double cost)[] targets) |
| | 132375 | 90 | | { |
| | 132375 | 91 | | var worst = 0d; |
| | 766590 | 92 | | for (var i = 0; i < targets.Length; i++) |
| | 379004 | 93 | | { |
| | 379004 | 94 | | if (!(targets[i].cost > worst)) |
| | 154402 | 95 | | { |
| | 154402 | 96 | | continue; |
| | | 97 | | } |
| | | 98 | | |
| | 224602 | 99 | | worst = targets[i].cost; |
| | 224602 | 100 | | if (worst >= double.MaxValue) |
| | 128084 | 101 | | { |
| | 128084 | 102 | | break; |
| | | 103 | | } |
| | 96518 | 104 | | } |
| | | 105 | | |
| | 132375 | 106 | | return worst; |
| | 132375 | 107 | | } |
| | | 108 | | |
| | 11310 | 109 | | var enumerator = network.GetEdgeEnumerator(); |
| | | 110 | | |
| | 11310 | 111 | | _tree.Clear(); |
| | 11310 | 112 | | _visits.Clear(); |
| | 11310 | 113 | | _heap.Clear(); |
| | | 114 | | |
| | | 115 | | // add sources. |
| | 11310 | 116 | | var sourceForwardVisit = uint.MaxValue; |
| | 11310 | 117 | | if (source.Forward()) |
| | 11310 | 118 | | { |
| | | 119 | | // add forward. |
| | 11310 | 120 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| | 0 | 121 | | { |
| | 0 | 122 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 123 | | } |
| | | 124 | | |
| | 11310 | 125 | | var sourceCostForward = |
| | 11310 | 126 | | getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost; |
| | 11310 | 127 | | if (sourceCostForward > 0) |
| | 11310 | 128 | | { |
| | | 129 | | // can traverse edge in the forward direction. |
| | 11310 | 130 | | var sourceOffsetCostForward = sourceCostForward * (1 - source.sp.OffsetFactor()); |
| | 11310 | 131 | | sourceForwardVisit = |
| | 11310 | 132 | | _tree.AddVisit(enumerator, uint.MaxValue); |
| | 11310 | 133 | | _heap.Push((sourceForwardVisit, enumerator.EdgeId, enumerator.Head), sourceOffsetCostForward); |
| | 11310 | 134 | | } |
| | 11310 | 135 | | } |
| | | 136 | | |
| | 11310 | 137 | | var sourceBackwardVisit = uint.MaxValue; |
| | 11310 | 138 | | if (source.Backward()) |
| | 11305 | 139 | | { |
| | | 140 | | // add backward. |
| | 11305 | 141 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| | 0 | 142 | | { |
| | 0 | 143 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 144 | | } |
| | | 145 | | |
| | 11305 | 146 | | var sourceCostBackward = |
| | 11305 | 147 | | getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost; |
| | 11305 | 148 | | if (sourceCostBackward > 0) |
| | 8087 | 149 | | { |
| | | 150 | | // can traverse edge in the backward direction. |
| | 8087 | 151 | | var sourceOffsetCostBackward = sourceCostBackward * source.sp.OffsetFactor(); |
| | 8087 | 152 | | sourceBackwardVisit = |
| | 8087 | 153 | | _tree.AddVisit(enumerator, uint.MaxValue); |
| | 8087 | 154 | | _heap.Push((sourceBackwardVisit, enumerator.EdgeId, enumerator.Head), sourceOffsetCostBackward); |
| | 8087 | 155 | | } |
| | 11305 | 156 | | } |
| | | 157 | | |
| | | 158 | | // add targets. |
| | 11310 | 159 | | var bestTargets = new (uint pointer, double cost)[targets.Count]; |
| | 11310 | 160 | | var targetsPerVertex = new Dictionary<VertexId, List<int>>(); |
| | 397290 | 161 | | for (var t = 0; t < targets.Count; t++) |
| | 187335 | 162 | | { |
| | 187335 | 163 | | bestTargets[t] = (uint.MaxValue, double.MaxValue); |
| | 187335 | 164 | | var target = targets[t]; |
| | | 165 | | |
| | 187335 | 166 | | if (target.Forward()) |
| | 187332 | 167 | | { |
| | | 168 | | // add forward. |
| | 187332 | 169 | | if (!enumerator.MoveTo(target.sp.EdgeId, true)) |
| | 0 | 170 | | { |
| | 0 | 171 | | throw new Exception($"Edge in target {target} not found!"); |
| | | 172 | | } |
| | | 173 | | |
| | 187332 | 174 | | var targetCostForward = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)) |
| | 187332 | 175 | | .cost; |
| | 187332 | 176 | | if (targetCostForward > 0) |
| | 187332 | 177 | | { |
| | 187332 | 178 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| | 108951 | 179 | | { |
| | 108951 | 180 | | targetsAtVertex = new List<int>(); |
| | 108951 | 181 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| | 108951 | 182 | | } |
| | | 183 | | |
| | 187332 | 184 | | targetsAtVertex.Add(t); |
| | 187332 | 185 | | } |
| | 187332 | 186 | | } |
| | | 187 | | |
| | 187335 | 188 | | if (target.Backward()) |
| | 187333 | 189 | | { |
| | | 190 | | // add backward. |
| | 187333 | 191 | | if (!enumerator.MoveTo(target.sp.EdgeId, false)) |
| | 0 | 192 | | { |
| | 0 | 193 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 194 | | } |
| | | 195 | | |
| | 187333 | 196 | | var targetCostBackward = |
| | 187333 | 197 | | getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost; |
| | 187333 | 198 | | if (targetCostBackward > 0) |
| | 100215 | 199 | | { |
| | 100215 | 200 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| | 67461 | 201 | | { |
| | 67461 | 202 | | targetsAtVertex = new List<int>(); |
| | 67461 | 203 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| | 67461 | 204 | | } |
| | | 205 | | |
| | 100215 | 206 | | targetsAtVertex.Add(t); |
| | 100215 | 207 | | } |
| | 187333 | 208 | | } |
| | | 209 | | |
| | | 210 | | // consider paths 'within' a single edge. |
| | 187335 | 211 | | if (source.sp.EdgeId != target.sp.EdgeId) |
| | 177788 | 212 | | { |
| | 177788 | 213 | | continue; |
| | | 214 | | } |
| | | 215 | | |
| | 9547 | 216 | | if (source.sp.Offset == target.sp.Offset) |
| | 5176 | 217 | | { |
| | | 218 | | // source and target are identical. |
| | 5176 | 219 | | if (sourceForwardVisit != uint.MaxValue && |
| | 5176 | 220 | | target.Forward()) |
| | 5175 | 221 | | { |
| | 5175 | 222 | | bestTargets[t] = (sourceForwardVisit, 0); |
| | 5175 | 223 | | } |
| | 1 | 224 | | else if (sourceBackwardVisit != uint.MaxValue && |
| | 1 | 225 | | target.Backward()) |
| | 0 | 226 | | { |
| | 0 | 227 | | bestTargets[t] = (sourceForwardVisit, 0); |
| | 0 | 228 | | } |
| | 5176 | 229 | | } |
| | 4371 | 230 | | else if (source.sp.Offset < target.sp.Offset && |
| | 4371 | 231 | | source.Forward() && target.Forward()) |
| | 2622 | 232 | | { |
| | | 233 | | // the source is earlier in the direction of the edge |
| | | 234 | | // and the edge can be traversed in this direction. |
| | 2622 | 235 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| | 0 | 236 | | { |
| | 0 | 237 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 238 | | } |
| | | 239 | | |
| | 2622 | 240 | | var weight = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost * |
| | 2622 | 241 | | (target.sp.OffsetFactor() - source.sp.OffsetFactor()); |
| | 2622 | 242 | | bestTargets[t] = (sourceForwardVisit, weight); |
| | 2622 | 243 | | } |
| | 1749 | 244 | | else if (source.sp.Offset > target.sp.Offset && |
| | 1749 | 245 | | source.Backward() && target.Backward()) |
| | 1746 | 246 | | { |
| | | 247 | | // the source is earlier against the direction of the edge |
| | | 248 | | // and the edge can be traversed in this direction. |
| | 1746 | 249 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| | 0 | 250 | | { |
| | 0 | 251 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 252 | | } |
| | | 253 | | |
| | 1746 | 254 | | var weight = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost * |
| | 1746 | 255 | | (source.sp.OffsetFactor() - target.sp.OffsetFactor()); |
| | 1746 | 256 | | bestTargets[t] = (sourceBackwardVisit, weight); |
| | 1746 | 257 | | } |
| | 9547 | 258 | | } |
| | | 259 | | |
| | | 260 | | // update worst target cost. |
| | 11310 | 261 | | var worstTargetCost = GetWorst(bestTargets); |
| | | 262 | | |
| | | 263 | | // keep going until heap is empty. |
| | 310589 | 264 | | while (_heap.Count > 0) |
| | 303725 | 265 | | { |
| | 303725 | 266 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 267 | | |
| | | 268 | | // dequeue new visit. |
| | 303725 | 269 | | var currentEntry = _heap.Pop(out var currentCost); |
| | 391279 | 270 | | while (_visits.Contains((currentEntry.edgeId, currentEntry.vertexId))) |
| | 89511 | 271 | | { |
| | | 272 | | // visited before, skip. |
| | 89511 | 273 | | if (_heap.Count == 0) |
| | 1957 | 274 | | { |
| | 1957 | 275 | | currentEntry = (uint.MaxValue, default, default); |
| | 1957 | 276 | | break; |
| | | 277 | | } |
| | | 278 | | |
| | 87554 | 279 | | currentEntry = _heap.Pop(out currentCost); |
| | 87554 | 280 | | } |
| | | 281 | | |
| | 303725 | 282 | | var currentPointer = currentEntry.pointer; |
| | 303725 | 283 | | if (currentPointer == uint.MaxValue) |
| | 1957 | 284 | | { |
| | 1957 | 285 | | break; |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | // only call GetVisit after the visited check passes. |
| | 301768 | 289 | | var currentVisit = _tree.GetVisit(currentPointer); |
| | | 290 | | |
| | | 291 | | // log visit. |
| | 301768 | 292 | | if (currentVisit.previousPointer != uint.MaxValue) |
| | 282917 | 293 | | { |
| | 282917 | 294 | | _visits.Add((currentEntry.edgeId, currentEntry.vertexId)); |
| | 282917 | 295 | | } |
| | | 296 | | |
| | 301768 | 297 | | if (settled != null && await settled((currentEntry.edgeId, currentEntry.vertexId))) |
| | 50421 | 298 | | { |
| | | 299 | | // the best cost to this edge has already been found; current visit can not improve this anymore so we c |
| | 50421 | 300 | | continue; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | // check if the search needs to stop. |
| | 251347 | 304 | | if (currentCost > worstTargetCost) |
| | 2489 | 305 | | { |
| | | 306 | | // impossible to improve on cost to any target. |
| | 2489 | 307 | | break; |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | // check neighbours. |
| | 248858 | 311 | | if (!enumerator.MoveTo(currentVisit.vertex)) |
| | 0 | 312 | | { |
| | | 313 | | // no edges, move on! |
| | 0 | 314 | | continue; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | // check if this is a target. |
| | 248858 | 318 | | if (!targetsPerVertex.TryGetValue(currentVisit.vertex, out var targetsAtVertex)) |
| | 110948 | 319 | | { |
| | 110948 | 320 | | targetsAtVertex = null; |
| | 110948 | 321 | | } |
| | | 322 | | |
| | 1038793 | 323 | | while (enumerator.MoveNext()) |
| | 789935 | 324 | | { |
| | | 325 | | // filter out if u-turns or visits on the same edge. |
| | 789935 | 326 | | var neighbourEdge = enumerator.EdgeId; |
| | 789935 | 327 | | if (neighbourEdge == currentVisit.edge) |
| | 248858 | 328 | | { |
| | 248858 | 329 | | continue; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | // gets the cost of the current edge. |
| | 541077 | 333 | | var (neighbourCost, turnCost) = |
| | 541077 | 334 | | getDijkstraWeight(enumerator, new PreviousEdgeEnumerable(_tree, currentPointer)); |
| | 541077 | 335 | | if (neighbourCost is >= double.MaxValue or <= 0) |
| | 153341 | 336 | | { |
| | 153341 | 337 | | continue; |
| | | 338 | | } |
| | | 339 | | |
| | 387736 | 340 | | if (turnCost is >= double.MaxValue or < 0) |
| | 5 | 341 | | { |
| | 5 | 342 | | continue; |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | // if the vertex has targets, check if this edge is a match. |
| | 387731 | 346 | | var neighbourPointer = uint.MaxValue; |
| | 387731 | 347 | | if (targetsAtVertex != null) |
| | 219176 | 348 | | { |
| | | 349 | | // only consider targets when found for the 'from' vertex. |
| | | 350 | | // and when this in not a u-turn. |
| | 1625248 | 351 | | foreach (var t in targetsAtVertex) |
| | 483860 | 352 | | { |
| | 483860 | 353 | | var target = targets[t]; |
| | 483860 | 354 | | if (target.sp.EdgeId != neighbourEdge) |
| | 294651 | 355 | | { |
| | 294651 | 356 | | continue; |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | // check directions. |
| | 189209 | 360 | | if (enumerator.Forward && !target.Forward()) |
| | 0 | 361 | | { |
| | 0 | 362 | | continue; |
| | | 363 | | } |
| | | 364 | | |
| | 189209 | 365 | | if (!enumerator.Forward && !target.Backward()) |
| | 0 | 366 | | { |
| | 0 | 367 | | continue; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | // there is a target on this edge, calculate the cost. |
| | | 371 | | // calculate the cost from the 'from' vertex to the target. |
| | 189209 | 372 | | var targetCost = enumerator.Forward |
| | 189209 | 373 | | ? neighbourCost * target.sp.OffsetFactor() |
| | 189209 | 374 | | : neighbourCost * (1 - target.sp.OffsetFactor()); |
| | | 375 | | // this is the case where the target is on this edge |
| | | 376 | | // and there is a path to 'from' before. |
| | 189209 | 377 | | targetCost += currentCost; |
| | | 378 | | |
| | 189209 | 379 | | targetCost += turnCost; |
| | | 380 | | |
| | | 381 | | // if this is an improvement, use it! |
| | 189209 | 382 | | var targetBestCost = bestTargets[t].cost; |
| | 189209 | 383 | | if (!(targetCost < targetBestCost)) |
| | 68144 | 384 | | { |
| | 68144 | 385 | | continue; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | // this is an improvement. |
| | 121065 | 389 | | neighbourPointer = _tree.AddVisit(enumerator, currentPointer); |
| | 121065 | 390 | | bestTargets[t] = (neighbourPointer, targetCost); |
| | | 391 | | |
| | | 392 | | // update worst. |
| | 121065 | 393 | | worstTargetCost = GetWorst(bestTargets); |
| | 121065 | 394 | | } |
| | 219176 | 395 | | } |
| | | 396 | | |
| | 387731 | 397 | | if (queued != null && |
| | 387731 | 398 | | await queued((enumerator.EdgeId, enumerator.Head))) |
| | 0 | 399 | | { |
| | | 400 | | // don't queue this edge if the queued function returns true. |
| | 0 | 401 | | continue; |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | // add visit if not added yet. |
| | 387731 | 405 | | if (neighbourPointer == uint.MaxValue) |
| | 266669 | 406 | | { |
| | 266669 | 407 | | neighbourPointer = |
| | 266669 | 408 | | _tree.AddVisit(enumerator, currentPointer); |
| | 266669 | 409 | | } |
| | | 410 | | |
| | | 411 | | // add visit to heap. |
| | 387731 | 412 | | _heap.Push((neighbourPointer, enumerator.EdgeId, enumerator.Head), neighbourCost + currentCost + turnCos |
| | 387731 | 413 | | } |
| | 248858 | 414 | | } |
| | | 415 | | |
| | 11310 | 416 | | var paths = new (Path? path, double cost)[targets.Count]; |
| | 397290 | 417 | | for (var p = 0; p < paths.Length; p++) |
| | 187335 | 418 | | { |
| | 187335 | 419 | | var bestTarget = bestTargets[p]; |
| | 187335 | 420 | | if (bestTarget.pointer == uint.MaxValue) |
| | 58477 | 421 | | { |
| | 58477 | 422 | | paths[p] = (null, double.MaxValue); |
| | 58477 | 423 | | continue; |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | // build resulting path. |
| | 128858 | 427 | | var path = new Path(network); |
| | 128858 | 428 | | var visit = _tree.GetVisit(bestTarget.pointer); |
| | | 429 | | |
| | | 430 | | // path is at least two edges. |
| | 705558 | 431 | | while (true) |
| | 705558 | 432 | | { |
| | 705558 | 433 | | if (visit.previousPointer == uint.MaxValue) |
| | 128858 | 434 | | { |
| | 128858 | 435 | | enumerator.MoveTo(visit.edge); |
| | 128858 | 436 | | path.Prepend(visit.edge, visit.forward); |
| | 128858 | 437 | | break; |
| | | 438 | | } |
| | | 439 | | |
| | 576700 | 440 | | path.Prepend(visit.edge, visit.forward); |
| | 576700 | 441 | | visit = _tree.GetVisit(visit.previousPointer); |
| | 576700 | 442 | | } |
| | | 443 | | |
| | | 444 | | // add the offsets. |
| | 128858 | 445 | | var target = targets[p]; |
| | 128858 | 446 | | path.Offset1 = path.First.direction ? source.sp.Offset : (ushort)(ushort.MaxValue - source.sp.Offset); |
| | 128858 | 447 | | path.Offset2 = path.Last.direction |
| | 128858 | 448 | | ? target.sp.Offset |
| | 128858 | 449 | | : (ushort)(ushort.MaxValue - target.sp.Offset); |
| | | 450 | | |
| | 128858 | 451 | | paths[p] = (path, bestTarget.cost); |
| | 128858 | 452 | | } |
| | | 453 | | |
| | 11310 | 454 | | return paths; |
| | 11310 | 455 | | } |
| | | 456 | | |
| | | 457 | | [ThreadStatic] |
| | | 458 | | private static Dijkstra? _default; |
| | | 459 | | |
| | | 460 | | /// <summary> |
| | | 461 | | /// Gets a default dijkstra instance (reused per thread). |
| | | 462 | | /// </summary> |
| | 11310 | 463 | | public static Dijkstra Default => _default ??= new Dijkstra(); |
| | | 464 | | } |