simple: fix nil edge returns
Also clean up code formatting for consistency.
MODIFY
graph/simple/directed.go
graph/simple/directed.go
@@ -128,19 +128,20 @@ func (g *DirectedGraph) Node(id int64) graph.Node {
// Has returns whether the node exists within the graph.
func (g *DirectedGraph) Has(n graph.Node) bool {
_, ok := g.nodes[n.ID()]
-
return ok
}
// Nodes returns all the nodes in the graph.
func (g *DirectedGraph) Nodes() []graph.Node {
- nodes := make([]graph.Node, len(g.from))
+ if len(g.nodes) == 0 {
+ return nil
+ }
+ nodes := make([]graph.Node, len(g.nodes))
i := 0
for _, n := range g.nodes {
nodes[i] = n
i++
}
-
return nodes
}
@@ -167,7 +168,6 @@ func (g *DirectedGraph) From(n graph.Node) []graph.Node {
from[i] = g.nodes[id]
i++
}
-
return from
}
@@ -183,7 +183,6 @@ func (g *DirectedGraph) To(n graph.Node) []graph.Node {
to[i] = g.nodes[id]
i++
}
-
return to
}
@@ -192,12 +191,6 @@ func (g *DirectedGraph) To(n graph.Node) []graph.Node {
func (g *DirectedGraph) HasEdgeBetween(x, y graph.Node) bool {
xid := x.ID()
yid := y.ID()
- if _, ok := g.nodes[xid]; !ok {
- return false
- }
- if _, ok := g.nodes[yid]; !ok {
- return false
- }
if _, ok := g.from[xid][yid]; ok {
return true
}
@@ -208,12 +201,6 @@ func (g *DirectedGraph) HasEdgeBetween(x, y graph.Node) bool {
// Edge returns the edge from u to v if such an edge exists and nil otherwise.
// The node v must be directly reachable from u as defined by the From method.
func (g *DirectedGraph) Edge(u, v graph.Node) graph.Edge {
- if _, ok := g.nodes[u.ID()]; !ok {
- return nil
- }
- if _, ok := g.nodes[v.ID()]; !ok {
- return nil
- }
edge, ok := g.from[u.ID()][v.ID()]
if !ok {
return nil
@@ -223,12 +210,6 @@ func (g *DirectedGraph) Edge(u, v graph.Node) graph.Edge {
// HasEdgeFromTo returns whether an edge exists in the graph from u to v.
func (g *DirectedGraph) HasEdgeFromTo(u, v graph.Node) bool {
- if _, ok := g.nodes[u.ID()]; !ok {
- return false
- }
- if _, ok := g.nodes[v.ID()]; !ok {
- return false
- }
if _, ok := g.from[u.ID()][v.ID()]; !ok {
return false
}
@@ -240,6 +221,5 @@ func (g *DirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}
-
return len(g.from[n.ID()]) + len(g.to[n.ID()])
}

MODIFY
graph/simple/undirected.go
graph/simple/undirected.go
@@ -125,20 +125,24 @@ func (g *UndirectedGraph) Has(n graph.Node) bool {
// Nodes returns all the nodes in the graph.
func (g *UndirectedGraph) Nodes() []graph.Node {
+ if len(g.nodes) == 0 {
+ return nil
+ }
nodes := make([]graph.Node, len(g.nodes))
i := 0
for _, n := range g.nodes {
nodes[i] = n
i++
}
-
return nodes
}
// Edges returns all the edges in the graph.
func (g *UndirectedGraph) Edges() []graph.Edge {
+ if len(g.edges) == 0 {
+ return nil
+ }
var edges []graph.Edge
-
seen := make(map[[2]int64]struct{})
for _, u := range g.edges {
for _, e := range u {
@@ -152,7 +156,6 @@ func (g *UndirectedGraph) Edges() []graph.Edge {
edges = append(edges, e)
}
}
-
return edges
}
@@ -168,7 +171,6 @@ func (g *UndirectedGraph) From(n graph.Node) []graph.Node {
nodes[i] = g.nodes[from]
i++
}
-
return nodes
}
@@ -186,13 +188,11 @@ func (g *UndirectedGraph) Edge(u, v graph.Node) graph.Edge {
// EdgeBetween returns the edge between nodes x and y.
func (g *UndirectedGraph) EdgeBetween(x, y graph.Node) graph.Edge {
- // We don't need to check if neigh exists because
- // it's implicit in the edges access.
- if !g.Has(x) {
+ edge, ok := g.edges[x.ID()][y.ID()]
+ if !ok {
return nil
}
-
- return g.edges[x.ID()][y.ID()]
+ return edge
}
// Degree returns the degree of n in g.
@@ -200,6 +200,5 @@ func (g *UndirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}
-
return len(g.edges[n.ID()])
}

MODIFY
graph/simple/weighted_directed.go
graph/simple/weighted_directed.go
@@ -133,19 +133,20 @@ func (g *WeightedDirectedGraph) Node(id int64) graph.Node {
// Has returns whether the node exists within the graph.
func (g *WeightedDirectedGraph) Has(n graph.Node) bool {
_, ok := g.nodes[n.ID()]
-
return ok
}
// Nodes returns all the nodes in the graph.
func (g *WeightedDirectedGraph) Nodes() []graph.Node {
- nodes := make([]graph.Node, len(g.from))
+ if len(g.from) == 0 {
+ return nil
+ }
+ nodes := make([]graph.Node, len(g.nodes))
i := 0
for _, n := range g.nodes {
nodes[i] = n
i++
}
-
return nodes
}
@@ -183,7 +184,6 @@ func (g *WeightedDirectedGraph) From(n graph.Node) []graph.Node {
from[i] = g.nodes[id]
i++
}
-
return from
}
@@ -199,7 +199,6 @@ func (g *WeightedDirectedGraph) To(n graph.Node) []graph.Node {
to[i] = g.nodes[id]
i++
}
-
return to
}
@@ -208,12 +207,6 @@ func (g *WeightedDirectedGraph) To(n graph.Node) []graph.Node {
func (g *WeightedDirectedGraph) HasEdgeBetween(x, y graph.Node) bool {
xid := x.ID()
yid := y.ID()
- if _, ok := g.nodes[xid]; !ok {
- return false
- }
- if _, ok := g.nodes[yid]; !ok {
- return false
- }
if _, ok := g.from[xid][yid]; ok {
return true
}
@@ -230,12 +223,6 @@ func (g *WeightedDirectedGraph) Edge(u, v graph.Node) graph.Edge {
// WeightedEdge returns the weighted edge from u to v if such an edge exists and nil otherwise.
// The node v must be directly reachable from u as defined by the From method.
func (g *WeightedDirectedGraph) WeightedEdge(u, v graph.Node) graph.WeightedEdge {
- if _, ok := g.nodes[u.ID()]; !ok {
- return nil
- }
- if _, ok := g.nodes[v.ID()]; !ok {
- return nil
- }
edge, ok := g.from[u.ID()][v.ID()]
if !ok {
return nil
@@ -245,12 +232,6 @@ func (g *WeightedDirectedGraph) WeightedEdge(u, v graph.Node) graph.WeightedEdge
// HasEdgeFromTo returns whether an edge exists in the graph from u to v.
func (g *WeightedDirectedGraph) HasEdgeFromTo(u, v graph.Node) bool {
- if _, ok := g.nodes[u.ID()]; !ok {
- return false
- }
- if _, ok := g.nodes[v.ID()]; !ok {
- return false
- }
if _, ok := g.from[u.ID()][v.ID()]; !ok {
return false
}
@@ -280,6 +261,5 @@ func (g *WeightedDirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}
-
return len(g.from[n.ID()]) + len(g.to[n.ID()])
}

MODIFY
graph/simple/weighted_undirected.go
graph/simple/weighted_undirected.go
@@ -130,20 +130,24 @@ func (g *WeightedUndirectedGraph) Has(n graph.Node) bool {
// Nodes returns all the nodes in the graph.
func (g *WeightedUndirectedGraph) Nodes() []graph.Node {
+ if len(g.nodes) == 0 {
+ return nil
+ }
nodes := make([]graph.Node, len(g.nodes))
i := 0
for _, n := range g.nodes {
nodes[i] = n
i++
}
-
return nodes
}
// Edges returns all the edges in the graph.
func (g *WeightedUndirectedGraph) Edges() []graph.Edge {
+ if len(g.edges) == 0 {
+ return nil
+ }
var edges []graph.Edge
-
seen := make(map[[2]int64]struct{})
for _, u := range g.edges {
for _, e := range u {
@@ -157,14 +161,12 @@ func (g *WeightedUndirectedGraph) Edges() []graph.Edge {
edges = append(edges, e)
}
}
-
return edges
}
// WeightedEdges returns all the weighted edges in the graph.
func (g *WeightedUndirectedGraph) WeightedEdges() []graph.WeightedEdge {
var edges []graph.WeightedEdge
-
seen := make(map[[2]int64]struct{})
for _, u := range g.edges {
for _, e := range u {
@@ -178,7 +180,6 @@ func (g *WeightedUndirectedGraph) WeightedEdges() []graph.WeightedEdge {
edges = append(edges, e)
}
}
-
return edges
}
@@ -194,7 +195,6 @@ func (g *WeightedUndirectedGraph) From(n graph.Node) []graph.Node {
nodes[i] = g.nodes[from]
i++
}
-
return nodes
}
@@ -223,13 +223,11 @@ func (g *WeightedUndirectedGraph) EdgeBetween(x, y graph.Node) graph.Edge {
// WeightedEdgeBetween returns the weighted edge between nodes x and y.
func (g *WeightedUndirectedGraph) WeightedEdgeBetween(x, y graph.Node) graph.WeightedEdge {
- // We don't need to check if neigh exists because
- // it's implicit in the edges access.
- if !g.Has(x) {
+ edge, ok := g.edges[x.ID()][y.ID()]
+ if !ok {
return nil
}
-
- return g.edges[x.ID()][y.ID()]
+ return edge
}
// Weight returns the weight for the edge between x and y if Edge(x, y) returns a non-nil Edge.
@@ -255,6 +253,5 @@ func (g *WeightedUndirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}
-
return len(g.edges[n.ID()])
}

simple: fix nil edge returns

Also clean up code formatting for consistency.