Skip to content
Snippets Groups Projects
Unverified Commit e9b8ce05 authored by capossele's avatar capossele
Browse files

:rotating_light: Fix graph pkg linter warnings

parent a61c8b0b
No related branches found
No related tags found
No related merge requests found
...@@ -5,51 +5,54 @@ import ( ...@@ -5,51 +5,54 @@ import (
"sync" "sync"
) )
type nodeId int32 type nodeID int32
type SymbolTable map[string]nodeId type symbolTable map[string]nodeID
func (s SymbolTable) getId(name string) nodeId { func (s symbolTable) getID(name string) nodeID {
id, ok := s[name] id, ok := s[name]
if !ok { if !ok {
id = nodeId(len(s)) id = nodeID(len(s))
s[name] = id s[name] = id
} }
return id return id
} }
// Graph contains nodes and a symbolTable of a graph.
type Graph struct { type Graph struct {
SymbolTable symbolTable
Nodes nodes
} }
// New returns a graph.
func New(IDs []string) *Graph { func New(IDs []string) *Graph {
g := &Graph{ g := &Graph{
SymbolTable: make(SymbolTable, len(IDs)), symbolTable: make(symbolTable, len(IDs)),
Nodes: make(Nodes, len(IDs)), nodes: make(nodes, len(IDs)),
} }
for index, id := range IDs { for index, id := range IDs {
g.Nodes[index].ID = nodeId(index) g.nodes[index].ID = nodeID(index)
g.SymbolTable[id] = nodeId(index) g.symbolTable[id] = nodeID(index)
} }
return g return g
} }
// AddEdge adds an edge to the given graph.
func (g *Graph) AddEdge(a, b string) { func (g *Graph) AddEdge(a, b string) {
aid := g.SymbolTable.getId(a) aid := g.symbolTable.getID(a)
bid := g.SymbolTable.getId(b) bid := g.symbolTable.getID(b)
g.Nodes.AddEdge(aid, bid) g.nodes.AddEdge(aid, bid)
} }
type Node struct { type node struct {
ID nodeId ID nodeID
// adjacent edges // adjacent edges
Adj []nodeId Adj []nodeID
} }
func (n *Node) add(adjNode *Node) { func (n *node) add(adjNode *node) {
for _, id := range n.Adj { for _, id := range n.Adj {
if id == adjNode.ID { if id == adjNode.ID {
return return
...@@ -58,13 +61,13 @@ func (n *Node) add(adjNode *Node) { ...@@ -58,13 +61,13 @@ func (n *Node) add(adjNode *Node) {
n.Adj = append(n.Adj, adjNode.ID) n.Adj = append(n.Adj, adjNode.ID)
} }
type Nodes []Node type nodes []node
func (nl Nodes) get(id nodeId) *Node { func (nl nodes) get(id nodeID) *node {
return &nl[id] return &nl[id]
} }
func (nl Nodes) AddEdge(a, b nodeId) { func (nl nodes) AddEdge(a, b nodeID) {
an := nl.get(a) an := nl.get(a)
bn := nl.get(b) bn := nl.get(b)
...@@ -72,24 +75,24 @@ func (nl Nodes) AddEdge(a, b nodeId) { ...@@ -72,24 +75,24 @@ func (nl Nodes) AddEdge(a, b nodeId) {
bn.add(an) bn.add(an)
} }
// diameter is the maximum length of a shortest path in the network // Diameter is the maximum length of a shortest path in the network
func (nl Nodes) Diameter() int { func (nl nodes) Diameter() int {
cpus := runtime.NumCPU() cpus := runtime.NumCPU()
numNodes := len(nl) numNodes := len(nl)
nodesPerCpu := numNodes / cpus nodesPerCPU := numNodes / cpus
results := make([]int, cpus) results := make([]int, cpus)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(cpus) wg.Add(cpus)
start := 0 start := 0
for cpu := 0; cpu < cpus; cpu++ { for cpu := 0; cpu < cpus; cpu++ {
end := start + nodesPerCpu end := start + nodesPerCPU
if cpu == cpus-1 { if cpu == cpus-1 {
end = numNodes end = numNodes
} }
go func(cpu int, start, end nodeId) { go func(cpu int, start, end nodeID) {
defer wg.Done() defer wg.Done()
var diameter int var diameter int
q := &list{} q := &list{}
...@@ -100,14 +103,14 @@ func (nl Nodes) Diameter() int { ...@@ -100,14 +103,14 @@ func (nl Nodes) Diameter() int {
depths[i] = -1 depths[i] = -1
} }
df := nl.longestShortestPath(nodeId(id), q, depths) df := nl.longestShortestPath(nodeID(id), q, depths)
if df > diameter { if df > diameter {
diameter = df diameter = df
} }
} }
results[cpu] = diameter results[cpu] = diameter
}(cpu, nodeId(start), nodeId(end)) }(cpu, nodeID(start), nodeID(end))
start += nodesPerCpu start += nodesPerCPU
} }
wg.Wait() wg.Wait()
...@@ -124,9 +127,9 @@ func (nl Nodes) Diameter() int { ...@@ -124,9 +127,9 @@ func (nl Nodes) Diameter() int {
// bfs tracking data // bfs tracking data
type bfsNode int16 type bfsNode int16
func (nodes Nodes) longestShortestPath(start nodeId, q *list, depths []bfsNode) int { func (nl nodes) longestShortestPath(start nodeID, q *list, depths []bfsNode) int {
n := nodes.get(start) n := nl.get(start)
depths[n.ID] = 0 depths[n.ID] = 0
q.pushBack(n) q.pushBack(n)
...@@ -140,7 +143,7 @@ func (nodes Nodes) longestShortestPath(start nodeId, q *list, depths []bfsNode) ...@@ -140,7 +143,7 @@ func (nodes Nodes) longestShortestPath(start nodeId, q *list, depths []bfsNode)
for _, id := range n.Adj { for _, id := range n.Adj {
if depths[id] == -1 { if depths[id] == -1 {
depths[id] = depths[n.ID] + 1 depths[id] = depths[n.ID] + 1
q.pushBack(nodes.get(id)) q.pushBack(nl.get(id))
} }
} }
} }
......
...@@ -2,7 +2,7 @@ package graph ...@@ -2,7 +2,7 @@ package graph
type listElt struct { type listElt struct {
next *listElt next *listElt
node *Node node *node
} }
type list struct { type list struct {
...@@ -12,7 +12,7 @@ type list struct { ...@@ -12,7 +12,7 @@ type list struct {
free *listElt free *listElt
} }
func (l *list) getHead() *Node { func (l *list) getHead() *node {
elt := l.head elt := l.head
if elt == nil { if elt == nil {
return nil return nil
...@@ -32,7 +32,7 @@ func (l *list) getHead() *Node { ...@@ -32,7 +32,7 @@ func (l *list) getHead() *Node {
return n return n
} }
func (l *list) pushBack(n *Node) { func (l *list) pushBack(n *node) {
// Get a free listElt to use to point to this node // Get a free listElt to use to point to this node
elt := l.free elt := l.free
if elt == nil { if elt == nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment