Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/valuegraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,57 @@ end
# rem_vertex!
# ------------------------------------------------------

# TODO
function LG.rem_vertex!(g::ValGraph, v::Integer)
# Procedure: swap v and n, then remove n
v in vertices(g) || return false
n = nv(g)
self_loop_n = false # true if n is self-looped (see #820)

# Swap the vertex values of v and n
if v != n
for i in 1:length(g.vertexvals)
g.vertexvals[i][v] = g.vertexvals[i][n]
end
end

# Swap the edges and edge values of v and n

# remove the in_edges incident to v
neigs_v = copy(inneighbors(g, v))
for s in neigs_v
rem_edge!(g, s, v)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it were not better, if we would work directly with the adjacency lists here - after all this method does not have to solve the abstract case, but only has to work for this graph.

end
values_selfloop = nothing # TODO: fix this type instability
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not worry too much about type instability. If you have some variable of type Union{T, Nothing} for some type T, then Julia will still be able to create efficient code. In general, Julia can generate efficient code, as long as there are not too many types involved in an Union. That's why using @code_warntype will also color this variable yellow and not red.

if v != n
neigs_n = copy(inneighbors(g, n))
for s in neigs_n
values = get_edgeval(g, s, n, :)
# remove the edges incident to the last vertex
rem_edge!(g, s, n)
# add them back as incident to v
if s != n
add_edge!(g, s, v, values)
else # don't add an edge to the last vertex yet - see LG #820.
self_loop_n = true
values_selfloop = copy(values)
end
end
end

if self_loop_n
add_edge!(g, v, v, values_selfloop)
end

pop!(g.fadjlist)
for i in 1:length(g.vertexvals)
pop!(g.vertexvals[i])
end
for i in 1:length(g.edgevals)
pop!(g.edgevals[i])
end

return true
end

# ------------------------------------------------------
# has_edge
Expand Down