Skip to content

Commit 76cc078

Browse files
authored
Merge branch 'master' into sbromberger/adjmx
2 parents 3c5a741 + af451dc commit 76cc078

File tree

7 files changed

+69
-27
lines changed

7 files changed

+69
-27
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ os:
66
julia:
77
- 1.0
88
- nightly
9+
10+
matrix:
11+
allow_failures:
12+
- julia: nightly
13+
914
notifications:
1015
email: false
1116
git:
1217
depth: 99999999
1318

14-
## uncomment the following lines to allow failures on nightly julia
15-
## (tests will run but not make your overall status red)
16-
#matrix:
17-
# allow_failures:
18-
# - julia: nightly
19-
2019
## uncomment and modify the following lines to manually install system packages
2120
#addons:
2221
# apt: # apt-get for linux

src/StaticGraphs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ end
7373
end
7474

7575
nv(g::AbstractStaticGraph{T, U}) where T where U = T(length(g.f_ind) - 1)
76-
vertices(g::AbstractStaticGraph{T, U}) where T where U = one(T):nv(g)
76+
vertices(g::AbstractStaticGraph{T, U}) where T where U = Base.OneTo(nv(g))
7777

7878
has_edge(g::AbstractStaticGraph, e::AbstractStaticEdge) =
7979
insorted(dst(e), outneighbors(g, src(e)))

src/overrides.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ function adjacency_matrix(g::StaticDiGraph{I,U}, T::DataType; dir = :out) where
1111
dir != :out && @warn("direction `$dir` not defined for adjacency matrices on StaticGraphs; defaulting to `out`")
1212
return z
1313
end
14-

src/staticdigraph.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ end
2424
ne(g::StaticDiGraph{T, U}) where T where U = U(length(g.f_vec))
2525

