Skip to content

Commit 5097f23

Browse files
committed
Refactor heuristic node comparing
1 parent e0ad6d0 commit 5097f23

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/Dijkstra.NET/ShortestPath/AStar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static ShortestPathResult GetShortestPath(IDijkstraGraph graph, uint from
1111
var path = new Dictionary<uint, uint>();
1212
var distance = new Dictionary<uint, int> {[from] = 0};
1313
var d = new Dictionary<uint, int> {[from] = 0};
14-
var q = new SortedSet<uint>(new[] {from}, new NodeComparer(distance, heuristic));
14+
var q = new SortedSet<uint>(new[] {from}, new HeuristicNodeComparer(distance, heuristic));
1515
var current = new HashSet<uint>();
1616

1717
int Distance(uint key)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Dijkstra.NET.ShortestPath
5+
{
6+
internal class HeuristicNodeComparer : NodeComparer
7+
{
8+
private readonly Func<uint, uint, int> _heuristic;
9+
10+
public HeuristicNodeComparer(IDictionary<uint, int> distance, Func<uint, uint, int> heuristic) : base(distance)
11+
{
12+
_heuristic = heuristic;
13+
}
14+
15+
public override int Compare(uint x, uint y)
16+
{
17+
int xDistance = _distance.ContainsKey(x) ? _distance[x] + _heuristic(x, y) : Int32.MaxValue;
18+
int yDistance = _distance.ContainsKey(y) ? _distance[y] + _heuristic(y, x) : Int32.MaxValue;
19+
20+
int comparer = xDistance.CompareTo(yDistance);
21+
22+
if (comparer == 0)
23+
{
24+
return x.CompareTo(y);
25+
}
26+
27+
return comparer;
28+
}
29+
}
30+
}

src/Dijkstra.NET/ShortestPath/NodeComparer.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,17 @@ namespace Dijkstra.NET.ShortestPath
55
{
66
internal class NodeComparer : IComparer<uint>
77
{
8-
private readonly IDictionary<uint, int> _distance;
9-
private readonly Func<uint, uint, int> _heuristic;
8+
protected readonly IDictionary<uint, int> _distance;
109

1110
public NodeComparer(IDictionary<uint, int> distance)
1211
{
1312
_distance = distance;
14-
_heuristic = null;
15-
}
16-
public NodeComparer(IDictionary<uint, int> distance, Func<uint, uint, int> heuristic)
17-
{
18-
_distance = distance;
19-
_heuristic = heuristic;
2013
}
2114

22-
public int Compare(uint x, uint y)
15+
public virtual int Compare(uint x, uint y)
2316
{
24-
int xDistance = _distance.ContainsKey(x) ?
25-
_heuristic is null ? _distance[x] : _distance[x] + _heuristic(x, y)
26-
: Int32.MaxValue;
27-
int yDistance = _distance.ContainsKey(y) ?
28-
_heuristic is null ? _distance[y] : _distance[y] + _heuristic(y, x)
29-
: Int32.MaxValue;
17+
int xDistance = _distance.ContainsKey(x) ? _distance[x] : Int32.MaxValue;
18+
int yDistance = _distance.ContainsKey(y) ? _distance[y] : Int32.MaxValue;
3019

3120
int comparer = xDistance.CompareTo(yDistance);
3221

@@ -38,4 +27,4 @@ public int Compare(uint x, uint y)
3827
return comparer;
3928
}
4029
}
41-
}
30+
}

0 commit comments

Comments
 (0)