< Summary

Class:Itinero.Routing.Costs.ProfileCostFunctionCached
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/Costs/ProfileCostFunctionCached.cs
Covered lines:34
Uncovered lines:19
Coverable lines:53
Total lines:90
Line coverage:64.1% (34 of 53)
Covered branches:12
Total branches:24
Branch coverage:50% (12 of 24)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
Get(...)50%2459.57%

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
 517    internal ProfileCostFunctionCached(Profile profile, EdgeFactorCache edgeFactorCache, TurnCostFactorCache turnCostFac
 518    {
 519        _profile = profile;
 520        _edgeFactorCache = edgeFactorCache;
 521        _turnCostFactorCache = turnCostFactorCache;
 522    }
 23
 24    public (bool canAccess, bool canStop, double cost, double turnCost) Get(
 25        IEdgeEnumerator<RoutingNetwork> edgeEnumerator, bool forward = true,
 26        IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null)
 627    {
 628        previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty;
 29
 30        // get edge factor and length.
 31        EdgeFactor factor;
 632        var edgeTypeId = edgeEnumerator.EdgeTypeId;
 633        if (edgeTypeId == null)
 034        {
 035            factor = _profile.FactorInEdgeDirection(edgeEnumerator);
 036        }
 37        else
 638        {
 639            var edgeFactor = _edgeFactorCache.Get(edgeTypeId.Value);
 640            if (edgeFactor == null)
 541            {
 42                // no cached value, cache forward value.
 543                factor = _profile.Factor(edgeEnumerator.Attributes);
 544                _edgeFactorCache.Set(edgeTypeId.Value, factor);
 545            }
 46            else
 147            {
 148                factor = edgeFactor.Value;
 149            }
 50
 51            // cached value is always forward.
 652            if (!edgeEnumerator.Forward)
 253            {
 254                factor = factor.Reverse;
 255            }
 656        }
 57
 658        var lengthNullable = edgeEnumerator.Length;
 659        var length = lengthNullable ??
 660                     (uint)(edgeEnumerator.EdgeLength() * 100);
 661        var cost = forward ? factor.ForwardFactor * length : factor.BackwardFactor * length;
 662        var canAccess = forward ? factor.ForwardFactor > 0 : factor.BackwardFactor > 0;
 63
 664        var totalTurnCost = 0.0;
 665        var (_, turn) = previousEdges.FirstOrDefault();
 1266        if (turn == null) return (canAccess, factor.CanStop, cost, totalTurnCost);
 067        var turnCosts = edgeEnumerator.GetTurnCostToTail(turn.Value);
 68
 069        foreach (var (turnCostType, attributes, turnCost, prefixEdges) in turnCosts)
 070        {
 71            // TODO: compare prefix edges with the previous edges.
 72
 073            var turnCostFactor = _turnCostFactorCache.Get(turnCostType);
 074            if (turnCostFactor == null)
 075            {
 076                turnCostFactor = _profile.TurnCostFactor(attributes);
 077                _turnCostFactorCache.Set(turnCostType, turnCostFactor.Value);
 078            }
 079            if (turnCostFactor.Value.IsBinary && turnCost > 0)
 080            {
 081                totalTurnCost = double.MaxValue;
 082                break;
 83            }
 84
 085            totalTurnCost += turnCostFactor.Value.CostFactor * turnCost;
 086        }
 87
 088        return (canAccess, factor.CanStop, cost, totalTurnCost);
 689    }
 90}

Methods/Properties

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