dag: add HasVertex, HasEdge

This commit is contained in:
Mitchell Hashimoto 2016-01-14 13:52:01 -08:00
parent b333ffa3d2
commit 5d5045fdd6
2 changed files with 36 additions and 0 deletions

View File

@ -48,6 +48,16 @@ func (g *Graph) Edges() []Edge {
return result 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 // Add adds a vertex to the graph. This is safe to call multiple time with
// the same Vertex. // the same Vertex.
func (g *Graph) Add(v Vertex) Vertex { func (g *Graph) Add(v Vertex) Vertex {

View File

@ -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 { type hashVertex struct {
code interface{} code interface{}
} }