Skip to content

Commit 0a68fac

Browse files
mikantimholy
mikan
authored andcommitted
Fix issue #82: Reshape with Colon doesn't work, can segfault (#84)
1 parent 950bb88 commit 0a68fac

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/OffsetArrays.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Base.axes1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize th
9292
# Avoid the kw-arg on the range(r+x, length=length(r)) call in r .+ x
9393
@inline _slice(r, x) = IdentityUnitRange(Base._range(first(r) + x, nothing, nothing, length(r)))
9494

95-
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, IdentityUnitRange}
95+
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, IdentityUnitRange, Colon}
9696
function Base.similar(A::OffsetArray, ::Type{T}, dims::Dims) where T
9797
B = similar(parent(A), T, dims)
9898
end
@@ -101,6 +101,7 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxis,Vararg
101101
OffsetArray(B, map(indexoffset, inds))
102102
end
103103

104+
Base.reshape(A::AbstractArray, inds::OffsetAxis...) = reshape(A, inds)
104105
Base.reshape(A::AbstractArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}}) =
105106
OffsetArray(reshape(A, map(indexlength, inds)), map(indexoffset, inds))
106107

@@ -111,6 +112,8 @@ Base.reshape(A::OffsetArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}}) =
111112
# And for non-offset axes, we can just return a reshape of the parent directly
112113
Base.reshape(A::OffsetArray, inds::Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}}) = reshape(parent(A), inds)
113114
Base.reshape(A::OffsetArray, inds::Dims) = reshape(parent(A), inds)
115+
Base.reshape(A::OffsetArray, inds::Union{Int,Colon}...) = reshape(parent(A), inds)
116+
Base.reshape(A::OffsetArray, inds::Tuple{Vararg{Union{Int,Colon}}}) = reshape(parent(A), inds)
114117

115118
Base.similar(::Type{T}, shape::Tuple{OffsetAxis,Vararg{OffsetAxis}}) where {T<:AbstractArray} =
116119
OffsetArray(T(undef, map(indexlength, shape)), map(indexoffset, shape))
@@ -217,8 +220,10 @@ offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be short
217220

218221
indexoffset(r::AbstractRange) = first(r) - 1
219222
indexoffset(i::Integer) = 0
223+
indexoffset(i::Colon) = 0
220224
indexlength(r::AbstractRange) = length(r)
221225
indexlength(i::Integer) = i
226+
indexlength(i::Colon) = Colon()
222227

223228
@eval @deprecate $(Symbol("@unsafe")) $(Symbol("@inbounds"))
224229

test/runtests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ end
296296
@test isa(b, OffsetArray{Float64,4})
297297
@test pointer(parent(b)) === pointer(parent(a))
298298
@test axes(b) == (axes(a)..., IdentityUnitRange(1:1))
299+
300+
@test reshape(OffsetArray(-1:0, -1:0), :, 1) == reshape(-1:0, 2, 1)
301+
@test reshape(OffsetArray(-1:2, -1:2), -2:-1, :) == reshape(-1:2, -2:-1, 2)
299302
end
300303

301304
@testset "Indexing with OffsetArray axes" begin

0 commit comments

Comments
 (0)