pgraph: Add a named error for detected cycles
This makes it useful to find when a topological sort failed.
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
package pgraph
|
package pgraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -26,6 +27,9 @@ import (
|
|||||||
"github.com/purpleidea/mgmt/util/errwrap"
|
"github.com/purpleidea/mgmt/util/errwrap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrNotAcyclic specifies that a particular graph was not found to be a dag.
|
||||||
|
var ErrNotAcyclic = errors.New("not a dag!")
|
||||||
|
|
||||||
// Graph is the graph structure in this library. The graph abstract data type
|
// Graph is the graph structure in this library. The graph abstract data type
|
||||||
// (ADT) is defined as follows:
|
// (ADT) is defined as follows:
|
||||||
// * The directed graph arrows point from left to right. ( -> )
|
// * The directed graph arrows point from left to right. ( -> )
|
||||||
@@ -599,7 +603,7 @@ func (g *Graph) TopologicalSort() ([]Vertex, error) { // kahn's algorithm
|
|||||||
if in > 0 {
|
if in > 0 {
|
||||||
for n := range g.adjacency[c] {
|
for n := range g.adjacency[c] {
|
||||||
if remaining[n] > 0 {
|
if remaining[n] > 0 {
|
||||||
return nil, fmt.Errorf("not a dag")
|
return nil, ErrNotAcyclic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -622,7 +626,7 @@ func (g *Graph) Reachability(a, b Vertex) ([]Vertex, error) {
|
|||||||
return nil, fmt.Errorf("empty vertex")
|
return nil, fmt.Errorf("empty vertex")
|
||||||
}
|
}
|
||||||
if _, err := g.TopologicalSort(); err != nil {
|
if _, err := g.TopologicalSort(); err != nil {
|
||||||
return nil, err // not a dag?
|
return nil, err // not a dag!
|
||||||
}
|
}
|
||||||
|
|
||||||
vertices := g.OutgoingGraphVertices(a) // what points away from a ?
|
vertices := g.OutgoingGraphVertices(a) // what points away from a ?
|
||||||
|
|||||||
Reference in New Issue
Block a user