Last active
May 9, 2016 22:53
-
-
Save caelifer/51d924469e8f8035604cdf78659cc74c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
t1 := NewNode("A") | |
t2 := NewNode("B", t1) | |
t3 := NewNode("C", t1) | |
t4 := NewNode("D", t1, t2) | |
t5 := NewNode("E", t3, t4) | |
fmt.Println(t2) | |
fmt.Println(t3) | |
fmt.Println(t4) | |
fmt.Println(t5) | |
} | |
type Node struct { | |
Cmd string | |
DependsOn []*Node | |
} | |
func (n Node) String() string { | |
deps := []string{} | |
for _, d := range resolveDeps(&n) { | |
deps = append(deps, d.Cmd) | |
} | |
return n.Cmd + ": " + strings.Join(deps, " -> ") | |
} | |
func NewNode(cmd string, deps ...*Node) *Node { | |
return &Node{ | |
Cmd: cmd, | |
DependsOn: deps, | |
} | |
} | |
func resolveDeps(roots ...*Node) []*Node { | |
seen := map[*Node]bool{} | |
all := []*Node{} | |
var walk func(n *Node) | |
walk = func(n *Node) { | |
if seen[n] { | |
return | |
} | |
seen[n] = true | |
for _, n1 := range n.DependsOn { | |
walk(n1) | |
} | |
all = append(all, n) | |
} | |
for _, r := range roots { | |
walk(r) | |
} | |
return all | |
} | |
type executionPlan [][]*Node | |
func ExecutionPlan(nodes ...*Node) executionPlan { | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - https://play.golang.org/p/YXnNhHl01G