dag: add EdgesFrom, EdgesTo, needs tests
This commit is contained in:
parent
b9b23e8483
commit
4e8e6cd661
26
dag/graph.go
26
dag/graph.go
|
@ -48,6 +48,32 @@ func (g *Graph) Edges() []Edge {
|
|||
return result
|
||||
}
|
||||
|
||||
// EdgesFrom returns the list of edges from the given source.
|
||||
func (g *Graph) EdgesFrom(v Vertex) []Edge {
|
||||
var result []Edge
|
||||
from := hashcode(v)
|
||||
for _, e := range g.Edges() {
|
||||
if hashcode(e.Source()) == from {
|
||||
result = append(result, e)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// EdgesTo returns the list of edges to the given target.
|
||||
func (g *Graph) EdgesTo(v Vertex) []Edge {
|
||||
var result []Edge
|
||||
search := hashcode(v)
|
||||
for _, e := range g.Edges() {
|
||||
if hashcode(e.Target()) == search {
|
||||
result = append(result, e)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// HasVertex checks if the given Vertex is present in the graph.
|
||||
func (g *Graph) HasVertex(v Vertex) bool {
|
||||
return g.vertices.Include(v)
|
||||
|
|
|
@ -50,7 +50,8 @@ func (t *CBDEdgeTransformer) Transform(g *Graph) error {
|
|||
}
|
||||
|
||||
// Find the destroy edge. There should only be one.
|
||||
for _, e := range g.DownEdges(v).List() {
|
||||
for _, e := range g.EdgesTo(v) {
|
||||
log.Printf("WHAT: %#v", e)
|
||||
// Not a destroy edge, ignore it
|
||||
de, ok := e.(*DestroyEdge)
|
||||
if !ok {
|
||||
|
|
|
@ -7,7 +7,8 @@ import (
|
|||
|
||||
func TestCBDEdgeTransformer(t *testing.T) {
|
||||
g := Graph{Path: RootModulePath}
|
||||
g.Add(&graphNodeDestroyerTest{AddrString: "test.A"})
|
||||
g.Add(&graphNodeCreatorTest{AddrString: "test.A"})
|
||||
g.Add(&graphNodeDestroyerTest{AddrString: "test.A", CBD: true})
|
||||
g.Add(&graphNodeDestroyerTest{AddrString: "test.B"})
|
||||
|
||||
{
|
||||
|
@ -34,7 +35,9 @@ func TestCBDEdgeTransformer(t *testing.T) {
|
|||
}
|
||||
|
||||
const testTransformCBDEdgeBasicStr = `
|
||||
test.A
|
||||
test.A (destroy)
|
||||
test.A
|
||||
test.B (destroy)
|
||||
test.B (destroy)
|
||||
`
|
||||
|
|
Loading…
Reference in New Issue