From 9e4586d38be562d326e1523217980a83c7ccd9f5 Mon Sep 17 00:00:00 2001 From: msg_kurt Date: Sat, 5 Jan 2019 15:46:33 +0200 Subject: [PATCH] ShortestPath extension to getting custom info chain implemented --- src/Dijkstra.NET.Tests/EdgeTest.cs | 29 ++++++++++++ src/Dijkstra.NET/Graph/Node.cs | 3 +- .../ShortestPath/ShortestPathExtensions.cs | 45 +++++++++++++++++++ src/Samples/Dijkstra.Net40/Program.cs | 3 +- 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/Dijkstra.NET/ShortestPath/ShortestPathExtensions.cs diff --git a/src/Dijkstra.NET.Tests/EdgeTest.cs b/src/Dijkstra.NET.Tests/EdgeTest.cs index c549a02..e3a2492 100644 --- a/src/Dijkstra.NET.Tests/EdgeTest.cs +++ b/src/Dijkstra.NET.Tests/EdgeTest.cs @@ -1,4 +1,6 @@ using Dijkstra.NET.Graph; +using Dijkstra.NET.ShortestPath; +using System.Linq; using Xunit; namespace Dijkstra.NET.Tests @@ -119,5 +121,32 @@ public void Edges_Get_TEdgeCustom_Should_Work() Assert.True(act); } + [Fact] + public void Edges_Get_Full_Path_Custom() + { + var g = new Graph(); + g.AddNode(1); + g.AddNode(2); + g.AddNode(3); + + g.Connect(1, 2, 1, "First"); + g.Connect(2, 3, 1, "Second"); + + var path = g.Dijkstra(1, 3); + var pathCustom = path.GetPathEdgesCustom(g); + var pathCustomReversed = path.GetReversedPathEdgesCustom(g); + + bool act = pathCustom.Count() == 2; + bool act2 = pathCustomReversed.Count() == 2; + bool act3 = pathCustom.First() == "First"; + bool act4 = pathCustomReversed.First() == "Second"; + + Assert.True(act); + Assert.True(act2); + Assert.True(act3); + Assert.True(act3); + + } + } } diff --git a/src/Dijkstra.NET/Graph/Node.cs b/src/Dijkstra.NET/Graph/Node.cs index 53710c3..7105b14 100644 --- a/src/Dijkstra.NET/Graph/Node.cs +++ b/src/Dijkstra.NET/Graph/Node.cs @@ -59,8 +59,7 @@ public void EachEdge(Edge edge) public TEdgeCustom GetFirstEdgeCustom(int nodeEdgeKey) { return Edges.First(c => c.Node.Key == nodeEdgeKey).Item; - } - + } internal void AddEdge(in Edge edge) { diff --git a/src/Dijkstra.NET/ShortestPath/ShortestPathExtensions.cs b/src/Dijkstra.NET/ShortestPath/ShortestPathExtensions.cs new file mode 100644 index 0000000..2d0ec14 --- /dev/null +++ b/src/Dijkstra.NET/ShortestPath/ShortestPathExtensions.cs @@ -0,0 +1,45 @@ +using Dijkstra.NET.Graph; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Dijkstra.NET.ShortestPath +{ + public static class ShortestPathExtensions + { + public static IEnumerable GetReversedPathEdgesCustom(this ShortestPathResult result, Graph graph) where TEdgeCustom : IEquatable + { + return result.GetPathEdgesCustom(graph).Reverse(); + } + + public static IEnumerable GetPathEdgesCustom(this ShortestPathResult result, Graph graph) where TEdgeCustom : IEquatable + { + if (result.IsFounded) + { + using (var iterator = result.GetPath().GetEnumerator()) + { + if (!iterator.MoveNext()) + { + yield break; + } + var currentNode = iterator.Current; + while (iterator.MoveNext()) + { + yield return GetFirstEdgeCustom(graph, currentNode, iterator.Current); + currentNode = iterator.Current; + } + } + } + else + throw new System.Exception("path not founded"); + } + + static TEdgeCustom GetFirstEdgeCustom(Graph graph, uint currentNodeKey, uint edgeNodeKey) where TEdgeCustom : IEquatable + { + var currentNode = graph >> (int)currentNodeKey; + if (currentNode == null) + throw new ArgumentException("node not founded at graph"); + return currentNode.GetFirstEdgeCustom((int)edgeNodeKey); + } + } +} diff --git a/src/Samples/Dijkstra.Net40/Program.cs b/src/Samples/Dijkstra.Net40/Program.cs index 670aa5d..fbfd004 100644 --- a/src/Samples/Dijkstra.Net40/Program.cs +++ b/src/Samples/Dijkstra.Net40/Program.cs @@ -9,9 +9,10 @@ static void Main(string[] args) { var graph = new Graph() + 1 + 2; - bool connected = graph >> 1 >> 2 >> 5 ^ "edge information"; + bool connected = graph >> 1 >> 2 >> 5 ^ "edge information"; var immutablePath = graph.Dijkstra(1, 2); + var immutablePathDescriptions = immutablePath.GetPathEdgesCustom(graph); } } }