Skip to content

Commit 022badf

Browse files
committed
return of MetaDiGraph
1 parent 4938dba commit 022badf

File tree

5 files changed

+110
-82
lines changed

5 files changed

+110
-82
lines changed

src/MetaGraphs.jl

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import LightGraphs.SimpleGraphs:
2222
SimpleEdge, fadj, badj
2323

2424
export
25+
AbstractMetaGraph,
26+
MetaGraph,
27+
MetaDiGraph,
2528
meta_graph,
2629
weight_type,
2730
default_weight,
@@ -32,74 +35,66 @@ export
3235
DOTFormat,
3336
reverse
3437

35-
maybe_order_edge(graph, edge) =
36-
if is_directed(graph)
37-
edge
38-
else
39-
if is_ordered(edge)
40-
edge
41-
else
42-
reverse(edge)
43-
end
44-
end
4538

46-
include("metagraph.jl")
39+
abstract type AbstractMetaGraph{Vertex, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight} <: AbstractGraph{Vertex} end
4740

48-
function show(io::IO, meta::MetaGraph{<: Any, <: Any, AtVertex, AtEdge, GraphMeta, <: Any, Weight}) where {AtVertex, AtEdge, GraphMeta, Weight}
41+
function show(io::IO, meta::AbstractMetaGraph{<: Any, <: Any, AtVertex, AtEdge, GraphMeta, <: Any, Weight}) where {AtVertex, AtEdge, GraphMeta, Weight}
4942
print(io, "Meta graph based on a $(meta.inner_graph) with $AtVertex(s) at vertices, $AtEdge(s) at edges, $GraphMeta metadata, $Weight weights, and default weight $(meta.default_weight)")
5043
end
5144

52-
@inline fadj(meta::MetaGraph, arguments...) =
45+
@inline fadj(meta::AbstractMetaGraph, arguments...) =
5346
fadj(meta.inner_graph, arguments...)
54-
@inline badj(meta::MetaGraph, arguments...) =
47+
@inline badj(meta::AbstractMetaGraph, arguments...) =
5548
badj(meta.inner_graph, arguments...)
5649

57-
eltype(meta::MetaGraph) = eltype(meta.inner_graph)
58-
edgetype(meta::MetaGraph) = edgetype(meta.inner_graph)
59-
nv(meta::MetaGraph) = nv(meta.inner_graph)
60-
vertices(meta::MetaGraph) = vertices(meta.inner_graph)
50+
eltype(meta::AbstractMetaGraph) = eltype(meta.inner_graph)
51+
edgetype(meta::AbstractMetaGraph) = edgetype(meta.inner_graph)
52+
nv(meta::AbstractMetaGraph) = nv(meta.inner_graph)
53+
vertices(meta::AbstractMetaGraph) = vertices(meta.inner_graph)
6154

62-
ne(meta::MetaGraph) = ne(meta.inner_graph)
63-
edges(meta::MetaGraph) = edges(meta.inner_graph)
55+
ne(meta::AbstractMetaGraph) = ne(meta.inner_graph)
56+
edges(meta::AbstractMetaGraph) = edges(meta.inner_graph)
6457

65-
has_vertex(meta::MetaGraph, arguments...) =
58+
has_vertex(meta::AbstractMetaGraph, arguments...) =
6659
has_vertex(meta.inner_graph, arguments...)
67-
@inline has_edge(meta::MetaGraph, arguments...) =
60+
@inline has_edge(meta::AbstractMetaGraph, arguments...) =
6861
has_edge(meta.inner_graph, arguments...)
6962

70-
inneighbors(meta::MetaGraph, vertex::Integer) =
63+
inneighbors(meta::AbstractMetaGraph, vertex::Integer) =
7164
inneighbors(meta.inner_graph, vertex)
72-
outneighbors(meta::MetaGraph, vertex::Integer) = fadj(meta.inner_graph, vertex)
65+
outneighbors(meta::AbstractMetaGraph, vertex::Integer) = fadj(meta.inner_graph, vertex)
7366

