Skip to content

Commit 7b034c2

Browse files
authored
Merge pull request #48 from thchr/patch-1
Add data-free methods for `add_vertex!` and `add_edge!`
2 parents f546d96 + a10deba commit 7b034c2

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/graphs.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ end
8888
add_vertex!(meta_graph, label, data)
8989
9090
Add a vertex to MetaGraph `meta_graph` with label `label` having metadata `data`.
91+
If the `VertexData` type of `meta_graph` is `Nothing`, `data` can be omitted.
9192
92-
Return true if the vertex has been added, false incase the label already exists or vertex was not added.
93+
Return true if the vertex has been added, false in case the label already exists or vertex was not added.
9394
"""
9495
function Graphs.add_vertex!(meta_graph::MetaGraph, label, data)
9596
if haskey(meta_graph, label)
@@ -106,10 +107,15 @@ function Graphs.add_vertex!(meta_graph::MetaGraph, label, data)
106107
return added
107108
end
108109

110+
function Graphs.add_vertex!(meta_graph::MetaGraph{<:Any,<:Any,<:Any,Nothing}, label)
111+
return Graphs.add_vertex!(meta_graph, label, nothing)
112+
end
113+
109114
"""
110115
add_edge!(meta_graph, label_1, label_2, data)
111116
112117
Add an edge `(label_1, label_2)` to MetaGraph `meta_graph` with metadata `data`.
118+
If the `EdgeData` type of `meta_graph` is `Nothing`, `data` can be omitted.
113119
114120
Return `true` if the edge has been added, `false` otherwise.
115121
"""
@@ -122,6 +128,12 @@ function Graphs.add_edge!(meta_graph::MetaGraph, label_1, label_2, data)
122128
return added
123129
end
124130

131+
function Graphs.add_edge!(
132+
meta_graph::MetaGraph{<:Any,<:Any,<:Any,<:Any,Nothing}, label_1, label_2
133+
)
134+
return Graphs.add_edge!(meta_graph, label_1, label_2, nothing)
135+
end
136+
125137
## Remove vertex
126138

127139
function _rem_vertex!(meta_graph::MetaGraph, label, code)

test/misc.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@testset verbose = true "Short-form add_vertex!/add_edge!" begin
2+
# short-form
3+
mg = MetaGraph(
4+
Graph(); label_type=Symbol, vertex_data_type=Nothing, edge_data_type=Nothing
5+
)
6+
@test add_vertex!(mg, :A)
7+
@test add_vertex!(mg, :B)
8+
@test add_edge!(mg, :A, :B)
9+
10+
# long-form
11+
mg2 = MetaGraph(
12+
Graph(); label_type=Symbol, vertex_data_type=Nothing, edge_data_type=Nothing
13+
)
14+
@test add_vertex!(mg2, :A, nothing)
15+
@test add_vertex!(mg2, :B, nothing)
16+
@test add_edge!(mg2, :A, :B, nothing)
17+
18+
# compare
19+
@test mg == mg2
20+
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ using Test
3030
include(joinpath("tutorial", "4_type_stability.jl"))
3131
end
3232
end
33+
34+
@testset verbose = true "Miscellaneous" begin
35+
include("misc.jl")
36+
end
3337
end

0 commit comments

Comments
 (0)