| | | 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 | | /// Creating this struct is allocation-free. An enumerator is only allocated |
| | | 11 | | /// when iterated through the IEnumerable interface (e.g., when passed to ICostFunction). |
| | | 12 | | /// When iterated via foreach on the concrete type, the struct enumerator avoids boxing. |
| | | 13 | | /// </summary> |
| | | 14 | | internal readonly struct PreviousEdgeEnumerable : IEnumerable<(EdgeId edge, byte? turn)> |
| | | 15 | | { |
| | | 16 | | private readonly PathTree? _tree; |
| | | 17 | | private readonly uint _pointer; |
| | | 18 | | |
| | | 19 | | public PreviousEdgeEnumerable(PathTree tree, uint pointer) |
| | 542530 | 20 | | { |
| | 542530 | 21 | | _tree = tree; |
| | 542530 | 22 | | _pointer = pointer; |
| | 542530 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Returns true if there are no previous edges. |
| | | 27 | | /// </summary> |
| | 944060 | 28 | | public bool IsEmpty => _tree == null || _pointer == uint.MaxValue; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Returns a struct enumerator (no allocation when used via foreach on the concrete type). |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public Enumerator GetEnumerator() => new(_tree, _pointer); |
| | | 34 | | |
| | | 35 | | IEnumerator<(EdgeId edge, byte? turn)> IEnumerable<(EdgeId edge, byte? turn)>.GetEnumerator() => |
| | 542529 | 36 | | new Enumerator(_tree, _pointer); |
| | | 37 | | |
| | 0 | 38 | | IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_tree, _pointer); |
| | | 39 | | |
| | | 40 | | public struct Enumerator : IEnumerator<(EdgeId edge, byte? turn)> |
| | | 41 | | { |
| | | 42 | | private readonly PathTree? _tree; |
| | | 43 | | private uint _pointer; |
| | | 44 | | private (EdgeId edge, byte? turn) _current; |
| | | 45 | | |
| | | 46 | | internal Enumerator(PathTree? tree, uint pointer) |
| | 542529 | 47 | | { |
| | 542529 | 48 | | _tree = tree; |
| | 542529 | 49 | | _pointer = pointer; |
| | 542529 | 50 | | _current = default; |
| | 542529 | 51 | | } |
| | | 52 | | |
| | 542504 | 53 | | public (EdgeId edge, byte? turn) Current => _current; |
| | | 54 | | |
| | 0 | 55 | | object IEnumerator.Current => _current; |
| | | 56 | | |
| | | 57 | | public bool MoveNext() |
| | 542529 | 58 | | { |
| | 542554 | 59 | | if (_tree == null || _pointer == uint.MaxValue) return false; |
| | | 60 | | |
| | 542504 | 61 | | var (_, edge, _, head, next) = PathTreeExtensions.GetVisit(_tree, _pointer); |
| | 542504 | 62 | | _current = (edge, head); |
| | 542504 | 63 | | _pointer = next; |
| | 542504 | 64 | | return true; |
| | 542529 | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | public void Reset() { } |
| | | 68 | | |
| | 1085058 | 69 | | public void Dispose() { } |
| | | 70 | | } |
| | | 71 | | } |