Skip to content

Commit 0f2089f

Browse files
authored
Support conversion to AbstractArray (#234)
* improve conversion to AbstractArray * test AbstractArray conversion for static matrices
1 parent 0c3d0f6 commit 0f2089f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/OffsetArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ end
252252

253253
Base.convert(::Type{T}, M::AbstractArray) where {T<:OffsetArray} = M isa T ? M : T(M)
254254

255+
@inline AbstractArray{T,N}(M::OffsetArray{S,N}) where {T,S,N} = OffsetArray{T}(M)
256+
255257
# array initialization
256258
@inline function OffsetArray{T,N}(init::ArrayInitializer, inds::Tuple{Vararg{OffsetAxisKnownLength}}; kw...) where {T,N}
257259
_checkindices(N, inds, "indices")

test/runtests.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,38 @@ end
25402540
end
25412541
end
25422542

2543+
@testset "Conversion to AbstractArray{T}" begin
2544+
r = 1:4
2545+
T = Float64
2546+
V = typeof(map(T, r))
2547+
v = OffsetVector(r)
2548+
@test OffsetArray{T}(v) isa OffsetVector{T,V}
2549+
@test AbstractArray{T}(v) isa OffsetVector{T,V}
2550+
@test AbstractVector{T}(v) isa OffsetVector{T,V}
2551+
@test convert(AbstractVector{T}, v) isa OffsetVector{T,V}
2552+
@test convert(AbstractArray{T}, v) isa OffsetVector{T,V}
2553+
@test axes(OffsetArray{T}(v)) === axes(v)
2554+
@test axes(AbstractArray{T}(v)) === axes(v)
2555+
@test axes(AbstractVector{T}(v)) === axes(v)
2556+
@test axes(convert(AbstractVector{T}, v)) === axes(v)
2557+
@test axes(convert(AbstractArray{T}, v)) == axes(v)
2558+
2559+
A = SMatrix{2,2}(1, 0, 0, 1)
2560+
TA = typeof(map(T, A))
2561+
OA = OffsetMatrix(A, 3:4, 5:6)
2562+
@test OffsetArray{T}(OA) isa OffsetMatrix{T,TA}
2563+
@test AbstractArray{T}(OA) isa OffsetMatrix{T,TA}
2564+
@test AbstractMatrix{T}(OA) isa OffsetMatrix{T,TA}
2565+
@test convert(AbstractMatrix{T}, OA) isa OffsetMatrix{T,TA}
2566+
@test convert(AbstractArray{T}, OA) isa OffsetMatrix{T,TA}
2567+
@test axes(OffsetArray{T}(OA)) === axes(OA)
2568+
@test axes(AbstractArray{T}(OA)) === axes(OA)
2569+
@test axes(AbstractMatrix{T}(OA)) === axes(OA)
2570+
@test axes(convert(AbstractMatrix{T}, OA)) === axes(OA)
2571+
@test axes(convert(AbstractArray{T}, OA)) === axes(OA)
2572+
end
2573+
2574+
25432575
include("origin.jl")
25442576

25452577
@testset "misc" begin

0 commit comments

Comments
 (0)