diff --git a/9fans.net/go b/9fans.net/go new file mode 160000 index 0000000..65b8cf0 --- /dev/null +++ b/9fans.net/go @@ -0,0 +1 @@ +Subproject commit 65b8cf069318223b1e722b4b36e729e5e9bb9eab diff --git a/github.com/golang/lint b/github.com/golang/lint new file mode 160000 index 0000000..32a8716 --- /dev/null +++ b/github.com/golang/lint @@ -0,0 +1 @@ +Subproject commit 32a87160691b3c96046c0c678fe57c5bef761456 diff --git a/github.com/mikkelmilo/test.txt b/github.com/mikkelmilo/test.txt deleted file mode 100644 index e69de29..0000000 diff --git a/github.com/nsf/gocode b/github.com/nsf/gocode new file mode 160000 index 0000000..659c0a4 --- /dev/null +++ b/github.com/nsf/gocode @@ -0,0 +1 @@ +Subproject commit 659c0a429af764118d27692d02b77c544a32cfe3 diff --git a/github.com/rogpeppe/godef b/github.com/rogpeppe/godef new file mode 160000 index 0000000..f90a996 --- /dev/null +++ b/github.com/rogpeppe/godef @@ -0,0 +1 @@ +Subproject commit f90a99664522cad305e072e594a262ae1fcdd256 diff --git a/golang.org/x/tools b/golang.org/x/tools new file mode 160000 index 0000000..a17fa84 --- /dev/null +++ b/golang.org/x/tools @@ -0,0 +1 @@ +Subproject commit a17fa845d74d17bf6d466f84fd9d3c5602669e53 diff --git a/graphTester.go b/graphTester.go new file mode 100644 index 0000000..f3e2494 --- /dev/null +++ b/graphTester.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "lib/graph" +) + +func main() { + n := graph.Node{Value: "1"} + n2 := graph.Node{Value: "2"} + n3 := graph.Node{Value: "3"} + + 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.InsertMessage("message string") + n.InsertMessage("2") + fmt.Println(n.GetMessages()) +} diff --git a/hello.go b/hello.go index 5395750..a4c94ae 100644 --- a/hello.go +++ b/hello.go @@ -1,7 +1,7 @@ package main import( - "lib" + "lib/collection" "fmt" ) diff --git a/lib/stack.go b/lib/collection/stack.go similarity index 100% rename from lib/stack.go rename to lib/collection/stack.go diff --git a/lib/graph/hello.go b/lib/graph/hello.go new file mode 100644 index 0000000..6bce4b1 --- /dev/null +++ b/lib/graph/hello.go @@ -0,0 +1,68 @@ +package graph + +//Node a graph node which can contain connections to other nodes +type Node struct { + Value string + connections []*Node + messages []string +} + +//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) { + //nref := &otherNode + n.connections = append(n.connections, otherNode) + otherNode.connections = append(otherNode.connections, n) +} + +//Disconnect disconnects to nodes +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:]...) + return 1 + } + return -1 +} + +//IsConnectedTo checks if this node is connected to the given node +func (n *Node) IsConnectedTo(otherNode *Node) bool { + if n.GetIndexOf(otherNode) != -1 { + return true + } + return false +} + +//GetIndexOf gets the index of the specified node +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 + return i + } + } + 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 +func (n *Node) InsertMessage(message string) { + n.messages = append(n.messages, message) //append returns a new slice, so we have to replace it +}