Commit 6d4652c7 authored by Jean de Klerk's avatar Jean de Klerk Committed by Alan Donovan

digraph: add transpose

Change-Id: Idd6299fd7397840000139ce4fbea43a95ee76b1e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/186797Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a4e10e6d
......@@ -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)
......
......@@ -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"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment