-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
64 lines (49 loc) · 1.1 KB
/
check.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
package internal
import "fmt"
func (graph *Graph[E]) Check() error {
_, left := graph.Steps()
if len(left) != 0 {
return fmt.Errorf("found cycle between vertices: %v", left)
} else {
return nil
}
}
func (graph *Graph[E]) Steps() ([][]string, []string) {
steps := make([][]string, 0)
graph.Lock()
graph.reset()
for {
var names []string
for name, vertex := range graph.Vertices {
// vertex is todo and all dep vertices are done, so it can be processed
if vertex.Status == StatusTodo {
ready := true
for _, dep := range vertex.Dependencies {
if dep.Status != StatusDone {
ready = false
}
}
if ready {
names = append(names, name)
}
}
}
if len(names) == 0 {
break
}
// set vertex status to be done
for _, name := range names {
graph.Vertices[name].Status = StatusDone
}
steps = append(steps, names)
}
// found vertices which are never done
left := make([]string, 0)
for _, vertex := range graph.Vertices {
if vertex.Status != StatusDone {
left = append(left, vertex.Name)
}
}
graph.Unlock()
return steps, left
}