@@ -119,31 +119,31 @@ julia> reshape(1:6, 2, 3)
119
119
"""
120
120
reshape
121
121
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))
125
125
reshape (parent:: AbstractArray , dims:: Dims ) = _reshape (parent, dims)
126
126
127
127
# 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,
136
135
" 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,
138
137
" must be divisible by the product of the new dimensions " , dims)))
139
138
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}}} )
141
141
# promote the dims to `Int` at least
142
142
dims = map (x -> x isa Colon ? x : promote_type (typeof (x), Int)(x), _dims)
143
+ dims isa Tuple{Vararg{Integer}} && return dims
143
144
pre = _before_colon (dims... )
144
145
post = _after_colon (dims... )
145
- _any_colon (post... ) && throw1 (dims)
146
- len = length (A)
146
+ _any_colon (post... ) && _reshape_throwcolon (dims)
147
147
_reshape_uncolon_computesize (len, dims, pre, post)
148
148
end
149
149
@inline function _reshape_uncolon_computesize (len:: Int , dims, pre:: Tuple{Vararg{Int}} , post:: Tuple{Vararg{Int}} )
@@ -167,18 +167,20 @@ end
167
167
(pre... , sz, post... )
168
168
end
169
169
@inline function _reshape_uncolon_computesize_nonempty (len, dims, pr)
170
- iszero (pr) && throw2 (len, dims)
170
+ iszero (pr) && _reshape_throwsize (len, dims)
171
171
(quo, rem) = divrem (len, pr)
172
- iszero (rem) || throw2 (len, dims)
172
+ iszero (rem) || _reshape_throwsize (len, dims)
173
173
quo
174
174
end
175
175
@inline _any_colon () = false
176
176
@inline _any_colon (dim:: Colon , tail... ) = true
177
177
@inline _any_colon (dim:: Any , tail... ) = _any_colon (tail... )
178
178
@inline _before_colon (dim:: Any , tail... ) = (dim, _before_colon (tail... )... )
179
179
@inline _before_colon (dim:: Colon , tail... ) = ()
180
+ @inline _before_colon () = ()
180
181
@inline _after_colon (dim:: Any , tail... ) = _after_colon (tail... )
181
182
@inline _after_colon (dim:: Colon , tail... ) = tail
183
+ @inline _after_colon () = ()
182
184
183
185
reshape (parent:: AbstractArray{T,N} , ndims:: Val{N} ) where {T,N} = parent
184
186
function reshape (parent:: AbstractArray , ndims:: Val{N} ) where N
0 commit comments