diff --git a/go/pov/cmd/cmd b/go/pov/cmd/cmd new file mode 100755 index 0000000..09765fd Binary files /dev/null and b/go/pov/cmd/cmd differ diff --git a/go/pov/cmd/main.go b/go/pov/cmd/main.go new file mode 100644 index 0000000..c525c41 --- /dev/null +++ b/go/pov/cmd/main.go @@ -0,0 +1,13 @@ +package main + +import pov ".." + +func main() { + g := pov.New() + g.AddNode("sibling") + g.AddNode("x") + g.AddNode("parent") + g.AddArc("parent", "sibling") + g.AddArc("parent", "x") + pov.PrintGraph(g) +} diff --git a/go/pov/pov.go b/go/pov/pov.go index 38be5a5..95c943d 100644 --- a/go/pov/pov.go +++ b/go/pov/pov.go @@ -11,6 +11,7 @@ func New() *Graph { return g } +// GetNode returns the node from the graph with label lbl func (g *Graph) GetNode(lbl string) *Node { for i := range g.leaves { if n := g.leaves[i].GetNode(lbl); n != nil { @@ -20,20 +21,24 @@ func (g *Graph) GetNode(lbl string) *Node { return nil } +// AddNode adds a top level leaf with label lbl func (g *Graph) AddNode(lbl string) { g.leaves = append(g.leaves, Node{label: lbl}) } +// addRealNode adds the node n to the top level func (g *Graph) addRealNode(n *Node) { - g.leaves = append(g.leaves, n) + g.leaves = append(g.leaves, *n) } +// AddArc creates a new node after to named from func (g *Graph) AddArc(from, to string) { if n := g.GetNode(to); n != nil { n.AddNode(from) } } +// ArcList returns a list of all arcs func (g *Graph) ArcList() []string { var ret []string for i := range g.leaves { @@ -42,16 +47,19 @@ func (g *Graph) ArcList() []string { return ret } +// ChangeRoot changes the graph from starting at oldRoot going to newRoot func (g *Graph) ChangeRoot(oldRoot, newRoot string) *Graph { // First of all, find the newRoot node ret := New() // The new graph will start with newRoot and have newRoot's leaves - if rt := g.GetNode(newRoot); rt == nil { + var rt *Node + if rt = g.GetNode(newRoot); rt == nil { return ret } // It'll have one more leaf, it's parent node - ret.AddNode(rt) - return g + //rt.addRealNode(g.GetNode(oldRoot)) + ret.addRealNode(rt) + return ret } func (g *Graph) getPath(from, to string) []string { @@ -79,6 +87,10 @@ func (n *Node) AddNode(lbl string) { n.leaves = append(n.leaves, Node{label: lbl}) } +func (n *Node) addRealNode(nd *Node) { + n.leaves = append(n.leaves, *nd) +} + func (n *Node) GetNode(lbl string) *Node { if n.label == lbl { return n diff --git a/go/pov/pov_helper.go b/go/pov/pov_helper.go new file mode 100644 index 0000000..fb3d565 --- /dev/null +++ b/go/pov/pov_helper.go @@ -0,0 +1,20 @@ +package pov + +import ( + "fmt" + "strings" +) + +func PrintGraph(g *Graph) { + for i := range g.leaves { + PrintNode(&g.leaves[i], 1) + } +} + +func PrintNode(n *Node, lvl int) { + strings.Repeat(" ", lvl) + fmt.Println(n.label) + for i := range n.leaves { + PrintNode(&n.leaves[i], lvl+1) + } +}