2626
# sorted src, dst vectors for forward and backward edgelists.
27-
function StaticDiGraph(n_v, f_ss::AbstractVector, f_ds::AbstractVector, b_ss::AbstractVector, b_ds::AbstractVector)
27+
function StaticDiGraph(nvtx::I, f_ss::AbstractVector{F}, f_ds::AbstractVector{D}, b_ss::AbstractVector{B}, b_ds::AbstractVector{S}) where {I<:Integer,S<:Integer,D<:Integer,B<:Integer,F<:Integer}
2828
length(f_ss) == length(f_ds) == length(b_ss) == length(b_ds) || error("source and destination vectors must be equal length")
29-
(n_v == 0 || length(f_ss) == 0) && return StaticDiGraph(UInt8[], UInt8[1], UInt8[], UInt8[1])
30-
f_ind = [searchsortedfirst(f_ss, x) for x in 1:n_v]
29+
(nvtx == 0 || length(f_ss) == 0) && return StaticDiGraph(UInt8[], UInt8[1], UInt8[], UInt8[1])
30+
f_ind = [searchsortedfirst(f_ss, x) for x in Base.OneTo(nvtx)]
3131
push!(f_ind, length(f_ss)+1)
32-
b_ind = [searchsortedfirst(b_ss, x) for x in 1:n_v]
32+
b_ind = [searchsortedfirst(b_ss, x) for x in Base.OneTo(nvtx)]
3333
push!(b_ind, length(b_ss)+1)
3434
T = mintype(maximum(f_ds))
3535
U = mintype(f_ind[end])
@@ -42,21 +42,25 @@ function StaticDiGraph(n_v, f_ss::AbstractVector, f_ds::AbstractVector, b_ss::Ab
4242
end
4343

4444
# sorted src, dst tuples for forward and backward
45-
function StaticDiGraph(n_v, f_sd::Vector{Tuple{T, T}}, b_sd::Vector{Tuple{T, T}}) where T <: Integer
45+
function StaticDiGraph(nvtx::I, f_sd::Vector{Tuple{T, T}}, b_sd::Vector{Tuple{T, T}}) where {T<:Integer,I<:Integer}
4646
f_ss = [x[1] for x in f_sd]
4747
f_ds = [x[2] for x in f_sd]
4848
b_ss = [x[1] for x in b_sd]
4949
b_ds = [x[2] for x in b_sd]
5050

51-
StaticDiGraph(n_v, f_ss, f_ds, b_ss, b_ds)
51+
return StaticDiGraph(nvtx, f_ss, f_ds, b_ss, b_ds)
5252
end
5353

5454
function StaticDiGraph(g::LightGraphs.SimpleGraphs.SimpleDiGraph)
5555
ne(g) == 0 && return StaticDiGraph(nv(g), Array{Tuple{UInt8, UInt8},1}(), Array{Tuple{UInt8, UInt8},1}())
5656
f_sd = [Tuple(e) for e in edges(g)]
5757
b_sd = sort([Tuple(reverse(e)) for e in edges(g)])
5858

59-
StaticDiGraph(nv(g), f_sd, b_sd)
59+
return StaticDiGraph(nv(g), f_sd, b_sd)
60+
end
61+
62+
function StaticDiGraph()
63+
return StaticDiGraph(UInt8[], UInt8[1], UInt8[], UInt8[1])
6064
end
6165

6266
function StaticDiGraph{T, U}(s::StaticDiGraph) where T <: Integer where U <: Integer
@@ -68,7 +72,6 @@ function StaticDiGraph{T, U}(s::StaticDiGraph) where T <: Integer where U <: Int
6872
end
6973

7074

71-
#
7275
==(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
7376

7477
degree(g::StaticDiGraph, v::Integer) = indegree(g, v) + outdegree(g, v)
@@ -85,6 +88,7 @@ outdegree(g::StaticDiGraph) = [outdegree(g, v) for v in vertices(g)]
8588
Return `true` if `g` is a directed graph.
8689
"""
8790
is_directed(::Type{StaticDiGraph}) = true
91+
is_directed(::Type{StaticDiGraph{T}}) where T = true
8892
is_directed(::Type{StaticDiGraph{T, U}}) where T where U = true
8993
is_directed(g::StaticDiGraph) = true
9094

src/staticgraph.jl

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

13-
1413
# sorted src, dst vectors
15-
# note: this requires reverse edges included in the sorted vector.
16-
function StaticGraph(n_v, ss::AbstractVector, ds::AbstractVector)
14+
# note: this requires reverse edges included in the sorted vector.
15+
function StaticGraph(nvtx::I, ss::AbstractVector{S}, ds::AbstractVector{D}) where {I<:Integer,S<:Integer,D<:Integer}
1716
length(ss) != length(ds) && error("source and destination vectors must be equal length")
18-
(n_v == 0 || length(ss) == 0) && return StaticGraph(UInt8[], UInt8[1])
19-
f_ind = [searchsortedfirst(ss, x) for x in 1:n_v]
17+
(nvtx == 0 || length(ss) == 0) && return StaticGraph()
18+
f_ind = [searchsortedfirst(ss, x) for x in Base.OneTo(nvtx)]
2019
push!(f_ind, length(ss)+1)
2120
T = mintype(maximum(ds))
2221
U = mintype(f_ind[end])
@@ -30,33 +29,37 @@ function StaticGraph{T, U}(s::StaticGraph) where T <: Integer where U <: Integer
3029
end
3130

3231
# sorted src, dst tuples
33-
function StaticGraph(n_v, sd::Vector{Tuple{T, T}}) where T <: Integer
32+
function StaticGraph(nvtx::I, sd::Vector{Tuple{T, T}}) where {T<:Integer, I<:Integer}
3433
ss = [x[1] for x in sd]
3534
ds = [x[2] for x in sd]
36-
StaticGraph(n_v, ss, ds)
35+
return StaticGraph(nvtx, ss, ds)
3736
end
3837

3938
function StaticGraph(g::LightGraphs.SimpleGraphs.SimpleGraph)
4039
ne(g) == 0 && return StaticGraph(nv(g), Array{Tuple{UInt8, UInt8},1}())
4140
sd1 = [Tuple(e) for e in edges(g)]
4241
ds1 = [Tuple(reverse(e)) for e in edges(g)]
43-
4442
sd = sort(vcat(sd1, ds1))
45-
StaticGraph(nv(g), sd)
43+
return StaticGraph(nv(g), sd)
44+
end
45+
46+
function StaticGraph()
47+
return StaticGraph(UInt8[], UInt8[1])
4648
end
4749

4850
badj(g::StaticGraph, s) = fadj(g, s)
4951

5052
ne(g::StaticGraph{T, U}) where T where U = U(length(g.f_vec) ÷ 2)
5153

52-
function has_edge(g::StaticGraph, e::StaticGraphEdge)
54+
function has_edge(e::StaticGraphEdge, g::StaticGraph)
5355
u, v = Tuple(e)
5456
(u > nv(g) || v > nv(g)) && return false
5557
if degree(g, u) > degree(g, v)
5658
u, v = v, u
5759
end
5860
return insorted(v, fadj(g, u))
5961
end
62+
6063
==(g::StaticGraph, h::StaticGraph) = g.f_vec == h.f_vec && g.f_ind == h.f_ind
6164

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

src/utils.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
struct UnsafeVectorView{T,U} <: AbstractVector{T}
2+
offset::U
3+
len::U
4+
ptr::Ptr{T}
5+
end
6+
7+
@inline UnsafeVectorView(parent::Union{Vector, Base.FastContiguousSubArray}, range::UnitRange) = UnsafeVectorView(start(range) - 1, length(range), pointer(parent))
8+
@inline Base.size(v::UnsafeVectorView) = (v.len,)
9+
@inline Base.getindex(v::UnsafeVectorView, idx) = unsafe_load(v.ptr, idx + v.offset)
10+
@inline Base.setindex!(v::UnsafeVectorView, value, idx) = unsafe_store!(v.ptr, value, idx + v.offset)
11+
@inline Base.length(v::UnsafeVectorView) = v.len
12+
Base.IndexStyle(::Type{V}) where {V <: UnsafeVectorView} = IndexLinear()
13+
14+
"""
15+
UnsafeVectorView only works for isbits types. For other types, we're already
16+
allocating lots of memory elsewhere, so creating a new SubArray is fine.
17+
This function looks type-unstable, but the isbits(T) test can be evaluated
18+
by the compiler, so the result is actually type-stable.
19+
From https://github.com/rdeits/NNLS.jl/blob/0a9bf56774595b5735bc738723bd3cb94138c5bd/src/NNLS.jl#L218.
20+
"""
21+
@inline function fastview(parent::Union{Vector{T}, Base.FastContiguousSubArray{T}}, range::UnitRange) where T
22+
if isbits(T)
23+
UnsafeVectorView(parent, range)
24+
else
25+
view(parent, range)
26+
end
27+
end
28+
129
"""
230
mintype(v)
331
@@ -11,4 +39,3 @@ function mintype(v::T) where T <: Integer
1139
return T
1240
end
1341

14-

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const testdir = dirname(@__FILE__)
1717
gu = squash(g)
1818
sg = StaticGraph(g)
1919
sgu = StaticGraph(gu)
20+
gempty = StaticGraph()
21+
gdempty = StaticDiGraph()
2022
@test eltype(StaticGraph{UInt128, UInt128}(sgu)) == UInt128
2123
@test sprint(show, sg) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
2224
@test sprint(show, sgu) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
@@ -55,6 +57,14 @@ const testdir = dirname(@__FILE__)
5557
@test z[1,2]
5658
@test !z[1,4]
5759
@test z == adjacency_matrix(hu, Bool, dir=:in) == adjacency_matrix(hu, Bool, dir=:both)
60+
61+
# empty constructors
62+
@test nv(gempty) === 0x00
63+
@test typeof(LightGraphs.nv(gempty)) == UInt8
64+
@test length(LightGraphs.edges(gempty)) === 0x00
65+
@test nv(gdempty) === 0x00
66+
@test length(LightGraphs.edges(gdempty)) === 0x00
67+
5868
end # staticgraph
5969

6070
@testset "staticdigraph" begin

0 commit comments

Comments
 (0)