@@ -340,22 +340,39 @@ _similar_axes_or_length(AT, ax::I, ::I) where {I} = similar(AT, map(_indexlength
340340# reshape accepts a single colon
341341Base. reshape (A:: AbstractArray , inds:: OffsetAxis... ) = reshape (A, inds)
342342function Base. reshape (A:: AbstractArray , inds:: Tuple{OffsetAxis,Vararg{OffsetAxis}} )
343- AR = reshape (A , map (_indexlength, inds))
343+ AR = reshape (no_offset_view (A) , map (_indexlength, inds))
344344 O = OffsetArray (AR, map (_offset, axes (AR), inds))
345345 return _popreshape (O, axes (AR), _filterreshapeinds (inds))
346346end
347347
348348# Reshaping OffsetArrays can "pop" the original OffsetArray wrapper and return
349349# an OffsetArray(reshape(...)) instead of an OffsetArray(reshape(OffsetArray(...)))
350+ # Short-circuit for AbstractVectors if the axes are compatible to get around the Base restriction
351+ # to 1-based vectors
352+ function _reshape (A:: AbstractVector , inds:: Tuple{OffsetAxis} )
353+ @noinline throw_dimerr (ind:: Integer ) = throw (
354+ DimensionMismatch (" parent has $(size (A,1 )) elements, which is incompatible with length $ind " ))
355+ @noinline throw_dimerr (ind) = throw (
356+ DimensionMismatch (" parent has $(size (A,1 )) elements, which is incompatible with indices $ind " ))
357+ _checksize (first (inds), size (A,1 )) || throw_dimerr (first (inds))
358+ A
359+ end
360+ _reshape (A, inds) = _reshape2 (A, inds)
361+ _reshape2 (A, inds) = reshape (A, inds)
362+ # avoid a stackoverflow by relegating to the parent if no_offset_view returns an offsetarray
363+ _reshape2 (A:: OffsetArray , inds) = reshape (parent (A), inds)
364+ _reshape_nov (A, inds) = _reshape (no_offset_view (A), inds)
365+
350366Base. reshape (A:: OffsetArray , inds:: Tuple{OffsetAxis,Vararg{OffsetAxis}} ) =
351- OffsetArray (reshape (parent (A), map (_indexlength, inds)) , map (_indexoffset , inds))
367+ OffsetArray (_reshape (parent (A), inds), map (_toaxis , inds))
352368# And for non-offset axes, we can just return a reshape of the parent directly
353- Base. reshape (A:: OffsetArray , inds:: Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}} ) = reshape (parent (A), inds)
354- Base. reshape (A:: OffsetArray , inds:: Dims ) = reshape (parent (A), inds)
355- Base. reshape (A:: OffsetArray , :: Colon ) = reshape (parent (A), Colon ())
369+ Base. reshape (A:: OffsetArray , inds:: Tuple{Union{Integer,Base.OneTo},Vararg{Union{Integer,Base.OneTo}}} ) = _reshape_nov (A, inds)
370+ Base. reshape (A:: OffsetArray , inds:: Dims ) = _reshape_nov (A, inds)
356371Base. reshape (A:: OffsetVector , :: Colon ) = A
357- Base. reshape (A:: OffsetArray , inds:: Union{Int,Colon} ...) = reshape (parent (A), inds)
358- Base. reshape (A:: OffsetArray , inds:: Tuple{Vararg{Union{Int,Colon}}} ) = reshape (parent (A), inds)
372+ Base. reshape (A:: OffsetVector , :: Tuple{Colon} ) = A
373+ Base. reshape (A:: OffsetArray , :: Colon ) = reshape (A, (Colon (),))
374+ Base. reshape (A:: OffsetArray , inds:: Union{Int,Colon} ...) = reshape (A, inds)
375+ Base. reshape (A:: OffsetArray , inds:: Tuple{Vararg{Union{Int,Colon}}} ) = _reshape_nov (A, inds)
359376
360377# permutedims in Base does not preserve axes, and can not be fixed in a non-breaking way
361378# This is a stopgap solution
0 commit comments