Skip to content

Commit a868f25

Browse files
committed
Revert back to vertex properties
1 parent f059115 commit a868f25

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

src/dict_utils.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Base.getindex(g::MetaGraph) = g.graph_data
1010
1111
Return vertex metadata for `label`.
1212
"""
13-
Base.getindex(g::MetaGraph, label) = g.vertex_data[label]
13+
Base.getindex(g::MetaGraph, label) = g.vertex_properties[label][2]
1414

1515
"""
1616
getindex(g, label_1, label_2)
@@ -24,7 +24,7 @@ Base.getindex(g::MetaGraph, label_1, label_2) = g.edge_data[arrange(g, label_1,
2424
2525
Determine whether a graph `g` contains the vertex `label`.
2626
"""
27-
Base.haskey(g::MetaGraph, label) = haskey(g.vertex_data, label)
27+
Base.haskey(g::MetaGraph, label) = haskey(g.vertex_properties, label)
2828

2929
"""
3030
haskey(g, label_1, label_2)
@@ -101,10 +101,9 @@ Copy properties from `oldg` to `newg` following vertex map `vmap`.
101101
function _copy_props!(oldg::G, newg::G, vmap) where {G<:MetaGraph}
102102
for (newv, oldv) in enumerate(vmap)
103103
oldl = oldg.vertex_labels[oldv]
104-
data = oldg.vertex_data[oldl]
104+
_, data = oldg.vertex_properties[oldl]
105105
newg.vertex_labels[newv] = oldl
106-
newg.vertex_codes[oldl] = newv
107-
newg.vertex_data[oldl] = data
106+
newg.vertex_properties[oldl] = (newv, data)
108107
end
109108
for newe in edges(newg.graph)
110109
vertex_labels = newg.vertex_labels

src/graphs.jl

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Find the vertex code (or index) associated with label `label`.
2424
2525
This can be useful to pass to methods inherited from `Graphs`. Note, however, that vertex codes can be reassigned after vertex deletion.
2626
"""
27-
code_for(g::MetaGraph, label) = g.vertex_codes[label]
27+
code_for(g::MetaGraph, label) = g.vertex_properties[label][1]
2828

2929
"""
3030
label_for(g::MetaGraph, v)
@@ -45,8 +45,9 @@ Set vertex metadata for `label` to `data`.
4545
Return `true` if the operation succeeds, and `false` if `g` has no such vertex.
4646
"""
4747
function set_data!(g::MetaGraph, label, data)
48-
if haskey(g.vertex_data, label)
49-
g.vertex_data[label] = data
48+
if haskey(g.vertex_properties, label)
49+
code, old_data = g.vertex_properties[label]
50+
g.vertex_properties[label] = (code, data)
5051
return true
5152
else
5253
return false
@@ -84,8 +85,7 @@ function Graphs.add_vertex!(g::MetaGraph, label, data)
8485
if added
8586
v = nv(g)
8687
g.vertex_labels[v] = label
87-
g.vertex_codes[label] = v
88-
g.vertex_data[label] = data
88+
g.vertex_properties[label] = (v, data)
8989
end
9090
return added
9191
end
@@ -110,8 +110,7 @@ end
110110

111111
function _rem_vertex!(g::MetaGraph, label, v)
112112
vertex_labels = g.vertex_labels
113-
vertex_codes = g.vertex_codes
114-
vertex_data = g.vertex_data
113+
vertex_properties = g.vertex_properties
115114
edge_data = g.edge_data
116115
lastv = nv(g)
117116
for n in outneighbors(g, v)
@@ -124,13 +123,12 @@ function _rem_vertex!(g::MetaGraph, label, v)
124123
if removed
125124
if v != lastv # ignore if we're removing the last vertex.
126125
lastl = vertex_labels[lastv]
127-
lastvprop = vertex_data[lastl]
126+
lastvprop = vertex_properties[lastl]
128127
vertex_labels[v] = lastl
129-
vertex_codes[lastl] = v
130-
vertex_data[lastl] = lastvprop
128+
vertex_properties[lastl] = lastvprop
131129
end
132-
delete!(vertex_data, label)
133130
delete!(vertex_labels, lastv)
131+
delete!(vertex_properties, label)
134132
end
135133
return removed
136134
end
@@ -164,8 +162,7 @@ function Graphs.induced_subgraph(
164162
newg = MetaGraph(
165163
inducedgraph,
166164
empty(g.vertex_labels),
167-
empty(g.vertex_codes),
168-
empty(g.vertex_data),
165+
empty(g.vertex_properties),
169166
empty(g.edge_data),
170167
g.graph_data,
171168
g.weight_function,
@@ -178,8 +175,7 @@ end
178175
function Graphs.reverse(g::MetaDiGraph)
179176
rg = reverse(g.graph)
180177
rvertex_labels = copy(g.vertex_labels)
181-
rvertex_codes = copy(g.vertex_codes)
182-
rvertex_data = copy(g.vertex_data)
178+
rvertex_properties = copy(g.vertex_properties)
183179
redge_data = empty(g.edge_data)
184180
rgraph_data = g.graph_data
185181
rweight_function = g.weight_function
@@ -192,8 +188,7 @@ function Graphs.reverse(g::MetaDiGraph)
192188
rg = MetaGraph(
193189
rg,
194190
rvertex_labels,
195-
rvertex_codes,
196-
rvertex_data,
191+
rvertex_properties,
197192
redge_data,
198193
rgraph_data,
199194
rweight_function,

src/metagraph.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ It is recommended not to set `Label` to an integer type, so as to avoid confusio
99
# Fields
1010
- `g::Graph`: underlying, data-less graph with vertex indices of type `T`
1111
- `vertex_labels::Dict{T,Label}`: dictionary mapping vertex codes to vertex labels
12-
- `vertex_codes::Dict{Label,T}`: dictionary mapping vertex labels to vertex codes
13-
- `vertex_data::Dict{Label,VertexData}`: dictionary mapping vertex labels to vertex metadata
12+
- `vertex_properties::Dict{Label,Tuple{T,VertexData}}`: dictionary mapping vertex labels to vertex codes & data
1413
- `edge_data::Dict{Tuple{Label,Label},EdgeData}`: dictionary mapping edge labels such as `(label_u, label_v)` to edge metadata
1514
- `graph_data::GraphData`: graph metadata
1615
- `weight_function::WeightFunction`: function defining edge weight from edge metadata
@@ -21,8 +20,7 @@ struct MetaGraph{
2120
} <: AbstractGraph{T}
2221
graph::Graph
2322
vertex_labels::Dict{T,Label}
24-
vertex_codes::Dict{Label,T}
25-
vertex_data::Dict{Label,VertexData}
23+
vertex_properties::Dict{Label,Tuple{T,VertexData}}
2624
edge_data::Dict{Tuple{Label,Label},EdgeData}
2725
graph_data::GraphData
2826
weight_function::WeightFunction
@@ -53,12 +51,13 @@ function MetaGraph(
5351
) where {T}
5452
if Label <: Integer
5553
@warn "Constructing a MetaGraph with integer labels is not advised."
54+
elseif nv(graph) > 0
55+
@warn "Constructing a MetaGraph with a nonempty underlying graph is not advised."
5656
end
5757
return MetaGraph(
5858
graph,
5959
Dict{T,Label}(),
60-
Dict{Label,T}(),
61-
Dict{Label,VertexData}(),
60+
Dict{Label,Tuple{T,VertexData}}(),
6261
Dict{Tuple{Label,Label},EdgeData}(),
6362
graph_data,
6463
weight_function,

src/persistence.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function savedot(io::IO, g::MetaGraph)
6767
end
6868
end
6969

70-
for label in keys(g.vertex_data)
70+
for label in keys(g.vertex_properties)
7171
write(io, " ")
7272
write(io, label)
7373
show_meta_list(io, g[label])

0 commit comments

Comments
 (0)