1
0
mirror of https://github.com/golang/go synced 2024-11-18 07:04:52 -07:00

digraph: add transpose

Change-Id: Idd6299fd7397840000139ce4fbea43a95ee76b1e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/186797
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Jean de Klerk 2019-07-18 14:20:30 -06:00 committed by Alan Donovan
parent a4e10e6d0e
commit 6d4652c779
2 changed files with 20 additions and 0 deletions

View File

@ -17,6 +17,8 @@ The support commands are:
the set of all nodes
degree
the in-degree and out-degree of each node
transpose
the reverse of the input edges
preds <node> ...
the set of immediate predecessors of the specified nodes
succs <node> ...
@ -101,6 +103,8 @@ The support commands are:
the set of all nodes
degree
the in-degree and out-degree of each node
transpose
the reverse of the input edges
preds <node> ...
the set of immediate predecessors of the specified nodes
succs <node> ...
@ -391,6 +395,21 @@ func digraph(cmd string, args []string) error {
fmt.Fprintf(stdout, "%d\t%d\t%s\n", len(rev[label]), len(g[label]), label)
}
case "transpose":
if len(args) != 0 {
return fmt.Errorf("usage: digraph tranpose")
}
var revEdges []string
for node, succs := range g.transpose() {
for succ := range succs {
revEdges = append(revEdges, fmt.Sprintf("%s %s", node, succ))
}
}
sort.Strings(revEdges) // make output deterministic
for _, e := range revEdges {
fmt.Fprintln(stdout, e)
}
case "succs", "preds":
if len(args) == 0 {
return fmt.Errorf("usage: digraph %s <label> ...", cmd)

View File

@ -38,6 +38,7 @@ d c
}{
{"nodes", g1, "nodes", nil, "belt\nhat\njacket\npants\nshirt\nshoes\nshorts\nsocks\nsweater\ntie\n"},
{"reverse", g1, "reverse", []string{"jacket"}, "jacket\nshirt\nsweater\n"},
{"transpose", g1, "transpose", nil, "belt pants\njacket sweater\npants shorts\nshoes pants\nshoes socks\nsweater shirt\ntie shirt\n"},
{"forward", g1, "forward", []string{"socks"}, "shoes\nsocks\n"},
{"forward multiple args", g1, "forward", []string{"socks", "sweater"}, "jacket\nshoes\nsocks\nsweater\n"},
{"scss", g2, "sccs", nil, "a\nb\nc d\n"},