| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Itinero.Network; |
| | | 4 | | using Itinero.Routing.DataStructures; |
| | | 5 | | |
| | | 6 | | namespace Itinero.Routing.Flavours.Dijkstra; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A lazy, struct-based enumerable over previous edges in a path tree. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Creating this is allocation-free and <c>foreach</c> on the concrete type uses the struct |
| | | 13 | | /// enumerator. Reaching it through <see cref="IEnumerable{T}"/> is not free, and that was the |
| | | 14 | | /// trap: <c>ICostFunction.Get</c> declared the parameter as the interface, so every call boxed |
| | | 15 | | /// the struct, and every enumeration boxed the enumerator too. In the allocation profile those |
| | | 16 | | /// were 10.4% and 12.9% — **23% of everything the service allocated**, for a sequence whose |
| | | 17 | | /// first element is all anyone reads. |
| | | 18 | | /// |
| | | 19 | | /// So the cost function takes this concrete type now. Public for that reason, with an internal |
| | | 20 | | /// constructor: implementations outside Itinero receive one and pass it on, but only the search |
| | | 21 | | /// can create one. |
| | | 22 | | /// </remarks> |
| | | 23 | | public readonly struct PreviousEdgeEnumerable : IEnumerable<(EdgeId edge, byte? turn)> |
| | | 24 | | { |
| | | 25 | | private readonly PathTree? _tree; |
| | | 26 | | private readonly uint _pointer; |
| | | 27 | | private readonly EdgeId _singleEdge; |
| | | 28 | | private readonly byte? _singleTurn; |
| | | 29 | | private readonly bool _hasSingle; |
| | | 30 | | |
| | | 31 | | internal PreviousEdgeEnumerable(PathTree tree, uint pointer) |
| | 561868 | 32 | | { |
| | 561868 | 33 | | _tree = tree; |
| | 561868 | 34 | | _pointer = pointer; |
| | 561868 | 35 | | } |
| | | 36 | | |
| | | 37 | | private PreviousEdgeEnumerable(EdgeId edge, byte? turn) |
| | 28 | 38 | | { |
| | 28 | 39 | | _pointer = uint.MaxValue; |
| | 28 | 40 | | _singleEdge = edge; |
| | 28 | 41 | | _singleTurn = turn; |
| | 28 | 42 | | _hasSingle = true; |
| | 28 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// A sequence of exactly one previous edge. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <remarks> |
| | | 49 | | /// The callers that need this were writing <c>new[] { (edge, order) }</c> — an array |
| | | 50 | | /// allocated per call to carry a single element through an interface that then boxed it. |
| | | 51 | | /// Held inline here instead, so the whole thing stays on the stack. |
| | | 52 | | /// </remarks> |
| | 28 | 53 | | public static PreviousEdgeEnumerable ForEdge(EdgeId edge, byte? turn) => new(edge, turn); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Returns true if there are no previous edges. |
| | | 57 | | /// </summary> |
| | 964222 | 58 | | public bool IsEmpty => !_hasSingle && (_tree == null || _pointer == uint.MaxValue); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns a struct enumerator (no allocation when used via foreach on the concrete type). |
| | | 62 | | /// </summary> |
| | 1112227 | 63 | | public Enumerator GetEnumerator() => new(_tree, _pointer, _singleEdge, _singleTurn, _hasSingle); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The most recent previous edge, or null when there is none. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <remarks> |
| | | 69 | | /// Every cost function today wants exactly this and used to get it with |
| | | 70 | | /// <c>previousEdges.FirstOrDefault()</c> — a LINQ call through the interface, which is what |
| | | 71 | | /// boxed the enumerator. The full sequence stays available for the prefix comparison the |
| | | 72 | | /// turn-cost code has a TODO for; this is just the part anyone actually reads. |
| | | 73 | | /// </remarks> |
| | | 74 | | public (EdgeId edge, byte? turn)? First |
| | | 75 | | { |
| | | 76 | | get |
| | 1112227 | 77 | | { |
| | 1112227 | 78 | | var enumerator = this.GetEnumerator(); |
| | | 79 | | |
| | 1112227 | 80 | | return enumerator.MoveNext() ? enumerator.Current : null; |
| | 1112227 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | IEnumerator<(EdgeId edge, byte? turn)> IEnumerable<(EdgeId edge, byte? turn)>.GetEnumerator() => |
| | 21 | 85 | | new Enumerator(_tree, _pointer, _singleEdge, _singleTurn, _hasSingle); |
| | | 86 | | |
| | | 87 | | IEnumerator IEnumerable.GetEnumerator() => |
| | 0 | 88 | | new Enumerator(_tree, _pointer, _singleEdge, _singleTurn, _hasSingle); |
| | | 89 | | |
| | | 90 | | public struct Enumerator : IEnumerator<(EdgeId edge, byte? turn)> |
| | | 91 | | { |
| | | 92 | | private readonly PathTree? _tree; |
| | | 93 | | private uint _pointer; |
| | | 94 | | private (EdgeId edge, byte? turn) _current; |
| | | 95 | | private bool _single; |
| | | 96 | | |
| | | 97 | | internal Enumerator(PathTree? tree, uint pointer, EdgeId singleEdge, byte? singleTurn, |
| | | 98 | | bool hasSingle) |
| | 1112248 | 99 | | { |
| | 1112248 | 100 | | _tree = tree; |
| | 1112248 | 101 | | _pointer = pointer; |
| | 1112248 | 102 | | _current = hasSingle ? (singleEdge, singleTurn) : default; |
| | 1112248 | 103 | | _single = hasSingle; |
| | 1112248 | 104 | | } |
| | | 105 | | |
| | 561749 | 106 | | public (EdgeId edge, byte? turn) Current => _current; |
| | | 107 | | |
| | 0 | 108 | | object IEnumerator.Current => _current; |
| | | 109 | | |
| | | 110 | | public bool MoveNext() |
| | 1112248 | 111 | | { |
| | 1112248 | 112 | | if (_single) |
| | 28 | 113 | | { |
| | | 114 | | // _current already holds it; yield once and stop. |
| | 28 | 115 | | _single = false; |
| | 28 | 116 | | return true; |
| | | 117 | | } |
| | | 118 | | |
| | 1662719 | 119 | | if (_tree == null || _pointer == uint.MaxValue) return false; |
| | | 120 | | |
| | 561721 | 121 | | var (_, edge, _, head, next) = PathTreeExtensions.GetVisit(_tree, _pointer); |
| | 561721 | 122 | | _current = (edge, head); |
| | 561721 | 123 | | _pointer = next; |
| | 561721 | 124 | | return true; |
| | 1112248 | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | public void Reset() { } |
| | | 128 | | |
| | 42 | 129 | | public void Dispose() { } |
| | | 130 | | } |
| | | 131 | | } |