Skip to content

Commit 11f54ad

Browse files
lmondadagdalle
andauthored
[bugfix] Base.eltype works on graph types as well as instances (#144)
* Cover bug in tests * Change eltype to act on type * Implement eltype on AbstractGraph instead * Fix eltype extension * Proper subtyping * Proper subtyping on nonbacktracking * Remove useless exception since eltype is no longer in interface * Fix typos * Remove eltype in generic test graph tyoe --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
1 parent 1ca400d commit 11f54ad

File tree

9 files changed

+9
-15
lines changed

9 files changed

+9
-15
lines changed

docs/src/ecosystem/interface.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This section is designed to guide developers who wish to write their own graph s
55
All Graphs.jl functions rely on a standard API to function. As long as your graph structure is a subtype of [`AbstractGraph`](@ref) and implements the following API functions with the given return values, all functions within the Graphs.jl package should just work:
66

77
- [`edges`](@ref)
8-
- [Base.eltype](https://docs.julialang.org/en/latest/base/collections/#Base.eltype)
98
- [`edgetype`](@ref) (example: `edgetype(g::CustomGraph) = Graphs.SimpleEdge{eltype(g)})`)
109
- [`has_edge`](@ref)
1110
- [`has_vertex`](@ref)

src/SimpleGraphs/simpledigraph.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function SimpleDiGraph(
2424
return SimpleDiGraph{T}(ne, fadjlist, badjlist)
2525
end
2626

27-
eltype(x::SimpleDiGraph{T}) where {T} = T
2827

2928
# DiGraph{UInt8}(6), DiGraph{Int16}(7), DiGraph{Int8}()
3029
"""

src/SimpleGraphs/simplegraph.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ function SimpleGraph(ne, fadjlist::Vector{Vector{T}}) where {T}
1919
return SimpleGraph{T}(ne, fadjlist)
2020
end
2121

22-
eltype(x::SimpleGraph{T}) where {T} = T
23-
2422
# Graph{UInt8}(6), Graph{Int16}(7), Graph{UInt8}()
2523
"""
2624
SimpleGraph{T}(n=0)

src/Test/Test.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ end
4949
Graphs.is_directed(::Type{<:GenericGraph}) = false
5050
Graphs.is_directed(::Type{<:GenericDiGraph}) = true
5151

52-
Base.eltype(g::GenericGraph) = eltype(g.g)
53-
Base.eltype(g::GenericDiGraph) = eltype(g.g)
54-
5552
Graphs.edges(g::GenericGraph) = (GenericEdge(e) for e in Graphs.edges(g.g))
5653
Graphs.edges(g::GenericDiGraph) = (GenericEdge(e) for e in Graphs.edges(g.g))
5754

src/interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ Return the type of graph `g`'s edge
118118
edgetype(g::AbstractGraph) = _NI("edgetype")
119119

120120
"""
121-
eltype(g)
121+
eltype(G)
122122
123123
Return the type of the graph's vertices (must be <: Integer)
124124
"""
125-
eltype(g::AbstractGraph) = _NI("eltype")
125+
eltype(::Type{<:AbstractGraph{T}}) where {T} = T
126126

127127
"""
128128
nv(g)

src/linalg/nonbacktracking.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ end
8585

8686
size(nbt::Nonbacktracking) = (nbt.m, nbt.m)
8787
size(nbt::Nonbacktracking, i::Number) = size(nbt)[i]
88-
eltype(nbt::Nonbacktracking) = Float64
88+
eltype(::Type{<:Nonbacktracking}) = Float64
8989
issymmetric(nbt::Nonbacktracking) = false
9090

9191
function *(nbt::Nonbacktracking, x::Vector{T}) where {T<:Number}

test/interface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mutable struct DummyEdge <: AbstractEdge{Int} end
1818
@test_throws Graphs.NotImplementedError edgefun2edges(dummyedge, dummyedge)
1919
end
2020

21-
for graphfunbasic in [nv, ne, vertices, edges, is_directed, edgetype, eltype]
21+
for graphfunbasic in [nv, ne, vertices, edges, is_directed, edgetype]
2222
@test_throws Graphs.NotImplementedError graphfunbasic(dummygraph)
2323
end
2424

test/linalg/spectral.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt))
6161
@test size(n10, 1) == n10.m
6262
@test size(n10, 2) == n10.m
6363
@test eltype(n10) == Float64
64+
@test eltype(typeof(n10)) == Float64
6465
@test !issymmetric(n10)
6566

6667
contract!(z, n10, v)

test/simplegraphs/simplegraphs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ using Random: Random
8989
T = @inferred(eltype(g))
9090
@test @inferred(nv(SimpleGraph{T}(6))) == 6
9191

92-
@test @inferred(eltype(SimpleGraph(T))) == T
92+
@test @inferred(eltype(SimpleGraph{T})) == T
9393
@test @inferred(eltype(SimpleGraph{T}(adjmx1))) == T
9494

9595
ga = SimpleGraph(10)
@@ -156,7 +156,7 @@ using Random: Random
156156
T = @inferred(eltype(g))
157157
@test @inferred(nv(SimpleDiGraph{T}(6))) == 6
158158

159-
@test @inferred(eltype(SimpleDiGraph(T))) == T
159+
@test @inferred(eltype(SimpleDiGraph{T})) == T
160160
@test @inferred(eltype(SimpleDiGraph{T}(adjmx2))) == T
161161

162162
ga = SimpleDiGraph(10)
@@ -271,7 +271,7 @@ using Random: Random
271271
edge_set_any = Set{Any}(edge_list)
272272

273273
g1 = @inferred SimpleDiGraph(edge_list)
274-
# we can't infer the return type of SimpleDiGraphFromIterator at the moment
274+
# we can't infer the return type of SimpleDiGraphFromIterator at the moment
275275
g2 = SimpleDiGraphFromIterator(edge_list)
276276
g3 = SimpleDiGraphFromIterator(edge_iter)
277277
g4 = SimpleDiGraphFromIterator(edge_set)
@@ -312,7 +312,7 @@ using Random: Random
312312
))
313313
end
314314

315-
# check if multiple edges && multiple self-loops result in the
315+
# check if multiple edges && multiple self-loops result in the
316316
# correct number of edges & vertices
317317
# edges using integers < 1 should be ignored
318318
g_undir = SimpleGraph(0)

0 commit comments

Comments
 (0)