Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary reshape methods #367

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,13 @@ _reshape2(A::OffsetArray, inds) = reshape(parent(A), inds)
_reshape_nov(A, inds) = _reshape(no_offset_view(A), inds)

# And for non-offset axes, we can just return a reshape of the parent directly
Base.reshape(A::OffsetArray, inds::Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}}) = _reshape_nov(A, inds)
Base.reshape(A::OffsetArray, inds::Tuple{Integer,Vararg{Integer}}) = _reshape_nov(A, inds)
Base.reshape(A::OffsetArray, inds::Dims) = _reshape_nov(A, inds)
Base.reshape(A::OffsetVector, ::Colon) = A
Base.reshape(A::OffsetVector, ::Tuple{Colon}) = A
Base.reshape(A::OffsetArray, inds::Union{Int,Colon}...) = reshape(A, inds)
Base.reshape(A::OffsetArray, inds::Tuple{Vararg{Union{Int,Colon}}}) = _reshape_nov(A, inds)
# The following two additional methods for Colon are added to resolve method ambiguities to
# Base: https://github.com/JuliaLang/julia/pull/45387#issuecomment-1132859663
Base.reshape(A::OffsetArray, inds::Colon) = reshape(parent(A), inds)
Base.reshape(A::OffsetArray, inds::Tuple{Colon}) = reshape(parent(A), inds)
if VERSION < v"1.10.7"
# the specialized reshape(parent::AbstractVector, ::Tuple{Colon}) is available in Base at least on this version
Base.reshape(A::OffsetVector, ::Tuple{Colon}) = A
Base.reshape(A::OffsetArray, inds::Tuple{Vararg{Union{Int,Colon}}}) = _reshape_nov(A, inds)
end

# permutedims in Base does not preserve axes, and can not be fixed in a non-breaking way
# This is a stopgap solution
Expand Down
32 changes: 30 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,28 @@ end
end
end

# custom FillArray with BigInt axes, used to test `reshape`
struct MyBigFill{T,N} <: AbstractArray{T,N}
val :: T
axes :: NTuple{N,Base.OneTo{BigInt}}
end
MyBigFill(val, sz::NTuple{N,BigInt}) where {N} = MyBigFill(val, map(Base.OneTo, sz))
MyBigFill(val, sz::Tuple{Vararg{Integer}}) = MyBigFill(val, map(BigInt, sz))
Base.size(M::MyBigFill) = map(length, M.axes)
Base.axes(M::MyBigFill) = M.axes
function Base.getindex(M::MyBigFill{<:Any,N}, ind::Vararg{Integer,N}) where {N}
checkbounds(M, ind...)
M.val
end
function Base.isassigned(M::MyBigFill{<:Any,N}, ind::Vararg{BigInt,N}) where {N}
checkbounds(M, ind...)
true
end
function Base.reshape(M::MyBigFill, ind::NTuple{N,BigInt}) where {N}
length(M) == prod(ind) || throw(ArgumentError("length mismatch in reshape"))
MyBigFill(M.val, ind)
end

@testset "reshape" begin
A0 = [1 3; 2 4]
A = OffsetArray(A0, (-1,2))
Expand Down Expand Up @@ -1846,10 +1868,11 @@ end
@test axes(R) == (1:2, 1:3)

r = OffsetArray(ZeroBasedRange(3:4), 1);
@test reshape(r, 2) == 3:4
@test reshape(r, (2,)) == 3:4
@test reshape(r, 2) == reshape(r, big(2)) == 3:4
@test reshape(r, (2,)) == reshape(r, (big(2),)) == 3:4
@test reshape(r, :) == 3:4
@test reshape(r, (:,)) == 3:4
@test reshape(r, big(2), 1) == reshape(3:4, 2, 1)

# getindex for a reshaped array that wraps an offset array is broken on 1.0
if VERSION >= v"1.1"
Expand Down Expand Up @@ -1901,6 +1924,11 @@ end
catch e
e isa TypeError || rethrow()
end
@testset "Tuple{Vararg{Integer}}" begin
M = MyBigFill(4, (2, 3))
O = OffsetArray(M)
@test vec(O) isa MyBigFill
end
end

@testset "permutedims" begin
Expand Down
Loading