< Summary

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

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
 9using Itinero.Routing.Flavours.Dijkstra;
 10
 11namespace Itinero.Routing.Costs;
 12
 13internal class ProfileCostFunctionCached : ICostFunction
 14{
 15    private readonly Profile _profile;
 16    private readonly EdgeFactorCache _edgeFactorCache;
 17    private readonly TurnCostFactorCache _turnCostFactorCache;
 18
 1371219    internal ProfileCostFunctionCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFac
 1371220    {
 1371221        _profile = profile;
 1371222        _edgeFactorCache = edgeFactorCache;
 1371223        _turnCostFactorCache = turnCostFactorCache;
 1371224    }
 25
 26    public (bool canAccess, bool canStop, bool localAccess, double cost, double turnCost) Get(
 27        IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool tailToHead = true,
 28        PreviousEdgeEnumerable previousEdges = default)
 109602329    {
 30
 31        // get edge factor and length.
 32        EdgeFactor factor;
 109602333        var edgeTypeId = edgeEnumerator.EdgeTypeId;
 109602334        if (edgeTypeId == null)
 2835        {
 2836            factor = _profile.FactorInEdgeDirection(edgeEnumerator);
 2837        }
 38        else
 109599539        {
 109599540            var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value);
 109599541            if (edgeFactor == null)
 180242            {
 43                // no cached value, cache forward value.
 180244                factor = _profile.Factor(edgeEnumerator.Attributes);
 180245                _edgeFactorCache.Set(edgeTypeId.Value, factor);
 180246            }
 47            else
 109419348            {
 109419349                factor = edgeFactor.Value;
 109419350            }
 51
 52            // cached value is always forward.
 109599553            if (!edgeEnumerator.Forward)
 49551054            {
 49551055                factor = factor.Reverse;
 49551056            }
 109599557        }
 58
 109602359        var lengthNullable = edgeEnumerator.Length;
 109602360        var length = lengthNullable ??
 109602361                     (uint)(edgeEnumerator.EdgeLength() * 100);
 109602362        var cost = tailToHead ? factor.ForwardFactor * length : factor.BackwardFactor * length;
 109602363        var canAccess = tailToHead ? factor.ForwardFactor > 0 : factor.BackwardFactor > 0;
 109602364        var localAccess = factor.IsLocalAccess;
 65
 109602366        var totalTurnCost = 0.0;
 109602367        var turn = previousEdges.First?.turn;
 217286468        if (turn == null) return (canAccess, factor.CanStop, localAccess, cost, totalTurnCost);
 69
 1918270        if (tailToHead)
 1916971        {
 1916972            var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value);
 73
 9580074            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 1916175            {
 76                // TODO: compare prefix edges with the previous edges.
 77
 1916178                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 1916179                if (turnCostFactor == null)
 5580                {
 5581                    turnCostFactor = _profile.TurnCostFactor(attributes);
 5582                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 5583                }
 84
 1916185                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 2986                {
 2987                    totalTurnCost = double.MaxValue;
 2988                    break;
 89                }
 90
 1913291                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 1913292            }
 1916993        }
 94        else
 1395        {
 1396            var turnCosts = edgeEnumerator.GetTurnCostFromTail(turn.Value);
 97
 4998            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 999            {
 100                // TODO: compare prefix edges with the previous edges.
 101
 9102                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 9103                if (turnCostFactor == null)
 1104                {
 1105                    turnCostFactor = _profile.TurnCostFactor(attributes);
 1106                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 1107                }
 108
 9109                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 8110                {
 8111                    totalTurnCost = double.MaxValue;
 8112                    break;
 113                }
 114
 1115                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 1116            }
 13117        }
 118
 19182119        return (canAccess, factor.CanStop, localAccess, cost, totalTurnCost);
 1096023120    }
 121}

Methods/Properties

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