Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
fixes #22 and a few other items, adds precompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbromberger committed Feb 28, 2018
1 parent 17334c4 commit 9bd7567
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 30 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
LightGraphs 0.11
LightGraphs 0.12
JLD2
65 changes: 49 additions & 16 deletions src/MetaGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__precompile__()
module MetaGraphs
using LightGraphs
using JLD2
Expand All @@ -12,7 +13,7 @@ import LightGraphs:
AbstractGraph, src, dst, edgetype, nv,
ne, vertices, edges, is_directed,
add_vertex!, add_edge!, rem_vertex!, rem_edge!,
has_vertex, has_edge, in_neighbors, out_neighbors,
has_vertex, has_edge, inneighbors, outneighbors,
weights, indegree, outdegree, degree,
induced_subgraph,
loadgraph, savegraph, AbstractGraphFormat
Expand Down Expand Up @@ -67,8 +68,8 @@ edges(g::AbstractMetaGraph) = edges(g.graph)
has_vertex(g::AbstractMetaGraph, x...) = has_vertex(g.graph, x...)
@inline has_edge(g::AbstractMetaGraph, x...) = has_edge(g.graph, x...)

in_neighbors(g::AbstractMetaGraph, v::Integer) = in_neighbors(g.graph, v)
out_neighbors(g::AbstractMetaGraph, v::Integer) = fadj(g.graph, v)
inneighbors(g::AbstractMetaGraph, v::Integer) = inneighbors(g.graph, v)
outneighbors(g::AbstractMetaGraph, v::Integer) = fadj(g.graph, v)

issubset(g::T, h::T) where T<:AbstractMetaGraph = issubset(g.graph, h.graph)

Expand Down Expand Up @@ -125,11 +126,38 @@ function add_vertex!(g::AbstractMetaGraph, s::Symbol, v)
end

function rem_vertex!(g::AbstractMetaGraph, v::Integer)
lastprops = props(g, nv(g))
v in vertices(g) || return false
lastv = nv(g)
lastvprops = props(g, lastv)

lasteoutprops = Dict(n => props(g, lastv, n) for n in outneighbors(g, lastv))
lasteinprops = Dict(n => props(g, n, lastv) for n in inneighbors(g, lastv))
clear_props!(g, v)
delete!(g.vprops, nv(g))
for n in outneighbors(g, lastv)
clear_props!(g, lastv, n)
end

for n in inneighbors(g, lastv)
clear_props!(g, n, lastv)
end

for n in outneighbors(g, v)
clear_props!(g, v, n)
end

for n in inneighbors(g, v)
clear_props!(g, n, v)
end
clear_props!(g, lastv)
retval = rem_vertex!(g.graph, v)
retval && set_props!(g, v, lastprops)
retval && set_props!(g, v, lastvprops)
for n in outneighbors(g, v)
set_props!(g, v, n, lasteoutprops[n])
end

for n in inneighbors(g, v)
set_props!(g, n, v, lasteinprops[n])
end
return retval
end

