Skip to content

Commit 593a693

Browse files
committed
change distanceMap interface
1 parent dc9a64c commit 593a693

File tree

9 files changed

+80
-79
lines changed

9 files changed

+80
-79
lines changed

Assets/Editor/Test/Graph/BatchedGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void Performance()
126126
var goal = new Vector2Int(UnityEngine.Random.Range(0, board.Width), UnityEngine.Random.Range(0, board.Height));
127127
var path = graph.GetPath(start, goal);
128128

129-
var completeMap = new DistanceMap<Vector2Int>(board, start, goal);
129+
var completeMap = new DistanceMap<Vector2Int>(board, start);
130130
var paths = new Vector2Int[99][];
131131
completeMap.SearchPaths(goal, paths, out var writtenCount);
132132
var completePath = paths[0];

Assets/Editor/Test/Graph/DistanceMap.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ public void LimitedDistances()
3939
public void LimitedDistances2()
4040
{
4141
var board = new Board(10, 10);
42-
var distanceMap = new DistanceMap<Vector2Int>(board, new Vector2Int(5, 5), new Vector2Int(9, 6));
42+
var distanceMap = new DistanceMap<Vector2Int>(board, new Vector2Int(5, 5));
4343
var paths = new Vector2Int[10][];
4444
distanceMap.SearchPaths(new Vector2Int(9, 6), paths, out var writtenCount);
4545
Assert.AreNotEqual(0, writtenCount);
46-
distanceMap.SearchPaths(new Vector2Int(9, 7), paths, out writtenCount);
47-
Assert.AreEqual(0, writtenCount);
46+
Assert.IsFalse(distanceMap.Distances.ContainsKey(new Vector2Int(9, 7)));
4847
}
4948

5049
[Test]

