85 lines
2.7 KiB
C#
85 lines
2.7 KiB
C#
using BeWo.Data.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BeWo.Service.ServiceUtils.DistanceCalculator
|
|
{
|
|
public class AddressRouteMatrix
|
|
{
|
|
public int OriginsLength { get; set; }
|
|
public int DestinationsLength { get; set; }
|
|
|
|
public Address[] OriginAddresses { get; set; }
|
|
public Address[] DestinationAddresses { get; set; }
|
|
public Dictionary<Address, int> Origins { get; set; }
|
|
public Dictionary<Address, int> Destinations { get; set; }
|
|
public AddressRoute[,] Routes { get; set; }
|
|
|
|
public AddressRouteMatrix(Address[] originAddresses, Address[] destinationAddresses)
|
|
{
|
|
OriginAddresses = originAddresses;
|
|
Origins = new Dictionary<Address, int>();
|
|
OriginsLength = originAddresses.Length;
|
|
|
|
DestinationAddresses = destinationAddresses;
|
|
Destinations = new Dictionary<Address, int>();
|
|
DestinationsLength = destinationAddresses.Length;
|
|
|
|
int i;
|
|
for (i = 0; i < OriginsLength; i++)
|
|
{
|
|
Origins.Add(originAddresses[i], i);
|
|
}
|
|
|
|
for (i = 0; i < DestinationsLength; i++)
|
|
{
|
|
Destinations.Add(destinationAddresses[i], i);
|
|
}
|
|
|
|
Routes = new AddressRoute[OriginsLength, DestinationsLength];
|
|
}
|
|
|
|
public void SetRoute(Address origin, Address destination, AddressRoute value) => Routes[Origins[origin], Destinations[destination]] = value;
|
|
public AddressRoute GetRoute(Address origin, Address destination)
|
|
{
|
|
return Routes[Origins[origin], Destinations[destination]];
|
|
}
|
|
|
|
public IEnumerable<AddressRoute> GetUnkownRoutes()
|
|
{
|
|
for (int i = 0; i < OriginsLength; i++)
|
|
{
|
|
for (int j = 0; j < DestinationsLength; j++)
|
|
{
|
|
var route = Routes[i, j];
|
|
|
|
if (route is null)
|
|
{
|
|
route = new AddressRoute
|
|
{
|
|
Origin = OriginAddresses[i],
|
|
Destination = DestinationAddresses[j]
|
|
};
|
|
|
|
Routes[i, j] = route;
|
|
|
|
yield return route;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool CanUseRoute(Address origin, Address destination)
|
|
{
|
|
if (!Origins.ContainsKey(origin))
|
|
return false;
|
|
if (!Destinations.ContainsKey(destination))
|
|
return false;
|
|
return true;
|
|
}
|
|
}
|
|
}
|