github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/dag/graph.go (about) 1 package dag 2 3 import ( 4 "bytes" 5 "fmt" 6 "sort" 7 "sync" 8 ) 9 10 // Graph is used to represent a dependency graph. 11 type Graph struct { 12 vertices *Set 13 edges *Set 14 downEdges map[Vertex]*Set 15 upEdges map[Vertex]*Set 16 once sync.Once 17 } 18 19 // Vertex of the graph. 20 type Vertex interface{} 21 22 // NamedVertex is an optional interface that can be implemented by Vertex 23 // to give it a human-friendly name that is used for outputting the graph. 24 type NamedVertex interface { 25 Vertex 26 Name() string 27 } 28 29 // Vertices returns the list of all the vertices in the graph. 30 func (g *Graph) Vertices() []Vertex { 31 list := g.vertices.List() 32 result := make([]Vertex, len(list)) 33 for i, v := range list { 34 result[i] = v.(Vertex) 35 } 36 37 return result 38 } 39 40 // Edges returns the list of all the edges in the graph. 41 func (g *Graph) Edges() []Edge { 42 list := g.edges.List() 43 result := make([]Edge, len(list)) 44 for i, v := range list { 45 result[i] = v.(Edge) 46 } 47 48 return result 49 } 50 51 // Add adds a vertex to the graph. This is safe to call multiple time with 52 // the same Vertex. 53 func (g *Graph) Add(v Vertex) Vertex { 54 g.once.Do(g.init) 55 g.vertices.Add(v) 56 return v 57 } 58 59 // Remove removes a vertex from the graph. This will also remove any 60 // edges with this vertex as a source or target. 61 func (g *Graph) Remove(v Vertex) Vertex { 62 // Delete the vertex itself 63 g.vertices.Delete(v) 64 65 // Delete the edges to non-existent things 66 for _, target := range g.DownEdges(v).List() { 67 g.RemoveEdge(BasicEdge(v, target)) 68 } 69 for _, source := range g.UpEdges(v).List() { 70 g.RemoveEdge(BasicEdge(source, v)) 71 } 72 73 return nil 74 } 75 76 // Replace replaces the original Vertex with replacement. If the original 77 // does not exist within the graph, then false is returned. Otherwise, true 78 // is returned. 79 func (g *Graph) Replace(original, replacement Vertex) bool { 80 // If we don't have the original, we can't do anything 81 if !g.vertices.Include(original) { 82 return false 83 } 84 85 // Add our new vertex, then copy all the edges 86 g.Add(replacement) 87 for _, target := range g.DownEdges(original).List() { 88 g.Connect(BasicEdge(replacement, target)) 89 } 90 for _, source := range g.UpEdges(original).List() { 91 g.Connect(BasicEdge(source, replacement)) 92 } 93 94 // Remove our old vertex, which will also remove all the edges 95 g.Remove(original) 96 97 return true 98 } 99 100 // RemoveEdge removes an edge from the graph. 101 func (g *Graph) RemoveEdge(edge Edge) { 102 g.once.Do(g.init) 103 104 // Delete the edge from the set 105 g.edges.Delete(edge) 106 107 // Delete the up/down edges 108 if s, ok := g.downEdges[edge.Source()]; ok { 109 s.Delete(edge.Target()) 110 } 111 if s, ok := g.upEdges[edge.Target()]; ok { 112 s.Delete(edge.Source()) 113 } 114 } 115 116 // DownEdges returns the outward edges from the source Vertex v. 117 func (g *Graph) DownEdges(v Vertex) *Set { 118 g.once.Do(g.init) 119 return g.downEdges[v] 120 } 121 122 // UpEdges returns the inward edges to the destination Vertex v. 123 func (g *Graph) UpEdges(v Vertex) *Set { 124 g.once.Do(g.init) 125 return g.upEdges[v] 126 } 127 128 // Connect adds an edge with the given source and target. This is safe to 129 // call multiple times with the same value. Note that the same value is 130 // verified through pointer equality of the vertices, not through the 131 // value of the edge itself. 132 func (g *Graph) Connect(edge Edge) { 133 g.once.Do(g.init) 134 135 source := edge.Source() 136 target := edge.Target() 137 138 // Do we have this already? If so, don't add it again. 139 if s, ok := g.downEdges[source]; ok && s.Include(target) { 140 return 141 } 142 143 // Add the edge to the set 144 g.edges.Add(edge) 145 146 // Add the down edge 147 s, ok := g.downEdges[source] 148 if !ok { 149 s = new(Set) 150 g.downEdges[source] = s 151 } 152 s.Add(target) 153 154 // Add the up edge 155 s, ok = g.upEdges[target] 156 if !ok { 157 s = new(Set) 158 g.upEdges[target] = s 159 } 160 s.Add(source) 161 } 162 163 // String outputs some human-friendly output for the graph structure. 164 func (g *Graph) String() string { 165 var buf bytes.Buffer 166 167 // Build the list of node names and a mapping so that we can more 168 // easily alphabetize the output to remain deterministic. 169 vertices := g.Vertices() 170 names := make([]string, 0, len(vertices)) 171 mapping := make(map[string]Vertex, len(vertices)) 172 for _, v := range vertices { 173 name := VertexName(v) 174 names = append(names, name) 175 mapping[name] = v 176 } 177 sort.Strings(names) 178 179 // Write each node in order... 180 for _, name := range names { 181 v := mapping[name] 182 targets := g.downEdges[v] 183 184 buf.WriteString(fmt.Sprintf("%s\n", name)) 185 186 // Alphabetize dependencies 187 deps := make([]string, 0, targets.Len()) 188 for _, target := range targets.List() { 189 deps = append(deps, VertexName(target)) 190 } 191 sort.Strings(deps) 192 193 // Write dependencies 194 for _, d := range deps { 195 buf.WriteString(fmt.Sprintf(" %s\n", d)) 196 } 197 } 198 199 return buf.String() 200 } 201 202 func (g *Graph) init() { 203 g.vertices = new(Set) 204 g.edges = new(Set) 205 g.downEdges = make(map[Vertex]*Set) 206 g.upEdges = make(map[Vertex]*Set) 207 } 208 209 // VertexName returns the name of a vertex. 210 func VertexName(raw Vertex) string { 211 switch v := raw.(type) { 212 case NamedVertex: 213 return v.Name() 214 case fmt.Stringer: 215 return fmt.Sprintf("%s", v) 216 default: 217 return fmt.Sprintf("%v", v) 218 } 219 }