Skip to content

Commit

Permalink
Nested views with static indexing (#657)
Browse files Browse the repository at this point in the history
This allows StaticIndexing to appear at any position when making a view.
  • Loading branch information
mateuszbaran authored and c42f committed Sep 19, 2019
1 parent 98d35ff commit 935dc85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,13 @@ Base.checkindex(B::Type{Bool}, inds::AbstractUnitRange, i::StaticIndexing{T}) wh

# unsafe_view

Base.unsafe_view(A::AbstractArray, i::StaticIndexing{T}) where T = Base.unsafe_view(A, unwrap(i))
# unsafe_view need only deal with vargs of `StaticIndexing`, as wrapped by to_indices.
# i1 is explicitly specified to avoid ambiguities with Base
Base.unsafe_view(A::AbstractArray, i1::StaticIndexing, indices::StaticIndexing...) = Base.unsafe_view(A, unwrap(i1), map(unwrap, indices)...)

# Views of views need a new method for Base.SubArray because storing indices
# wrapped in StaticIndexing in field indices of SubArray causes all sorts of problems.
# Additionally, in some cases the SubArray constructor may be called directly
# instead of unsafe_view so we need this method too (Base._maybe_reindex
# is a good example)
Base.SubArray(A::AbstractArray, indices::NTuple{<:Any,StaticIndexing}) = Base.SubArray(A, map(unwrap, indices))
13 changes: 13 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,18 @@ using StaticArrays, Test
a = collect(11:20)
@test view(a, SVector(1,2,3)) == [11,12,13]
@test_throws BoundsError view(a, SVector(1,11,3))
B = rand(Int,3,4,5,6)
Bv = view(B, 1, (@SVector [2, 1]), [2, 3], (@SVector [4]))
@test Bv == B[1, [2,1], 2:3, [4]]
@test axes(Bv, 1) === SOneTo(2)
@test axes(Bv, 3) === SOneTo(1)
Bvv = view(Bv, (@SVector [1, 2]), 2, 1)
@test axes(Bvv) === (SOneTo(2),)
@test Bvv[1] == B[1, 2, 3, 4]
Bvv[1] = 100
@test Bvv[1] == 100
@test B[1,2,3,4] == 100
@test eltype(Bvv) == Int
@test Bvv[:] == [B[1,2,3,4], B[1,1,3,4]]
end
end

0 comments on commit 935dc85

Please sign in to comment.