removed duplicate

This commit is contained in:
C9Glax 2022-11-13 14:59:06 +01:00
parent 3498fe5ae3
commit adda057b88
4 changed files with 22 additions and 46 deletions

View File

@ -3,7 +3,7 @@ using Logging;
using astar; using astar;
Logger logger = new (LogType.CONSOLE, LogLevel.DEBUG); 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; logger.level = LogLevel.DEBUG;
Random r = new(); Random r = new();
@ -13,8 +13,8 @@ do
{ {
do do
{ {
n1 = graph.GetNodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); n1 = graph.NodeAtIndex(r.Next(0, graph.GetNodeCount() - 1));
n2 = graph.GetNodeAtIndex(r.Next(0, graph.GetNodeCount() - 1)); n2 = graph.NodeAtIndex(r.Next(0, graph.GetNodeCount() - 1));
_route = new Astar().FindPath(graph, n1, n2, logger); _route = new Astar().FindPath(graph, n1, n2, logger);
} while (!_route.routeFound); } while (!_route.routeFound);
} while (Console.ReadKey().Key.Equals(ConsoleKey.Enter)); } while (Console.ReadKey().Key.Equals(ConsoleKey.Enter));

View File

@ -3,29 +3,21 @@ namespace Graph
{ {
public class Graph public class Graph
{ {
private List<Node> nodes { get; } private Dictionary<ulong, Node> nodes { get; }
public Graph() public Graph()
{ {
this.nodes = new(); this.nodes = new();
} }
public bool AddNode(Node n) public bool AddNode(ulong id, Node n)
{ {
if (!this.ContainsNode(n.id)) return this.nodes.TryAdd(id, n);
{
this.nodes.Add(n);
return true;
}
else
{
return false;
}
} }
public Node GetNodeAtIndex(int i) public Node NodeAtIndex(int i)
{ {
return this.nodes[i]; return this.nodes.Values.ToArray()[i];
} }
public int GetNodeCount() public int GetNodeCount()
@ -35,44 +27,30 @@ namespace Graph
public Node? GetNode(ulong id) public Node? GetNode(ulong id)
{ {
foreach(Node n in this.nodes) if (this.nodes.TryGetValue(id, out Node? n))
{ return n;
if (n.id == id) else
return n; return null;
}
return null;
} }
public bool ContainsNode(ulong id) 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) public bool RemoveNode(ulong id)
{ {
Node? n = this.GetNode(id); return this.nodes.Remove(id);
if(n != null)
{
this.nodes.Remove(n);
return true;
}
else
{
return false;
}
} }
public bool RemoveNode(Node n) public bool RemoveNode(Node n)
{ {
if (this.RemoveNode(n.id)) throw new NotImplementedException();
{
this.nodes.Remove(n);
return true;
}
else
{
return false;
}
} }
public Node ClosestNodeToCoordinates(float lat, float lon) public Node ClosestNodeToCoordinates(float lat, float lon)

View File

@ -5,12 +5,10 @@
public float lat { get; } public float lat { get; }
public float lon { get; } public float lon { get; }
public ulong id { get; }
public HashSet<Edge> edges { get; } public HashSet<Edge> edges { get; }
public Node(ulong id, float lat, float lon) public Node(float lat, float lon)
{ {
this.id = id;
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
this.edges = new(); this.edges = new();

View File

@ -118,7 +118,7 @@ namespace OpenStreetMap_Importer
{ {
float lat = Convert.ToSingle(_reader.GetAttribute("lat").Replace('.', ',')); float lat = Convert.ToSingle(_reader.GetAttribute("lat").Replace('.', ','));
float lon = Convert.ToSingle(_reader.GetAttribute("lon").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]); logger?.Log(LogLevel.VERBOSE, "NODE {0} {1} {2} {3}", id, lat, lon, occuranceCount[id]);
} }
} }