| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using Itinero.Network.Enumerators.Edges; |
| | | 3 | | using Itinero.Routing.Costs.Caches; |
| | | 4 | | |
| | | 5 | | namespace Itinero.Profiles; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A cached version of a profile. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ProfileCached |
| | | 11 | | { |
| | | 12 | | private readonly Profile _profile; |
| | | 13 | | private readonly EdgeFactorCache _edgeFactorCache; |
| | | 14 | | private readonly TurnCostFactorCache _turnCostFactorCache; |
| | | 15 | | |
| | 21 | 16 | | internal ProfileCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFactorCache) |
| | 21 | 17 | | { |
| | 21 | 18 | | _profile = profile; |
| | 21 | 19 | | _edgeFactorCache = edgeFactorCache; |
| | 21 | 20 | | _turnCostFactorCache = turnCostFactorCache; |
| | 21 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets an edge factor for the current edge. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="edgeEnumerator">The enumerator with the current edge.</param> |
| | | 27 | | /// <returns>The edge factor.</returns> |
| | | 28 | | public EdgeFactor Factor(RoutingNetworkEdgeEnumerator edgeEnumerator) |
| | 30 | 29 | | { |
| | | 30 | | // get edge factor and length. |
| | | 31 | | EdgeFactor factor; |
| | 30 | 32 | | var edgeTypeId = edgeEnumerator.EdgeTypeId; |
| | 30 | 33 | | if (edgeTypeId == null) |
| | 0 | 34 | | { |
| | 0 | 35 | | factor = _profile.FactorInEdgeDirection(edgeEnumerator); |
| | 0 | 36 | | } |
| | | 37 | | else |
| | 30 | 38 | | { |
| | 30 | 39 | | var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value); |
| | 30 | 40 | | if (edgeFactor == null) |
| | 21 | 41 | | { |
| | | 42 | | // no cached value, cache forward value. |
| | 21 | 43 | | factor = _profile.Factor(edgeEnumerator.Attributes); |
| | 21 | 44 | | _edgeFactorCache.Set(edgeTypeId.Value, factor); |
| | 21 | 45 | | } |
| | | 46 | | else |
| | 9 | 47 | | { |
| | 9 | 48 | | factor = edgeFactor.Value; |
| | 9 | 49 | | } |
| | | 50 | | |
| | | 51 | | // cached value is always forward. |
| | 30 | 52 | | if (!edgeEnumerator.Forward) |
| | 11 | 53 | | { |
| | 11 | 54 | | factor = factor.Reverse; |
| | 11 | 55 | | } |
| | 30 | 56 | | } |
| | | 57 | | |
| | 30 | 58 | | return factor; |
| | 30 | 59 | | } |
| | | 60 | | } |