Skip to content

Commit b704533

Browse files
authored
Remove unnecessary reshape methods (#367)
* Remove unnecessary `reshape` methods * version-limit methods with Colon * Require at least one `Integer` in `reshape` method * Add `BigInt` tests * Specific test for `Integer` reshaping * Move MyBigFill`struct defn to global scope * Update `MyBigFill` methods in test
1 parent 4ffdc70 commit b704533

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/OffsetArrays.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,13 @@ _reshape2(A::OffsetArray, inds) = reshape(parent(A), inds)
376376
_reshape_nov(A, inds) = _reshape(no_offset_view(A), inds)
377377

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

390387
# permutedims in Base does not preserve axes, and can not be fixed in a non-breaking way
391388
# This is a stopgap solution

test/runtests.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,28 @@ end
17491749
end
17501750
end
17511751

1752+
# custom FillArray with BigInt axes, used to test `reshape`
1753+
struct MyBigFill{T,N} <: AbstractArray{T,N}
1754+
val :: T
1755+
axes :: NTuple{N,Base.OneTo{BigInt}}
1756+
end
1757+
MyBigFill(val, sz::NTuple{N,BigInt}) where {N} = MyBigFill(val, map(Base.OneTo, sz))
1758+
MyBigFill(val, sz::Tuple{Vararg{Integer}}) = MyBigFill(val, map(BigInt, sz))
1759+
Base.size(M::MyBigFill) = map(length, M.axes)
1760+
Base.axes(M::MyBigFill) = M.axes
1761+
function Base.getindex(M::MyBigFill{<:Any,N}, ind::Vararg{Integer,N}) where {N}
1762+
checkbounds(M, ind...)
1763+
M.val
1764+
end
1765+
function Base.isassigned(M::MyBigFill{<:Any,N}, ind::Vararg{BigInt,N}) where {N}
1766+
checkbounds(M, ind...)
1767+
true
1768+
end
1769+
function Base.reshape(M::MyBigFill, ind::NTuple{N,BigInt}) where {N}
1770+
length(M) == prod(ind) || throw(ArgumentError("length mismatch in reshape"))
1771+
MyBigFill(M.val, ind)
1772+
end
1773+
17521774
@testset "reshape" begin
17531775
A0 = [1 3; 2 4]
17541776
A = OffsetArray(A0, (-1,2))
@@ -1846,10 +1868,11 @@ end
18461868
@test axes(R) == (1:2, 1:3)
18471869

18481870
r = OffsetArray(ZeroBasedRange(3:4), 1);
1849-
@test reshape(r, 2) == 3:4
1850-
@test reshape(r, (2,)) == 3:4
1871+
@test reshape(r, 2) == reshape(r, big(2)) == 3:4
1872+
@test reshape(r, (2,)) == reshape(r, (big(2),)) == 3:4
18511873
@test reshape(r, :) == 3:4
18521874
@test reshape(r, (:,)) == 3:4
1875+
@test reshape(r, big(2), 1) == reshape(3:4, 2, 1)
18531876

18541877
# getindex for a reshaped array that wraps an offset array is broken on 1.0
18551878
if VERSION >= v"1.1"
@@ -1901,6 +1924,11 @@ end
19011924
catch e
19021925
e isa TypeError || rethrow()
19031926
end
1927+
@testset "Tuple{Vararg{Integer}}" begin
1928+
M = MyBigFill(4, (2, 3))
1929+
O = OffsetArray(M)
1930+
@test vec(O) isa MyBigFill
1931+
end
19041932
end
19051933

19061934
@testset "permutedims" begin

0 commit comments

Comments
 (0)