terraform/dag/graph.go

145 lines
3.1 KiB
Go
Raw Normal View History

2015-01-23 02:12:32 +01:00
package dag
import (
"bytes"
"fmt"
"sort"
2015-01-24 00:01:58 +01:00
"sync"
)
// Graph is used to represent a dependency graph.
type Graph struct {
2015-01-30 21:25:49 +01:00
vertices *set
2015-01-24 00:01:58 +01:00
edges []Edge
downEdges map[Vertex]*set
upEdges map[Vertex]*set
once sync.Once
}
2015-01-24 00:01:58 +01:00
// Vertex of the graph.
type Vertex interface{}
2015-01-24 00:01:58 +01:00
// NamedVertex is an optional interface that can be implemented by Vertex
// to give it a human-friendly name that is used for outputting the graph.
type NamedVertex interface {
Vertex
Name() string
}
2015-01-24 00:01:58 +01:00
// Vertices returns the list of all the vertices in the graph.
func (g *Graph) Vertices() []Vertex {
2015-01-30 21:25:49 +01:00
list := g.vertices.List()
result := make([]Vertex, len(list))
for i, v := range list {
result[i] = v.(Vertex)
}
return result
2015-01-24 00:01:58 +01:00
}
// Edges returns the list of all the edges in the graph.
func (g *Graph) Edges() []Edge {
return g.edges
}
// 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 {
2015-01-24 00:01:58 +01:00
g.once.Do(g.init)
2015-01-30 21:25:49 +01:00
g.vertices.Add(v)
return v
2015-01-24 00:01:58 +01:00
}
// Connect adds an edge with the given source and target. This is safe to
// call multiple times with the same value. Note that the same value is
// verified through pointer equality of the vertices, not through the
// value of the edge itself.
func (g *Graph) Connect(edge Edge) {
g.once.Do(g.init)
source := edge.Source()
target := edge.Target()
// Do we have this already? If so, don't add it again.
if s, ok := g.downEdges[source]; ok && s.Include(target) {
return
}
// TODO: add all edges
g.edges = append(g.edges, edge)
// Add the down edge
s, ok := g.downEdges[source]
if !ok {
s = new(set)
g.downEdges[source] = s
}
s.Add(target)
// Add the up edge
s, ok = g.upEdges[target]
if !ok {
s = new(set)
g.upEdges[target] = s
}
s.Add(source)
}
// String outputs some human-friendly output for the graph structure.
func (g *Graph) String() string {
var buf bytes.Buffer
// Build the list of node names and a mapping so that we can more
// easily alphabetize the output to remain deterministic.
2015-01-30 21:25:49 +01:00
vertices := g.Vertices()
names := make([]string, 0, len(vertices))
mapping := make(map[string]Vertex, len(vertices))
for _, v := range vertices {
name := VertexName(v)
names = append(names, name)
2015-01-24 00:01:58 +01:00
mapping[name] = v
}
sort.Strings(names)
// Write each node in order...
for _, name := range names {
2015-01-24 00:01:58 +01:00
v := mapping[name]
targets := g.downEdges[v]
buf.WriteString(fmt.Sprintf("%s\n", name))
// Alphabetize dependencies
2015-01-24 00:01:58 +01:00
deps := make([]string, 0, targets.Len())
for _, target := range targets.List() {
deps = append(deps, VertexName(target))
}
sort.Strings(deps)
// Write dependencies
for _, d := range deps {
buf.WriteString(fmt.Sprintf(" %s\n", d))
}
}
return buf.String()
}
2015-01-24 00:01:58 +01:00
func (g *Graph) init() {
2015-01-30 21:25:49 +01:00
g.vertices = new(set)
2015-01-24 00:01:58 +01:00
g.edges = make([]Edge, 0, 2)
g.downEdges = make(map[Vertex]*set)
g.upEdges = make(map[Vertex]*set)
}
// VertexName returns the name of a vertex.
func VertexName(raw Vertex) string {
2015-01-24 00:01:58 +01:00
switch v := raw.(type) {
case NamedVertex:
return v.Name()
2015-01-24 00:01:58 +01:00
case fmt.Stringer:
return fmt.Sprintf("%s", v)
2015-01-24 00:01:58 +01:00
default:
return fmt.Sprintf("%v", v)
}
}