Skip to content

Commit 35e9b1f

Browse files
committed
Fix ArrayInterface.size for dynamically sized vectors, add test for that, and also fix known_length for reinterpreted static ranges.
1 parent 664ea95 commit 35e9b1f

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.14.8"
3+
version = "2.14.9"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/stridelayout.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ end
422422
end
423423

424424

425+
@inline function known_length(::Type{T}) where {T <: Base.ReinterpretArray}
426+
_known_length(known_length(parent_type(T)), eltype(T), eltype(parent_type(T)))
427+
end
428+
_known_length(::Nothing, _, __) = nothing
429+
@inline _known_length(L::Integer, ::Type{T}, ::Type{P}) where {T,P} = L * sizeof(P) ÷ sizeof(T)
430+
425431
# These methods help handle identifying axes that dont' directly propagate from the
426432
# parent array axes. They may be worth making a formal part of the API, as they provide
427433
# a low traffic spot to change what axes_types produces.
@@ -617,7 +623,7 @@ end
617623
Expr(:block, Expr(:meta, :inline), t)
618624
end
619625

620-
@inline size(v::AbstractVector) = (static_length(axes_types(v, 1)),)
626+
@inline size(v::AbstractVector) = (static_length(v),)
621627
@inline size(B::Union{Transpose{T,A},Adjoint{T,A}}) where {T,A<:AbstractMatrix{T}} =
622628
permute(size(parent(B)), Val{(2, 1)}())
623629
@inline size(B::PermutedDimsArray{T,N,I1,I2,A}) where {T,N,I1,I2,A<:AbstractArray{T,N}} =

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,16 @@ end
371371
Rr = reinterpret(Int32, R)
372372
Ar = reinterpret(Float32, A)
373373

374-
374+
sv5 = @SVector(zeros(5)); v5 = Vector{Float64}(undef, 5);
375+
@test @inferred(ArrayInterface.size(sv5)) === (StaticInt(5),)
376+
@test @inferred(ArrayInterface.size(v5)) === (5,)
375377
@test @inferred(ArrayInterface.size(A)) === (3,4,5)
376378
@test @inferred(ArrayInterface.size(Ap)) === (2,5)
377379
@test @inferred(ArrayInterface.size(A)) === size(A)
378380
@test @inferred(ArrayInterface.size(Ap)) === size(Ap)
379381
@test @inferred(ArrayInterface.size(R)) === (StaticInt(2),)
380382
@test @inferred(ArrayInterface.size(Rr)) === (StaticInt(4),)
381-
383+
@test @inferred(ArrayInterface.known_length(Rr)) === 4
382384

383385
@test @inferred(ArrayInterface.size(S)) === (StaticInt(2), StaticInt(3), StaticInt(4))
384386
@test @inferred(ArrayInterface.size(Sp)) === (2, 2, StaticInt(3))

0 commit comments

Comments
 (0)