Skip to content

Commit b8753a5

Browse files
authored
Reshape accepts a single colon for AbstractArrays (#220)
* reshape accepts one colon for AbstractArrays * Add tests with different ndims * fix offset for colon
1 parent ce0a06d commit b8753a5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _strip_IdOffsetRange(r::IdOffsetRange) = parent(r)
1111
_strip_IdOffsetRange(r) = r
1212

1313
_offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent)
14-
_offset(axparent::AbstractUnitRange, ax::Integer) = 1 - first(axparent)
14+
_offset(axparent::AbstractUnitRange, ::Union{Integer, Colon}) = 1 - first(axparent)
1515

1616
"""
1717
OffsetArrays.AxisConversionStyle(typeof(indices))

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,27 @@ end
14961496
@test reshape(OffsetArray(-1:0, -1:0), :) == OffsetArray(-1:0, -1:0)
14971497
@test reshape(A, :) == reshape(A0, :)
14981498

1499+
# reshape with one Colon for AbstractArrays
1500+
B = reshape(A0, -10:-9, :)
1501+
@test B isa OffsetArray{Int,2}
1502+
@test parent(B) === A0
1503+
@test axes(B, 1) == -10:-9
1504+
@test axes(B, 2) == axes(A0, 2)
1505+
1506+
B = reshape(A0, -10:-9, 3:3, :)
1507+
@test B isa OffsetArray{Int,3}
1508+
@test same_value(A0, B)
1509+
@test axes(B, 1) == -10:-9
1510+
@test axes(B, 2) == 3:3
1511+
@test axes(B, 3) == 1:2
1512+
1513+
B = reshape(A0, -10:-9, 3:4, :)
1514+
@test B isa OffsetArray{Int,3}
1515+
@test same_value(A0, B)
1516+
@test axes(B, 1) == -10:-9
1517+
@test axes(B, 2) == 3:4
1518+
@test axes(B, 3) == 1:1
1519+
14991520
# pop the parent
15001521
B = reshape(A, size(A))
15011522
@test B == A0

0 commit comments

Comments
 (0)