-
Notifications
You must be signed in to change notification settings - Fork 3
/
critical_path.go
63 lines (52 loc) · 1.36 KB
/
critical_path.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
package gdag
type criticalPath struct {
path map[int]struct{} // key は Node の index
sumHour float64
}
func (cp *criticalPath) contains(n *Node) bool {
_, ok := cp.path[n.index]
return ok
}
type criticalPathCalculator struct {
allPaths [][]*Node // start からすべてのパス
}
func newCriticalPathCalculator() *criticalPathCalculator {
return &criticalPathCalculator{}
}
func (cc *criticalPathCalculator) getCriticalPaths(start *Node) []*criticalPath {
cc.walk(start, []*Node{})
criticalPaths := []*criticalPath{}
for _, path := range cc.allPaths {
critical := &criticalPath{path: map[int]struct{}{}}
for _, n := range path {
critical.path[n.index] = struct{}{}
critical.sumHour += n.hour
}
if critical.sumHour == 0 {
continue
}
if len(criticalPaths) == 0 {
criticalPaths = append(criticalPaths, critical)
continue
}
if critical.sumHour == criticalPaths[0].sumHour {
criticalPaths = append(criticalPaths, critical)
continue
}
if critical.sumHour > criticalPaths[0].sumHour {
criticalPaths = []*criticalPath{critical}
continue
}
}
return criticalPaths
}
func (cc *criticalPathCalculator) walk(current *Node, path []*Node) {
path = append(path, current)
if len(current.downstream) == 0 {
cc.allPaths = append(cc.allPaths, path)
return
}
for _, n := range current.downstream {
cc.walk(n, path)
}
}