From 6d4652c779c4add9e1a69db058dabafddba21c37 Mon Sep 17 00:00:00 2001 From: Jean de Klerk Date: Thu, 18 Jul 2019 14:20:30 -0600 Subject: [PATCH] digraph: add transpose Change-Id: Idd6299fd7397840000139ce4fbea43a95ee76b1e Reviewed-on: https://go-review.googlesource.com/c/tools/+/186797 Reviewed-by: Alan Donovan Run-TryBot: Alan Donovan TryBot-Result: Gobot Gobot --- cmd/digraph/digraph.go | 19 +++++++++++++++++++ cmd/digraph/digraph_test.go | 1 + 2 files changed, 20 insertions(+) diff --git a/cmd/digraph/digraph.go b/cmd/digraph/digraph.go index 73835d8713..4c682c3ecb 100644 --- a/cmd/digraph/digraph.go +++ b/cmd/digraph/digraph.go @@ -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 ... the set of immediate predecessors of the specified nodes succs ... @@ -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 ... the set of immediate predecessors of the specified nodes succs ... @@ -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