Assets/Package/Runtime/Graph/AStarSearch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public readonly struct AStarSearch<T>
1111
{
1212
public readonly IGraph<T> graph;
1313
readonly System.Func<T, T, double> heuristicFunction;
14-
public readonly DistanceMap<T> memo;
14+
public readonly DistanceMapCore<T> memo;
1515
public readonly T Start => memo.Start;
1616
readonly List<T> tasksToResume;
1717

@@ -20,7 +20,7 @@ public AStarSearch(IGraph<T> graph, in T start, System.Func<T, T, double> heuris
2020
this.heuristicFunction = heuristicFunction;
2121
this.graph = graph;
2222

23-
memo = new DistanceMap<T>(
23+
memo = new DistanceMapCore<T>(
2424
start,
2525
new Dictionary<T, double>(),
2626
new Dictionary<T, List<T>>());

Assets/Package/Runtime/Graph/BatchedGraph.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public readonly T[] GetPath(T goal)
8181
{
8282
if (owner.heuristicFunction == null)
8383
{
84-
var distanceMap = new DistanceMap<T>(owner.graph, start, new T[] { goal });
84+
var distanceMap = new DistanceMap<T>(owner.graph, start);
8585
var path = distanceMap.SearchPath(goal);
8686
return path;
8787
}
@@ -152,8 +152,9 @@ public BatchedGraph(IGraph<T> graph, in T startNode, double batchRadius, double
152152
continue;
153153
}
154154

155-
var newBatch = new Batch(new DistanceMap<T>(graph, root, batchRadius));
156-
155+
var d = new DistanceMap<T>(graph, root);
156+
d.SolveWithin(batchRadius);
157+
var newBatch = new Batch(d);
157158
batches.Add(newBatch);
158159
nodeBatchMap[root] = newBatch;
159160

@@ -199,7 +200,7 @@ public BatchedGraph(IGraph<T> graph, in T startNode, double batchRadius, double
199200
}
200201
if (batchEdgeLength > batchRadius)
201202
{
202-
newBatch.distanceMap.Solve(null, batchEdgeLength);
203+
newBatch.distanceMap.SolveWithin(batchEdgeLength);
203204
}
204205
}
205206

@@ -234,7 +235,8 @@ public BatchedGraph(IGraph<T> graph, in T startNode, double batchRadius, double
234235
var unlinkedBatches = new List<Batch>();
235236
foreach (var it in batches)
236237
{
237-
var map = new DistanceMap<Batch>(batchGraph, it, startNodeBatch);
238+
var map = new DistanceMap<Batch>(batchGraph, it);
239+
map.SolveAny(new[] { startNodeBatch });
238240
if (map.Distances.ContainsKey(startNodeBatch))
239241
{
240242
linkedBatches.Add(it.Root);
@@ -252,7 +254,7 @@ public BatchedGraph(IGraph<T> graph, in T startNode, double batchRadius, double
252254
}
253255
foreach (var it in unlinkedBatches)
254256
{
255-
it.distanceMap.Solve(linkedBatches.Memory.Span);
257+
it.distanceMap.SolveAny(linkedBatches.Memory.Span);
256258
var linked = false;
257259
foreach (var linkedBatch in linkedBatches.Memory.Span)
258260
{
@@ -285,7 +287,8 @@ T[] SearchRootToNearestRoot(in T start, out AStarSearch<T> aStar)
285287
if (heuristicFunction == null)
286288
{
287289
aStar = default;
288-
var startToBatch = new DistanceMap<T>(graph, start, roots);
290+
var startToBatch = new DistanceMap<T>(graph, start);
291+
startToBatch.SolveAny(roots);
289292

290293
T? firstRoot = default;
291294
var foundStartToFirstRootPath = false;
@@ -317,7 +320,7 @@ void GetBatchToGoalPath(Batch startBatch, Batch lastBatch, T goal, ref PathCombi
317320
Batch[] path;
318321
if (heuristicFunction == null)
319322
{
320-
var batchDistance = new DistanceMap<Batch>(batchGraph, startBatch, lastBatch);
323+
var batchDistance = new DistanceMap<Batch>(batchGraph, startBatch);
321324
path = batchDistance.SearchPath(lastBatch);
322325
}
323326
else

Assets/Package/Runtime/Graph/BatchedGraphU.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ public BatchedGraphU(IGraphU<T> graph, in T startNode, double batchRadius, doubl
244244
var unlinkedBatches = new List<Batch>();
245245
foreach (var it in batches)
246246
{
247-
var map = new DistanceMap<Batch>(batchGraph, it, startNodeBatch);
247+
var map = new DistanceMap<Batch>(batchGraph, it);
248+
map.SolveAny(new[] { startNodeBatch });
248249
if (map.Distances.ContainsKey(startNodeBatch))
249250
{
250251
linkedBatches[linkedBatchesWrittenCount] = it.Root;
@@ -327,7 +328,7 @@ void GetBatchToGoalPath(Batch startBatch, Batch lastBatch, T goal, ref PathCombi
327328
Batch[] path;
328329
if (heuristicFunction == null)
329330
{
330-
var batchDistance = new DistanceMap<Batch>(batchGraph, startBatch, lastBatch);
331+
var batchDistance = new DistanceMap<Batch>(batchGraph, startBatch);
331332
path = batchDistance.SearchPath(lastBatch);
332333
}
333334
else

Assets/Package/Runtime/Graph/DistanceMap.cs

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,32 @@ namespace TSKT
88
{
99
public readonly struct DistanceMap<T>
1010
{
11-
readonly public T Start { get; }
12-
readonly public Dictionary<T, double> Distances { get; }
13-
readonly public Dictionary<T, List<T>> ReversedEdges { get; }
11+
readonly DistanceMapCore<T> core;
12+
public readonly T Start => core.Start;
13+
public readonly IReadOnlyDictionary<T, double> Distances => core.Distances;
14+
public readonly IReadOnlyDictionary<T, List<T>> ReversedEdges => core.ReversedEdges;
1415

15-
readonly Graphs.PriorityQueue<T>? tasks;
16-
readonly IGraph<T>? graph;
16+
readonly Graphs.PriorityQueue<T> tasks;
17+
readonly IGraph<T> graph;
1718

18-
readonly public bool Finished => (tasks == null || tasks.Count == 0);
19+
public readonly bool Completed => tasks.Count == 0;
1920

20-
public DistanceMap(in T start, Dictionary<T, double> distances, Dictionary<T, List<T>> reversedEdges)
21-
{
22-
Start = start;
23-
Distances = distances;
24-
ReversedEdges = reversedEdges;
25-
26-
graph = null;
27-
tasks = null;
28-
}
29-
30-
public DistanceMap(IGraph<T> graph, in T start, double maxDistance = double.PositiveInfinity)
31-
: this(graph, start, null, maxDistance)
32-
{
33-
}
34-
35-
public DistanceMap(IGraph<T> graph, in T start, in T goal, double maxDistance = double.PositiveInfinity)
36-
: this(graph, start, new[] { goal }, maxDistance)
37-
{
38-
}
3921

40-
public DistanceMap(IGraph<T> graph, in T start, ReadOnlySpan<T> goals, double maxDistance = double.PositiveInfinity)
22+
public DistanceMap(IGraph<T> graph, in T start)
4123
{
24+
core = new(start, new Dictionary<T, double>(), new Dictionary<T, List<T>>());
4225
this.graph = graph;
43-
Start = start;
44-
Distances = new Dictionary<T, double>();
45-
ReversedEdges = new Dictionary<T, List<T>>();
4626
tasks = new Graphs.PriorityQueue<T>();
47-
tasks.Enqueue(OrderKeyConvert.ToUint64(0.0), Start);
48-
Distances.Add(Start, 0.0);
49-
50-
Solve(goals, maxDistance);
27+
tasks.Enqueue(OrderKeyConvert.ToUint64(0.0), start);
28+
core.Distances.Add(start, 0.0);
5129
}
52-
public readonly void Solve(T[]? goals, double maxDistance = double.PositiveInfinity)
30+
public readonly void SolveWithin(double maxDistance)
5331
{
54-
if (goals == null)
55-
{
56-
Solve(Span<T>.Empty, maxDistance);
57-
}
58-
else
59-
{
60-
Solve(goals.AsSpan(), maxDistance);
61-
}
32+
SolveAny(Span<T>.Empty, maxDistance);
6233
}
6334

64-
public readonly void Solve(ReadOnlySpan<T> goals, double maxDistance = double.PositiveInfinity)
35+
public readonly void SolveAny(ReadOnlySpan<T> goals, double maxDistance = double.PositiveInfinity)
6536
{
66-
if (tasks == null)
67-
{
68-
throw new System.NullReferenceException();
69-
}
70-
if (graph == null)
71-
{
72-
throw new System.NullReferenceException();
73-
}
74-
7537
foreach (var it in goals)
7638
{
7739
if (Distances.ContainsKey(it))
@@ -138,10 +100,10 @@ public readonly void Solve(ReadOnlySpan<T> goals, double maxDistance = double.Po
138100
else
139101
{
140102
var nearNodes = new List<T>() { currentNode };
141-
ReversedEdges.Add(nextNode, nearNodes);
103+
core.ReversedEdges.Add(nextNode, nearNodes);
142104
}
143105

144-
Distances[nextNode] = startToNextNodeDistance;
106+
core.Distances[nextNode] = startToNextNodeDistance;
145107
tasks.Enqueue(OrderKeyConvert.ToUint64(startToNextNodeDistance), nextNode);
146108
}
147109
}
@@ -152,6 +114,39 @@ public readonly void Solve(ReadOnlySpan<T> goals, double maxDistance = double.Po
152114
tasks.Enqueue(OrderKeyConvert.ToUint64(distance), node);
153115
}
154116
}
117+
readonly void Solve(T goal)
118+
{
119+
SolveAny(new[] { goal });
120+
}
121+
public readonly void SearchPaths(T goal, Span<T[]> destination, out int writtenCount)
122+
{
123+
Solve(goal);
124+
core.SearchPaths(goal, destination, out writtenCount);
125+
}
126+
public readonly T[] SearchPath(in T goal)
127+
{
128+
Solve(goal);
129+
return core.SearchPath(goal);
130+
}
131+
public readonly void SearchPath(in T goal, ref List<T> result)
132+
{
133+
Solve(goal);
134+
core.SearchPath(goal, ref result);
135+
}
136+
}
137+
138+
public readonly struct DistanceMapCore<T>
139+
{
140+
public readonly T Start { get; }
141+
public readonly Dictionary<T, double> Distances { get; }
142+
public readonly Dictionary<T, List<T>> ReversedEdges { get; }
143+
144+
public DistanceMapCore(in T start, Dictionary<T, double> distances, Dictionary<T, List<T>> reversedEdges)
145+
{
146+
Start = start;
147+
Distances = distances;
148+
ReversedEdges = reversedEdges;
149+
}
155150

156151
public readonly void SearchPaths(T goal, Span<T[]> destination, out int writtenCount)
157152
{

Assets/Package/Runtime/Graph/DistanceMapU.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace TSKT
99
{
10-
public class DistanceMapU<T> where T : unmanaged, IEquatable<T>
10+
public readonly struct DistanceMapU<T> where T : unmanaged, IEquatable<T>
1111
{
1212
readonly DistanceMapUCore<T> core;
1313
public T Start => core.Start;
@@ -33,12 +33,12 @@ public DistanceMapU(IGraphU<T> graph, in T start)
3333
tasks.Enqueue(OrderKeyConvert.ToUint64(0.0), start);
3434
}
3535

36-
public void SolveWithin(double maxDistance)
36+
public readonly void SolveWithin(double maxDistance)
3737
{
3838
SolveAny(Span<T>.Empty, maxDistance);
3939
}
4040

41-
public void SolveAny(ReadOnlySpan<T> goals, double maxDistance = double.PositiveInfinity)
41+
public readonly void SolveAny(ReadOnlySpan<T> goals, double maxDistance = double.PositiveInfinity)
4242
{
4343
foreach (var it in goals)
4444
{
@@ -129,24 +129,24 @@ public void SolveAny(ReadOnlySpan<T> goals, double maxDistance = double.Positive
129129
continueNodes.Dispose();
130130
}
131131

132-
void Solve(T goal)
132+
readonly void Solve(T goal)
133133
{
134134
Span<T> goals = stackalloc T[] { goal };
135135
SolveAny(goals);
136136
}
137-
public void SearchPaths(T goal, Span<T[]> destination, out int writtenCount)
137+
public readonly void SearchPaths(T goal, Span<T[]> destination, out int writtenCount)
138138
{
139139
Solve(goal);
140140
core.SearchPaths(goal, destination, out writtenCount);
141141
}
142142

143-
public T[] SearchPath(in T goal)
143+
public readonly T[] SearchPath(in T goal)
144144
{
145145
Solve(goal);
146146
return core.SearchPath(goal);
147147
}
148148

149-
public void SearchPath(in T goal, ref List<T> result)
149+
public readonly void SearchPath(in T goal, ref List<T> result)
150150
{
151151
Solve(goal);
152152
core.SearchPath(goal, ref result);

Assets/Package/Runtime/Graph/Graph.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ public bool TryGetWeight(T start, T end, out double result)
150150

151151
public DistanceMap<T> ComputeDistancesFrom(T node, double maxDistance = double.PositiveInfinity)
152152
{
153-
return new DistanceMap<T>(this, node, maxDistance);
153+
var result = new DistanceMap<T>(this, node);
154+
result.SolveWithin(maxDistance);
155+
return result;
154156
}
155157
}
156158
}

Assets/Package/Runtime/TSKT.Math.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "TSKT.Math",
33
"rootNamespace": "",
44
"references": [
5-
"GUID:d8b63aba1907145bea998dd612889d6b"
5+
"GUID:d8b63aba1907145bea998dd612889d6b",
6+
"GUID:e0cd26848372d4e5c891c569017e11f1"
67
],
78
"includePlatforms": [],
89
"excludePlatforms": [],

0 commit comments

Comments
 (0)