diff --git a/internal/dag/dag.go b/internal/dag/dag.go index d72b3988e..5aca57944 100644 --- a/internal/dag/dag.go +++ b/internal/dag/dag.go @@ -2,7 +2,6 @@ package dag import ( "fmt" - "sort" "strings" "github.com/hashicorp/terraform/internal/tfdiags" @@ -218,49 +217,6 @@ func (g *AcyclicGraph) DepthFirstWalk(start Set, f DepthWalkFunc) error { return nil } -// SortedDepthFirstWalk does a depth-first walk of the graph starting from -// the vertices in start, always iterating the nodes in a consistent order. -func (g *AcyclicGraph) SortedDepthFirstWalk(start []Vertex, f DepthWalkFunc) error { - seen := make(map[Vertex]struct{}) - frontier := make([]*vertexAtDepth, len(start)) - for i, v := range start { - frontier[i] = &vertexAtDepth{ - Vertex: v, - Depth: 0, - } - } - for len(frontier) > 0 { - // Pop the current vertex - n := len(frontier) - current := frontier[n-1] - frontier = frontier[:n-1] - - // Check if we've seen this already and return... - if _, ok := seen[current.Vertex]; ok { - continue - } - seen[current.Vertex] = struct{}{} - - // Visit the current node - if err := f(current.Vertex, current.Depth); err != nil { - return err - } - - // Visit targets of this in a consistent order. - targets := AsVertexList(g.downEdgesNoCopy(current.Vertex)) - sort.Sort(byVertexName(targets)) - - for _, t := range targets { - frontier = append(frontier, &vertexAtDepth{ - Vertex: t, - Depth: current.Depth + 1, - }) - } - } - - return nil -} - // ReverseDepthFirstWalk does a depth-first walk _up_ the graph starting from // the vertices in start. func (g *AcyclicGraph) ReverseDepthFirstWalk(start Set, f DepthWalkFunc) error { @@ -299,55 +255,3 @@ func (g *AcyclicGraph) ReverseDepthFirstWalk(start Set, f DepthWalkFunc) error { return nil } - -// SortedReverseDepthFirstWalk does a depth-first walk _up_ the graph starting from -// the vertices in start, always iterating the nodes in a consistent order. -func (g *AcyclicGraph) SortedReverseDepthFirstWalk(start []Vertex, f DepthWalkFunc) error { - seen := make(map[Vertex]struct{}) - frontier := make([]*vertexAtDepth, len(start)) - for i, v := range start { - frontier[i] = &vertexAtDepth{ - Vertex: v, - Depth: 0, - } - } - for len(frontier) > 0 { - // Pop the current vertex - n := len(frontier) - current := frontier[n-1] - frontier = frontier[:n-1] - - // Check if we've seen this already and return... - if _, ok := seen[current.Vertex]; ok { - continue - } - seen[current.Vertex] = struct{}{} - - // Add next set of targets in a consistent order. - targets := AsVertexList(g.upEdgesNoCopy(current.Vertex)) - sort.Sort(byVertexName(targets)) - for _, t := range targets { - frontier = append(frontier, &vertexAtDepth{ - Vertex: t, - Depth: current.Depth + 1, - }) - } - - // Visit the current node - if err := f(current.Vertex, current.Depth); err != nil { - return err - } - } - - return nil -} - -// byVertexName implements sort.Interface so a list of Vertices can be sorted -// consistently by their VertexName -type byVertexName []Vertex - -func (b byVertexName) Len() int { return len(b) } -func (b byVertexName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } -func (b byVertexName) Less(i, j int) bool { - return VertexName(b[i]) < VertexName(b[j]) -} diff --git a/internal/dag/dag_test.go b/internal/dag/dag_test.go index 0402cda39..9c8cdb794 100644 --- a/internal/dag/dag_test.go +++ b/internal/dag/dag_test.go @@ -392,7 +392,10 @@ func TestAcyclicGraph_ReverseDepthFirstWalk_WithRemoval(t *testing.T) { var visits []Vertex var lock sync.Mutex - err := g.SortedReverseDepthFirstWalk([]Vertex{1}, func(v Vertex, d int) error { + root := make(Set) + root.Add(1) + + err := g.ReverseDepthFirstWalk(root, func(v Vertex, d int) error { lock.Lock() defer lock.Unlock() visits = append(visits, v)