added further modularity and stuff
This commit is contained in:
parent
7ee0d083bb
commit
72da984139
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user