Skip to content

Commit 52dc3ac

Browse files
authored
Merge pull request #6 from JuliaGraphs/sbromberger/conversion-and-bugfix
fixes bug in mintype and allows upconversion of staticgraphs
2 parents cd4fbe2 + c3a2888 commit 52dc3ac

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/staticdigraph.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function StaticDiGraph(n_v, f_ss::AbstractVector, f_ds::AbstractVector, b_ss::Ab
3131
push!(f_ind, length(f_ss)+1)
3232
b_ind = [searchsortedfirst(b_ss, x) for x in 1:n_v]
3333
push!(b_ind, length(b_ss)+1)
34-
T = mintype(f_ds)
35-
U = mintype(f_ind)
34+
T = mintype(maximum(f_ds))
35+
U = mintype(f_ind[end])
3636
return StaticDiGraph{T, U}(
3737
convert(Vector{T}, f_ds),
3838
convert(Vector{U}, f_ind),
@@ -57,7 +57,16 @@ function StaticDiGraph(g::LightGraphs.SimpleGraphs.SimpleDiGraph)
5757

5858
StaticDiGraph(nv(g), f_sd, b_sd)
5959
end
60-
60+
61+
function StaticDiGraph{T, U}(s::StaticDiGraph) where T <: Integer where U <: Integer
62+
new_fvec = T.(s.f_vec)
63+
new_find = U.(s.f_ind)
64+
new_bvec = T.(s.b_vec)
65+
new_bind = U.(s.b_ind)
66+
return StaticDiGraph(new_fvec, new_find, new_bvec, new_bind)
67+
end
68+
69+
6170
#
6271
==(g::StaticDiGraph, h::StaticDiGraph) = g.f_vec == h.f_vec && g.f_ind == h.f_ind && g.b_vec == h.b_vec && g.b_ind == h.b_ind
6372

src/staticgraph.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ function StaticGraph(n_v, ss::AbstractVector, ds::AbstractVector)
1818
(n_v == 0 || length(ss) == 0) && return StaticGraph(UInt8[], UInt8[1])
1919
f_ind = [searchsortedfirst(ss, x) for x in 1:n_v]
2020
push!(f_ind, length(ss)+1)
21-
T = mintype(ds)
22-
U = mintype(f_ind)
21+
T = mintype(maximum(ds))
22+
U = mintype(f_ind[end])
2323
return StaticGraph{T, U}(convert(Vector{T},ds), convert(Vector{U}, f_ind))
2424
end
2525

26+
function StaticGraph{T, U}(s::StaticGraph) where T <: Integer where U <: Integer
27+
new_fvec = T.(s.f_vec)
28+
new_find = U.(s.f_ind)
29+
return StaticGraph(new_fvec, new_find)
30+
end
31+
2632
# sorted src, dst tuples
2733
function StaticGraph(n_v, sd::Vector{Tuple{T, T}}) where T <: Integer
2834
ss = [x[1] for x in sd]

src/utils.jl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,14 @@ end
2929
"""
3030
mintype(v)
3131
32-
Returns the minimum integer type required to fit all elements
33-
in sorted vector `v`.
34-
35-
### Implementation Notes
36-
`v` is assumed to be sorted.
32+
Returns the minimum integer type required to represent integer `v`.
3733
"""
38-
function mintype(v::AbstractVector)
34+
function mintype(v::T) where T <: Integer
3935
validtypes = [UInt8, UInt16, UInt32, UInt64, UInt128]
40-
l = v[end]
41-
for T in validtypes
42-
l <= typemax(T) && return T
36+
for U in validtypes
37+
v <= typemax(U) && return U
4338
end
44-
return eltype(v)
39+
return T
4540
end
4641

4742

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const testdir = dirname(@__FILE__)
1717
gu = squash(g)
1818
sg = StaticGraph(g)
1919
sgu = StaticGraph(gu)
20+
@test eltype(StaticGraph{UInt128, UInt128}(sgu)) == UInt128
2021
@test sprint(show, sg) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
2122
@test sprint(show, sgu) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
2223
testfn(fn, args...) =
@@ -57,6 +58,7 @@ const testdir = dirname(@__FILE__)
5758
dgu = squash(dg)
5859
dsg = StaticDiGraph(dg)
5960
dsgu = StaticDiGraph(dgu)
61+
@test eltype(StaticDiGraph{UInt128, UInt128}(dsgu)) == UInt128
6062
@test sprint(show, dsg) == "{5, 4} directed simple static {UInt8, UInt8} graph"
6163
@test sprint(show, dsgu) == "{5, 4} directed simple static {UInt8, UInt8} graph"
6264
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat())

0 commit comments

Comments
 (0)