Skip to content

Commit

Permalink
dag: add HasVertex, HasEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jan 14, 2016
1 parent b333ffa commit 5d5045f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dag/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions dag/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down

0 comments on commit 5d5045f

Please sign in to comment.