pgraph: Make some functions variadic for consistency

This will make it more useful when consuming these functions in a Txn
API which might be more convenient as a big one-liner.
This commit is contained in:
James Shubin
2022-11-11 20:20:14 -05:00
parent d8820fa185
commit 76e0345609

View File

@@ -198,11 +198,21 @@ func (g *Graph) AddVertex(xv ...Vertex) {
} }
} }
// DeleteVertex deletes a particular vertex from the graph. // DeleteVertex uses variadic input to delete all listed vertices from the
func (g *Graph) DeleteVertex(v Vertex) { // graph.
delete(g.adjacency, v) func (g *Graph) DeleteVertex(xv ...Vertex) {
for k := range g.adjacency { if len(xv) == 1 {
delete(g.adjacency[k], v) v := xv[0]
delete(g.adjacency, v)
for k := range g.adjacency {
delete(g.adjacency[k], v)
}
return
}
// handles case len(xv) == 0 and len(xv) > 1
for _, v := range xv {
g.DeleteVertex(v)
} }
} }
@@ -215,12 +225,18 @@ func (g *Graph) AddEdge(v1, v2 Vertex, e Edge) {
g.adjacency[v1][v2] = e g.adjacency[v1][v2] = e
} }
// DeleteEdge deletes a particular edge from the graph. // DeleteEdge uses variadic input to delete all the listed edges from the graph.
func (g *Graph) DeleteEdge(e Edge) { func (g *Graph) DeleteEdge(xe ...Edge) {
if len(xe) == 0 {
return
}
// handles case len(xv) > 0
for v1 := range g.adjacency { for v1 := range g.adjacency {
for v2, edge := range g.adjacency[v1] { for v2, edge := range g.adjacency[v1] {
if e == edge { for _, e := range xe {
delete(g.adjacency[v1], v2) if e == edge {
delete(g.adjacency[v1], v2)
}
} }
} }
} }