diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index a86ed021..4afab1ec 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -24,7 +24,7 @@ OffsetArray{T,N}(::Type{T}, inds::Vararg{UnitRange{Int},N}) = OffsetArray{T,N}(i # The next two are necessary for ambiguity resolution. Really, the # second method should not be necessary. -OffsetArray{T}(A::AbstractArray{T,0}, inds::Tuple{}) = OffsetArray(A, ()) +OffsetArray{T}(A::AbstractArray{T,0}, inds::Tuple{}) = OffsetArray{T,0,typeof(A)}(A, ()) OffsetArray{T,N}(A::AbstractArray{T,N}, inds::Tuple{}) = error("this should never be called") OffsetArray{T,N}(A::AbstractArray{T,N}, inds::NTuple{N,AbstractUnitRange}) = OffsetArray(A, map(indexoffset, inds)) @@ -42,7 +42,6 @@ Base.size(A::OffsetArray) = errmsg(A) Base.size(A::OffsetArray, d) = errmsg(A) Base.eachindex(::LinearSlow, A::OffsetArray) = CartesianRange(indices(A)) Base.eachindex(::LinearFast, A::OffsetVector) = indices(A, 1) -Base.summary(A::OffsetArray) = string(typeof(A))*" with indices "*string(indices(A)) # Implementations of indices and indices1. Since bounds-checking is # performance-critical and relies on indices, these are usually worth @@ -108,6 +107,10 @@ Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) = fill!(OffsetArray{typeo ### Low-level utilities ### +_length(A) = Base.unsafe_length(linearindices(A)) +_size(A) = map(Base.unsafe_length, indices(A)) +_size(A, d::Integer) = Base.unsafe_length(indices(A, d)) + # Computing a shifted index (subtracting the offset) offset{N}(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) = _offset((), offsets, inds) _offset(out, ::Tuple{}, ::Tuple{}) = out diff --git a/test/runtests.jl b/test/runtests.jl index 67fe31f9..7a209e84 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,6 +4,13 @@ using OffsetArrays @test isempty(detect_ambiguities(OffsetArrays, Base, Core)) # Basics +for n = 0:5 + a = OffsetArray(ones(Int,ntuple(d->1,n)), ntuple(x->x-1,n)) + @test length(linearindices(a)) == 1 + @test indices(a) == ntuple(x->x:x,n) + @test a[1] == 1 +end + y = OffsetArray(Float64, -1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1) @test indices(y) == (-1:1, -7:7, -128:512, -5:5, -1:1, -3:3, -2:2, -1:1) y[-1,-7,-128,-5,-1,-3,-2,-1] = 14 @@ -292,3 +299,22 @@ B = ones(1:3, -1:1) B = fill(5, 1:3, -1:1) @test indices(B) == (1:3,-1:1) @test all(B.==5) + +R0 = rand(5,8) +T0 = rand(Complex{Float64}, 5, 8) +for o in ((-1,0), (0,2), (3,-3)) + R = OffsetArray(R0, o) + Rs = circshift(R0, o) + @test_approx_eq fft(R) fft(Rs) + @test_approx_eq fft(R,1) fft(Rs,1) + @test_approx_eq fft(R,2) fft(Rs,2) + @test_approx_eq fft(R,1:2) fft(Rs,1:2) + @test_approx_eq fft(R,()) fft(Rs,()) + T = OffsetArray(T0, o) + Ts = circshift(T0, o) + @test_approx_eq ifft(T) ifft(Ts) + @test_approx_eq ifft(T,1) ifft(Ts,1) + @test_approx_eq ifft(T,2) ifft(Ts,2) + @test_approx_eq ifft(T,1:2) ifft(Ts,1:2) + @test_approx_eq ifft(T,()) ifft(Ts,()) +end