From adda057b887b3c10a10b433141f10ff5171f72f8 Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Sun, 13 Nov 2022 14:59:06 +0100 Subject: [PATCH] removed duplicate --- Executable/Program.cs | 6 ++-- Graph/Graph.cs | 56 +++++++++--------------------- Graph/Node.cs | 4 +-- OpenStreetMap Importer/Importer.cs | 2 +- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/Executable/Program.cs b/Executable/Program.cs index 03dfbcb..a476d52 100644 --- a/Executable/Program.cs +++ b/Executable/Program.cs @@ -3,7 +3,7 @@ using Logging; using astar; Logger logger = new (LogType.CONSOLE, LogLevel.DEBUG); -Graph.Graph graph = OpenStreetMap_Importer.Importer.Import(@"", true, logger); +Graph.Graph graph = OpenStreetMap_Importer.Importer.Import(@"C:\Users\glax\Downloads\oberbayern-latest.osm", true, logger); logger.level = LogLevel.DEBUG; Random r = new(); @@ -13,8 +13,8 @@ do { do { - n1 = graph.GetNodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); - n2 = graph.GetNodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); + n1 = graph.NodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); + n2 = graph.NodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); _route = new Astar().FindPath(graph, n1, n2, logger); } while (!_route.routeFound); } while (Console.ReadKey().Key.Equals(ConsoleKey.Enter)); \ No newline at end of file diff --git a/Graph/Graph.cs b/Graph/Graph.cs index 8ec209e..efeae28 100644 --- a/Graph/Graph.cs +++ b/Graph/Graph.cs @@ -3,29 +3,21 @@ namespace Graph { public class Graph { - private List nodes { get; } + private Dictionary nodes { get; } public Graph() { this.nodes = new(); } - public bool AddNode(Node n) + public bool AddNode(ulong id, Node n) { - if (!this.ContainsNode(n.id)) - { - this.nodes.Add(n); - return true; - } - else - { - return false; - } + return this.nodes.TryAdd(id, n); } - public Node GetNodeAtIndex(int i) + public Node NodeAtIndex(int i) { - return this.nodes[i]; + return this.nodes.Values.ToArray()[i]; } public int GetNodeCount() @@ -35,44 +27,30 @@ namespace Graph public Node? GetNode(ulong id) { - foreach(Node n in this.nodes) - { - if (n.id == id) - return n; - } - return null; + if (this.nodes.TryGetValue(id, out Node? n)) + return n; + else + return null; } public bool ContainsNode(ulong id) { - return this.GetNode(id) != null; + return this.nodes.ContainsKey(id); + } + + public bool ContainsNode(Node n) + { + return this.nodes.Values.Contains(n); } public bool RemoveNode(ulong id) { - Node? n = this.GetNode(id); - if(n != null) - { - this.nodes.Remove(n); - return true; - } - else - { - return false; - } + return this.nodes.Remove(id); } public bool RemoveNode(Node n) { - if (this.RemoveNode(n.id)) - { - this.nodes.Remove(n); - return true; - } - else - { - return false; - } + throw new NotImplementedException(); } public Node ClosestNodeToCoordinates(float lat, float lon) diff --git a/Graph/Node.cs b/Graph/Node.cs index b63bcd5..c450258 100644 --- a/Graph/Node.cs +++ b/Graph/Node.cs @@ -5,12 +5,10 @@ public float lat { get; } public float lon { get; } - public ulong id { get; } public HashSet edges { get; } - public Node(ulong id, float lat, float lon) + public Node(float lat, float lon) { - this.id = id; this.lat = lat; this.lon = lon; this.edges = new(); diff --git a/OpenStreetMap Importer/Importer.cs b/OpenStreetMap Importer/Importer.cs index 4e9b9f6..83c329d 100644 --- a/OpenStreetMap Importer/Importer.cs +++ b/OpenStreetMap Importer/Importer.cs @@ -118,7 +118,7 @@ namespace OpenStreetMap_Importer { float lat = Convert.ToSingle(_reader.GetAttribute("lat").Replace('.', ',')); float lon = Convert.ToSingle(_reader.GetAttribute("lon").Replace('.', ',')); - _graph.AddNode(new Node(id, lat, lon)); + _graph.AddNode(id, new Node(lat, lon)); logger?.Log(LogLevel.VERBOSE, "NODE {0} {1} {2} {3}", id, lat, lon, occuranceCount[id]); } }