74-
issubset(meta::MetaGraph, meta2::MetaGraph) =
67+
issubset(meta::AbstractMetaGraph, meta2::AbstractMetaGraph) =
7568
issubset(meta.inner_graph, meta2.inner_graph)
7669

77-
# TODO: not order edges more than once
78-
79-
@inline add_edge!(meta::MetaGraph, arguments...) =
70+
@inline add_edge!(meta::AbstractMetaGraph, arguments...) =
8071
add_edge!(meta.inner_graph, arguments...)
8172

82-
@inline function delete!(meta::MetaGraph, edge::Edge)
73+
function setindex!(meta::AbstractMetaGraph, value, edge::AbstractEdge)
74+
meta.edge_meta[maybe_order_edge(meta, edge)] = value
75+
add_edge!(meta, edge)
76+
end
77+
78+
@inline function delete!(meta::AbstractMetaGraph, edge::Edge)
8379
delete!(meta.edge_meta, maybe_order_edge(meta, edge))
8480
rem_edge!(meta.inner_graph, edge)
8581
end
86-
@inline rem_edge!(meta::MetaGraph, edge::Edge) = delete!(meta, edge)
82+
@inline rem_edge!(meta::AbstractMetaGraph, edge::Edge) = delete!(meta, edge)
8783

88-
add_vertex!(meta::MetaGraph) = add_vertex!(meta.inner_graph)
89-
function push!(meta::MetaGraph, value)
84+
add_vertex!(meta::AbstractMetaGraph) = add_vertex!(meta.inner_graph)
85+
function push!(meta::AbstractMetaGraph, value)
9086
add_vertex!(meta) || return false
9187
last_vertex = nv(meta)
9288
meta[last_vertex] = value
9389
return last_vertex
9490
end
9591

9692
function move_meta!(meta, old_edge::AbstractEdge, new_edge::AbstractEdge)
97-
meta[maybe_order_edge(meta, new_edge)] =
98-
pop!(meta.edge_meta, maybe_order_edge(meta, old_edge))
93+
meta[new_edge] = pop!(meta.edge_meta, maybe_order_edge(meta, old_edge))
9994
return nothing
10095
end
10196

102-
function delete!(meta::MetaGraph, deleted_vertex::Integer)
97+
function delete!(meta::AbstractMetaGraph, deleted_vertex::Integer)
10398
moved_vertex = nv(meta)
10499
# delete meta data for the old vertex
105100
delete!(meta.vertex_meta, deleted_vertex)
@@ -136,22 +131,22 @@ function delete!(meta::MetaGraph, deleted_vertex::Integer)
136131
return result
137132
end
138133

139-
rem_vertex!(meta::MetaGraph, vertex) = delete!(meta, vertex)
134+
rem_vertex!(meta::AbstractMetaGraph, vertex) = delete!(meta, vertex)
140135

141-
struct MetaWeights{Weight <: Real, InnerMetaGraph} <: AbstractMatrix{Weight}
142-
inner_meta_graph::InnerMetaGraph
136+
struct MetaWeights{Weight <: Real, InnerAbstractMetaGraph} <: AbstractMatrix{Weight}
137+
inner_meta_graph::InnerAbstractMetaGraph
143138
end
144139

145140
show(io::IO, weights::MetaWeights) = print(io, "metaweights")
146141
show(io::IO, ::MIME"text/plain", weights::MetaWeights) = show(io, weights)
147142

148-
MetaWeights(meta::MetaGraph) = MetaWeights{weight_type(meta), typeof(meta)}(meta)
143+
MetaWeights(meta::AbstractMetaGraph) = MetaWeights{weight_type(meta), typeof(meta)}(meta)
149144

150145
is_directed(::Type{<: MetaWeights{<: Any, InnerMetaGraph}}) where {InnerMetaGraph} =
151146
is_directed(InnerMetaGraph)
152147

