-
Notifications
You must be signed in to change notification settings - Fork 153
/
walk.go
200 lines (162 loc) · 4.53 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package plan
import (
"errors"
"sort"
)
// TopDownWalk will execute f for each plan node in the Spec.
// It always visits a node before visiting its predecessors.
func (plan *Spec) TopDownWalk(f func(node Node) error) error {
visited := make(map[Node]struct{})
roots := make([]Node, 0, len(plan.Roots))
for root := range plan.Roots {
roots = append(roots, root)
}
// Make sure to sort the roots first otherwise
// an in-consistent walk order is possible.
sort.Slice(roots, func(i, j int) bool {
return roots[i].ID() < roots[j].ID()
})
postFn := func(Node) error {
return nil
}
for _, root := range roots {
err := walk(root, f, postFn, visited)
if err != nil {
return err
}
}
return nil
}
// BottomUpWalk will execute f for each plan node in the Spec,
// starting from the sources, and only visiting a node after all its
// predecessors have been visited.
func (plan *Spec) BottomUpWalk(f func(Node) error) error {
visited := make(map[Node]struct{})
roots := make([]Node, 0, len(plan.Roots))
for root := range plan.Roots {
roots = append(roots, root)
}
// Make sure to sort the roots first otherwise
// an in-consistent walk order is possible.
sort.Slice(roots, func(i, j int) bool {
return roots[i].ID() < roots[j].ID()
})
preFn := func(Node) error {
return nil
}
for _, root := range roots {
err := walk(root, preFn, f, visited)
if err != nil {
return err
}
}
return nil
}
func walk(node Node, preFn, postFn func(Node) error, visited map[Node]struct{}) error {
if _, ok := visited[node]; ok {
return nil
}
visited[node] = struct{}{}
// Pre-order traversal
if err := preFn(node); err != nil {
return err
}
for _, pred := range node.Predecessors() {
if err := walk(pred, preFn, postFn, visited); err != nil {
return err
}
}
// Post-order traversal
return postFn(node)
}
// WalkPredecessor visits every node in the plan rooted at `roots` in topological order,
// following predecessor links. If a cycle is detected, no node is visited and
// an error is returned.
func WalkPredecessors(roots []Node, visitFn func(node Node) error) error {
tw := newTopologicalWalk(Node.Predecessors, visitFn)
for _, root := range roots {
if err := tw.walk(root); err != nil {
return err
}
}
return tw.visit()
}
// WalkSuccessors visits every node in the plan rooted at `roots` in topological order,
// following successor links. If a cycle is detected, no node is visited and
// an error is returned.
func WalkSuccessors(roots []Node, visitFn func(node Node) error) error {
tw := newTopologicalWalk(Node.Successors, visitFn)
for _, root := range roots {
if err := tw.walk(root); err != nil {
return err
}
}
return tw.visit()
}
// TopologicalWalk visits every node in the plan in topological order.
// If a cycle is detected, no node is visited and an error is returned.
func (plan *Spec) TopologicalWalk(visitFn func(node Node) error) error {
tw := newTopologicalWalk(Node.Predecessors, visitFn)
roots := make([]Node, 0, len(plan.Roots))
for root := range plan.Roots {
roots = append(roots, root)
}
// Make sure to sort the roots first otherwise
// an in-consistent walk order is possible.
sort.Slice(roots, func(i, j int) bool {
return roots[i].ID() < roots[j].ID()
})
for _, root := range roots {
if err := tw.walk(root); err != nil {
return err
}
}
return tw.visit()
}
type topologicalWalk struct {
navigationFn func(node Node) []Node
visitFn func(node Node) error
temporaryMarks map[Node]bool
permanentMarks map[Node]bool
callStack []func() error
}
func newTopologicalWalk(navigationFn func(node Node) []Node, visitFn func(node Node) error) *topologicalWalk {
return &topologicalWalk{
navigationFn: navigationFn,
visitFn: visitFn,
temporaryMarks: make(map[Node]bool),
permanentMarks: make(map[Node]bool),
callStack: make([]func() error, 0),
}
}
func (tw *topologicalWalk) pushVisit(node Node) {
fn := func() error {
return tw.visitFn(node)
}
tw.callStack = append(tw.callStack, fn)
}
func (tw *topologicalWalk) walk(node Node) error {
if tw.temporaryMarks[node] {
return errors.New("cycle detected")
}
if !tw.permanentMarks[node] {
tw.temporaryMarks[node] = true
for _, n := range tw.navigationFn(node) {
if err := tw.walk(n); err != nil {
return err
}
}
tw.permanentMarks[node] = true
tw.temporaryMarks[node] = false
tw.pushVisit(node)
}
return nil
}
func (tw *topologicalWalk) visit() error {
for i := len(tw.callStack) - 1; i >= 0; i-- {
if err := tw.callStack[i](); err != nil {
return err
}
}
return nil
}