-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwalk.go
More file actions
31 lines (29 loc) · 687 Bytes
/
walk.go
File metadata and controls
31 lines (29 loc) · 687 Bytes
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
package ssp
func in(a []Node, n Node) bool {
for _, na := range a {
if na == n {
return true
}
}
return false
}
func Walk(g Graph, f Visitor) {
// Here, we could use maps, but we need slices.
// We want a deterministic range, for a deterministic walk.
roots := g.Roots()
for len(roots) > 0 {
next := make([]Node, 0)
for _, root := range roots {
for _, a := range g.Adjacents(root) {
f(a)
n := a.To()
// Before adding this node to the next layer, check if we have already added it.
// This is the situation that happens when 2 or more nodes point to the same one.
if !in(next, n) {
next = append(next, n)
}
}
}
roots = next
}
}