Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
e ∈ edges(g) (#752)
Browse files Browse the repository at this point in the history
* e in edges(g)

* review of "e in edges(g)"
  • Loading branch information
mschauer authored and sbromberger committed Sep 12, 2017
1 parent 47585a7 commit cb5d93b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/graphtypes/simplegraphs/SimpleGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SimpleGraphs

import Base:
eltype, show, ==, Pair, Tuple, copy, length, start, next, done, issubset, zero
eltype, show, ==, Pair, Tuple, copy, length, start, next, done, issubset, zero, in

import LightGraphs:
_NI, _insert_and_dedup!, AbstractGraph, AbstractEdge, AbstractEdgeIter,
Expand Down
2 changes: 2 additions & 0 deletions src/graphtypes/simplegraphs/simpleedgeiter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ function ==(e1::SimpleEdgeIter, e2::SimpleEdgeIter)
return true
end

in(e, es::SimpleEdgeIter) = has_edge(es.g, e)

show(io::IO, eit::SimpleEdgeIter) = write(io, "SimpleEdgeIter $(ne(eit.g))")
show(io::IO, s::SimpleEdgeIterState) = write(io, "SimpleEdgeIterState [$(s.s), $(s.di)]")
7 changes: 6 additions & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ vertices(g::AbstractGraph) = _NI("vertices")
Return (an iterator to or collection of) the edges of a graph.
For `AbstractSimpleGraph`s it returns a [`SimpleEdgeIter`](@ref).
The expressions `e in edges(g)` and `e ∈ edges(ga)` evaluate as
calls to [`has_edge`](@ref).
### Implementation Notes
A returned iterator is valid for one pass over the edges, and
Expand Down Expand Up @@ -162,8 +164,11 @@ has_vertex(x, v) = _NI("has_vertex")

"""
has_edge(g, e)
e ∈ edges(g)
Return true if the graph `g` has an edge `e`.
Return true if the graph `g` has an edge `e`.
The expressions `e in edges(g)` and `e ∈ edges(ga)` evaluate as
calls to `has_edge`, c.f. [`edges`](@ref).
"""
has_edge(x, e) = _NI("has_edge")

Expand Down
21 changes: 21 additions & 0 deletions test/graphtypes/simplegraphs/simpleedgeiter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
add_edge!(ga, 5, 10)
add_edge!(ga, 10, 3)

e1 = Edge(3, 10)
e2 = (3, 10)
@test e1 edges(ga)
@test e2 edges(ga)
@test (3, 9) edges(ga)

for u in 1:20, v in 1:20
b = has_edge(ga, u, v)
@test b == @inferred (u, v) edges(ga)
@test b == @inferred (u => v) edges(ga)
@test b == @inferred Edge(u, v) edges(ga)
end

eit = edges(ga)
es = @inferred(start(eit))

Expand All @@ -44,12 +57,20 @@
@test edges(ga) == edges(gb)
@test edges(gb) == edges(ga)


ga = SimpleDiGraph(10)
add_edge!(ga, 3, 2)
add_edge!(ga, 3, 10)
add_edge!(ga, 5, 10)
add_edge!(ga, 10, 3)

for u in 1:20, v in 1:20
b = has_edge(ga, u, v)
@test b == @inferred (u, v) edges(ga)
@test b == @inferred (u => v) edges(ga)
@test b == @inferred Edge(u, v) edges(ga)
end

eit = @inferred(edges(ga))
es = @inferred(start(eit))

Expand Down

0 comments on commit cb5d93b

Please sign in to comment.