Skip to content

Commit e599c71

Browse files
authored
Merge pull request #52 from JuliaGraphs/fix_addition
Prevent addition in underlying graph when dictionary operations failed
2 parents 1944ef0 + 1433c8a commit e599c71

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/graphs.jl

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,18 @@ function Graphs.add_vertex!(meta_graph::MetaGraph, label, data)
149149
if haskey(meta_graph, label)
150150
return false
151151
end
152-
nvnum = nv(meta_graph.graph)
152+
nv_prev = nv(meta_graph.graph)
153+
code = nv_prev + 1
154+
meta_graph.vertex_labels[code] = label
155+
meta_graph.vertex_properties[label] = (code, data)
153156
add_vertex!(meta_graph.graph)
154-
added = nvnum + 1 == nv(meta_graph.graph)
155-
if added
156-
code = nv(meta_graph)
157-
meta_graph.vertex_labels[code] = label
158-
meta_graph.vertex_properties[label] = (code, data)
157+
if nv(meta_graph.graph) == nv_prev # undo
158+
delete!(meta_graph.vertex_labels, code)
159+
delete!(meta_graph.vertex_properties, label)
160+
return false
161+
else
162+
return true
159163
end
160-
return added
161164
end
162165

163166
function Graphs.add_vertex!(meta_graph::MetaGraph{<:Any,<:Any,<:Any,Nothing}, label)
@@ -173,12 +176,20 @@ If the `EdgeData` type of `meta_graph` is `Nothing`, `data` can be omitted.
173176
Return `true` if the edge has been added, `false` otherwise.
174177
"""
175178
function Graphs.add_edge!(meta_graph::MetaGraph, label_1, label_2, data)
179+
if !haskey(meta_graph, label_1) || !haskey(meta_graph, label_2)
180+
return false
181+
end
176182
code_1, code_2 = code_for(meta_graph, label_1), code_for(meta_graph, label_2)
177-
added = add_edge!(meta_graph.graph, code_1, code_2)
178-
if added
179-
meta_graph.edge_data[arrange(meta_graph, label_1, label_2, code_1, code_2)] = data
183+
label_tup = arrange(meta_graph, label_1, label_2, code_1, code_2)
184+
meta_graph.edge_data[label_tup] = data
185+
ne_prev = ne(meta_graph.graph)
186+
add_edge!(meta_graph.graph, code_1, code_2)
187+
if ne(meta_graph.graph) == ne_prev # undo
188+
delete!(meta_graph.edge_data, label_tup)
189+
return false
190+
else
191+
return true
180192
end
181-
return added
182193
end
183194

184195
function Graphs.add_edge!(

test/tutorial/1_basics.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ colors[:red] = (255, 0, 0);
3030
colors[:green] = (0, 255, 0);
3131
colors[:blue] = (0, 0, 255);
3232

33+
# Note that you cannot use labels or metadata that is incoherent with the types you specified at construction.
34+
35+
@test_throws MethodError colors[:red] = "(255, 0, 0)" #src
36+
@test_throws MethodError colors["red"] = (255, 0, 0) #src
37+
3338
# ### Edges
3439

3540
# Use `setindex!` with two keys to add a new edge between the given labels and containing the given metadata. Beware that this time, nonexistent labels will throw an error.

0 commit comments

Comments
 (0)