Expand Down Expand Up @@ -227,16 +255,20 @@ has_prop(g::AbstractMetaGraph, u::Integer, v::Integer, prop::Symbol) = has_prop(
Bulk set (merge) properties contained in `dict` with graph `g`, vertex `v`, or
edge `e` (optionally referenced by source vertex `s` and destination vertex `d`).
Will do nothing if vertex or edge does not exist.
"""
set_props!(g::AbstractMetaGraph, d::Dict) = merge!(g.gprops, d)
set_props!(g::AbstractMetaGraph, v::Integer, d::Dict) =
if length(intersect(keys(d), g.indices)) != 0
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
elseif !_hasdict(g, v)
g.vprops[v] = d
else
merge!(g.vprops[v], d)
function set_props!(g::AbstractMetaGraph, v::Integer, d::Dict)
if has_vertex(g, v)
if length(intersect(keys(d), g.indices)) != 0
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
elseif !_hasdict(g, v)
g.vprops[v] = d
else
merge!(g.vprops[v], d)
end
end
end
# set_props!(g::AbstractMetaGraph, e::SimpleEdge, d::Dict) is dependent on directedness.

set_props!(g::AbstractMetaGraph{T}, u::Integer, v::Integer, d::Dict) where T = set_props!(g, Edge(T(u), T(v)), d)
Expand All @@ -249,6 +281,7 @@ set_props!(g::AbstractMetaGraph{T}, u::Integer, v::Integer, d::Dict) where T = s
Set (replace) property `prop` with value `val` in graph `g`, vertex `v`, or
edge `e` (optionally referenced by source vertex `s` and destination vertex `d`).
Will return false if vertex or edge does not exist, true otherwise
"""
set_prop!(g::AbstractMetaGraph, prop::Symbol, val) = set_props!(g, Dict(prop => val))
set_prop!(g::AbstractMetaGraph, v::Integer, prop::Symbol, val) =
Expand All @@ -269,7 +302,7 @@ set_prop!(g::AbstractMetaGraph{T}, u::Integer, v::Integer, prop::Symbol, val) wh
Remove property `prop` from graph `g`, vertex `v`, or edge `e`
(optionally referenced by source vertex `s` and destination vertex `d`).
If property does not exist, will not do anything.
If property, vertex, or edge does not exist, will not do anything.
"""

rem_prop!(g::AbstractMetaGraph, prop::Symbol) = delete!(g.gprops, prop)
Expand Down Expand Up @@ -339,8 +372,8 @@ end
Remove all defined properties from graph `g`, vertex `v`, or edge `e`
(optionally referenced by source vertex `s` and destination vertex `d`).
"""
clear_props!(g::AbstractMetaGraph, v::Integer) = _hasdict(g, v) && (g.vprops[v] = PropDict())
clear_props!(g::AbstractMetaGraph, e::SimpleEdge) = _hasdict(g, e) && (g.eprops[e] = PropDict())
clear_props!(g::AbstractMetaGraph, v::Integer) = _hasdict(g, v) && delete!(g.vprops, v)
clear_props!(g::AbstractMetaGraph, e::SimpleEdge) = _hasdict(g, e) && delete!(g.eprops, e)
clear_props!(g::AbstractMetaGraph) = g.gprops = PropDict()

clear_props!(g::AbstractMetaGraph{T}, u::Integer, v::Integer) where T = clear_props!(g, Edge(T(u), T(v)))
Expand Down
10 changes: 6 additions & 4 deletions src/metadigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ weighttype(g::MetaDiGraph{T,U}) where T where U = U
props(g::MetaDiGraph, e::SimpleEdge) = get(g.eprops, e, PropDict())

function set_props!(g::MetaDiGraph, e::SimpleEdge, d::Dict)
if !_hasdict(g, e)
g.eprops[e] = d
else
merge!(g.eprops[e], d)
if has_edge(g, e)
if !_hasdict(g, e)
g.eprops[e] = d
else
merge!(g.eprops[e], d)
end
end
end

Expand Down
10 changes: 6 additions & 4 deletions src/metagraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ end

function set_props!(g::MetaGraph, _e::SimpleEdge, d::Dict)
e = LightGraphs.is_ordered(_e) ? _e : reverse(_e)
if !_hasdict(g, e)
g.eprops[e] = d
else
merge!(g.eprops[e], d)
if has_edge(g, e)
if !_hasdict(g, e)
g.eprops[e] = d
else
merge!(g.eprops[e], d)
end
end
end

Expand Down
34 changes: 29 additions & 5 deletions test/metagraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ importall MetaGraphs

@test @inferred(vertices(mg)) == 1:4
@test Edge(2,3) in edges(mg)
@test @inferred(out_neighbors(mg,2)) == in_neighbors(mg,2) == neighbors(mg,2)
@test @inferred(outneighbors(mg,2)) == inneighbors(mg,2) == neighbors(mg,2)
@test @inferred(has_edge(mg, 2, 3))
@test @inferred(has_edge(mg, 3, 2))

Expand All @@ -70,7 +70,7 @@ importall MetaGraphs
@test @inferred(!rem_vertex!(mga, 10))

@test @inferred(zero(mg)) == MetaGraph{eltype(mg), weighttype(mg)}()
@test @inferred(eltype(mg)) == eltype(out_neighbors(mg, 1)) == eltype(nv(mg))
@test @inferred(eltype(mg)) == eltype(outneighbors(mg, 1)) == eltype(nv(mg))
T = @inferred(eltype(mg))
U = @inferred(weighttype(mg))
@test @inferred(nv(MetaGraph{T, U}(6))) == 6
Expand Down Expand Up @@ -104,8 +104,8 @@ importall MetaGraphs

@test @inferred(vertices(mg)) == 1:4
@test Edge(2,3) in edges(mg)
@test @inferred(out_neighbors(mg,2)) == [3]
@test @inferred(in_neighbors(mg,2)) == [1]
@test @inferred(outneighbors(mg,2)) == [3]
@test @inferred(inneighbors(mg,2)) == [1]
@test @inferred(has_edge(mg, 2, 3))
@test @inferred(!has_edge(mg, 3, 2))

Expand All @@ -128,7 +128,7 @@ importall MetaGraphs
@test @inferred(!rem_vertex!(mga, 10))

@test @inferred(zero(mg)) == MetaDiGraph{eltype(mg), weighttype(mg)}()
@test @inferred(eltype(mg)) == eltype(out_neighbors(mg, 1)) == eltype(nv(mg))
@test @inferred(eltype(mg)) == eltype(outneighbors(mg, 1)) == eltype(nv(mg))
T = @inferred(eltype(mg))
U = @inferred(weighttype(mg))
@test @inferred(nv(MetaDiGraph{T, U}(6))) == 6
Expand Down Expand Up @@ -306,6 +306,30 @@ importall MetaGraphs
@test get_prop(mga, 1, :name) == "5"
@test isempty(props(mga, 5))

# test for #22
mga = MetaGraph(PathGraph(4))
set_prop!(mga, 1, 2, :name, "1, 2")
set_prop!(mga, 2, 3, :name, "2, 3")
set_prop!(mga, 3, 4, :name, "3, 4")
@test rem_vertex!(mga, 2)
@test nv(mga) == 3
@test ne(mga) == 1
@test length(mga.eprops) == 1 # should only be edge 2=>3
@test props(mga, 2, 3)[:name] == "3, 4"

mga = MetaDiGraph(PathDiGraph(4))
set_prop!(mga, 1, 2, :name, "1, 2")
set_prop!(mga, 2, 3, :name, "2, 3")
set_prop!(mga, 3, 4, :name, "3, 4")
@test rem_vertex!(mga, 2)
@test nv(mga) == 3
@test ne(mga) == 1
@test length(mga.eprops) == 1 # should only be edge 3=>2
@test props(mga, 3, 2)[:name] == "3, 4"




end

@testset "MetaIndexing" begin
Expand Down

0 comments on commit 9bd7567

Please sign in to comment.