Made graph more generic and efficient

This commit is contained in:
Mikkel Milo 2016-02-25 13:49:33 +01:00
parent ab4115c1e1
commit 855894b734
2 changed files with 12 additions and 98 deletions

View File

@ -12,12 +12,16 @@ func main() {
n3 := new(graph.Node)
n3.Value = "3"
n.Connect(n2)
n.Connect(n3)
n2.Connect(n3)
//n.DisconnectAll()
fmt.Println(n.IsConnectedTo(n3))
fmt.Println("n -> n2: ", n2.IsConnectedTo(n), " n <- n2: ", n.IsConnectedTo(n2))
fmt.Println("n -> n3: ", n.IsConnectedTo(n3), " n <- n3: ", n3.IsConnectedTo(n))
fmt.Println("n2 -> n3: ", n2.IsConnectedTo(n3), " n2 <- n3: ", n3.IsConnectedTo(n2))
n.SetPointTo(n2)
n.SetPointTo(n3)
n2.SetPointTo(n3)
n2.SetPointTo(n)
n3.SetPointTo(n)
n.DisconnectAll()
fmt.Println("n -> n2: ", n.IsPointingTo(n2), " n <- n2: ", n2.IsPointingTo(n))
fmt.Println("n -> n3: ", n.IsPointingTo(n3), " n <- n3: ", n3.IsPointingTo(n))
fmt.Println("n2 -> n3: ", n2.IsPointingTo(n3), " n2 <- n3: ", n3.IsPointingTo(n2))
}

View File

@ -1,90 +0,0 @@
package graph
import (
"fmt"
)
//Node a graph node which can contain Connections to other nodes
type Node struct {
Value string
Connections []*Node
Messages []string
}
//GetConnection returns the i'th connected node if it exists
func (n *Node) GetConnection(i int) (*Node, error) {
if len(n.Connections) > i && i >= 0 {
return n.Connections[i], nil
}
return n, fmt.Errorf("slice out of bounds")
}
//Connect connects two nodes
func (n *Node) Connect(otherNode *Node) {
n.Connections = append(n.Connections, otherNode)
otherNode.Connections = append(otherNode.Connections, n)
}
//SetPointTo sets this Node to point towards the given Node
func (n *Node) SetPointTo(otherNode *Node) {
if n.IsConnectedTo(otherNode) == false {
n.Connections = append(n.Connections, otherNode)
}
}
//RemovePointerTo removes pointer to a node (a "child")
func (n *Node) RemovePointerTo(otherNode *Node) int {
fmt.Println(otherNode)
i := n.GetIndexOf(otherNode)
if i != -1 {
n.Connections = append(n.Connections[:i], n.Connections[i+1:]...)
return i
}
return -1
}
//Disconnect disconnects two nodes by removing pointers for BOTH
func (n *Node) Disconnect(otherNode *Node) int {
i := n.GetIndexOf(otherNode)
j := otherNode.GetIndexOf(n)
if i != -1 && j != -1 {
//remove othernode from n.Connections, and remove n from otherNode.Connections
n.Connections = append(n.Connections[:i], n.Connections[i+1:]...)
otherNode.Connections = append(otherNode.Connections[:j], otherNode.Connections[j+1:]...)
return 1
}
return -1
}
//DisconnectAll disconnects all Nodes
func (n *Node) DisconnectAll() {
for _, v := range n.Connections {
n.Disconnect(v)
}
}
//IsConnectedTo checks if this node is connected (has a pointer) to the given node
func (n *Node) IsConnectedTo(otherNode *Node) bool {
nPointsToOther := n.GetIndexOf(otherNode)
otherPointsToN := otherNode.GetIndexOf(n)
if nPointsToOther != -1 && otherPointsToN != -1 {
return true
}
return false
}
//GetIndexOf gets the index of the specified node.
//May alternatively be used to check if n is pointing to otherNode
func (n *Node) GetIndexOf(otherNode *Node) int {
for i, v := range n.Connections {
if v == otherNode { //addresses are compared
return i
}
}
return -1
}
//InsertMessage inserts a string message in the Messages field of this node
func (n *Node) InsertMessage(message string) {
n.Messages = append(n.Messages, message) //append returns a new slice, so we have to replace it
}