@@ -154,6 +154,11 @@ _maybe_reshape_parent(A::AbstractArray, ::NTuple{1, Bool}) = reshape(A, Val(1))
154
154
_maybe_reshape_parent (A:: AbstractArray{<:Any,1} , :: NTuple{1, Bool} ) = reshape (A, Val (1 ))
155
155
_maybe_reshape_parent (A:: AbstractArray{<:Any,N} , :: NTuple{N, Bool} ) where {N} = A
156
156
_maybe_reshape_parent (A:: AbstractArray , :: NTuple{N, Bool} ) where {N} = reshape (A, Val (N))
157
+ # The trailing singleton indices could be eliminated after bounds checking.
158
+ rm_singleton_indices (ndims:: Tuple , J1, Js... ) = (J1, rm_singleton_indices (IteratorsMD. _splitrest (ndims, index_ndims (J1)), Js... )... )
159
+ rm_singleton_indices (:: Tuple{} , :: ScalarIndex , Js... ) = rm_singleton_indices ((), Js... )
160
+ rm_singleton_indices (:: Tuple ) = ()
161
+
157
162
"""
158
163
view(A, inds...)
159
164
@@ -200,15 +205,12 @@ julia> view(2:5, 2:3) # returns a range as type is immutable
200
205
3:4
201
206
```
202
207
"""
203
- function view (A:: AbstractArray{<:Any,N} , I:: Vararg{Any,M} ) where {N, M}
208
+ function view (A:: AbstractArray , I:: Vararg{Any,M} ) where {M}
204
209
@inline
205
210
J = map (i-> unalias (A,i), to_indices (A, I))
206
211
@boundscheck checkbounds (A, J... )
207
- if length (J) > ndims (A) && J[N+ 1 : end ] isa Tuple{Vararg{Int}}
208
- # view([1,2,3], :, 1) does not need to reshape
209
- return unsafe_view (A, J[1 : N]. .. )
210
- end
211
- unsafe_view (_maybe_reshape_parent (A, index_ndims (J... )), J... )
212
+ J′ = rm_singleton_indices (ntuple (Returns (true ), Val (ndims (A))), J... )
213
+ unsafe_view (_maybe_reshape_parent (A, index_ndims (J′... )), J′... )
212
214
end
213
215
214
216
# Ranges implement getindex to return recomputed ranges; use that for views, too (when possible)
0 commit comments