Skip to content

Commit

Permalink
Simplify RemoveEdge.
Browse files Browse the repository at this point in the history
  • Loading branch information
echlebek committed Jul 24, 2014
1 parent 9ea0cfe commit 8a93f9f
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,19 @@ func (g *AdjacencyList) RemoveEdge(u, v Vertex) {
if v < u {
u, v = v, u
}

vertices, ok := g.edges[u]
if !ok {
return
}
var (
idx int = -1
vtx Vertex
)
for idx, vtx = range vertices {

for idx, vtx := range vertices {
if vtx == v {
// Remove the edge
g.edges[u] = append(vertices[:idx], vertices[idx+1:len(vertices)]...)
break
}
}
if idx >= 0 {
// Remove the edge
g.edges[u] = append(vertices[:idx], vertices[idx+1:len(vertices)]...)
}
}

// VertexSlice is a convenience type for sorting vertices by ID.
Expand Down Expand Up @@ -184,19 +180,14 @@ func (g *DirectedAdjacencyList) RemoveEdge(u, v Vertex) {
if !ok {
return
}
var (
idx int = -1
vtx Vertex
)
for idx, vtx = range vertices {

for idx, vtx := range vertices {
if vtx == v {
// Remove the edge
g.edges[u] = append(vertices[:idx], vertices[idx+1:len(vertices)]...)
break
}
}
if idx >= 0 {
// Remove the edge
g.edges[u] = append(vertices[:idx], vertices[idx+1:len(vertices)]...)
}
}

func (g *DirectedAdjacencyList) AddVertex() Vertex {
Expand Down

0 comments on commit 8a93f9f

Please sign in to comment.