calculateDepthMap calculates dependency depth for each task
(tasks []*model.Task, taskMap map[string]*model.Task)
| 53 | |
| 54 | // calculateDepthMap calculates dependency depth for each task |
| 55 | func calculateDepthMap(tasks []*model.Task, taskMap map[string]*model.Task) map[string]int { |
| 56 | memo := make(map[string]int) |
| 57 | |
| 58 | var getDepth func(taskID string, visited map[string]bool) int |
| 59 | getDepth = func(taskID string, visited map[string]bool) int { |
| 60 | if depth, ok := memo[taskID]; ok { |
| 61 | return depth |
| 62 | } |
| 63 | |
| 64 | if visited[taskID] { |
| 65 | return 0 |
| 66 | } |
| 67 | |
| 68 | task, exists := taskMap[taskID] |
| 69 | if !exists { |
| 70 | return 0 |
| 71 | } |
| 72 | |
| 73 | visited[taskID] = true |
| 74 | defer delete(visited, taskID) |
| 75 | |
| 76 | maxDepth := 0 |
| 77 | for _, depID := range task.Dependencies { |
| 78 | depth := getDepth(depID, visited) |
| 79 | if depth > maxDepth { |
| 80 | maxDepth = depth |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | result := maxDepth + 1 |
| 85 | memo[taskID] = result |
| 86 | return result |
| 87 | } |
| 88 | |
| 89 | for _, task := range tasks { |
| 90 | getDepth(task.ID, make(map[string]bool)) |
| 91 | } |
| 92 | |
| 93 | return memo |
| 94 | } |
| 95 | |
| 96 | // calculateTopologicalOrder assigns a topological order to each task |
| 97 | func calculateTopologicalOrder(tasks []*model.Task, taskMap map[string]*model.Task) map[string]int { |
no outgoing calls