| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Itinero.Routes; |
| | 7 | | using Itinero.Routes.Builders; |
| | 8 | | using Itinero.Routes.Paths; |
| | 9 | |
|
| | 10 | | namespace Itinero.Routing; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Many to many extensions. |
| | 14 | | /// </summary> |
| | 15 | | public static class IRouterManyToManyExtensions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Calculates the paths. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="manyToManyRouter">The router.</param> |
| | 21 | | /// <param name="cancellationToken"></param> |
| | 22 | | /// <returns>The paths.</returns> |
| | 23 | | public static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> Paths(this IRouterManyToMany manyToManyRouter, |
| 0 | 24 | | { |
| 0 | 25 | | var sources = manyToManyRouter.Sources; |
| 0 | 26 | | var targets = manyToManyRouter.Targets; |
| | 27 | |
|
| 0 | 28 | | if (!sources.TryToUndirected(out var sourcesUndirected) || |
| 0 | 29 | | !targets.TryToUndirected(out var targetsUndirected)) |
| 0 | 30 | | { |
| 0 | 31 | | return await manyToManyRouter.CalculateAsync(sources, targets); |
| | 32 | | } |
| | 33 | | else |
| 0 | 34 | | { |
| 0 | 35 | | return await manyToManyRouter.CalculateAsync(sourcesUndirected, targetsUndirected, cancellationToken); |
| | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Calculates the routes. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="manyToManyRouter">The router.</param> |
| | 43 | | /// <param name="cancellationToken"></param> |
| | 44 | | /// <returns>The paths.</returns> |
| | 45 | | public static async Task<IReadOnlyList<IReadOnlyList<Result<Route>>>> Calculate(this IRouterManyToMany manyToManyRou |
| 0 | 46 | | { |
| 0 | 47 | | var start = DateTime.Now.Ticks; |
| 0 | 48 | | var paths = await manyToManyRouter.Paths(cancellationToken); |
| 0 | 49 | | var end = DateTime.Now.Ticks; |
| 0 | 50 | | Console.WriteLine(new TimeSpan(end - start)); |
| 0 | 51 | | return paths.Select(x => |
| 0 | 52 | | { |
| 0 | 53 | | return x.Select(y => |
| 0 | 54 | | { |
| 0 | 55 | | if (y.IsError) return y.ConvertError<Route>(); |
| 0 | 56 | |
|
| 0 | 57 | | return manyToManyRouter.Settings.RouteBuilder.Build(manyToManyRouter.Network, |
| 0 | 58 | | manyToManyRouter.Settings.Profile, y); |
| 0 | 59 | | }).ToArray(); |
| 0 | 60 | | }).ToArray(); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Calculates the weights. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="manyToManyWeightRouter">The router.</param> |
| | 67 | | /// <returns></returns> |
| | 68 | | /// <exception cref="Exception"></exception> |
| | 69 | | public static Task<Result<IReadOnlyList<IReadOnlyList<double?>>>> Calculate( |
| | 70 | | this IRouterWeights<IRouterManyToMany> manyToManyWeightRouter) |
| 0 | 71 | | { |
| 0 | 72 | | return Task.FromResult(new Result<IReadOnlyList<IReadOnlyList<double?>>>("Not implemented")); |
| | 73 | |
|
| | 74 | | // var profileHandler = manyToManyWeightRouter.Router.Network.GetCostFunctionFor( |
| | 75 | | // manyToManyWeightRouter.Router.Settings.Profile); |
| | 76 | | // var paths = manyToManyWeightRouter.Router.Paths(); |
| | 77 | | // return paths.Select(x => |
| | 78 | | // { |
| | 79 | | // return x.Select(y => y.Weight(profileHandler.GetForwardWeight)).ToArray(); |
| | 80 | | // }).ToArray(); |
| 0 | 81 | | } |
| | 82 | | } |