Skip to content

Commit cf8b30d

Browse files
authored
Merge pull request #12 from bks-nist/master
fix haskey(g, :v1, :v2) for nonexistent vertices
2 parents 554c495 + bd03ddd commit cf8b30d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/MetaGraphsNext.jl

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,45 @@ function getindex(g::MetaGraph, label)
185185
end
186186
getindex(g::MetaGraph, label_1, label_2) = g.eprops[arrange(g, label_1, label_2)]
187187

188+
"""
189+
haskey(g, :label)
190+
191+
Determine whether a graph `g` contains the vertex `:label`.
192+
193+
```jldoctest; setup = :(using MetaGraphsNext; using LightGraphs: Graph)
194+
julia> colors = MetaGraph(Graph(), VertexMeta = String, EdgeMeta = Symbol, gprops = "special");
195+
196+
julia> colors[:red] = "warm";
197+
198+
julia> haskey(colors, :red)
199+
true
200+
201+
julia> haskey(colors, :blue)
202+
false
203+
```
204+
"""
188205
haskey(g::MetaGraph, label) = haskey(g.vprops, label)
189-
haskey(g::MetaGraph, label_1, label_2) = haskey(g.eprops, arrange(g, label_1, label_2))
206+
207+
"""
208+
haskey(g, :v1, :v2)
209+
210+
Determine whether a graph `g` contains an edge from `:v1` to `:v2`. The order of `:v1` and `:v2`
211+
only matters if `g` is a digraph.
212+
213+
```jldoctest; setup = :(using MetaGraphsNext; using LightGraphs: Graph)
214+
julia> colors = MetaGraph(Graph(), VertexMeta = String, EdgeMeta = Symbol, gprops = "special");
215+
216+
julia> colors[:red] = "warm"; colors[:blue] = "cool"; colors[:red, :blue] = :purple
217+
:purple
218+
219+
julia> haskey(colors, :red, :blue) && haskey(colors, :blue, :red)
220+
true
221+
222+
julia> haskey(colors, :red, :yellow)
223+
false
224+
```
225+
"""
226+
haskey(g::MetaGraph, label_1, label_2) = haskey(g, label_1) && haskey(g, label_2) && haskey(g.eprops, arrange(g, label_1, label_2))
190227

191228
function setindex!(g::MetaGraph, val, label)
192229
vprops = g.vprops

src/metagraph.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ julia> colors[:red, :yellow] = :orange;
106106
107107
julia> colors[:red, :yellow]
108108
:orange
109+
110+
julia> haskey(colors, :red, :yellow)
111+
true
112+
113+
julia> haskey(colors, :yellow, :red) # undirected graph, so vertex order doesn't matter
114+
true
109115
```
110116
111117
You can delete vertices and edges with `delete!`.
@@ -212,6 +218,9 @@ julia> rock_paper_scissors[:rock, :scissors] = :rock_beats_scissors; rock_paper_
212218
julia> is_directed(rock_paper_scissors)
213219
true
214220
221+
julia> haskey(rock_paper_scissors, :scissors, :rock)
222+
false
223+
215224
julia> haskey(reverse(rock_paper_scissors), :scissors, :rock)
216225
true
217226

0 commit comments

Comments
 (0)