153148
function getindex(weights::MetaWeights{Weight}, in_vertex::Integer, out_vertex::Integer)::Weight where {Weight}
154-
edge = maybe_order_edge(weights, Edge(in_vertex, out_vertex))
149+
edge = Edge(in_vertex, out_vertex)
155150
inner_meta_graph = weights.inner_meta_graph
156151
if haskey(inner_meta_graph, edge)
157152
Weight(inner_meta_graph.weight_function(inner_meta_graph[edge]))
@@ -165,15 +160,18 @@ function size(weights::MetaWeights)
165160
(vertices, vertices)
166161
end
167162

168-
weights(meta::MetaGraph) = MetaWeights(meta)
163+
weights(meta::AbstractMetaGraph) = MetaWeights(meta)
164+
165+
weight_type(meta::AbstractMetaGraph{<: Any, <: Any, <: Any, <: Any, <: Any, <: Any, Weight}) where {Weight} =
166+
Weight
169167

170-
getindex(meta::MetaGraph, vertex::Integer) = meta.vertex_meta[vertex]
171-
getindex(meta::MetaGraph, edge::AbstractEdge) = meta.edge_meta[edge]
168+
getindex(meta::AbstractMetaGraph, vertex::Integer) = meta.vertex_meta[vertex]
169+
getindex(meta::AbstractMetaGraph, edge::AbstractEdge) = meta.edge_meta[edge]
172170

173-
haskey(meta::MetaGraph, vertex::Integer) = haskey(meta.vertex_meta, vertex)
174-
haskey(meta::MetaGraph, edge::AbstractEdge) = haskey(meta.edge_meta, edge)
171+
haskey(meta::AbstractMetaGraph, vertex::Integer) = haskey(meta.vertex_meta, vertex)
172+
haskey(meta::AbstractMetaGraph, edge::AbstractEdge) = haskey(meta.edge_meta, edge)
175173

