< Summary

Class:Itinero.Routing.Alternatives.IRouterOneToOneWithAlternativesExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/Alternatives/IRouterOneToOneWithAlternativesExtensions.cs
Covered lines:27
Uncovered lines:0
Coverable lines:27
Total lines:75
Line coverage:100% (27 of 27)
Covered branches:8
Total branches:10
Branch coverage:80% (8 of 10)
Tag:284_38118444584

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
PathsAsync()87.5%8100%
CalculateAsync()50%2100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Routing/Alternatives/IRouterOneToOneWithAlternativesExtensions.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Itinero.Routes;
 6using Itinero.Routes.Paths;
 7using Itinero.Routing.Corridors;
 8
 9namespace Itinero.Routing.Alternatives;
 10
 11/// <summary>
 12/// One to one with alternatives extensions.
 13/// </summary>
 14public static class IRouterOneToOneWithAlternativesExtensions
 15{
 16    /// <summary>
 17    /// Calculates the fastest path and its alternatives.
 18    /// </summary>
 19    /// <returns>
 20    /// The fastest path first, then the alternatives cheapest-first. Never empty unless the
 21    /// endpoints are not connected, in which case an error comes back.
 22    /// </returns>
 23    /// <remarks>
 24    /// One search: the alternatives come out of the cost ellipse around the fastest route, which
 25    /// the route's own search maps on the way past.
 26    /// </remarks>
 27    public static async Task<Result<IReadOnlyList<Path>>> PathsAsync(
 28        this IRouterOneToOneWithAlternatives withAlternatives,
 29        CancellationToken cancellationToken = default)
 830    {
 831        if (withAlternatives.Source.direction != null || withAlternatives.Target.direction != null)
 132        {
 133            return new Result<IReadOnlyList<Path>>(
 134                "Alternatives are not supported for directed endpoints");
 35        }
 36
 737        var settings = withAlternatives.AlternativeRouteSettings;
 738        var network = withAlternatives.Network;
 739        var source = withAlternatives.Source.sp;
 740        var target = withAlternatives.Target.sp;
 41
 42        // The budget IS the cost constraint, so every path in the corridor satisfies it by
 43        // construction and only overlap and the detour bound are left to apply.
 744        var searched = await network.RouteCorridorAsync(withAlternatives.Settings, source, target,
 745            settings.MaxCostFactor, cancellationToken);
 746        if (searched.IsError) return new Result<IReadOnlyList<Path>>(searched.ErrorMessage);
 47
 748        var (route, corridor) = searched.Value;
 749        var paths = new List<Path>(settings.MaxNumberOfAlternativeRoutes) { route.Path };
 850        if (settings.MaxNumberOfAlternativeRoutes <= 1) return paths;
 51
 652        paths.AddRange(network.AlternativesFor(route, corridor, settings.MaxOverlap,
 653            settings.MaxNumberOfAlternativeRoutes - 1, settings.LocalDetourFactor,
 654            cancellationToken));
 55
 656        return paths;
 857    }
 58
 59    /// <summary>
 60    /// Calculates the fastest route and its alternatives.
 61    /// </summary>
 62    /// <returns>The fastest route first, then the alternatives cheapest-first.</returns>
 63    public static async Task<Result<IReadOnlyList<Route>>> CalculateAsync(
 64        this IRouterOneToOneWithAlternatives withAlternatives,
 65        CancellationToken cancellationToken = default)
 166    {
 167        var paths = await withAlternatives.PathsAsync(cancellationToken);
 168        if (paths.IsError) return new Result<IReadOnlyList<Route>>(paths.ErrorMessage);
 69
 370        var routes = paths.Value.Select(path => withAlternatives.Settings.RouteBuilder.Build(
 371            withAlternatives.Network, withAlternatives.Settings.Profile, path).Value).ToList();
 72
 173        return new Result<IReadOnlyList<Route>>(routes);
 174    }
 75}

Methods/Properties

PathsAsync()
CalculateAsync()