Skip to content

Commit 0b9a40e

Browse files
committed
provide type-inferred length
resolves SciML#138 Need to check correct general computation of length of Axis.
1 parent efd8cc6 commit 0b9a40e

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/axis.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Base.merge(axs::Axis...) = Axis(merge(indexmap.(axs)...))
142142

143143
Base.firstindex(ax::AbstractAxis) = first(viewindex(first(indexmap(ax))))
144144
Base.lastindex(ax::AbstractAxis) = last(viewindex(last(indexmap(ax))))
145+
Base.length(ax::AbstractAxis) = lastindex(ax) - firstindex(ax) + 1
145146

146147
Base.keys(ax::AbstractAxis) = keys(indexmap(ax))
147148

src/componentarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ last_index(x) = last(x)
235235
last_index(x::ViewAxis) = last_index(viewindex(x))
236236
last_index(x::AbstractAxis) = last_index(last(indexmap(x)))
237237

238+
# length information is in Axis, use it to make SVector creation type stable
239+
Base.length(ca::ComponentArray) = prod(length.(getaxes(ca)))
240+
Base.size(ca::ComponentArray) = map(length, getaxes(ca))
241+
238242
# Reduce singleton dimensions
239243
remove_nulls() = ()
240244
remove_nulls(x1, args...) = (x1, remove_nulls(args...)...)

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,15 @@ end
598598
@test convert(Cholesky{Float32,Matrix{Float32}}, chol).factors isa Matrix{Float32}
599599
end
600600

601+
@testset "length typestable" begin
602+
# function boundary, so that cv is type-inferred
603+
test_create_svector = (cv) -> SVector{length(cv)}(cv)
604+
@inferred test_create_svector(ComponentVector(a=1:3));
605+
@inferred test_create_svector(cmat);
606+
test_create_smatrix = (cmat) -> SMatrix{size(cmat)...}(cmat)
607+
@test (@inferred test_create_smatrix(cmat)) isa SMatrix
608+
end;
609+
601610
@testset "Autodiff" begin
602611
include("autodiff_tests.jl")
603612
end

0 commit comments

Comments
 (0)