Skip to content

Commit 95d1c5d

Browse files
author
Maslov
committed
Добавлено CellsPositionsOnMap для быстрого доступа к позиции ячеек оптимального маршрута и построения на карте узлов и линий между ними.
Небольшой рефакторинг кода.
1 parent 5e84a34 commit 95d1c5d

File tree

8 files changed

+36
-24
lines changed

8 files changed

+36
-24
lines changed

Northrend.Alodi/Classes/Cell.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ public class Cell : ICell
1313

1414
public decimal Latitude { get; init; }
1515

16+
public int PositionX { get; init; }
17+
public int PositionY { get; init; }
18+
1619
Dictionary<string, decimal> mIntegralVelocities = [];
1720
public Dictionary<string, decimal> IntegralVelocities => mIntegralVelocities;
1821

19-
public Cell(decimal longitude, decimal latitude)
22+
public Cell(decimal longitude, decimal latitude, int positionX, int positionY)
2023
{
2124
Longitude = longitude;
2225
Latitude = latitude;
26+
PositionX = positionX;
27+
PositionY = positionY;
2328
}
2429

2530
public void AddIntegralVelocity(string date, decimal integralVelocity)

Northrend.Alodi/Classes/Factory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Northrend.Alodi.Classes
1010
{
1111
public static class Factory
1212
{
13-
public static ICell CreateCell(decimal longitude, decimal latitude)
14-
=> new Cell(longitude, latitude);
13+
public static ICell CreateCell(decimal longitude, decimal latitude, int positionX, int positionY)
14+
=> new Cell(longitude, latitude, positionX, positionY);
1515

1616
public static IMap CreateMap(int x, int y)
1717
=> new Map(x, y);

Northrend.Alodi/Classes/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Node : INode
1919
public decimal Longitude { get ; init; }
2020
public decimal Latitude { get; init; }
2121

22-
ICell? mCell = null;
22+
ICell? mCell;
2323
public ICell? Cell => mCell;
2424

2525

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33

44
namespace Northrend.Alodi.Classes
55
{
6-
public class RouteNode : IEquatable<RouteNode>, IRouteNode
6+
public class RouteByNodes : IEquatable<IRouteByNodes>, IRouteByNodes
77
{
88
readonly List<INode> mNodes = [];
99
public List<INode> Nodes => mNodes;
1010

1111
decimal mDistance = 0;
1212
public decimal Distance => mDistance;
1313

14-
public RouteNode() { }
15-
public RouteNode(IEnumerable<INode> nodes)
14+
public IEnumerable<(int i, int j)> CellsPositionsOnMap
15+
=> Nodes.Select(x => (x.Cell.PositionX, x.Cell.PositionY) );
16+
17+
public RouteByNodes() { }
18+
public RouteByNodes(IEnumerable<INode> nodes)
1619
{
1720
foreach (var node in nodes)
1821
Add(node);
@@ -38,10 +41,10 @@ public void Add(INode node)
3841
public bool IsExistNodeByName(string nodeName)
3942
=> Nodes.Any(x => x.Name.Equals(nodeName, StringComparison.OrdinalIgnoreCase));
4043

41-
public RouteNode Clone()
42-
=> new(Nodes);
44+
public IRouteByNodes Clone()
45+
=> new RouteByNodes(Nodes);
4346

44-
public bool Equals(RouteNode? other)
47+
public bool Equals(IRouteByNodes? other)
4548
{
4649
if (other is null)
4750
return false;

Northrend.Alodi/Interfaces/ICell.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Northrend.Alodi.Interfaces
88
{
99
public interface ICell : ICoordinate
1010
{
11+
int PositionX { get; }
12+
int PositionY { get; }
1113
Dictionary<string, decimal> IntegralVelocities { get; }
1214

1315
void AddIntegralVelocity(string date, decimal integralVelocity);

Northrend.Alodi/Interfaces/IRouteNode.cs renamed to Northrend.Alodi/Interfaces/IRouteByNodes.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Northrend.Alodi.Interfaces
44
{
5-
public interface IRouteNode
5+
public interface IRouteByNodes
66
{
77
decimal Distance { get; }
88
List<INode> Nodes { get; }
9-
9+
IEnumerable<(int i, int j)> CellsPositionsOnMap { get; }
1010
void Add(INode node);
1111
bool IsExistNodeByName(string nodeName);
12-
RouteNode Clone();
13-
bool Equals(RouteNode? other);
12+
IRouteByNodes Clone();
13+
bool Equals(IRouteByNodes? other);
14+
1415
}
1516
}

Northrend.Alodi/Services/ImportDataService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public ImportDataService(IServiceProvider serviceProvider)
6060
//вызываем ошибку
6161
}
6262

63-
var cell = Factory.CreateCell(longitude, latitude);
63+
var cell = Factory.CreateCell(longitude, latitude, col - 1, row - 1);
6464

6565
for (int i = 2; i < package.Workbook.Worksheets.Count; i++)
6666
{

Northrend.Alodi/Services/RoutesCreatorService.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Northrend.Alodi.Services
77
{
88
public class RoutesCreatorService
99
{
10-
readonly ConcurrentBag<IRouteNode> mCreatedRoutes = [];
10+
readonly ConcurrentBag<IRouteByNodes> mCreatedRoutes = [];
1111
readonly ConcurrentBag<Task> mTasks = [];
1212

1313
static readonly byte mMaxDegreeOfParallelism = 32;
@@ -32,7 +32,7 @@ public RoutesCreatorService(IServiceProvider serviceProvider) { }
3232
/// <param name="cancellationToken">Токен отмены анализа в случае, если анализ будет длиться продолжительное время.</param>
3333
/// <returns>Возвращает кортеж из списка маршрутов, отсортированных по дистанции, а также флаг успешности выполнения поиска маршуртов.
3434
/// Возвращает true, если успешно выполнен поиск, иначе возвращает false.</returns>
35-
public (IEnumerable<IRouteNode> Routes, bool IsSuccess) CreateRoutesByNodes(INodesMap? nodes, string startNodeName, string endNodeName, CancellationToken? cancellationToken = null)
35+
public (IEnumerable<IRouteByNodes> Routes, bool IsSuccess) CreateRoutesByNodes(INodesMap? nodes, string startNodeName, string endNodeName, CancellationToken? cancellationToken = null)
3636
{
3737
if (nodes is null)
3838
return ([], false);
@@ -50,14 +50,14 @@ public RoutesCreatorService(IServiceProvider serviceProvider) { }
5050
if (cancellationToken is null)
5151
cancellationToken = new CancellationTokenSource(new TimeSpan(0, mTimeWatingToAbortAnalyzeInSeconds, 0)).Token;
5252

53-
ConcurrentBag<IRouteNode> routes = [];
53+
ConcurrentBag<IRouteByNodes> routes = [];
5454
foreach (var nodeLine in mStartPoint.NextNodes)
5555
{
5656
var node = mNodes.GetNodeByName(nodeLine.Key);
5757
if (node is null)
5858
return ([], false);
5959

60-
var route = new RouteNode();
60+
var route = new RouteByNodes();
6161
route.Add(mStartPoint);
6262
route.Add(node);
6363
routes.Add(route);
@@ -68,15 +68,15 @@ public RoutesCreatorService(IServiceProvider serviceProvider) { }
6868
}
6969

7070

71-
bool AnalyzeNodesToCreateRoutes(IReadOnlyCollection<IRouteNode> routes)
71+
bool AnalyzeNodesToCreateRoutes(IReadOnlyCollection<IRouteByNodes> routes)
7272
{
7373
if (mNodes is null)
7474
return false;
7575

7676
if (mStartPoint is null || mEndPoint is null)
7777
return false;
7878

79-
ConcurrentBag<IRouteNode> tempRoutes = [];
79+
ConcurrentBag<IRouteByNodes> tempRoutes = [];
8080

8181
Task[] tasks = new Task[routes.Count];
8282
int taskPosition = 0;
@@ -137,7 +137,7 @@ bool AnalyzeNodesToCreateRoutes(IReadOnlyCollection<IRouteNode> routes)
137137
}
138138

139139

140-
public (IRouteNode? UpdatedRoute, bool IsSuccess) FindCellsForRouteNodes(IMap? map, IRouteNode? route, decimal shift)
140+
public (IRouteByNodes? UpdatedRoute, bool IsSuccess) FindCellsForRouteNodes(IMap? map, IRouteByNodes? route, decimal shift)
141141
{
142142
if(map is null || route is null)
143143
return (null, false);
@@ -153,9 +153,10 @@ bool AnalyzeNodesToCreateRoutes(IReadOnlyCollection<IRouteNode> routes)
153153
map.Cells[i, j].Latitude - deltaY,
154154
deltaX * 2,
155155
deltaY * 2);
156-
156+
157157
foreach (var node in route.Nodes)
158-
if (rectangle.Contains(node.Longitude, node.Latitude, shift))
158+
if (rectangle.Contains(node.Longitude, node.Latitude, shift)
159+
&& node.Cell is null)
159160
node.AddCell(map.Cells[i, j]);
160161
}
161162

0 commit comments

Comments
 (0)