176-
function setindex!(meta::MetaGraph, value, vertex::Integer)
174+
function setindex!(meta::AbstractMetaGraph, value, vertex::Integer)
177175
meta.vertex_meta[vertex] = value
178176
return nothing
179177
end
@@ -192,7 +190,7 @@ julia> weight_function(meta_graph(Graph(), weight_function = identity))(0)
192190
0
193191
```
194192
"""
195-
weight_function(meta::MetaGraph) = meta.weight_function
193+
weight_function(meta::AbstractMetaGraph) = meta.weight_function
196194

197195
"""
198196
default_weight(meta)
@@ -208,7 +206,7 @@ julia> default_weight(meta_graph(Graph(), default_weight = 2.0))
208206
2.0
209207
```
210208
"""
211-
default_weight(meta::MetaGraph) = meta.default_weight
209+
default_weight(meta::AbstractMetaGraph) = meta.default_weight
212210

213211
"""
214212
filter_edges(meta, a_function)
@@ -231,7 +229,7 @@ julia> filter_edges(test, isequal(:a))
231229
Edge 1 => 2
232230
```
233231
"""
234-
filter_edges(meta::MetaGraph, a_function::Function) =
232+
filter_edges(meta::AbstractMetaGraph, a_function::Function) =
235233
findall(a_function, meta.edge_meta)
236234

237235
"""
@@ -254,7 +252,7 @@ julia> filter_vertices(test, isequal(:a))
254252
1
255253
```
256254
"""
257-
filter_vertices(meta::MetaGraph, a_function::Function) =
255+
filter_vertices(meta::AbstractMetaGraph, a_function::Function) =
258256
findall(a_function, meta.vertex_meta)
259257

260258
function copy_meta!(old_meta, new_meta, vertex_map)
@@ -265,21 +263,19 @@ function copy_meta!(old_meta, new_meta, vertex_map)
265263
end
266264
for new_edge in edges(new_meta)
267265
in_vertex, out_vertex = Tuple(new_edge)
268-
old_edge = maybe_order_edge(old_meta,
269-
Edge(vertex_map[in_vertex], vertex_map[out_vertex])
270-
)
266+
old_edge = Edge(vertex_map[in_vertex], vertex_map[out_vertex])
271267
if haskey(old_meta, old_edge)
272268
new_meta[new_edge] = old_meta[old_edge]
273269
end
274270
end
275271
return nothing
276272
end
277273

278-
function induced_subgraph(meta::MetaGraph{Vertex}, vertices::AbstractVector{Vertex}) where {Vertex <: Integer}
274+
function induced_subgraph(meta::AbstractMetaGraph{Vertex}, vertices::AbstractVector{Vertex}) where {Vertex <: Integer}
279275
induced_graph, vertex_map =
280276
induced_subgraph(meta.inner_graph, vertices)
281277
induced_meta =
282-
MetaGraph(induced_graph,
278+
typeof(meta)(induced_graph,
283279
empty(meta.vertex_meta),
284280
empty(meta.edge_meta),
285281
meta.graph_meta,
@@ -294,11 +290,21 @@ end
294290
# how this might work, but if the property is a vector, a generic way to append to
295291
# it would be a good thing.
296292

297-
==(meta::MetaGraph, meta2::MetaGraph) =
298-
meta.inner_graph == meta2.inner_graph
293+
==(meta::AbstractMetaGraph, meta2::AbstractMetaGraph) = meta.inner_graph == meta2.inner_graph
294+
295+
copy(meta::AbstractMetaGraph) = deepcopy(meta)
299296

300-
copy(meta::MetaGraph) = deepcopy(meta)
297+
zero(meta::AbstractMetaGraph{<:Any, InnerGraph, AtVertex, AtEdge, GraphMeta}) where {InnerGraph, AtVertex, AtEdge, GraphMeta} =
298+
meta_graph(InnerGraph();
299+
AtVertex = AtVertex,
300+
AtEdge = AtEdge,
301+
graph_meta = GraphMeta(),
302+
weight_function = meta.weight_function,
303+
default_weight = meta.default_weight
304+
)
301305

306+
include("metadigraph.jl")
307+
include("metagraph.jl")
302308
include("overrides.jl")
303309
include("persistence.jl")
304310

src/metadigraph.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct MetaDiGraph{Vertex <: Integer, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight <: Real} <: AbstractMetaGraph{Vertex, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight}
2+
inner_graph::InnerGraph
3+
vertex_meta::Dict{Vertex, AtVertex}
4+
edge_meta::Dict{Edge{Vertex}, AtEdge}
5+
graph_meta::GraphMeta
6+
weight_function::WeightFunction
7+
default_weight::Weight
8+
end
9+
10+
function meta_graph(inner_graph::DiGraph{Vertex};
11+
AtVertex = Nothing,
12+
AtEdge = Nothing,
13+
graph_meta = nothing,
14+
weight_function = edge_meta -> 1.0,
15+
default_weight = 1.0
16+
) where {Vertex}
17+
MetaDiGraph(
18+
inner_graph,
19+
Dict{Vertex, AtVertex}(),
20+
Dict{Edge{Vertex}, AtEdge}(),
21+
graph_meta,
22+
weight_function,
23+
default_weight
24+
)
25+
end
26+
27+
SimpleDiGraph(g::MetaDiGraph) = g.graph
28+
29+
is_directed(::Type{<: MetaDiGraph}) = true
30+
31+
maybe_order_edge(::MetaDiGraph, edge) = edge

src/metagraph.jl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct MetaGraph{Vertex <: Integer, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight <: Real} <: AbstractGraph{Vertex}
1+
struct MetaGraph{Vertex <: Integer, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight <: Real} <: AbstractMetaGraph{Vertex, InnerGraph, AtVertex, AtEdge, GraphMeta, WeightFunction, Weight}
22
inner_graph::InnerGraph
33
vertex_meta::Dict{Vertex, AtVertex}
44
edge_meta::Dict{Edge{Vertex}, AtEdge}
@@ -118,22 +118,13 @@ function meta_graph(inner_graph::AbstractGraph{Vertex};
118118
)
119119
end
120120

121-
is_directed(::Type{<: MetaGraph{<: Any, InnerGraph}}) where {InnerGraph} =
122-
is_directed(InnerGraph)
121+
SimpleGraph(g::MetaGraph) = g.graph
123122

124-
weight_type(meta::MetaGraph{<: Any, <: Any, <: Any, <: Any, <: Any, <: Any, Weight}) where {Weight} =
125-
Weight
123+
is_directed(::Type{<: MetaGraph}) = false
126124

127-
function setindex!(meta::MetaGraph, value, edge::AbstractEdge)
128-
meta.edge_meta[maybe_order_edge(meta, edge)] = value
129-
add_edge!(meta, edge)
130-
end
131-
132-
zero(meta::MetaGraph{<:Any, InnerGraph, AtVertex, AtEdge, GraphMeta}) where {InnerGraph, AtVertex, AtEdge, GraphMeta} =
133-
meta_graph(InnerGraph();
134-
AtVertex = AtVertex,
135-
AtEdge = AtEdge,
136-
graph_meta = GraphMeta(),
137-
weight_function = meta.weight_function,
138-
default_weight = meta.default_weight
139-
)
125+
maybe_order_edge(::MetaGraph, edge) =
126+
if is_ordered(edge)
127+
edge
128+
else
129+
reverse(edge)
130+
end

src/overrides.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function reverse(meta::MetaGraph{<: Any, <: DiGraph})
1+
function reverse(meta::MetaDiGraph)
22
return MetaGraph(reverse(meta.inner_graph),
33
meta.vertex_meta,
44
Dict(reverse(edge) => value for (edge, value) in meta.edge_meta),

src/persistence.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
struct MGFormat <: AbstractGraphFormat end
55
6-
You can save `MetaGraph`s in a `MGFormat`, currently based on `JLD2`.
6+
You can save `AbstractMetaGraph`s in a `MGFormat`, currently based on `JLD2`.
77
88
```jldoctest
99
julia> using MetaGraphs
@@ -30,7 +30,7 @@ export MGFormat
3030
struct DOTFormat <: AbstractGraphFormat end
3131
3232
For supported metadata formats (edge.meta. AbstractDict, NamedTuple, Nothing), you
33-
can save `MetaGraph`s in `DOTFormat`.
33+
can save `AbstractMetaGraph`s in `DOTFormat`.
3434
3535
```jldoctest DotFormat
3636
julia> using MetaGraphs
@@ -82,7 +82,7 @@ function loadgraph(filename::AbstractString, ::String, ::MGFormat)
8282
@load filename meta
8383
return meta
8484
end
85-
function savegraph(filename::AbstractString, meta::MetaGraph)
85+
function savegraph(filename::AbstractString, meta::AbstractMetaGraph)
8686
@save filename meta
8787
return 1
8888
end
@@ -119,7 +119,7 @@ function show_meta(io::IO, meta::Union{AbstractDict, NamedTuple})
119119
return nothing
120120
end
121121

122-
function savedot(io::IO, meta::MetaGraph)
122+
function savedot(io::IO, meta::AbstractMetaGraph)
123123
dash = if is_directed(meta)
124124
print(io, "digraph {\n")
125125
"->"
@@ -152,7 +152,7 @@ function savedot(io::IO, meta::MetaGraph)
152152
return nothing
153153
end
154154

155-
function savegraph(filename::AbstractString, meta::MetaGraph, ::DOTFormat)
155+
function savegraph(filename::AbstractString, meta::AbstractMetaGraph, ::DOTFormat)
156156
open(filename, "w") do io
157157
savedot(io, meta)
158158
end

0 commit comments

Comments
 (0)