Skip to content

Commit

Permalink
eltype and ndims of an OffsetArray matches that of the parent (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Oct 7, 2020
1 parent 87666ee commit 08f969b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ julia> OffsetArray(a, OffsetArrays.Origin(0)) # short notation for `Origin(0, ..
"""
struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
parent::AA
offsets::NTuple{N,Int}
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray}
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray{T,N}}
@boundscheck overflow_check.(axes(parent), offsets)
new{T, N, AA}(parent, offsets)
end
Expand All @@ -103,14 +103,14 @@ end
Type alias and convenience constructor for one-dimensional [`OffsetArray`](@ref)s.
"""
const OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
const OffsetVector{T,AA<:AbstractVector{T}} = OffsetArray{T,1,AA}

"""
OffsetMatrix(A, index1, index2)
Type alias and convenience constructor for two-dimensional [`OffsetArray`](@ref)s.
"""
const OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}
const OffsetMatrix{T,AA<:AbstractMatrix{T}} = OffsetArray{T,2,AA}

function overflow_check(r, offset::T) where T
# This gives some performance boost https://github.com/JuliaLang/julia/issues/33273
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ end
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), (2, 4))
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), ())
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3))

# eltype of an OffsetArray should match that of the parent (issue #162)
@test_throws TypeError OffsetVector{Float64,Vector{ComplexF64}}
# ndim of an OffsetArray should match that of the parent
@test_throws TypeError OffsetVector{Float64,Matrix{Float64}}
end

@testset "OffsetMatrix" begin
Expand Down Expand Up @@ -416,6 +421,11 @@ end
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), 2,0,0)
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), ())
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2))

# eltype of an OffsetArray should match that of the parent (issue #162)
@test_throws TypeError OffsetMatrix{Float64,Matrix{ComplexF64}}
# ndim of an OffsetArray should match that of the parent
@test_throws TypeError OffsetMatrix{Float64,Vector{Float64}}
end

# no need to duplicate the 2D case here,
Expand Down Expand Up @@ -466,6 +476,11 @@ end
@test eltype(a) == Bool
end
end

# eltype of an OffsetArray should match that of the parent (issue #162)
@test_throws TypeError OffsetArray{Float64,2,Matrix{ComplexF64}}
# ndim of an OffsetArray should match that of the parent
@test_throws TypeError OffsetArray{Float64,3,Matrix{Float64}}
end

@testset "custom range types" begin
Expand Down

0 comments on commit 08f969b

Please sign in to comment.