small adjustments

This commit is contained in:
Mikkel Milo 2016-02-22 14:16:11 +01:00
parent 78cf24be0c
commit ab4115c1e1
3 changed files with 18 additions and 51 deletions

View File

@ -6,8 +6,7 @@ import (
)
func main() {
n := new(graph.Node)
n.Value = "1"
n := &graph.Node{Value: "1"}
n2 := new(graph.Node)
n2.Value = "2"
n3 := new(graph.Node)
@ -16,7 +15,7 @@ func main() {
n.Connect(n2)
n.Connect(n3)
n2.Connect(n3)
n.DisconnectAll()
//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))

View File

@ -1,17 +0,0 @@
package main
import(
"lib/collection"
"fmt"
)
func main() {
var s collection.Stack
s.Push("world")
s.Push("hello, ")
for s.Size() > 0 {
fmt.Print(s.Pop())
}
fmt.Println()
// Output: hello, world
}

View File

@ -4,41 +4,31 @@ import (
"fmt"
)
//Node a graph node which can contain connections to other nodes
//Node a graph node which can contain Connections to other nodes
type Node struct {
Value string
connections []*Node
messages []string
Connections []*Node
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
if len(n.Connections) > i && i >= 0 {
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
}
//GetMessages returns all messages in this Node
func (n *Node) GetMessages() []string {
return n.messages
}
//Connect connects two nodes
func (n *Node) Connect(otherNode *Node) {
n.connections = append(n.connections, otherNode)
otherNode.connections = append(otherNode.connections, n)
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)
n.Connections = append(n.Connections, otherNode)
}
}
@ -47,7 +37,7 @@ 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:]...)
n.Connections = append(n.Connections[:i], n.Connections[i+1:]...)
return i
}
return -1
@ -58,9 +48,9 @@ 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:]...)
//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
@ -68,7 +58,7 @@ func (n *Node) Disconnect(otherNode *Node) int {
//DisconnectAll disconnects all Nodes
func (n *Node) DisconnectAll() {
for _, v := range n.connections {
for _, v := range n.Connections {
n.Disconnect(v)
}
}
@ -86,7 +76,7 @@ func (n *Node) IsConnectedTo(otherNode *Node) bool {
//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 {
for i, v := range n.Connections {
if v == otherNode { //addresses are compared
return i
}
@ -94,12 +84,7 @@ func (n *Node) GetIndexOf(otherNode *Node) int {
return -1
}
//NrOfConnectedNodes number of nodes connected to this node
func (n *Node) NrOfConnectedNodes() int {
return len(n.connections)
}
//InsertMessage inserts a string message in the messages field of this node
//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
n.Messages = append(n.Messages, message) //append returns a new slice, so we have to replace it
}