From 855894b734cff7227298944af281d5bd6b85107c Mon Sep 17 00:00:00 2001 From: Mikkel Milo Date: Thu, 25 Feb 2016 13:49:33 +0100 Subject: [PATCH] Made graph more generic and efficient --- graphTester.go | 20 ++++++----- lib/graph/hello.go | 90 ---------------------------------------------- 2 files changed, 12 insertions(+), 98 deletions(-) delete mode 100644 lib/graph/hello.go diff --git a/graphTester.go b/graphTester.go index 384216c..fa2e898 100644 --- a/graphTester.go +++ b/graphTester.go @@ -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)) } diff --git a/lib/graph/hello.go b/lib/graph/hello.go deleted file mode 100644 index bafc6f3..0000000 --- a/lib/graph/hello.go +++ /dev/null @@ -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 -}