-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow.go
More file actions
88 lines (81 loc) · 2.78 KB
/
Copy pathShow.go
File metadata and controls
88 lines (81 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 MaIII Themd
package dijkstra
import (
"fmt"
"io"
"strings"
)
// Show writes a human-readable dump of the whole graph -- every vertex
// with its coordinates, cumulative weight, visited flag and outgoing
// edges -- to w. Unlike Print (which targets stdout) it works with any
// io.Writer: a file, a bytes.Buffer in a test, or a log. Use ShowFunc
// to override the per-vertex formatting.
func (g *StGraph) Show(w io.Writer) {
g.ShowFunc(w, nil)
}
// ShowFunc writes the graph to w, formatting each vertex with the
// caller-supplied line function -- so you can customise the output
// without re-implementing the iteration or the locking. Passing nil
// uses DefaultVertexLine (the same format as Show and Print).
//
// line receives a copy of each vertex and should only format it: the
// graph's read lock is held while it runs, so do not call back into the
// graph (and do not mutate the vertex's Edges slice) from inside it.
func (g *StGraph) ShowFunc(w io.Writer, line func(v StVertex) string) {
if line == nil {
line = DefaultVertexLine
}
g.mu.RLock()
defer g.mu.RUnlock()
for i := range g.vertex {
_, _ = io.WriteString(w, line(g.vertex[i]))
}
}
// DefaultVertexLine is the formatter Show and Print use when no custom
// one is supplied. It is exported so a ShowFunc callback can reuse or
// extend it.
func DefaultVertexLine(v StVertex) string {
var b strings.Builder
fmt.Fprintf(&b, "Vertex %s (%.2f,%.2f), w = %.2f, visited %v\n",
v.Name, v.X, v.Y, v.Weight, v.Visited)
b.WriteString("\tEdge ")
for _, e := range v.Edges {
oneway := ""
if e.IsOneWay {
oneway = ", one-way"
}
fmt.Fprintf(&b, "(%s, %.2f%s) | ", e.ToVertexName, e.Weight, oneway)
}
b.WriteString("\n\n")
return b.String()
}
// ShowDijkstra writes the post-search state of every vertex (cumulative
// weight, parent, visited) together with only the edges on the
// shortest-path tree to w. Call it after DijkstraSearch, DijkstraRun or
// an A* search.
func (g *StGraph) ShowDijkstra(w io.Writer) {
g.mu.RLock()
defer g.mu.RUnlock()
for i := range g.vertex {
v := g.vertex[i]
fmt.Fprintf(w, "Vertex %s, w %.2f, (%.2f,%.2f), visited %v, parent %s\n",
v.Name, v.Weight, v.X, v.Y, v.Visited, v.Parent)
_, _ = io.WriteString(w, "\tEdge ")
for _, e := range v.Edges {
if e.isShort {
fmt.Fprintf(w, "(%s, %.2f) | ", e.ToVertexName, e.Weight)
}
}
_, _ = io.WriteString(w, "\n\n")
}
}
// PathString renders a path returned by DijkstraSearch / AStarSearch as
// "A -> B -> C (cost 6)", so callers don't have to loop over the slice
// just to print a result. An empty path returns "(no path)".
func PathString(path []StPath) string {
if len(path) == 0 {
return "(no path)"
}
return fmt.Sprintf("%s (cost %g)", joinArrow(path), path[len(path)-1].Cost)
}