pgraph: Implement FilterGraph in terms of FilterGraphWithFn
Simplify the code base by having one core implementation.
This commit is contained in:
@@ -501,23 +501,10 @@ func (g *Graph) DFS(start Vertex) []Vertex {
|
|||||||
|
|
||||||
// FilterGraph builds a new graph containing only vertices from the list.
|
// FilterGraph builds a new graph containing only vertices from the list.
|
||||||
func (g *Graph) FilterGraph(vertices []Vertex) (*Graph, error) {
|
func (g *Graph) FilterGraph(vertices []Vertex) (*Graph, error) {
|
||||||
newGraph, err := NewGraph(g.Name)
|
fn := func(v Vertex) (bool, error) {
|
||||||
if err != nil {
|
return VertexContains(v, vertices), nil
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
for k1, x := range g.adjacency {
|
return g.FilterGraphWithFn(fn)
|
||||||
contains := VertexContains(k1, vertices)
|
|
||||||
if contains {
|
|
||||||
newGraph.AddVertex(k1)
|
|
||||||
}
|
|
||||||
for k2, e := range x {
|
|
||||||
//log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name)
|
|
||||||
if contains && VertexContains(k2, vertices) {
|
|
||||||
newGraph.AddEdge(k1, k2, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newGraph, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterGraphWithFn builds a new graph containing only vertices which match. It
|
// FilterGraphWithFn builds a new graph containing only vertices which match. It
|
||||||
|
|||||||
Reference in New Issue
Block a user