From 2826ff2502a89e73495f5eb60eacdfba1541b6f7 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 1 Apr 2023 14:42:49 +0200 Subject: [PATCH] Fixed deserialization issue with wrong default values ToString() uniform --- OSMDatastructure/OsmNode.cs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/OSMDatastructure/OsmNode.cs b/OSMDatastructure/OsmNode.cs index 6ffbbd9..560865c 100644 --- a/OSMDatastructure/OsmNode.cs +++ b/OSMDatastructure/OsmNode.cs @@ -1,3 +1,6 @@ +using System.ComponentModel; +using System.Runtime.Serialization; + namespace OSMDatastructure.Graph; [Serializable] @@ -11,6 +14,14 @@ public class OsmNode [NonSerialized]public double currentPathWeight = double.MaxValue; [NonSerialized]public double currentPathLength = double.MaxValue; [NonSerialized]public double directDistanceToGoal = double.MaxValue; + + [OnDeserialized] + internal void SetDefaultValues(StreamingContext context) + { + currentPathWeight = double.MaxValue; + currentPathLength = double.MaxValue; + directDistanceToGoal = double.MaxValue; + } public OsmNode(ulong nodeId, float lat, float lon) { @@ -28,10 +39,10 @@ public class OsmNode public OsmEdge? GetEdgeToNode(OsmNode n) { - foreach (OsmEdge e in this.edges) - if (e.neighborId == n.nodeId) - return e; - return null; + HashSet e = edges.Where(edge => edge.neighborId == n.nodeId).ToHashSet(); + if (e.Count > 0) + return e.First(); + else return null; } public override bool Equals(object? obj) @@ -39,15 +50,10 @@ public class OsmNode return obj != null && obj.GetType() == this.GetType() && ((OsmNode)obj).nodeId == this.nodeId; } - public override int GetHashCode() - { - return HashCode.Combine(coordinates); - } - public override string ToString() { if(previousPathNode is not null) - return $"{nodeId} {coordinates} {edges.Count} {directDistanceToGoal} {currentPathWeight} {currentPathLength} {previousPathNode.nodeId}"; + return $"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} p:{previousPathNode.nodeId}"; return $"{nodeId} {coordinates} ec:{edges.Count} d:{directDistanceToGoal} w:{currentPathWeight} l:{currentPathLength} null"; }