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

Commit 6fb3813

Browse files
authored
Merge pull request #29 from JuliaGraphs/sbromberger/fix-22
fixes #22 and a few other items, adds precompilation
2 parents 17334c4 + 9bd7567 commit 6fb3813

File tree

5 files changed

+91
-30
lines changed

5 files changed

+91
-30
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 0.6
2-
LightGraphs 0.11
2+
LightGraphs 0.12
33
JLD2

src/MetaGraphs.jl

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
__precompile__()
12
module MetaGraphs
23
using LightGraphs
34
using JLD2
@@ -12,7 +13,7 @@ import LightGraphs:
1213
AbstractGraph, src, dst, edgetype, nv,
1314
ne, vertices, edges, is_directed,
1415
add_vertex!, add_edge!, rem_vertex!, rem_edge!,
15-
has_vertex, has_edge, in_neighbors, out_neighbors,
16+
has_vertex, has_edge, inneighbors, outneighbors,
1617
weights, indegree, outdegree, degree,
1718
induced_subgraph,
1819
loadgraph, savegraph, AbstractGraphFormat
@@ -67,8 +68,8 @@ edges(g::AbstractMetaGraph) = edges(g.graph)
6768
has_vertex(g::AbstractMetaGraph, x...) = has_vertex(g.graph, x...)
6869
@inline has_edge(g::AbstractMetaGraph, x...) = has_edge(g.graph, x...)
6970

70-
in_neighbors(g::AbstractMetaGraph, v::Integer) = in_neighbors(g.graph, v)
71-
out_neighbors(g::AbstractMetaGraph, v::Integer) = fadj(g.graph, v)
71+
inneighbors(g::AbstractMetaGraph, v::Integer) = inneighbors(g.graph, v)
72+
outneighbors(g::AbstractMetaGraph, v::Integer) = fadj(g.graph, v)
7273

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

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

127128
function rem_vertex!(g::AbstractMetaGraph, v::Integer)
128-
lastprops = props(g, nv(g))
129+
v in vertices(g) || return false
130+
lastv = nv(g)
131+
lastvprops = props(g, lastv)
132+
133+
lasteoutprops = Dict(n => props(g, lastv, n) for n in outneighbors(g, lastv))
134+
lasteinprops = Dict(n => props(g, n, lastv) for n in inneighbors(g, lastv))
129135
clear_props!(g, v)
130-
delete!(g.vprops, nv(g))
136+
for n in outneighbors(g, lastv)
137+
clear_props!(g, lastv, n)
138+
end
139+
140+
for n in inneighbors(g, lastv)
141+
clear_props!(g, n, lastv)
142+
end
143+
144+
for n in outneighbors(g, v)
145+
clear_props!(g, v, n)
146+
end
147+
148+
for n in inneighbors(g, v)
149+
clear_props!(g, n, v)
150+
end
151+
clear_props!(g, lastv)
131152
retval = rem_vertex!(g.graph, v)
132-
retval && set_props!(g, v, lastprops)
153+
retval && set_props!(g, v, lastvprops)
154+
for n in outneighbors(g, v)
155+
set_props!(g, v, n, lasteoutprops[n])
156+
end
157+
158+
for n in inneighbors(g, v)
159+
set_props!(g, n, v, lasteinprops[n])
160+
end
133161
return retval
134162
end
135163

