Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active May 9, 2016 22:53
Show Gist options
  • Save caelifer/51d924469e8f8035604cdf78659cc74c to your computer and use it in GitHub Desktop.
Save caelifer/51d924469e8f8035604cdf78659cc74c to your computer and use it in GitHub Desktop.
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
}
@caelifer
Copy link
Author

caelifer commented May 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment