< Summary

Class:Itinero.Routing.Costs.ProfileCostFunctionCached
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/Costs/ProfileCostFunctionCached.cs
Covered lines:66
Uncovered lines:7
Coverable lines:73
Total lines:119
Line coverage:90.4% (66 of 73)
Covered branches:31
Total branches:34
Branch coverage:91.1% (31 of 34)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
Get(...)91.17%3489.55%

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
 1139017    internal ProfileCostFunctionCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFac
 1139018    {
 1139019        _profile = profile;
 1139020        _edgeFactorCache = edgeFactorCache;
 1139021        _turnCostFactorCache = turnCostFactorCache;
 1139022    }
 23
 24    public (bool canAccess, bool canStop, double cost, double turnCost) Get(
 25        IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool tailToHead = true,
 26        IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null)
 107039927    {
 107039928        previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty;
 29
 30        // get edge factor and length.
 31        EdgeFactor factor;
 107039932        var edgeTypeId = edgeEnumerator.EdgeTypeId;
 107039933        if (edgeTypeId == null)
 034        {
 035            factor = _profile.FactorInEdgeDirection(edgeEnumerator);
 036        }
 37        else
 107039938        {
 107039939            var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value);
 107039940            if (edgeFactor == null)
 178741            {
 42                // no cached value, cache forward value.
 178743                factor = _profile.Factor(edgeEnumerator.Attributes);
 178744                _edgeFactorCache.Set(edgeTypeId.Value, factor);
 178745            }
 46            else
 106861247            {
 106861248                factor = edgeFactor.Value;
 106861249            }
 50
 51            // cached value is always forward.
 107039952            if (!edgeEnumerator.Forward)
 48299253            {
 48299254                factor = factor.Reverse;
 48299255            }
 107039956        }
 57
 107039958        var lengthNullable = edgeEnumerator.Length;
 107039959        var length = lengthNullable ??
 107039960                     (uint)(edgeEnumerator.EdgeLength() * 100);
 107039961        var cost = tailToHead ? factor.ForwardFactor * length : factor.BackwardFactor * length;
 107039962        var canAccess = tailToHead ? factor.ForwardFactor > 0 : factor.BackwardFactor > 0;
 63
 107039964        var totalTurnCost = 0.0;
 107039965        var (_, turn) = previousEdges.FirstOrDefault();
 213876666        if (turn == null) return (canAccess, factor.CanStop, cost, totalTurnCost);
 67
 203268        if (tailToHead)
 202469        {
 202470            var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value);
 71
 1009272            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 201973            {
 74                // TODO: compare prefix edges with the previous edges.
 75
 201976                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 201977                if (turnCostFactor == null)
 2578                {
 2579                    turnCostFactor = _profile.TurnCostFactor(attributes);
 2580                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 2581                }
 82
 201983                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 1884                {
 1885                    totalTurnCost = double.MaxValue;
 1886                    break;
 87                }
 88
 200189                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 200190            }
 202491        }
 92        else
 893        {
 894            var turnCosts = edgeEnumerator.GetTurnCostFromTail(turn.Value);
 95
 3196            foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 697            {
 98                // TODO: compare prefix edges with the previous edges.
 99
 6100                var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 6101                if (turnCostFactor == null)
 0102                {
 0103                    turnCostFactor = _profile.TurnCostFactor(attributes);
 0104                    _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 0105                }
 106
 6107                if (turnCostFactor.Value.IsBinary && turnCost > 0)
 5108                {
 5109                    totalTurnCost = double.MaxValue;
 5110                    break;
 111                }
 112
 1113                totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 1114            }
 8115        }
 116
 2032117        return (canAccess, factor.CanStop, cost, totalTurnCost);
 1070399118    }
 119}

Methods/Properties

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