@@ -227,16 +255,20 @@ has_prop(g::AbstractMetaGraph, u::Integer, v::Integer, prop::Symbol) = has_prop(
227255
228256
Bulk set (merge) properties contained in `dict` with graph `g`, vertex `v`, or
229257
edge `e` (optionally referenced by source vertex `s` and destination vertex `d`).
258+
Will do nothing if vertex or edge does not exist.
230259
"""
231260
set_props!(g::AbstractMetaGraph, d::Dict) = merge!(g.gprops, d)
232-
set_props!(g::AbstractMetaGraph, v::Integer, d::Dict) =
233-
if length(intersect(keys(d), g.indices)) != 0
234-
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
235-
elseif !_hasdict(g, v)
236-
g.vprops[v] = d
237-
else
238-
merge!(g.vprops[v], d)
261+
function set_props!(g::AbstractMetaGraph, v::Integer, d::Dict)
262+
if has_vertex(g, v)
263+
if length(intersect(keys(d), g.indices)) != 0
264+
error("The following properties are indexing_props and cannot be updated: $(intersect(keys(d), g.indices))")
265+
elseif !_hasdict(g, v)
266+
g.vprops[v] = d
267+
else
268+
merge!(g.vprops[v], d)
269+
end
239270
end
271+
end
240272
# set_props!(g::AbstractMetaGraph, e::SimpleEdge, d::Dict) is dependent on directedness.
241273

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

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

346379
clear_props!(g::AbstractMetaGraph{T}, u::Integer, v::Integer) where T = clear_props!(g, Edge(T(u), T(v)))

src/metadigraph.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ weighttype(g::MetaDiGraph{T,U}) where T where U = U
5959
props(g::MetaDiGraph, e::SimpleEdge) = get(g.eprops, e, PropDict())
6060

6161
function set_props!(g::MetaDiGraph, e::SimpleEdge, d::Dict)
62-
if !_hasdict(g, e)
63-
g.eprops[e] = d
64-
else
65-
merge!(g.eprops[e], d)
62+
if has_edge(g, e)
63+
if !_hasdict(g, e)
64+
g.eprops[e] = d
65+
else
66+
merge!(g.eprops[e], d)
67+
end
6668
end
6769
end
6870

src/metagraph.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ end
5858

5959
function set_props!(g::MetaGraph, _e::SimpleEdge, d::Dict)
6060
e = LightGraphs.is_ordered(_e) ? _e : reverse(_e)
61-
if !_hasdict(g, e)
62-
g.eprops[e] = d
63-
else
64-
merge!(g.eprops[e], d)
61+
if has_edge(g, e)
62+
if !_hasdict(g, e)
63+
g.eprops[e] = d
64+
else
65+
merge!(g.eprops[e], d)
66+
end
6567
end
6668
end
6769

test/metagraphs.jl

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ importall MetaGraphs
4747

4848
@test @inferred(vertices(mg)) == 1:4
4949
@test Edge(2,3) in edges(mg)
50-
@test @inferred(out_neighbors(mg,2)) == in_neighbors(mg,2) == neighbors(mg,2)
50+
@test @inferred(outneighbors(mg,2)) == inneighbors(mg,2) == neighbors(mg,2)
5151
@test @inferred(has_edge(mg, 2, 3))
5252
@test @inferred(has_edge(mg, 3, 2))
5353

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

7272
@test @inferred(zero(mg)) == MetaGraph{eltype(mg), weighttype(mg)}()
73-
@test @inferred(eltype(mg)) == eltype(out_neighbors(mg, 1)) == eltype(nv(mg))
73+
@test @inferred(eltype(mg)) == eltype(outneighbors(mg, 1)) == eltype(nv(mg))
7474
T = @inferred(eltype(mg))
7575
U = @inferred(weighttype(mg))
7676
@test @inferred(nv(MetaGraph{T, U}(6))) == 6
@@ -104,8 +104,8 @@ importall MetaGraphs
104104

105105
@test @inferred(vertices(mg)) == 1:4
106106
@test Edge(2,3) in edges(mg)
107-
@test @inferred(out_neighbors(mg,2)) == [3]
108-
@test @inferred(in_neighbors(mg,2)) == [1]
107+
@test @inferred(outneighbors(mg,2)) == [3]
108+
@test @inferred(inneighbors(mg,2)) == [1]
109109
@test @inferred(has_edge(mg, 2, 3))
110110
@test @inferred(!has_edge(mg, 3, 2))
111111

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

130130
@test @inferred(zero(mg)) == MetaDiGraph{eltype(mg), weighttype(mg)}()
131-
@test @inferred(eltype(mg)) == eltype(out_neighbors(mg, 1)) == eltype(nv(mg))
131+
@test @inferred(eltype(mg)) == eltype(outneighbors(mg, 1)) == eltype(nv(mg))
132132
T = @inferred(eltype(mg))
133133
U = @inferred(weighttype(mg))
134134
@test @inferred(nv(MetaDiGraph{T, U}(6))) == 6
@@ -306,6 +306,30 @@ importall MetaGraphs
306306
@test get_prop(mga, 1, :name) == "5"
307307
@test isempty(props(mga, 5))
308308

309+
# test for #22
310+
mga = MetaGraph(PathGraph(4))
311+
set_prop!(mga, 1, 2, :name, "1, 2")
312+
set_prop!(mga, 2, 3, :name, "2, 3")
313+
set_prop!(mga, 3, 4, :name, "3, 4")
314+
@test rem_vertex!(mga, 2)
315+
@test nv(mga) == 3
316+
@test ne(mga) == 1
317+
@test length(mga.eprops) == 1 # should only be edge 2=>3
318+
@test props(mga, 2, 3)[:name] == "3, 4"
319+
320+
mga = MetaDiGraph(PathDiGraph(4))
321+
set_prop!(mga, 1, 2, :name, "1, 2")
322+
set_prop!(mga, 2, 3, :name, "2, 3")
323+
set_prop!(mga, 3, 4, :name, "3, 4")
324+
@test rem_vertex!(mga, 2)
325+
@test nv(mga) == 3
326+
@test ne(mga) == 1
327+
@test length(mga.eprops) == 1 # should only be edge 3=>2
328+
@test props(mga, 3, 2)[:name] == "3, 4"
329+
330+
331+
332+
309333
end
310334

311335
@testset "MetaIndexing" begin

0 commit comments

Comments
 (0)