dag: make vertices a set
This commit is contained in:
parent
254689389a
commit
ae4d20f8ce
21
dag/graph.go
21
dag/graph.go
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
// Graph is used to represent a dependency graph.
|
// Graph is used to represent a dependency graph.
|
||||||
type Graph struct {
|
type Graph struct {
|
||||||
vertices []Vertex
|
vertices *set
|
||||||
edges []Edge
|
edges []Edge
|
||||||
downEdges map[Vertex]*set
|
downEdges map[Vertex]*set
|
||||||
upEdges map[Vertex]*set
|
upEdges map[Vertex]*set
|
||||||
|
@ -28,7 +28,13 @@ type NamedVertex interface {
|
||||||
|
|
||||||
// Vertices returns the list of all the vertices in the graph.
|
// Vertices returns the list of all the vertices in the graph.
|
||||||
func (g *Graph) Vertices() []Vertex {
|
func (g *Graph) Vertices() []Vertex {
|
||||||
return g.vertices
|
list := g.vertices.List()
|
||||||
|
result := make([]Vertex, len(list))
|
||||||
|
for i, v := range list {
|
||||||
|
result[i] = v.(Vertex)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edges returns the list of all the edges in the graph.
|
// Edges returns the list of all the edges in the graph.
|
||||||
|
@ -40,7 +46,7 @@ func (g *Graph) Edges() []Edge {
|
||||||
// the same Vertex.
|
// the same Vertex.
|
||||||
func (g *Graph) Add(v Vertex) Vertex {
|
func (g *Graph) Add(v Vertex) Vertex {
|
||||||
g.once.Do(g.init)
|
g.once.Do(g.init)
|
||||||
g.vertices = append(g.vertices, v)
|
g.vertices.Add(v)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,9 +91,10 @@ func (g *Graph) String() string {
|
||||||
|
|
||||||
// Build the list of node names and a mapping so that we can more
|
// Build the list of node names and a mapping so that we can more
|
||||||
// easily alphabetize the output to remain deterministic.
|
// easily alphabetize the output to remain deterministic.
|
||||||
names := make([]string, 0, len(g.vertices))
|
vertices := g.Vertices()
|
||||||
mapping := make(map[string]Vertex, len(g.vertices))
|
names := make([]string, 0, len(vertices))
|
||||||
for _, v := range g.vertices {
|
mapping := make(map[string]Vertex, len(vertices))
|
||||||
|
for _, v := range vertices {
|
||||||
name := VertexName(v)
|
name := VertexName(v)
|
||||||
names = append(names, name)
|
names = append(names, name)
|
||||||
mapping[name] = v
|
mapping[name] = v
|
||||||
|
@ -118,7 +125,7 @@ func (g *Graph) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graph) init() {
|
func (g *Graph) init() {
|
||||||
g.vertices = make([]Vertex, 0, 5)
|
g.vertices = new(set)
|
||||||
g.edges = make([]Edge, 0, 2)
|
g.edges = make([]Edge, 0, 2)
|
||||||
g.downEdges = make(map[Vertex]*set)
|
g.downEdges = make(map[Vertex]*set)
|
||||||
g.upEdges = make(map[Vertex]*set)
|
g.upEdges = make(map[Vertex]*set)
|
||||||
|
|
Loading…
Reference in New Issue