1
+ __precompile__ ()
1
2
module MetaGraphs
2
3
using LightGraphs
3
4
using JLD2
@@ -12,7 +13,7 @@ import LightGraphs:
12
13
AbstractGraph, src, dst, edgetype, nv,
13
14
ne, vertices, edges, is_directed,
14
15
add_vertex!, add_edge!, rem_vertex!, rem_edge!,
15
- has_vertex, has_edge, in_neighbors, out_neighbors ,
16
+ has_vertex, has_edge, inneighbors, outneighbors ,
16
17
weights, indegree, outdegree, degree,
17
18
induced_subgraph,
18
19
loadgraph, savegraph, AbstractGraphFormat
@@ -67,8 +68,8 @@ edges(g::AbstractMetaGraph) = edges(g.graph)
67
68
has_vertex (g:: AbstractMetaGraph , x... ) = has_vertex (g. graph, x... )
68
69
@inline has_edge (g:: AbstractMetaGraph , x... ) = has_edge (g. graph, x... )
69
70
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)
72
73
73
74
issubset (g:: T , h:: T ) where T<: AbstractMetaGraph = issubset (g. graph, h. graph)
74
75
@@ -125,11 +126,38 @@ function add_vertex!(g::AbstractMetaGraph, s::Symbol, v)
125
126
end
126
127
127
128
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))
129
135
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)
131
152
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
133
161
return retval
134
162
end
135
163
@@ -227,16 +255,20 @@ has_prop(g::AbstractMetaGraph, u::Integer, v::Integer, prop::Symbol) = has_prop(
227
255
228
256
Bulk set (merge) properties contained in `dict` with graph `g`, vertex `v`, or
229
257
edge `e` (optionally referenced by source vertex `s` and destination vertex `d`).
258
+ Will do nothing if vertex or edge does not exist.
230
259
"""
231
260
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
239
270
end
271
+ end
240
272
# set_props!(g::AbstractMetaGraph, e::SimpleEdge, d::Dict) is dependent on directedness.
241
273
242
274
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
249
281
250
282
Set (replace) property `prop` with value `val` in graph `g`, vertex `v`, or
251
283
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
252
285
"""
253
286
set_prop! (g:: AbstractMetaGraph , prop:: Symbol , val) = set_props! (g, Dict (prop => val))
254
287
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
269
302
270
303
Remove property `prop` from graph `g`, vertex `v`, or edge `e`
271
304
(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.
273
306
"""
274
307
275
308
rem_prop! (g:: AbstractMetaGraph , prop:: Symbol ) = delete! (g. gprops, prop)
339
372
Remove all defined properties from graph `g`, vertex `v`, or edge `e`
340
373
(optionally referenced by source vertex `s` and destination vertex `d`).
341
374
"""
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 )
344
377
clear_props! (g:: AbstractMetaGraph ) = g. gprops = PropDict ()
345
378
346
379
clear_props! (g:: AbstractMetaGraph{T} , u:: Integer , v:: Integer ) where T = clear_props! (g, Edge (T (u), T (v)))
0 commit comments