Skip to content

Commit

Permalink
Reshape accepts a single colon for AbstractArrays (#220)
Browse files Browse the repository at this point in the history
* reshape accepts one colon for AbstractArrays

* Add tests with different ndims

* fix offset for colon
  • Loading branch information
jishnub authored Apr 29, 2021
1 parent ce0a06d commit b8753a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b8753a5

Please sign in to comment.