-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_execution_order.go
More file actions
141 lines (116 loc) · 2.59 KB
/
task_execution_order.go
File metadata and controls
141 lines (116 loc) · 2.59 KB
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
package main
import "fmt"
// T1-T2
// T2-T3
// T4-T6
// T2-T4
// T6-T7
// T1 [T2]
// T2 [T3, T4]
// T3 []
// T4 [T6]
// T6 [T7]
// T7 []
// first flatern the unqiue tasks
// and mark its dependcies
// map[Task]{
// Rank: 1,
// Dependcies: [Task,...]
// IsDone: bool
// }
// we need list [] of tasks ordered by execution rank/position
// check if the dependend tasks completed, then make this into the queue
type taskProp struct {
Rank int
Dependcies []string
IsDone bool
}
func main() {
input := [][]string{
{"T1", "T2"},
{"T2", "T3"},
{"T4", "T6"},
{"T2", "T4"},
{"T6", "T7"},
{"T5", "T1"},
}
tasksMap := make(map[string]taskProp)
uniqueTasks := []string{}
// first flatern the unqiue tasks
for _, tasks := range input {
t := tasks[0]
dt := tasks[1]
tprop, ok := tasksMap[t]
if !ok {
tasksMap[t] = taskProp{
Rank: 0,
Dependcies: []string{
dt,
},
IsDone: false,
}
uniqueTasks = append(uniqueTasks, t)
} else {
tprop.Dependcies = append(tprop.Dependcies, dt)
tasksMap[t] = tprop
}
// add depended task into the registary
_, ok = tasksMap[dt]
if !ok {
tasksMap[dt] = taskProp{
Rank: 0,
Dependcies: []string{},
IsDone: false,
}
uniqueTasks = append(uniqueTasks, dt)
}
}
// find the rank
totalTasks := len(tasksMap)
fmt.Println(totalTasks, uniqueTasks)
fmt.Printf("%v\n", tasksMap)
// finding the order of executions
// we need list [] of tasks ordered by execution rank/position
// T3,T7, T6, T4, T2, T1
// [T3 T7 T6 T4 T2 T1]
tasksOrder := []string{}
for {
for _, t := range uniqueTasks { // fixed len(6)
// 1. check if has 0 dependencies
taskProp := tasksMap[t]
if taskProp.IsDone {
continue
}
if len(taskProp.Dependcies) == 0 {
// add it to the queue
tasksOrder = append(tasksOrder, t)
// mark this has done
taskProp.IsDone = true
tasksMap[t] = taskProp
continue
}
// 2. check if the dependend tasks completed, then make this into the queue
allSubTasksDone := true
for _, subT := range taskProp.Dependcies {
subTaskProp := tasksMap[subT]
// if any one not completed then no
if !subTaskProp.IsDone {
allSubTasksDone = false
}
}
// check if the sub tasks are done
if allSubTasksDone {
// add it to the queue
tasksOrder = append(tasksOrder, t)
// mark this has done
taskProp.IsDone = true
tasksMap[t] = taskProp
}
}
// fmt.Println("Current Queue: ", tasksOrder, len(tasksOrder))
if len(tasksOrder) >= totalTasks {
break
}
}
fmt.Println("Order of tasks: ", tasksOrder)
}