From 78cf24be0c090f66e17c4e676ba474ab4c00bebe Mon Sep 17 00:00:00 2001 From: Mikkel Milo Date: Sun, 21 Feb 2016 01:05:54 +0100 Subject: [PATCH] Added further functionality to check for connected nodes and disconnect --- 9fans.net/go | 1 - graphTester.go | 15 +++++++++------ lib/collection/stack.go | 27 --------------------------- lib/graph/hello.go | 32 +++++++++++++++++++++++++------- 4 files changed, 34 insertions(+), 41 deletions(-) delete mode 160000 9fans.net/go delete mode 100644 lib/collection/stack.go diff --git a/9fans.net/go b/9fans.net/go deleted file mode 160000 index 65b8cf0..0000000 --- a/9fans.net/go +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 65b8cf069318223b1e722b4b36e729e5e9bb9eab diff --git a/graphTester.go b/graphTester.go index 598f574..e5065a6 100644 --- a/graphTester.go +++ b/graphTester.go @@ -6,15 +6,18 @@ import ( ) func main() { - n := &graph.Node{Value: "1"} - n2 := &graph.Node{Value: "2"} - n3 := &graph.Node{Value: "3"} + n := new(graph.Node) + n.Value = "1" + n2 := new(graph.Node) + n2.Value = "2" + n3 := new(graph.Node) + n3.Value = "3" n.Connect(n2) n.Connect(n3) - n3.Connect(n.GetConnections()[0]) - //n.Disconnect(&n2) test - //fmt.Println(n.RemovePointerTo(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)) diff --git a/lib/collection/stack.go b/lib/collection/stack.go deleted file mode 100644 index 0c37828..0000000 --- a/lib/collection/stack.go +++ /dev/null @@ -1,27 +0,0 @@ -// Package collection implements a generic stack. -package collection - -// The zero value for Stack is an empty stack ready to use. -type Stack struct { - data []interface{} -} - -// Push adds x to the top of the stack. -func (s *Stack) Push(x interface{}) { - s.data = append(s.data, x) -} - -// Pop removes and returns the top element of the stack. -// It’s a run-time error to call Pop on an empty stack. -func (s *Stack) Pop() interface{} { - i := len(s.data) - 1 - res := s.data[i] - s.data[i] = nil // to avoid memory leak - s.data = s.data[:i] - return res -} - -// Size returns the number of elements in the stack. -func (s *Stack) Size() int { - return len(s.data) -} diff --git a/lib/graph/hello.go b/lib/graph/hello.go index bfcdb32..2e7f509 100644 --- a/lib/graph/hello.go +++ b/lib/graph/hello.go @@ -11,6 +11,14 @@ type Node struct { messages []string } +//GetConnection returns the i'th connected node if it exists +func (n *Node) GetConnection(i int) (*Node, error) { + if n.NrOfConnectedNodes() > i { + return n.connections[i], nil + } + return n, fmt.Errorf("slice out of bounds") +} + //GetConnections returns all connections func (n *Node) GetConnections() []*Node { return n.connections @@ -29,7 +37,9 @@ func (n *Node) Connect(otherNode *Node) { //SetPointTo sets this Node to point towards the given Node func (n *Node) SetPointTo(otherNode *Node) { - n.connections = append(n.connections, otherNode) + if n.IsConnectedTo(otherNode) == false { + n.connections = append(n.connections, otherNode) + } } //RemovePointerTo removes pointer to a node (a "child") @@ -56,20 +66,28 @@ func (n *Node) Disconnect(otherNode *Node) int { 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 { - if n.GetIndexOf(otherNode) != -1 { + 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 +//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 := range n.connections { - //fmt.Printf("%p and %p \n", n.connections[i], otherNode) - - if n.connections[i] == otherNode { //addresses are compared + for i, v := range n.connections { + if v == otherNode { //addresses are compared return i } }