Skip to content

Commit 1b32415

Browse files
committed
Simpler reshaping for SubArray
1 parent d9e52d5 commit 1b32415

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

base/subarray.jl

+7-17
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ immutable SubArray{T,N,P,I,L} <: AbstractArray{T,N}
1212
first_index::Int # for linear indexing and pointer, only valid when L==true
1313
stride1::Int # used only for linear indexing
1414
end
15+
# Ensure the dimensionality of the parent matches the number of indices
16+
function SubArray{N}(parent::AbstractArray, indexes::NTuple{N}, dims::Tuple)
17+
SubArray(reshape(parent, Val{N}), indexes, dims)
18+
end
1519
# Compute the linear indexability of the indices, and combine it with the linear indexing of the parent
16-
function SubArray(parent::AbstractArray, indexes::Tuple, dims::Tuple)
20+
function SubArray{T,N}(parent::AbstractArray{T,N}, indexes::NTuple{N}, dims::Tuple)
1721
SubArray(linearindexing(viewindexing(indexes), linearindexing(parent)), parent, indexes, convert(Dims, dims))
1822
end
1923
function SubArray{P, I, N}(::LinearSlow, parent::P, indexes::I, dims::NTuple{N, Int})
@@ -77,14 +81,7 @@ parentindexes(a::AbstractArray) = ntuple(i->1:size(a,i), ndims(a))
7781

7882
## SubArray creation
7983
# Drops singleton dimensions (those indexed with a scalar)
80-
slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) = (@_propagate_inbounds_meta; _slice(A, I...))
81-
slice(A::AbstractVector, i::ViewIndex) = (@_propagate_inbounds_meta; _slice(A, i))
82-
slice(A::AbstractArray, i::ViewIndex) = (@_propagate_inbounds_meta; _slice(reshape(A, length(A)), i))
83-
function slice{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770
84-
@_propagate_inbounds_meta
85-
_slice(reshape(A, Val{N}), I...)
86-
end
87-
function _slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
84+
function slice(A::AbstractArray, I::ViewIndex...)
8885
@_inline_meta
8986
@boundscheck checkbounds(A, I...)
9087
J = to_indexes(I...)
@@ -96,14 +93,7 @@ keep_leading_scalars(T::Tuple{Real, Vararg{Real}}) = T
9693
keep_leading_scalars(T::Tuple{Real, Vararg{Any}}) = (@_inline_meta; (NoSlice(T[1]), keep_leading_scalars(tail(T))...))
9794
keep_leading_scalars(T::Tuple{Any, Vararg{Any}}) = (@_inline_meta; (T[1], keep_leading_scalars(tail(T))...))
9895

99-
sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N}) = (@_propagate_inbounds_meta; _sub(A, I...))
100-
sub(A::AbstractVector, i::ViewIndex) = (@_propagate_inbounds_meta; _sub(A, i))
101-
sub(A::AbstractArray, i::ViewIndex) = (@_propagate_inbounds_meta; _sub(reshape(A, length(A)), i))
102-
function sub{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770
103-
@_propagate_inbounds_meta
104-
_sub(reshape(A, Val{N}), I...)
105-
end
106-
function _sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
96+
function sub(A::AbstractArray, I::Vararg...)
10797
@_inline_meta
10898
@boundscheck checkbounds(A, I...)
10999
J = keep_leading_scalars(to_indexes(I...))

0 commit comments

Comments
 (0)