path: use node ID in dominator tree for node retrieval
* path: use node ID in dominator tree for node retrieval
Updates
* graph/path: use DominatorOf and DominatedBy in test case
Change written by in
MODIFY
graph/path/control_flow_lt.go
graph/path/control_flow_lt.go
@@ -197,13 +197,13 @@ type DominatorTree struct {
// Root returns the root of the tree.
func (d DominatorTree) Root() graph.Node { return d.root }
-// DominatorOf returns the immediate dominator of n.
-func (d DominatorTree) DominatorOf(n graph.Node) graph.Node {
- return d.dominatorOf[n.ID()]
+// DominatorOf returns the immediate dominator of the node with the given ID.
+func (d DominatorTree) DominatorOf(id int64) graph.Node {
+ return d.dominatorOf[id]
}
-// DominatedBy returns a slice of all nodes immediately dominated by n.
-// Elements of the slice are retained by the DominatorTree.
-func (d DominatorTree) DominatedBy(n graph.Node) []graph.Node {
- return d.dominatedBy[n.ID()]
+// DominatedBy returns a slice of all nodes immediately dominated by the node
+// with the given ID. Elements of the slice are retained by the DominatorTree.
+func (d DominatorTree) DominatedBy(id int64) []graph.Node {
+ return d.dominatedBy[id]
}

MODIFY
graph/path/control_flow_test.go
graph/path/control_flow_test.go
@@ -146,22 +146,23 @@ func TestDominators(t *testing.T) {
{"DominatorsSLT", DominatorsSLT},
} {
got := alg.fn(test.n, g)
- if !reflect.DeepEqual(got.root, test.want.root) {
- t.Errorf("unexpected dominator tree root from %s: got:%v want:%v",
- alg.name, got.root, test.want.root)
+ for q, want := range test.want.dominatorOf {
+ node := got.DominatorOf(q)
+ if node != want {
+ t.Errorf("unexpected dominator tree result from %s dominated of %v: got:%v want:%v",
+ alg.name, q, node, want)
}
-
- if !reflect.DeepEqual(got.dominatorOf, test.want.dominatorOf) {
- t.Errorf("unexpected dominator tree from %s: got:%v want:%v",
- alg.name, got.dominatorOf, test.want.dominatorOf)
}
for _, nodes := range got.dominatedBy {
sort.Sort(ordered.ByID(nodes))
}
- if !reflect.DeepEqual(got.dominatedBy, test.want.dominatedBy) {
- t.Errorf("unexpected dominator tree from %s: got:%v want:%v",
- alg.name, got.dominatedBy, test.want.dominatedBy)
+ for q, want := range test.want.dominatedBy {
+ nodes := got.DominatedBy(q)
+ if !reflect.DeepEqual(nodes, want) {
+ t.Errorf("unexpected dominator tree result from %s dominated by %v: got:%v want:%v",
+ alg.name, q, nodes, want)
+ }
}
}
}

path: use node ID in dominator tree for node retrieval (#791)

* path: use node ID in dominator tree for node retrieval

Updates #31.

* graph/path: use DominatorOf and DominatedBy in test case

Change written by @kortschak in https://github.com/gonum/gonum/pull/791#issuecomment-453787216