From b8753a59b0e973c957f971da543fb60381020f48 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 29 Apr 2021 11:11:17 +0400 Subject: [PATCH] Reshape accepts a single colon for AbstractArrays (#220) * reshape accepts one colon for AbstractArrays * Add tests with different ndims * fix offset for colon --- src/utils.jl | 2 +- test/runtests.jl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 8299f598..392f0316 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -11,7 +11,7 @@ _strip_IdOffsetRange(r::IdOffsetRange) = parent(r) _strip_IdOffsetRange(r) = r _offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent) -_offset(axparent::AbstractUnitRange, ax::Integer) = 1 - first(axparent) +_offset(axparent::AbstractUnitRange, ::Union{Integer, Colon}) = 1 - first(axparent) """ OffsetArrays.AxisConversionStyle(typeof(indices)) diff --git a/test/runtests.jl b/test/runtests.jl index 110c6adc..162d2d7e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1496,6 +1496,27 @@ end @test reshape(OffsetArray(-1:0, -1:0), :) == OffsetArray(-1:0, -1:0) @test reshape(A, :) == reshape(A0, :) + # reshape with one Colon for AbstractArrays + B = reshape(A0, -10:-9, :) + @test B isa OffsetArray{Int,2} + @test parent(B) === A0 + @test axes(B, 1) == -10:-9 + @test axes(B, 2) == axes(A0, 2) + + B = reshape(A0, -10:-9, 3:3, :) + @test B isa OffsetArray{Int,3} + @test same_value(A0, B) + @test axes(B, 1) == -10:-9 + @test axes(B, 2) == 3:3 + @test axes(B, 3) == 1:2 + + B = reshape(A0, -10:-9, 3:4, :) + @test B isa OffsetArray{Int,3} + @test same_value(A0, B) + @test axes(B, 1) == -10:-9 + @test axes(B, 2) == 3:4 + @test axes(B, 3) == 1:1 + # pop the parent B = reshape(A, size(A)) @test B == A0