@@ -183,26 +183,20 @@ homogenize_shape(shape::Tuple{Vararg{SOneTo}}) = Size(map(last, shape))
183183homogenize_shape (shape:: Tuple{Vararg{HeterogeneousShape}} ) = map (last, shape)
184184
185185
186- @inline reshape (a:: StaticArray , s:: Size ) = similar_type (a, s)(Tuple (a))
187- @inline reshape (a:: AbstractArray , s:: Size ) = _reshape (a, IndexStyle (a), s)
188- @inline reshape (a:: StaticArray , s:: Tuple{SOneTo,Vararg{SOneTo}} ) = reshape (a, homogenize_shape (s))
189- @generated function _reshape (a:: AbstractArray , indexstyle, s:: Size{S} ) where {S}
190- if indexstyle == IndexLinear
191- exprs = [:(a[$ i]) for i = 1 : prod (S)]
192- else
193- exprs = [:(a[$ (inds)]) for inds ∈ CartesianIndices (S)]
194- end
195-
196- return quote
197- @_inline_meta
198- if length (a) != prod (s)
199- throw (DimensionMismatch (" Tried to resize dynamic object of size $(size (a)) to $s " ))
200- end
201- return similar_type (a, s)(tuple ($ (exprs... )))
202- end
186+ @inline reshape (a:: SArray , s:: Size ) = similar_type (a, s)(Tuple (a))
187+ @inline reshape (a:: AbstractArray , s:: Size ) = __reshape (a, ((typeof (s). parameters... ). .. ,), s)
188+ @inline reshape (a:: SArray , s:: Tuple{SOneTo,Vararg{SOneTo}} ) = reshape (a, homogenize_shape (s))
189+ @inline function reshape (a:: StaticArray , s:: Tuple{SOneTo,Vararg{SOneTo}} )
190+ return __reshape (a, map (u -> last (u), s), homogenize_shape (s))
191+ end
192+ @inline function __reshape (a, shape, :: Size{S} ) where {S}
193+ return SizedArray {Tuple{S...}} (Base. _reshape (a, shape))
194+ end
195+ @inline function __reshape (a:: SizedArray , shape, :: Size{S} ) where {S}
196+ return SizedArray {Tuple{S...}} (Base. _reshape (a. data, shape))
203197end
204198
205- reshape (a:: Array , :: Size{S} ) where {S} = SizedArray {Tuple{S...}} (a)
199+ reshape (a:: Vector , :: Size{S} ) where {S} = SizedArray {Tuple{S...}} (a)
206200
207201Base. rdims (out:: Val{N} , inds:: Tuple{SOneTo, Vararg{SOneTo}} ) where {N} = Base. rdims (ntuple (i -> SOneTo (1 ), Val (N)), inds)
208202Base. rdims (out:: Tuple{Any} , inds:: Tuple{SOneTo, Vararg{SOneTo}} ) = (SOneTo (Base. rdims_trailing (inds... )),)
0 commit comments