Skip to content

Commit c2499b1

Browse files
committed
Working sub/slice definitions
1 parent 40ad6be commit c2499b1

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

base/reshapedarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims)
3838
reshape(parent::AbstractArray, len::Integer) = reshape(parent, (Int(len),))
3939
reshape(parent::AbstractArray, dims::Int...) = reshape(parent, dims)
4040

41+
reshape{T,N}(parent::AbstractArray{T,N}, ndims::Type{Val{N}}) = parent
4142
@generated function reshape{T,AN,N}(parent::AbstractArray{T,AN}, ndims::Type{Val{N}})
4243
if AN < N
4344
# Add trailing singleton dimensions

base/subarray.jl

+30-24
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,23 @@ parentindexes(a::AbstractArray) = ntuple(i->1:size(a,i), ndims(a))
7777

7878
## SubArray creation
7979
# 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...)
80+
function slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
81+
@_inline_meta
82+
@boundscheck checkbounds(A, I...)
83+
unsafe_slice(A, I...)
8684
end
87-
function _slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
85+
function slice(A::AbstractArray, i::ViewIndex)
86+
@_inline_meta
87+
@boundscheck checkbounds(A, i)
88+
unsafe_slice(reshape(A, Val{1}), i)
89+
end
90+
function slice{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770
8891
@_inline_meta
8992
@boundscheck checkbounds(A, I...)
93+
unsafe_slice(reshape(A, Val{N}), I...)
94+
end
95+
function unsafe_slice{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
96+
@_inline_meta
9097
J = to_indexes(I...)
9198
SubArray(A, J, index_shape(A, J...))
9299
end
@@ -96,16 +103,23 @@ keep_leading_scalars(T::Tuple{Real, Vararg{Real}}) = T
96103
keep_leading_scalars(T::Tuple{Real, Vararg{Any}}) = (@_inline_meta; (NoSlice(T[1]), keep_leading_scalars(tail(T))...))
97104
keep_leading_scalars(T::Tuple{Any, Vararg{Any}}) = (@_inline_meta; (T[1], keep_leading_scalars(tail(T))...))
98105

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...)
106+
function sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
107+
@_inline_meta
108+
@boundscheck checkbounds(A, I...)
109+
unsafe_sub(A, I...)
105110
end
106-
function _sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
111+
function sub(A::AbstractArray, i::ViewIndex)
112+
@_inline_meta
113+
@boundscheck checkbounds(A, i)
114+
unsafe_sub(reshape(A, Val{1}), i)
115+
end
116+
function sub{N}(A::AbstractArray, I::Vararg{ViewIndex,N}) # TODO: DEPRECATE FOR #14770
107117
@_inline_meta
108118
@boundscheck checkbounds(A, I...)
119+
unsafe_sub(reshape(A, Val{N}), I...)
120+
end
121+
function unsafe_sub{T,N}(A::AbstractArray{T,N}, I::Vararg{ViewIndex,N})
122+
@_inline_meta
109123
J = keep_leading_scalars(to_indexes(I...))
110124
SubArray(A, J, index_shape(A, J...))
111125
end
@@ -218,22 +232,14 @@ function setindex!(V::FastContiguousSubArray, x, i::Real)
218232
end
219233
# Nonscalar setindex! falls back to the defaults
220234

221-
slice{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N}) = _slice(V, I...)
222-
slice{T}(V::SubArray{T,1}, i::ViewIndex) = _slice(V, i)
223-
slice(V::SubArray, i::ViewIndex) = _slice(reshape(V, length(V)), i)
224-
function _slice{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
235+
function unsafe_slice{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
225236
@_inline_meta
226-
@boundscheck checkbounds(V, I...)
227237
idxs = reindex(V, V.indexes, to_indexes(I...))
228238
SubArray(V.parent, idxs, index_shape(V.parent, idxs...))
229239
end
230240

231-
sub{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N}) = _sub(V, I...)
232-
sub{T}(V::SubArray{T,1}, i::ViewIndex) = _sub(V, i)
233-
sub(V::SubArray, i::ViewIndex) = _sub(reshape(V, length(V)), i)
234-
function _sub{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
241+
function unsafe_sub{T,N}(V::SubArray{T,N}, I::Vararg{ViewIndex,N})
235242
@_inline_meta
236-
@boundscheck checkbounds(V, I...)
237243
idxs = reindex(V, V.indexes, keep_leading_scalars(to_indexes(I...)))
238244
SubArray(V.parent, idxs, index_shape(V.parent, idxs...))
239245
end

0 commit comments

Comments
 (0)