Skip to content

Treat zero-dimensional views as contiguous SubArrays #29895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ check_parent_index_match(parent, ::NTuple{N, Bool}) where {N} =
throw(BoundsError(T(parent, indices, offset1, stride1), I))

# This computes the linear indexing compatibility for a given tuple of indices
viewindexing() = IndexLinear()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function viewindexing() is never called without an argument in Base, so I assume this line was meant as the fallback case for viewindexing(tail(I)). Changing the argument to an empty tuple provides the correct linear indexing if all indices are ScalarIndex.

viewindexing(I::Tuple{}) = IndexLinear()
# Leading scalar indices simply increase the stride
viewindexing(I::Tuple{ScalarIndex, Vararg{Any}}) = (@_inline_meta; viewindexing(tail(I)))
# Slices may begin a section which may be followed by any number of Slices
Expand Down Expand Up @@ -236,8 +236,10 @@ function getindex(V::FastSubArray, i::Int)
@inbounds r = V.parent[V.offset1 + V.stride1*i]
r
end
# We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange
FastContiguousSubArray{T,N,P,I<:Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}}} = SubArray{T,N,P,I,true}
# We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange,
# or if all the indices are scalars, i.e. the view is for a single value only
FastContiguousSubArray{T,N,P,I<:Union{Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}},
Tuple{Vararg{ScalarIndex}}}} = SubArray{T,N,P,I,true}
function getindex(V::FastContiguousSubArray, i::Int)
@_inline_meta
@boundscheck checkbounds(V, i)
Expand Down Expand Up @@ -283,6 +285,7 @@ stride(V::SubArray, d::Integer) = d <= ndims(V) ? strides(V)[d] : strides(V)[end
compute_stride1(parent::AbstractArray, I::NTuple{N,Any}) where {N} =
(@_inline_meta; compute_stride1(1, fill_to_length(axes(parent), OneTo(1), Val(N)), I))
compute_stride1(s, inds, I::Tuple{}) = s
compute_stride1(s, inds, I::Tuple{Vararg{ScalarIndex}}) = s
compute_stride1(s, inds, I::Tuple{ScalarIndex, Vararg{Any}}) =
(@_inline_meta; compute_stride1(s*unsafe_length(inds[1]), tail(inds), tail(I)))
compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
Expand Down
3 changes: 3 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,6 @@ function _test_27632(A)
end
# check that this doesn't crash
_test_27632(view(ones(Int64, (1, 1, 1)), 1, 1, 1))

# issue #29608 - views of single values can be considered contiguous
@test Base.iscontiguous(view(ones(1), 1))