| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using Itinero.Network; |
| | | 3 | | using Itinero.Network.Enumerators.Edges; |
| | | 4 | | using Itinero.Routing.DataStructures; |
| | | 5 | | |
| | | 6 | | namespace Itinero.Routing.Flavours.Dijkstra; |
| | | 7 | | |
| | | 8 | | internal static class PathTreeExtensions |
| | | 9 | | { |
| | | 10 | | // TODO: this can all be represented much more compact. |
| | | 11 | | |
| | | 12 | | // data6 bit layout: |
| | | 13 | | // bit 0 = forward |
| | | 14 | | // bit 1 = leftMain (state-bit for access-aware edge-based Dijkstra) |
| | | 15 | | // bit 2 = localAccess |
| | | 16 | | // Backward-compat: callers that don't track state-bits pack only bit 0, |
| | | 17 | | // so the existing GetVisit forward read continues to work via the mask. |
| | | 18 | | private const uint ForwardBit = 1U; |
| | | 19 | | private const uint LeftMainBit = 2U; |
| | | 20 | | private const uint LocalAccessBit = 4U; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Adds a new visit the path tree. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="tree">The tree.</param> |
| | | 26 | | /// <param name="vertex">The vertex.</param> |
| | | 27 | | /// <param name="edge">The edge.</param> |
| | | 28 | | /// <param name="forward">The edge direction.</param> |
| | | 29 | | /// <param name="head">The head index.</param> |
| | | 30 | | /// <param name="previousPointer">The pointer to the previous entry.</param> |
| | | 31 | | /// <returns>A pointer to the visit.</returns> |
| | | 32 | | public static uint AddVisit(this PathTree tree, VertexId vertex, EdgeId edge, bool forward, byte? head, |
| | | 33 | | uint previousPointer) |
| | 0 | 34 | | => AddVisit(tree, vertex, edge, forward, leftMain: false, localAccess: false, head, previousPointer); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Access-aware overload. Packs the <paramref name="leftMain"/> and |
| | | 38 | | /// <paramref name="localAccess"/> state-bits alongside the direction so the |
| | | 39 | | /// edge-based Dijkstra can recover them O(1) on pop. |
| | | 40 | | /// </summary> |
| | | 41 | | public static uint AddVisit(this PathTree tree, VertexId vertex, EdgeId edge, bool forward, |
| | | 42 | | bool leftMain, bool localAccess, byte? head, uint previousPointer) |
| | 414415 | 43 | | { |
| | 414415 | 44 | | var data0 = vertex.TileId; |
| | 414415 | 45 | | var data1 = vertex.LocalId; |
| | 414415 | 46 | | var data2 = edge.TileId; |
| | 414415 | 47 | | var data3 = edge.LocalId; |
| | 414415 | 48 | | var data4 = head ?? uint.MaxValue; |
| | 414415 | 49 | | var data5 = previousPointer; |
| | 414415 | 50 | | var data6 = (forward ? ForwardBit : 0U) |
| | 414415 | 51 | | | (leftMain ? LeftMainBit : 0U) |
| | 414415 | 52 | | | (localAccess ? LocalAccessBit : 0U); |
| | | 53 | | |
| | 414415 | 54 | | return tree.Add(data0, data1, data2, data3, data4, data5, data6); |
| | 414415 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Adds a new visit to the path tree. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="tree">The tree.</param> |
| | | 61 | | /// <param name="enumerator">The current edge.</param> |
| | | 62 | | /// <param name="previousPointer">The pointer to the previous entry.</param> |
| | | 63 | | /// <returns>A pointer to the visit.</returns> |
| | | 64 | | public static uint AddVisit(this PathTree tree, RoutingNetworkEdgeEnumerator enumerator, uint previousPointer) |
| | 0 | 65 | | { |
| | 0 | 66 | | return tree.AddVisit(enumerator.Head, enumerator.EdgeId, enumerator.Forward, enumerator.HeadOrder, |
| | 0 | 67 | | previousPointer); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Access-aware overload of <see cref="AddVisit(PathTree, RoutingNetworkEdgeEnumerator, uint)"/>. |
| | | 72 | | /// </summary> |
| | | 73 | | public static uint AddVisit(this PathTree tree, RoutingNetworkEdgeEnumerator enumerator, |
| | | 74 | | bool leftMain, bool localAccess, uint previousPointer) |
| | 414415 | 75 | | { |
| | 414415 | 76 | | return tree.AddVisit(enumerator.Head, enumerator.EdgeId, enumerator.Forward, |
| | 414415 | 77 | | leftMain, localAccess, enumerator.HeadOrder, previousPointer); |
| | 414415 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets the visit at the given location. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="tree">The tree.</param> |
| | | 84 | | /// <param name="pointer">The pointer.</param> |
| | | 85 | | /// <returns>The visit.</returns> |
| | | 86 | | public static (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) GetVisit(this PathTree |
| | | 87 | | uint pointer) |
| | 1260184 | 88 | | { |
| | 1260184 | 89 | | tree.Get(pointer, out var data0, out var data1, out var data2, out var data3, out var data4, out var data5, out |
| | | 90 | | |
| | 1260184 | 91 | | var head = data4 == uint.MaxValue ? null : (byte?)data4; |
| | | 92 | | |
| | 1260184 | 93 | | return (new VertexId(data0, data1), new EdgeId(data2, data3), (data6 & ForwardBit) != 0, head, data5); |
| | 1260184 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Access-aware overload of <see cref="GetVisit"/> that also returns the |
| | | 98 | | /// packed <c>leftMain</c> and <c>localAccess</c> state-bits. |
| | | 99 | | /// </summary> |
| | | 100 | | public static (VertexId vertex, EdgeId edge, bool forward, bool leftMain, bool localAccess, byte? head, uint previou |
| | | 101 | | GetVisitWithState(this PathTree tree, uint pointer) |
| | 305908 | 102 | | { |
| | 305908 | 103 | | tree.Get(pointer, out var data0, out var data1, out var data2, out var data3, out var data4, out var data5, out |
| | | 104 | | |
| | 305908 | 105 | | var head = data4 == uint.MaxValue ? null : (byte?)data4; |
| | | 106 | | |
| | 305908 | 107 | | return (new VertexId(data0, data1), new EdgeId(data2, data3), |
| | 305908 | 108 | | (data6 & ForwardBit) != 0, |
| | 305908 | 109 | | (data6 & LeftMainBit) != 0, |
| | 305908 | 110 | | (data6 & LocalAccessBit) != 0, |
| | 305908 | 111 | | head, data5); |
| | 305908 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Enumerates the edges backwards from the visit at the given pointer. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="tree">The tree.</param> |
| | | 118 | | /// <param name="pointer">The pointer.</param> |
| | | 119 | | /// <returns>The edges and their turn.</returns> |
| | | 120 | | public static IEnumerable<(EdgeId edge, byte? turn)> GetPreviousEdges(this PathTree tree, uint pointer) |
| | 0 | 121 | | { |
| | 0 | 122 | | while (pointer != uint.MaxValue) |
| | 0 | 123 | | { |
| | 0 | 124 | | var (_, edge, _, head, next) = tree.GetVisit(pointer); |
| | | 125 | | |
| | 0 | 126 | | yield return (edge, head); |
| | | 127 | | |
| | 0 | 128 | | pointer = next; |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | #if DEBUG |
| | | 133 | | public static IEnumerable<(EdgeId edge, bool forward, byte? turn)> GetPathDebug(this PathTree tree, uint pointer) |
| | 0 | 134 | | { |
| | 0 | 135 | | while (pointer != uint.MaxValue) |
| | 0 | 136 | | { |
| | 0 | 137 | | var (_, edge, forward, head, next) = tree.GetVisit(pointer); |
| | | 138 | | |
| | 0 | 139 | | yield return (edge, forward, head); |
| | | 140 | | |
| | 0 | 141 | | pointer = next; |
| | 0 | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | #endif |
| | | 145 | | } |