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,12 +198,22 @@ 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.
func (g *Graph) DeleteVertex(xv ...Vertex) {
if len(xv) == 1 {
v := xv[0]
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)
} }
return
}
// handles case len(xv) == 0 and len(xv) > 1
for _, v := range xv {
g.DeleteVertex(v)
}
} }
// AddEdge adds a directed edge to the graph from v1 to v2. // AddEdge adds a directed edge to the graph from v1 to v2.
@@ -215,16 +225,22 @@ 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] {
for _, e := range xe {
if e == edge { if e == edge {
delete(g.adjacency[v1], v2) delete(g.adjacency[v1], v2)
} }
} }
} }
} }
}
// HasVertex returns if the input vertex exists in the graph. // HasVertex returns if the input vertex exists in the graph.
func (g *Graph) HasVertex(v Vertex) bool { func (g *Graph) HasVertex(v Vertex) bool {