NewGraph creates a new graph from a list of tasks
(tasks []*model.Task)
| 63 | |
| 64 | // NewGraph creates a new graph from a list of tasks |
| 65 | func NewGraph(tasks []*model.Task) *Graph { |
| 66 | g := &Graph{ |
| 67 | Tasks: tasks, |
| 68 | TaskMap: make(map[string]*model.Task), |
| 69 | Adjacency: make(map[string][]string), |
| 70 | RevAdjacency: make(map[string][]string), |
| 71 | } |
| 72 | |
| 73 | // Build task map |
| 74 | for _, task := range tasks { |
| 75 | g.TaskMap[task.ID] = task |
| 76 | } |
| 77 | |
| 78 | // Build adjacency lists |
| 79 | for _, task := range tasks { |
| 80 | for _, depID := range task.Dependencies { |
| 81 | // task depends on depID |
| 82 | // so depID -> task in forward adjacency (depID blocks task) |
| 83 | g.Adjacency[depID] = append(g.Adjacency[depID], task.ID) |
| 84 | // task -> depID in reverse adjacency |
| 85 | g.RevAdjacency[task.ID] = append(g.RevAdjacency[task.ID], depID) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return g |
| 90 | } |
| 91 | |
| 92 | // GetDownstream returns all tasks that depend on the given task (transitively) |
| 93 | func (g *Graph) GetDownstream(taskID string) map[string]bool { |
no outgoing calls