A small graph package that implements basic graph features
This commit is contained in:
parent
39559bb5a6
commit
7ee0d083bb
1
9fans.net/go
Submodule
1
9fans.net/go
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 65b8cf069318223b1e722b4b36e729e5e9bb9eab
|
1
github.com/golang/lint
Submodule
1
github.com/golang/lint
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 32a87160691b3c96046c0c678fe57c5bef761456
|
1
github.com/nsf/gocode
Submodule
1
github.com/nsf/gocode
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 659c0a429af764118d27692d02b77c544a32cfe3
|
1
github.com/rogpeppe/godef
Submodule
1
github.com/rogpeppe/godef
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit f90a99664522cad305e072e594a262ae1fcdd256
|
1
golang.org/x/tools
Submodule
1
golang.org/x/tools
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit a17fa845d74d17bf6d466f84fd9d3c5602669e53
|
24
graphTester.go
Normal file
24
graphTester.go
Normal file
|
@ -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())
|
||||
}
|
68
lib/graph/hello.go
Normal file
68
lib/graph/hello.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user