Skip to content

Commit

Permalink
loose eltype when fillvalue is nothing or missing (#24)
Browse files Browse the repository at this point in the history
loose eltype for nothing and missing
  • Loading branch information
johnnychen94 authored Mar 9, 2020
1 parent 1762776 commit 244f397
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/PaddedViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,23 @@ struct PaddedView{T,N,I,A} <: AbstractArray{T,N}
data::A
indices::I

function PaddedView{T,N,I,A}(fillvalue::T,
data::AbstractArray{T,N},
function PaddedView{T,N,I,A}(fillvalue,
data,
indices::NTuple{N,AbstractUnitRange}) where {T,N,I,A}
ndims(data) == N || throw(DimensionMismatch("data and indices should have the same dimension, instead they're $(ndims(data)) and $N."))
new{T,N,I,A}(fillvalue, data, indices)
end
end

PaddedView(fillvalue, data::AbstractArray{T,N}, indices) where {T,N} =
PaddedView{T,N,typeof(indices),typeof(data)}(convert(T, fillvalue), data, indices)
function PaddedView(fillvalue, data::AbstractArray{T,N}, indices) where {T,N}
_T = fillvalue isa Union{Missing, Nothing} ? Union{typeof(fillvalue), T} : T
PaddedView{_T,N,typeof(indices),typeof(data)}(convert(_T, fillvalue), data, indices)
end

function PaddedView(fillvalue, data::AbstractArray{T,N}, sz::Tuple{Integer,Vararg{Integer}}) where {T,N}
inds = map(OneTo, sz)
PaddedView{T,N,typeof(inds),typeof(data)}(convert(T, fillvalue), data, inds)
_T = fillvalue isa Union{Missing, Nothing} ? Union{typeof(fillvalue), T} : T
PaddedView{_T,N,typeof(inds),typeof(data)}(convert(_T, fillvalue), data, inds)
end

# This method eliminates an ambiguity between the two below it
Expand Down
50 changes: 50 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,53 @@ end
str = String(take!(io))
@test endswith(str, "PaddedView(-1, ::Array{$Int,2}, (0:4, 1:3)) with eltype $Int with indices 0:4×1:3:\n -1 -1 -1\n 1 4 7\n 2 5 8\n 3 6 9\n -1 -1 -1")
end

@testset "similar" begin
for (T, v) in ((Int, 0),
(Int, 0.),
(Float64, 0.),
(Missing, missing),
(Nothing, nothing))
A = reshape(1:9, 3, 3)
Ap = @inferred(PaddedView(v, A, (0:4, 0:4)))
B = similar(Ap)
@test eltype(B) == eltype(Ap)
@test size(B) == size(Ap)
@test axes(B) == axes(Ap)

B = similar(Ap, (4, 4))
@test eltype(B) == eltype(Ap)
@test size(B) == (4, 4)
@test axes(B) == (Base.OneTo(4), Base.OneTo(4))

B = similar(Ap, (4, 4))
@test eltype(B) == eltype(Ap)
@test size(B) == (4, 4)
@test axes(B) == (Base.OneTo(4), Base.OneTo(4))

B = similar(Ap, Int, (4, 4))
@test eltype(B) == Int
@test size(B) == (4, 4)
@test axes(B) == (Base.OneTo(4), Base.OneTo(4))
end
end

@testset "nothing/missing" begin
for (T, v) in ((Missing, missing),
(Nothing, nothing))
A = reshape(1:9, 3, 3)
Ap = @inferred(PaddedView(v, A, (0:4, 0:4)))
@test axes(Ap) == (OffsetArrays.IdOffsetRange(0:4), OffsetArrays.IdOffsetRange(0:4))
@test eltype(Ap) === Union{T, eltype(A)}
@test Ap[0, 0] === v
@test Ap[1, 1] === 1
@test Ap[axes(A)...] == A

Ap = @inferred(PaddedView(v, A, (5, 5)))
@test axes(Ap) === (Base.OneTo(5), Base.OneTo(5))
@test eltype(Ap) === Union{T, eltype(A)}
@test Ap[4, 4] === v
@test Ap[1, 1] === 1
@test Ap[axes(A)...] == A
end
end

0 comments on commit 244f397

Please sign in to comment.