From 173ccf6861756f8d7238f2914628bf22fba26df7 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 5 Mar 2018 01:58:43 -0500 Subject: [PATCH] pgraph: Don't panic on new or nil graphs This adds a bit of flexibility so that we can still run a topological sort on a nil graph. --- pgraph/pgraph.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pgraph/pgraph.go b/pgraph/pgraph.go index 981a0adf..dbec8f63 100644 --- a/pgraph/pgraph.go +++ b/pgraph/pgraph.go @@ -58,7 +58,9 @@ func (g *Graph) Init() error { return fmt.Errorf("can't initialize graph with empty name") } - //g.adjacency = make(map[Vertex]map[Vertex]Edge) // not required + if g.adjacency == nil { + g.adjacency = make(map[Vertex]map[Vertex]Edge) + } //g.kv = make(map[string]interface{}) // not required return nil } @@ -427,6 +429,9 @@ func (g *Graph) DisconnectedGraphs() ([]*Graph, error) { // InDegree returns the count of vertices that point to me in one big lookup map. func (g *Graph) InDegree() map[Vertex]int { result := make(map[Vertex]int) + if g == nil || g.adjacency == nil { + return result + } for k := range g.adjacency { result[k] = 0 // initialize } @@ -442,7 +447,9 @@ func (g *Graph) InDegree() map[Vertex]int { // OutDegree returns the count of vertices that point away in one big lookup map. func (g *Graph) OutDegree() map[Vertex]int { result := make(map[Vertex]int) - + if g == nil || g.adjacency == nil { + return result + } for k := range g.adjacency { result[k] = 0 // initialize for range g.adjacency[k] {