2017-08-15

This commit is contained in:
Brian Buller 2017-08-15 17:06:46 -05:00
parent 297afbe06b
commit 3b6d381185
4 changed files with 49 additions and 4 deletions

BIN
go/pov/cmd/cmd Executable file

Binary file not shown.

13
go/pov/cmd/main.go Normal file
View File

@ -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)
}

View File

@ -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

20
go/pov/pov_helper.go Normal file
View File

@ -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)
}
}