From 72da9841396a5718f264e96424dd2500fde981cf Mon Sep 17 00:00:00 2001 From: milo Date: Sat, 20 Feb 2016 20:08:56 +0100 Subject: [PATCH] added further modularity and stuff --- graphTester.go | 23 +++++++++++------------ lib/graph/hello.go | 25 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/graphTester.go b/graphTester.go index f3e2494..4a8a637 100644 --- a/graphTester.go +++ b/graphTester.go @@ -6,19 +6,18 @@ import ( ) func main() { - n := graph.Node{Value: "1"} - n2 := graph.Node{Value: "2"} - n3 := graph.Node{Value: "3"} + n := &graph.Node{Value: "1"} + n2 := &graph.Node{Value: "2"} + n3 := &graph.Node{Value: "3"} - n.Connect(&n2) - n.Connect(&n3) + n.Connect(n2) + n.Connect(n3) n3.Connect(n.GetConnections()[0]) - n.Disconnect(&n2) - fmt.Println("n <-> n2: ", n2.IsConnectedTo(&n), n.IsConnectedTo(&n2)) - fmt.Println("n <-> n3: ", n3.IsConnectedTo(&n), n.IsConnectedTo(&n3)) - fmt.Println("n2 <-> n3: ", n2.IsConnectedTo(&n3), n3.IsConnectedTo(&n2)) + //n.Disconnect(&n2) + //fmt.Println(n.RemovePointerTo(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.InsertMessage("message string") - n.InsertMessage("2") - fmt.Println(n.GetMessages()) + fmt.Println(n.NrOfConnectedNodes()) } diff --git a/lib/graph/hello.go b/lib/graph/hello.go index 6bce4b1..bfcdb32 100644 --- a/lib/graph/hello.go +++ b/lib/graph/hello.go @@ -1,5 +1,9 @@ package graph +import ( + "fmt" +) + //Node a graph node which can contain connections to other nodes type Node struct { Value string @@ -19,12 +23,27 @@ func (n *Node) GetMessages() []string { //Connect connects two nodes func (n *Node) Connect(otherNode *Node) { - //nref := &otherNode n.connections = append(n.connections, otherNode) otherNode.connections = append(otherNode.connections, n) } -//Disconnect disconnects to nodes +//SetPointTo sets this Node to point towards the given Node +func (n *Node) SetPointTo(otherNode *Node) { + 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) @@ -37,7 +56,7 @@ func (n *Node) Disconnect(otherNode *Node) int { return -1 } -//IsConnectedTo checks if this node is connected to the given node +//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 { return true