Skip to content

Commit d6aed04

Browse files
committed
now working
1 parent 5bc0e4d commit d6aed04

File tree

8 files changed

+42
-40
lines changed

8 files changed

+42
-40
lines changed

src/StaticGraphs.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ export
4040

4141
include("utils.jl")
4242

43-
const StaticEdgeIter{G} = LightGraphs.SimpleGraphs.SimpleEdgeIter{G}
44-
const AbstractStaticEdge = AbstractSimpleEdge
45-
const StaticEdge = SimpleEdge
43+
const AbstractStaticEdge{T} = AbstractSimpleEdge{T}
44+
const StaticEdge{T} = SimpleEdge{T}
4645

4746
"""
4847
AbstractStaticGraph
@@ -86,9 +85,9 @@ vertices(g::AbstractStaticGraph{T, U}) where T where U = one(T):nv(g)
8685

8786

8887
has_edge(g::AbstractStaticGraph, e::AbstractStaticEdge) =
89-
insorted(dst(e), neighbors(g, src(e)))
88+
insorted(dst(e), out_neighbors(g, src(e)))
9089

91-
edgetype(g::AbstractStaticGraph) = StaticEdge
90+
edgetype(g::AbstractStaticGraph{T}) where T = StaticEdge{T}
9291
edges(g::AbstractStaticGraph) = StaticEdgeIter(g)
9392

9493
has_vertex(g::AbstractStaticGraph, v::Integer) = v in vertices(g)
@@ -109,4 +108,10 @@ include("persistence.jl")
109108
const SGraph = StaticGraph
110109
const SDiGraph = StaticDiGraph
111110

111+
const StaticEdgeIter{G} = LightGraphs.SimpleGraphs.SimpleEdgeIter{G}
112+
113+
eltype(::Type{StaticEdgeIter{StaticGraph{T, U}}}) where T where U = StaticGraphEdge{T}
114+
eltype(::Type{StaticEdgeIter{StaticDiGraph{T, U}}}) where T where U = StaticDiGraphEdge{T}
115+
116+
112117
end # module

src/persistence.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ end
3030

3131
function loadsg(fn::AbstractString, ::SDGFormat)
3232
@load fn f_vec f_ind b_vec b_ind
33-
return StaticGraph(f_vec, f_ind, b_vec, b_ind)
33+
return StaticDiGraph(f_vec, f_ind, b_vec, b_ind)
3434
end
3535

36-
loadgraph(fn::AbstractString, gname::String) = loadsg(fn, s)
36+
loadgraph(fn::AbstractString, gname::String, s::StaticGraphFormat) = loadsg(fn, s)
3737
savegraph(fn::AbstractString, g::AbstractStaticGraph) = savesg(fn, g)

src/staticdigraph.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ function StaticDiGraph(n_v, f_ss::AbstractVector, f_ds::AbstractVector, b_ss::Ab
3333
push!(b_ind, length(b_ss)+1)
3434
T = mintype(f_ds)
3535
U = mintype(f_ind)
36-
return StaticDiGraph{T}(
37-
convert(T, f_ds),
38-
convert(U, f_ind),
39-
convert(T, b_ds),
40-
convert(U, b_ind)
36+
return StaticDiGraph{T, U}(
37+
convert(Vector{T}, f_ds),
38+
convert(Vector{U}, f_ind),
39+
convert(Vector{T}, b_ds),
40+
convert(Vector{U}, b_ind)
4141
)
4242
end
4343

src/staticgraph.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ struct StaticGraph{T<:Integer, U<:Integer} <: AbstractStaticGraph{T, U}
1010
f_ind::Vector{U}
1111
end
1212

13-
badj(g::StaticGraph, s) = fadj(g, s)
14-
15-
ne(g::StaticGraph{T, U}) where T where U = T(length(g.f_vec) ÷ 2)
16-
1713

1814
# sorted src, dst vectors
1915
# note: this requires reverse edges included in the sorted vector.
@@ -42,6 +38,18 @@ function StaticGraph(g::LightGraphs.SimpleGraphs.SimpleGraph)
4238
StaticGraph(nv(g), sd)
4339
end
4440

41+
badj(g::StaticGraph, s) = fadj(g, s)
42+
43+
ne(g::StaticGraph{T, U}) where T where U = T(length(g.f_vec) ÷ 2)
44+
45+
function has_edge(g::StaticGraph, e::StaticGraphEdge)
46+
u, v = Tuple(e)
47+
(u > nv(g) || v > nv(g)) && return false
48+
if degree(g, u) > degree(g, v)
49+
u, v = v, u
50+
end
51+
return insorted(v, fadj(g, u))
52+
end
4553
==(g::StaticGraph, h::StaticGraph) = g.f_vec == h.f_vec && g.f_ind == h.f_ind
4654

4755
degree(g::StaticGraph{T, U}, v::Integer) where T where U = T(length(_fvrange(g, v)))

test/runtests.jl

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,24 @@ const testdir = dirname(@__FILE__)
88

99
@testset "StaticGraphs" begin
1010

11-
h = loadgraph(joinpath(testdir, "testdata", "house.jsg"), SGFormat())
12-
hu = loadgraph(joinpath(testdir, "testdata", "house-uint8.jsg"), SGFormat(UInt8))
13-
dh = loadgraph(joinpath(testdir, "testdata", "pathdg.jsg"), SDGFormat())
14-
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat(UInt8))
11+
hu = loadgraph(joinpath(testdir, "testdata", "house-uint8.jsg"), SGFormat())
12+
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat())
1513

1614
@testset "staticgraph" begin
17-
@test sprint(show, StaticGraph(Graph())) == "empty undirected simple static Int64 graph"
15+
@test sprint(show, StaticGraph(Graph())) == "empty undirected simple static {UInt8, UInt8} graph"
1816
g = smallgraph(:house)
1917
gu = squash(g)
2018
sg = StaticGraph(g)
2119
sgu = StaticGraph(gu)
22-
@test sprint(show, sg) == "{5, 6} undirected simple static Int64 graph"
23-
@test sprint(show, sgu) == "{5, 6} undirected simple static UInt8 graph"
20+
@test sprint(show, sg) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
21+
@test sprint(show, sgu) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
2422
testfn(fn, args...) =
25-
@inferred(fn(h, args...)) ==
2623
@inferred(fn(hu, args...)) ==
2724
@inferred(fn(sg, args...)) ==
2825
@inferred(fn(sgu, args...)) ==
2926
fn(g, args...)
3027

31-
@test h == sg
32-
@test hu == sgu
28+
@test hu == sg == sgu
3329
@test @inferred eltype(hu) == UInt8
3430
@test testfn(ne)
3531
@test testfn(nv)
@@ -49,31 +45,27 @@ const testdir = dirname(@__FILE__)
4945
@test @inferred has_vertex(hu, 1)
5046
@test @inferred !has_vertex(hu, 10)
5147
@test @inferred !is_directed(hu)
52-
@test @inferred !is_directed(h)
5348
@test @inferred !is_directed(StaticGraph)
54-
@test @inferred collect(edges(h)) == collect(edges(sg))
49+
@test @inferred collect(edges(hu)) == collect(edges(sg))
5550
end # staticgraph
5651

5752
@testset "staticdigraph" begin
58-
@test sprint(show, StaticDiGraph(DiGraph())) == "empty directed simple static Int64 graph"
53+
@test sprint(show, StaticDiGraph(DiGraph())) == "empty directed simple static {UInt8, UInt8} graph"
5954
dg = PathDiGraph(5)
6055
dgu = squash(dg)
6156
dsg = StaticDiGraph(dg)
6257
dsgu = StaticDiGraph(dgu)
63-
@test sprint(show, dsg) == "{5, 4} directed simple static Int64 graph"
64-
@test sprint(show, dsgu) == "{5, 4} directed simple static UInt8 graph"
65-
dh = loadgraph(joinpath(testdir, "testdata", "pathdg.jsg"), SDGFormat())
66-
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat(UInt8))
58+
@test sprint(show, dsg) == "{5, 4} directed simple static {UInt8, UInt8} graph"
59+
@test sprint(show, dsgu) == "{5, 4} directed simple static {UInt8, UInt8} graph"
60+
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat())
6761

6862
dtestfn(fn, args...) =
69-
@inferred(fn(dh, args...)) ==
7063
@inferred(fn(dhu, args...)) ==
7164
@inferred(fn(dsg, args...)) ==
7265
@inferred(fn(dsgu, args...)) ==
7366
fn(dg, args...)
7467

75-
@test dh == dsg
76-
@test dhu == dsgu
68+
@test dhu == dsg == dsgu
7769
@test @inferred eltype(dhu) == UInt8
7870
@test dtestfn(ne)
7971
@test dtestfn(nv)
@@ -93,9 +85,8 @@ const testdir = dirname(@__FILE__)
9385
@test @inferred has_vertex(dhu, 1)
9486
@test @inferred !has_vertex(dhu, 10)
9587
@test @inferred is_directed(dhu)
96-
@test @inferred is_directed(dh)
9788
@test @inferred is_directed(StaticDiGraph)
98-
@test @inferred collect(edges(dh)) == collect(edges(dsg))
89+
@test @inferred collect(edges(dhu)) == collect(edges(dsg))
9990
end # staticdigraph
10091

10192
@testset "utils" begin
@@ -110,9 +101,7 @@ const testdir = dirname(@__FILE__)
110101

111102
@testset "persistence" begin
112103
function writegraphs(f, fio)
113-
@test savegraph(f, h) == 1
114104
@test savegraph(f, hu) == 1
115-
@test savegraph(f, dh) == 1
116105
@test savegraph(f, dhu) == 1
117106
end
118107
mktemp(writegraphs)

test/testdata/house-uint8.jsg

774 Bytes
Binary file not shown.

test/testdata/house.jsg

-919 Bytes
Binary file not shown.

test/testdata/pathdg-uint8.jsg

928 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)