Merge pull request #4681 from hashicorp/f-graph-imp
dag: add HasVertex, HasEdge
This commit is contained in:
commit
bf5d97ce2c
10
dag/graph.go
10
dag/graph.go
|
@ -48,6 +48,16 @@ func (g *Graph) Edges() []Edge {
|
|||
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)
|
||||
}
|
||||
|
||||
// HasEdge checks if the given Edge is present in the graph.
|
||||
func (g *Graph) HasEdge(e Edge) bool {
|
||||
return g.edges.Include(e)
|
||||
}
|
||||
|
||||
// Add adds a vertex to the graph. This is safe to call multiple time with
|
||||
// the same Vertex.
|
||||
func (g *Graph) Add(v Vertex) Vertex {
|
||||
|
|
|
@ -98,6 +98,32 @@ func TestGraph_hashcode(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGraphHasVertex(t *testing.T) {
|
||||
var g Graph
|
||||
g.Add(1)
|
||||
|
||||
if !g.HasVertex(1) {
|
||||
t.Fatal("should have 1")
|
||||
}
|
||||
if g.HasVertex(2) {
|
||||
t.Fatal("should not have 2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGraphHasEdge(t *testing.T) {
|
||||
var g Graph
|
||||
g.Add(1)
|
||||
g.Add(2)
|
||||
g.Connect(BasicEdge(1, 2))
|
||||
|
||||
if !g.HasEdge(BasicEdge(1, 2)) {
|
||||
t.Fatal("should have 1,2")
|
||||
}
|
||||
if g.HasVertex(BasicEdge(2, 3)) {
|
||||
t.Fatal("should not have 2,3")
|
||||
}
|
||||
}
|
||||
|
||||
type hashVertex struct {
|
||||
code interface{}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue