< Summary

Class:Itinero.Routing.Costs.ProfileCostFunctionCached
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/Costs/ProfileCostFunctionCached.cs
Covered lines:74
Uncovered lines:0
Coverable lines:74
Total lines:120
Line coverage:100% (74 of 74)
Covered branches:33
Total branches:34
Branch coverage:97% (33 of 34)
Tag:263_26948838820

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
Get(...)97.05%34100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Routing/Costs/ProfileCostFunctionCached.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Itinero.Network;
 5using Itinero.Network.Enumerators.Edges;
 6using Itinero.Profiles;
 7using Itinero.Routing.Costs.Caches;
 8
 9namespace Itinero.Routing.Costs;
 10
 11internal class ProfileCostFunctionCached : ICostFunction
 12{
 13    private readonly Profile _profile;
 14    private readonly EdgeFactorCache _edgeFactorCache;
 15    private readonly TurnCostFactorCache _turnCostFactorCache;
 16
 1371217    internal ProfileCostFunctionCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFac
 1371218    {
 1371219        _profile = profile;
 1371220        _edgeFactorCache = edgeFactorCache;
 1371221        _turnCostFactorCache = turnCostFactorCache;
 1371222    }
 23
 24    public (bool canAccess, bool canStop, bool localAccess, double cost, double turnCost) Get(
 25        IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool tailToHead = true,
 26        IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null)
 109604727    {
 109604728        previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty;
 29
 30        // get edge factor and length.
 31        EdgeFactor factor;
 109604732        var edgeTypeId = edgeEnumerator.EdgeTypeId;
 109604733        if (edgeTypeId == null)
 3234        {
 3235            factor = _profile.FactorInEdgeDirection(edgeEnumerator);
 3236        }
 37        else
 109601538        {
 109601539            var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value);
 109601540            if (edgeFactor == null)
 180241            {
 42                // no cached value, cache forward value.
 180243                factor = _profile.Factor(edgeEnumerator.Attributes);
 180244                _edgeFactorCache.Set(edgeTypeId.Value, factor);
 180245            }
 46            else
 109421347            {
 109421348                factor = edgeFactor.Value;
 109421349            }
 50
 51            // cached value is always forward.
 109601552            if (!edgeEnumerator.Forward)
 49555253            {
 49555254                factor = factor.Reverse;
 49555255            }
 109601556        }
 57
 109604758        var lengthNullable = edgeEnumerator.Length;
 109604759        var length = lengthNullable ??
 109604760                     (uint)(edgeEnumerator.EdgeLength() * 100);
 109604761        var cost = tailToHead ? factor.ForwardFactor * length : factor.BackwardFactor * length;
 109604762        var canAccess = tailToHead ? factor.ForwardFactor > 0 : factor.BackwardFactor > 0;
 109604763        var localAccess = factor.IsLocalAccess;
 64
 109604765        var totalTurnCost = 0.0;
 109604766        var (_, turn) = previousEdges.FirstOrDefault();
 217290467        if (turn == null) return (canAccess, factor.CanStop, localAccess, cost, totalTurnCost);
 68
 1919069        if (tailToHead)
 1917370        {
 1917371            var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value);
 72
 9581673            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 1916574            {
 75                // TODO: compare prefix edges with the previous edges.
 76
 1916577                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 1916578                if (turnCostFactor == null)
 5579                {
 5580                    turnCostFactor = _profile.TurnCostFactor(attributes);
 5581                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 5582                }
 83
 1916584                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 3385                {
 3386                    totalTurnCost = double.MaxValue;
 3387                    break;
 88                }
 89
 1913290                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 1913291            }
 1917392        }
 93        else
 1794        {
 1795            var turnCosts = edgeEnumerator.GetTurnCostFromTail(turn.Value);
 96
 6597            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 1398            {
 99                // TODO: compare prefix edges with the previous edges.
 100
 13101                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 13102                if (turnCostFactor == null)
 1103                {
 1104                    turnCostFactor = _profile.TurnCostFactor(attributes);
 1105                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 1106                }
 107
 13108                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 12109                {
 12110                    totalTurnCost = double.MaxValue;
 12111                    break;
 112                }
 113
 1114                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 1115            }
 17116        }
 117
 19190118        return (canAccess, factor.CanStop, localAccess, cost, totalTurnCost);
 1096047119    }
 120}

Methods/Properties

.ctor(...)
Get(...)