Skip to content

Commit

Permalink
simple: fix nil edge returns
Browse files Browse the repository at this point in the history
Also clean up code fomatting for consistency.
  • Loading branch information
kortschak committed Dec 31, 2017
1 parent a361656 commit 1298ced
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 68 deletions.
23 changes: 0 additions & 23 deletions graph/simple/directed.go
Expand Up @@ -128,7 +128,6 @@ 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
}

Expand All @@ -140,7 +139,6 @@ func (g *DirectedGraph) Nodes() []graph.Node {
nodes[i] = n
i++
}

return nodes
}

Expand All @@ -167,7 +165,6 @@ func (g *DirectedGraph) From(n graph.Node) []graph.Node {
from[i] = g.nodes[id]
i++
}

return from
}

Expand All @@ -183,7 +180,6 @@ func (g *DirectedGraph) To(n graph.Node) []graph.Node {
to[i] = g.nodes[id]
i++
}

return to
}

Expand All @@ -192,12 +188,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
}
Expand All @@ -208,12 +198,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
Expand All @@ -223,12 +207,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
}
Expand All @@ -240,6 +218,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()])
}
13 changes: 3 additions & 10 deletions graph/simple/undirected.go
Expand Up @@ -131,14 +131,12 @@ func (g *UndirectedGraph) Nodes() []graph.Node {
nodes[i] = n
i++
}

return nodes
}

// Edges returns all the edges in the graph.
func (g *UndirectedGraph) Edges() []graph.Edge {
var edges []graph.Edge

seen := make(map[[2]int64]struct{})
for _, u := range g.edges {
for _, e := range u {
Expand All @@ -152,7 +150,6 @@ func (g *UndirectedGraph) Edges() []graph.Edge {
edges = append(edges, e)
}
}

return edges
}

Expand All @@ -168,7 +165,6 @@ func (g *UndirectedGraph) From(n graph.Node) []graph.Node {
nodes[i] = g.nodes[from]
i++
}

return nodes
}

Expand All @@ -186,20 +182,17 @@ 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.
func (g *UndirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}

return len(g.edges[n.ID()])
}
23 changes: 0 additions & 23 deletions graph/simple/weighted_directed.go
Expand Up @@ -133,7 +133,6 @@ 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
}

Expand All @@ -145,7 +144,6 @@ func (g *WeightedDirectedGraph) Nodes() []graph.Node {
nodes[i] = n
i++
}

return nodes
}

Expand Down Expand Up @@ -183,7 +181,6 @@ func (g *WeightedDirectedGraph) From(n graph.Node) []graph.Node {
from[i] = g.nodes[id]
i++
}

return from
}

Expand All @@ -199,7 +196,6 @@ func (g *WeightedDirectedGraph) To(n graph.Node) []graph.Node {
to[i] = g.nodes[id]
i++
}

return to
}

Expand All @@ -208,12 +204,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
}
Expand All @@ -230,12 +220,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
Expand All @@ -245,12 +229,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
}
Expand Down Expand Up @@ -280,6 +258,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()])
}
15 changes: 3 additions & 12 deletions graph/simple/weighted_undirected.go
Expand Up @@ -136,14 +136,12 @@ func (g *WeightedUndirectedGraph) Nodes() []graph.Node {
nodes[i] = n
i++
}

return nodes
}

// Edges returns all the edges in the graph.
func (g *WeightedUndirectedGraph) Edges() []graph.Edge {
var edges []graph.Edge

seen := make(map[[2]int64]struct{})
for _, u := range g.edges {
for _, e := range u {
Expand All @@ -157,14 +155,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 {
Expand All @@ -178,7 +174,6 @@ func (g *WeightedUndirectedGraph) WeightedEdges() []graph.WeightedEdge {
edges = append(edges, e)
}
}

return edges
}

Expand All @@ -194,7 +189,6 @@ func (g *WeightedUndirectedGraph) From(n graph.Node) []graph.Node {
nodes[i] = g.nodes[from]
i++
}

return nodes
}

Expand Down Expand Up @@ -223,13 +217,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.
Expand All @@ -255,6 +247,5 @@ func (g *WeightedUndirectedGraph) Degree(n graph.Node) int {
if _, ok := g.nodes[n.ID()]; !ok {
return 0
}

return len(g.edges[n.ID()])
}

0 comments on commit 1298ced

Please sign in to comment.