Added further functionality to check for connected nodes and disconnect

This commit is contained in:
Mikkel Milo 2016-02-21 01:05:54 +01:00
parent 5553cc5389
commit 78cf24be0c
4 changed files with 34 additions and 41 deletions

@ -1 +0,0 @@
Subproject commit 65b8cf069318223b1e722b4b36e729e5e9bb9eab

View File

@ -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))

View File

@ -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.
// Its 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)
}

View File

@ -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
}
}