dijkstra/internal/graph.go

43 lines
975 B
Go

package internal
import (
"errors"
)
type Graph struct {
Name string `hcl:"name"`
Nodes []*Node `hcl:"nodes,block"`
Links []*Link `hcl:"links,block"`
}
func (g *Graph) GetStartingNode() (Node, error) {
for i := 0; i < len(g.Nodes); i++ {
n := (*g.Nodes[i])
if n.IsStart == true {
return n, nil
}
}
return Node{}, errors.New("No start node found")
}
func (g *Graph) GetNeighbour(n Node) []Node {
var neighbourNodeName []string
for i := 0; i < len(g.Links); i++ {
if g.Links[i].From == n.Name {
neighbourNodeName = append(neighbourNodeName, g.Links[i].To)
}
}
// create temporary map of node by here name for direct future access
var nodeByName = make(map[string]Node)
for i := 0; i < len(g.Nodes); i++ {
nodeByName[g.Nodes[i].Name] = (*g.Nodes[i])
}
var neighbourNode = make([]Node, len(neighbourNodeName))
for i := 0; i < len(neighbourNodeName); i++ {
neighbourNode[i] = nodeByName[neighbourNodeName[i]]
}
return neighbourNode
}