From 39184c72954cf576a509cde6959934c87e60750e Mon Sep 17 00:00:00 2001 From: jishnub Date: Tue, 15 Jun 2021 14:22:47 +0400 Subject: [PATCH 1/3] reshape does not accept offset arrays --- base/reshapedarray.jl | 1 + test/abstractarray.jl | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 344858e76764a..f93e5d8b56021 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -183,6 +183,7 @@ function _reshape(v::AbstractVector, dims::Dims{1}) end # General reshape function _reshape(parent::AbstractArray, dims::Dims) + require_one_based_indexing(parent) n = length(parent) prod(dims) == n || _throw_dmrs(n, "size", dims) __reshape((parent, IndexStyle(parent)), dims) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 9d9c9b9c87776..27b09be92aea9 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -2050,3 +2050,7 @@ end test_unsetindex(MyMatrixUnsetIndexCartInds) test_unsetindex(MyMatrixUnsetIndexLinInds) end + +@testset "reshape for offset arrays" begin + @test_throws ArgumentError reshape(Base.IdentityUnitRange(0:1), (2,1)) +end From b4e95720c4d696ccab24022297969e935d1ca21b Mon Sep 17 00:00:00 2001 From: jishnub Date: Thu, 17 Jun 2021 11:00:52 +0400 Subject: [PATCH 2/3] fix linear indexing for ReshapedArray --- base/reshapedarray.jl | 7 ++++--- test/abstractarray.jl | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index f93e5d8b56021..0cd276be6494f 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -183,7 +183,6 @@ function _reshape(v::AbstractVector, dims::Dims{1}) end # General reshape function _reshape(parent::AbstractArray, dims::Dims) - require_one_based_indexing(parent) n = length(parent) prod(dims) == n || _throw_dmrs(n, "size", dims) __reshape((parent, IndexStyle(parent)), dims) @@ -252,7 +251,8 @@ end @inline function getindex(A::ReshapedArrayLF, index::Int) @boundscheck checkbounds(A, index) - @inbounds ret = parent(A)[index] + indexparent = index - firstindex(A) + firstindex(parent(A)) + @inbounds ret = parent(A)[indexparent] ret end @inline function getindex(A::ReshapedArray{T,N}, indices::Vararg{Int,N}) where {T,N} @@ -276,7 +276,8 @@ end @inline function setindex!(A::ReshapedArrayLF, val, index::Int) @boundscheck checkbounds(A, index) - @inbounds parent(A)[index] = val + indexparent = index - firstindex(A) + firstindex(parent(A)) + @inbounds parent(A)[indexparent] = val val end @inline function setindex!(A::ReshapedArray{T,N}, val, indices::Vararg{Int,N}) where {T,N} diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 27b09be92aea9..a07c0f79a9c25 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -2052,5 +2052,29 @@ end end @testset "reshape for offset arrays" begin - @test_throws ArgumentError reshape(Base.IdentityUnitRange(0:1), (2,1)) + r = reshape(Base.IdentityUnitRange(0:1), (2,1)) + @test r[eachindex(r)] == [0:1;] + + struct ZeroBasedArray{T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N} + a :: A + function ZeroBasedArray(a::AbstractArray) + Base.require_one_based_indexing(a) + new{eltype(a), ndims(a), typeof(a)}(a) + end + end + Base.parent(z::ZeroBasedArray) = z.a + Base.size(z::ZeroBasedArray) = size(parent(z)) + Base.axes(z::ZeroBasedArray) = map(x -> Base.IdentityUnitRange(0:x - 1), size(parent(z))) + Base.getindex(z::ZeroBasedArray{<:Any, N}, i::Vararg{Int,N}) where {N} = parent(z)[map(x -> x + 1, i)...] + Base.setindex!(z::ZeroBasedArray{<:Any, N}, val, i::Vararg{Int,N}) where {N} = parent(z)[map(x -> x + 1, i)...] = val + + z = ZeroBasedArray(collect(1:4)) + r2 = reshape(z, (4, 1)) + @test r2[CartesianIndices(r2)] == r2[LinearIndices(r2)] + r2[firstindex(r2)] = 34 + @test z[0] == 34 + r2[eachindex(r2)] = r2 .* 2 + for (i, j) in zip(eachindex(r2), eachindex(z)) + @test r2[i] == z[j] + end end From 9e27b85b5c07e4e477a2d4f03bbd54517a47408b Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 9 Mar 2024 20:18:22 +0530 Subject: [PATCH 3/3] Fix isassigned --- base/reshapedarray.jl | 3 ++- test/abstractarray.jl | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 0cd276be6494f..63e508e8835de 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -238,7 +238,8 @@ offset_if_vec(i::Integer, axs::Tuple) = i @inline function isassigned(A::ReshapedArrayLF, index::Int) @boundscheck checkbounds(Bool, A, index) || return false - @inbounds ret = isassigned(parent(A), index) + indexparent = index - firstindex(A) + firstindex(parent(A)) + @inbounds ret = isassigned(parent(A), indexparent) ret end @inline function isassigned(A::ReshapedArray{T,N}, indices::Vararg{Int, N}) where {T,N} diff --git a/test/abstractarray.jl b/test/abstractarray.jl index a07c0f79a9c25..e6bf0a9f24c11 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -2052,8 +2052,10 @@ end end @testset "reshape for offset arrays" begin - r = reshape(Base.IdentityUnitRange(0:1), (2,1)) - @test r[eachindex(r)] == [0:1;] + p = Base.IdentityUnitRange(3:4) + r = reshape(p, :, 1) + @test r[eachindex(r)] == UnitRange(p) + @test collect(r) == r struct ZeroBasedArray{T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N} a :: A @@ -2069,7 +2071,7 @@ end Base.setindex!(z::ZeroBasedArray{<:Any, N}, val, i::Vararg{Int,N}) where {N} = parent(z)[map(x -> x + 1, i)...] = val z = ZeroBasedArray(collect(1:4)) - r2 = reshape(z, (4, 1)) + r2 = reshape(z, :, 1) @test r2[CartesianIndices(r2)] == r2[LinearIndices(r2)] r2[firstindex(r2)] = 34 @test z[0] == 34