зеркало из https://github.com/golang/tools.git
cmd/digraph: use node instead of label ubiquitously
Currently, vertices are referred to as nodes in (most) user-facing documentation, and labels in the code itself (and some user-facing documentation). This CL changes all references to vertices to use the word node. Change-Id: I0a409c08122f198b11ff891cbea24b41aba89e40 Reviewed-on: https://go-review.googlesource.com/c/tools/+/200938 Reviewed-by: Jay Conrod <jayconrod@google.com> Run-TryBot: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Родитель
7defa796fe
Коммит
9525425c6f
|
@ -143,11 +143,11 @@ func main() {
|
||||||
type nodelist []string
|
type nodelist []string
|
||||||
|
|
||||||
func (l nodelist) println(sep string) {
|
func (l nodelist) println(sep string) {
|
||||||
for i, label := range l {
|
for i, node := range l {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
fmt.Fprint(stdout, sep)
|
fmt.Fprint(stdout, sep)
|
||||||
}
|
}
|
||||||
fmt.Fprint(stdout, label)
|
fmt.Fprint(stdout, node)
|
||||||
}
|
}
|
||||||
fmt.Fprintln(stdout)
|
fmt.Fprintln(stdout)
|
||||||
}
|
}
|
||||||
|
@ -155,30 +155,30 @@ func (l nodelist) println(sep string) {
|
||||||
type nodeset map[string]bool
|
type nodeset map[string]bool
|
||||||
|
|
||||||
func (s nodeset) sort() nodelist {
|
func (s nodeset) sort() nodelist {
|
||||||
labels := make(nodelist, len(s))
|
nodes := make(nodelist, len(s))
|
||||||
var i int
|
var i int
|
||||||
for label := range s {
|
for node := range s {
|
||||||
labels[i] = label
|
nodes[i] = node
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.Strings(labels)
|
sort.Strings(nodes)
|
||||||
return labels
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s nodeset) addAll(x nodeset) {
|
func (s nodeset) addAll(x nodeset) {
|
||||||
for label := range x {
|
for node := range x {
|
||||||
s[label] = true
|
s[node] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A graph maps nodes to the non-nil set of their immediate successors.
|
// A graph maps nodes to the non-nil set of their immediate successors.
|
||||||
type graph map[string]nodeset
|
type graph map[string]nodeset
|
||||||
|
|
||||||
func (g graph) addNode(label string) nodeset {
|
func (g graph) addNode(node string) nodeset {
|
||||||
edges := g[label]
|
edges := g[node]
|
||||||
if edges == nil {
|
if edges == nil {
|
||||||
edges = make(nodeset)
|
edges = make(nodeset)
|
||||||
g[label] = edges
|
g[node] = edges
|
||||||
}
|
}
|
||||||
return edges
|
return edges
|
||||||
}
|
}
|
||||||
|
@ -193,11 +193,11 @@ func (g graph) addEdges(from string, to ...string) {
|
||||||
|
|
||||||
func (g graph) reachableFrom(roots nodeset) nodeset {
|
func (g graph) reachableFrom(roots nodeset) nodeset {
|
||||||
seen := make(nodeset)
|
seen := make(nodeset)
|
||||||
var visit func(label string)
|
var visit func(node string)
|
||||||
visit = func(label string) {
|
visit = func(node string) {
|
||||||
if !seen[label] {
|
if !seen[node] {
|
||||||
seen[label] = true
|
seen[node] = true
|
||||||
for e := range g[label] {
|
for e := range g[node] {
|
||||||
visit(e)
|
visit(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,10 +210,10 @@ func (g graph) reachableFrom(roots nodeset) nodeset {
|
||||||
|
|
||||||
func (g graph) transpose() graph {
|
func (g graph) transpose() graph {
|
||||||
rev := make(graph)
|
rev := make(graph)
|
||||||
for label, edges := range g {
|
for node, edges := range g {
|
||||||
rev.addNode(label)
|
rev.addNode(node)
|
||||||
for succ := range edges {
|
for succ := range edges {
|
||||||
rev.addEdges(succ, label)
|
rev.addEdges(succ, node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rev
|
return rev
|
||||||
|
@ -225,30 +225,30 @@ func (g graph) sccs() []nodeset {
|
||||||
// Forward pass.
|
// Forward pass.
|
||||||
S := make(nodelist, 0, len(g)) // postorder stack
|
S := make(nodelist, 0, len(g)) // postorder stack
|
||||||
seen := make(nodeset)
|
seen := make(nodeset)
|
||||||
var visit func(label string)
|
var visit func(node string)
|
||||||
visit = func(label string) {
|
visit = func(node string) {
|
||||||
if !seen[label] {
|
if !seen[node] {
|
||||||
seen[label] = true
|
seen[node] = true
|
||||||
for e := range g[label] {
|
for e := range g[node] {
|
||||||
visit(e)
|
visit(e)
|
||||||
}
|
}
|
||||||
S = append(S, label)
|
S = append(S, node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for label := range g {
|
for node := range g {
|
||||||
visit(label)
|
visit(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reverse pass.
|
// Reverse pass.
|
||||||
rev := g.transpose()
|
rev := g.transpose()
|
||||||
var scc nodeset
|
var scc nodeset
|
||||||
seen = make(nodeset)
|
seen = make(nodeset)
|
||||||
var rvisit func(label string)
|
var rvisit func(node string)
|
||||||
rvisit = func(label string) {
|
rvisit = func(node string) {
|
||||||
if !seen[label] {
|
if !seen[node] {
|
||||||
seen[label] = true
|
seen[node] = true
|
||||||
scc[label] = true
|
scc[node] = true
|
||||||
for e := range rev[label] {
|
for e := range rev[node] {
|
||||||
rvisit(e)
|
rvisit(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -377,8 +377,8 @@ func digraph(cmd string, args []string) error {
|
||||||
return fmt.Errorf("usage: digraph nodes")
|
return fmt.Errorf("usage: digraph nodes")
|
||||||
}
|
}
|
||||||
nodes := make(nodeset)
|
nodes := make(nodeset)
|
||||||
for label := range g {
|
for node := range g {
|
||||||
nodes[label] = true
|
nodes[node] = true
|
||||||
}
|
}
|
||||||
nodes.sort().println("\n")
|
nodes.sort().println("\n")
|
||||||
|
|
||||||
|
@ -387,12 +387,12 @@ func digraph(cmd string, args []string) error {
|
||||||
return fmt.Errorf("usage: digraph degree")
|
return fmt.Errorf("usage: digraph degree")
|
||||||
}
|
}
|
||||||
nodes := make(nodeset)
|
nodes := make(nodeset)
|
||||||
for label := range g {
|
for node := range g {
|
||||||
nodes[label] = true
|
nodes[node] = true
|
||||||
}
|
}
|
||||||
rev := g.transpose()
|
rev := g.transpose()
|
||||||
for _, label := range nodes.sort() {
|
for _, node := range nodes.sort() {
|
||||||
fmt.Fprintf(stdout, "%d\t%d\t%s\n", len(rev[label]), len(g[label]), label)
|
fmt.Fprintf(stdout, "%d\t%d\t%s\n", len(rev[node]), len(g[node]), node)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "transpose":
|
case "transpose":
|
||||||
|
@ -412,7 +412,7 @@ func digraph(cmd string, args []string) error {
|
||||||
|
|
||||||
case "succs", "preds":
|
case "succs", "preds":
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("usage: digraph %s <label> ...", cmd)
|
return fmt.Errorf("usage: digraph %s <node> ...", cmd)
|
||||||
}
|
}
|
||||||
g := g
|
g := g
|
||||||
if cmd == "preds" {
|
if cmd == "preds" {
|
||||||
|
@ -430,7 +430,7 @@ func digraph(cmd string, args []string) error {
|
||||||
|
|
||||||
case "forward", "reverse":
|
case "forward", "reverse":
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("usage: digraph %s <label> ...", cmd)
|
return fmt.Errorf("usage: digraph %s <node> ...", cmd)
|
||||||
}
|
}
|
||||||
roots := make(nodeset)
|
roots := make(nodeset)
|
||||||
for _, root := range args {
|
for _, root := range args {
|
||||||
|
@ -485,14 +485,14 @@ func digraph(cmd string, args []string) error {
|
||||||
|
|
||||||
case "scc":
|
case "scc":
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return fmt.Errorf("usage: digraph scc <label>")
|
return fmt.Errorf("usage: digraph scc <node>")
|
||||||
}
|
}
|
||||||
label := args[0]
|
node := args[0]
|
||||||
if g[label] == nil {
|
if g[node] == nil {
|
||||||
return fmt.Errorf("no such node %q", label)
|
return fmt.Errorf("no such node %q", node)
|
||||||
}
|
}
|
||||||
for _, scc := range g.sccs() {
|
for _, scc := range g.sccs() {
|
||||||
if scc[label] {
|
if scc[node] {
|
||||||
scc.sort().println("\n")
|
scc.sort().println("\n")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче