pgraph: Panic if vertex is nil

These should be caught early.
This commit is contained in:
James Shubin
2024-12-18 13:49:13 -05:00
parent 2561dba8f5
commit 5858c8b501

View File

@@ -208,6 +208,9 @@ func (g *Graph) AddVertex(xv ...Vertex) {
g.adjacency = make(map[Vertex]map[Vertex]Edge) g.adjacency = make(map[Vertex]map[Vertex]Edge)
} }
for _, v := range xv { for _, v := range xv {
if v == nil {
panic("nil vertex")
}
if _, exists := g.adjacency[v]; !exists { if _, exists := g.adjacency[v]; !exists {
g.adjacency[v] = make(map[Vertex]Edge) g.adjacency[v] = make(map[Vertex]Edge)
} }
@@ -219,6 +222,9 @@ func (g *Graph) AddVertex(xv ...Vertex) {
func (g *Graph) DeleteVertex(xv ...Vertex) { func (g *Graph) DeleteVertex(xv ...Vertex) {
if len(xv) == 1 { if len(xv) == 1 {
v := xv[0] v := xv[0]
if v == nil {
panic("nil vertex")
}
delete(g.adjacency, v) delete(g.adjacency, v)
for k := range g.adjacency { for k := range g.adjacency {
delete(g.adjacency[k], v) delete(g.adjacency[k], v)