@@ -119,31 +119,31 @@ julia> reshape(1:6, 2, 3)
119119"""
120120reshape
121121
122- reshape (parent :: AbstractArray , dims :: IntOrInd... ) = reshape (parent, dims)
123- reshape (parent:: AbstractArray , shp :: Tuple{ Union{Integer,OneTo}, Vararg{Union{Integer,OneTo}}} ) = reshape (parent, to_shape (shp) )
124- reshape (parent:: AbstractArray , dims:: Tuple{Integer, Vararg{Integer}} ) = reshape (parent, map (Int, dims))
122+ # we collect the vararg indices and only define methods for tuples of indices
123+ reshape (parent:: AbstractArray , dims :: Union{Integer,Colon,AbstractUnitRange} ... ) = reshape (parent, dims )
124+ reshape (parent:: AbstractArray , dims:: Tuple{Vararg{Integer}} ) = reshape (parent, map (Int, dims))
125125reshape (parent:: AbstractArray , dims:: Dims ) = _reshape (parent, dims)
126126
127127# Allow missing dimensions with Colon():
128- reshape (parent:: AbstractVector , :: Colon ) = parent
129- reshape (parent:: AbstractVector , :: Tuple{Colon} ) = parent
130- reshape (parent:: AbstractArray , dims:: Int... ) = reshape (parent, dims)
131- reshape (parent:: AbstractArray , dims:: Integer... ) = reshape (parent, dims)
132- reshape (parent:: AbstractArray , dims:: Union{Integer,Colon} ...) = reshape (parent, dims)
133- reshape (parent:: AbstractArray , dims:: Tuple{Vararg{Union{Integer,Colon}}} ) = reshape (parent, _reshape_uncolon (parent, dims))
134-
135- @noinline throw1 (dims) = throw (DimensionMismatch (LazyString (" new dimensions " , dims,
128+ # convert axes to sizes using to_shape, and convert colons to sizes using _reshape_uncolon
129+ # We add a level of indirection to avoid method ambiguities in reshape
130+ reshape (parent:: AbstractArray , dims:: Tuple{Vararg{Union{Integer,Colon,OneTo}}} ) = _reshape_maybecolon (parent, dims)
131+ _reshape_maybecolon (parent:: AbstractVector , :: Tuple{Colon} ) = parent
132+ _reshape_maybecolon (parent:: AbstractArray , dims:: Tuple{Vararg{Union{Integer,Colon,OneTo}}} ) = reshape (parent, _reshape_uncolon (length (parent), to_shape (dims)))
133+
134+ @noinline _reshape_throwcolon (dims) = throw (DimensionMismatch (LazyString (" new dimensions " , dims,
136135 " may have at most one omitted dimension specified by `Colon()`" )))
137- @noinline throw2 (lenA, dims) = throw (DimensionMismatch (string (" array size " , lenA,
136+ @noinline _reshape_throwsize (lenA, dims) = throw (DimensionMismatch (LazyString (" array size " , lenA,
138137 " must be divisible by the product of the new dimensions " , dims)))
139138
140- @inline function _reshape_uncolon (A, _dims:: Tuple{Vararg{Union{Integer, Colon}}} )
139+ _reshape_uncolon (len, :: Tuple{Colon} ) = len
140+ @inline function _reshape_uncolon (len, _dims:: Tuple{Vararg{Union{Integer, Colon}}} )
141141 # promote the dims to `Int` at least
142142 dims = map (x -> x isa Colon ? x : promote_type (typeof (x), Int)(x), _dims)
143+ dims isa Tuple{Vararg{Integer}} && return dims
143144 pre = _before_colon (dims... )
144145 post = _after_colon (dims... )
145- _any_colon (post... ) && throw1 (dims)
146- len = length (A)
146+ _any_colon (post... ) && _reshape_throwcolon (dims)
147147 _reshape_uncolon_computesize (len, dims, pre, post)
148148end
149149@inline function _reshape_uncolon_computesize (len:: Int , dims, pre:: Tuple{Vararg{Int}} , post:: Tuple{Vararg{Int}} )
@@ -167,18 +167,20 @@ end
167167 (pre... , sz, post... )
168168end
169169@inline function _reshape_uncolon_computesize_nonempty (len, dims, pr)
170- iszero (pr) && throw2 (len, dims)
170+ iszero (pr) && _reshape_throwsize (len, dims)
171171 (quo, rem) = divrem (len, pr)
172- iszero (rem) || throw2 (len, dims)
172+ iszero (rem) || _reshape_throwsize (len, dims)
173173 quo
174174end
175175@inline _any_colon () = false
176176@inline _any_colon (dim:: Colon , tail... ) = true
177177@inline _any_colon (dim:: Any , tail... ) = _any_colon (tail... )
178178@inline _before_colon (dim:: Any , tail... ) = (dim, _before_colon (tail... )... )
179179@inline _before_colon (dim:: Colon , tail... ) = ()
180+ @inline _before_colon () = ()
180181@inline _after_colon (dim:: Any , tail... ) = _after_colon (tail... )
181182@inline _after_colon (dim:: Colon , tail... ) = tail
183+ @inline _after_colon () = ()
182184
183185reshape (parent:: AbstractArray{T,N} , ndims:: Val{N} ) where {T,N} = parent
184186function reshape (parent:: AbstractArray , ndims:: Val{N} ) where N
0 commit comments