AddVertex uses variadic input to add all listed vertices to the graph.
(xv ...Vertex)
| 229 | |
| 230 | // AddVertex uses variadic input to add all listed vertices to the graph. |
| 231 | func (obj *Graph) AddVertex(xv ...Vertex) { |
| 232 | if obj.adjacency == nil { // initialize on first use |
| 233 | obj.adjacency = make(map[Vertex]map[Vertex]Edge) |
| 234 | } |
| 235 | if obj.revadjmap == nil { // initialize on first use |
| 236 | obj.revadjmap = make(map[Vertex]map[Vertex]Edge) |
| 237 | } |
| 238 | for _, v := range xv { |
| 239 | if v == nil { |
| 240 | panic("nil vertex") |
| 241 | } |
| 242 | // The inner maps start as nil and get allocated by AddEdge on |
| 243 | // first use. Nil maps are safe to range over, read, len() and |
| 244 | // delete() from, so only the writes in AddEdge need to care. |
| 245 | // This halves the allocations for graphs full of vertices. |
| 246 | if _, exists := obj.adjacency[v]; !exists { |
| 247 | obj.adjacency[v] = nil |
| 248 | } |
| 249 | if _, exists := obj.revadjmap[v]; !exists { |
| 250 | obj.revadjmap[v] = nil |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // DeleteVertex uses variadic input to delete all listed vertices from the |
| 256 | // graph. |
no outgoing calls