2017-08-15
This commit is contained in:
parent
297afbe06b
commit
3b6d381185
BIN
go/pov/cmd/cmd
Executable file
BIN
go/pov/cmd/cmd
Executable file
Binary file not shown.
13
go/pov/cmd/main.go
Normal file
13
go/pov/cmd/main.go
Normal 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)
|
||||||
|
}
|
@ -11,6 +11,7 @@ func New() *Graph {
|
|||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNode returns the node from the graph with label lbl
|
||||||
func (g *Graph) GetNode(lbl string) *Node {
|
func (g *Graph) GetNode(lbl string) *Node {
|
||||||
for i := range g.leaves {
|
for i := range g.leaves {
|
||||||
if n := g.leaves[i].GetNode(lbl); n != nil {
|
if n := g.leaves[i].GetNode(lbl); n != nil {
|
||||||
@ -20,20 +21,24 @@ func (g *Graph) GetNode(lbl string) *Node {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddNode adds a top level leaf with label lbl
|
||||||
func (g *Graph) AddNode(lbl string) {
|
func (g *Graph) AddNode(lbl string) {
|
||||||
g.leaves = append(g.leaves, Node{label: lbl})
|
g.leaves = append(g.leaves, Node{label: lbl})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addRealNode adds the node n to the top level
|
||||||
func (g *Graph) addRealNode(n *Node) {
|
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) {
|
func (g *Graph) AddArc(from, to string) {
|
||||||
if n := g.GetNode(to); n != nil {
|
if n := g.GetNode(to); n != nil {
|
||||||
n.AddNode(from)
|
n.AddNode(from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArcList returns a list of all arcs
|
||||||
func (g *Graph) ArcList() []string {
|
func (g *Graph) ArcList() []string {
|
||||||
var ret []string
|
var ret []string
|
||||||
for i := range g.leaves {
|
for i := range g.leaves {
|
||||||
@ -42,16 +47,19 @@ func (g *Graph) ArcList() []string {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChangeRoot changes the graph from starting at oldRoot going to newRoot
|
||||||
func (g *Graph) ChangeRoot(oldRoot, newRoot string) *Graph {
|
func (g *Graph) ChangeRoot(oldRoot, newRoot string) *Graph {
|
||||||
// First of all, find the newRoot node
|
// First of all, find the newRoot node
|
||||||
ret := New()
|
ret := New()
|
||||||
// The new graph will start with newRoot and have newRoot's leaves
|
// 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
|
return ret
|
||||||
}
|
}
|
||||||
// It'll have one more leaf, it's parent node
|
// It'll have one more leaf, it's parent node
|
||||||
ret.AddNode(rt)
|
//rt.addRealNode(g.GetNode(oldRoot))
|
||||||
return g
|
ret.addRealNode(rt)
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graph) getPath(from, to string) []string {
|
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})
|
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 {
|
func (n *Node) GetNode(lbl string) *Node {
|
||||||
if n.label == lbl {
|
if n.label == lbl {
|
||||||
return n
|
return n
|
||||||
|
20
go/pov/pov_helper.go
Normal file
20
go/pov/pov_helper.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user