Skip to content

Commit

Permalink
add maybeview method for views macro with keyword syntax (#509)
Browse files Browse the repository at this point in the history
* add maybeview method for views macro with keyword syntax
  • Loading branch information
rafaqz authored Aug 5, 2023
1 parent a8bad3f commit 24e1b11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/array/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ end
setindex!(A, x, dims2indices(A, (i, I...))...)
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x, i1::StandardIndices, I::StandardIndices...) =
setindex!(parent(A), x, i1, I...)

# For @views macro to work with keywords
Base.maybeview(A::AbstractDimArray, args...; kw...) = view(A, args...; kw...)
3 changes: 3 additions & 0 deletions src/stack/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ end
end

@noinline _keysmismatch(K1, K2) = throw(ArgumentError("NamedTuple keys $K2 do not mach stack keys $K1"))

# For @views macro to work with keywords
Base.maybeview(A::AbstractDimStack, args...; kw...) = view(A, args...; kw...)
11 changes: 11 additions & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ end
@test length(refdims(a)) == 2
end
end

@testset "@views macro and maybeview work even with kw syntax" begin
v1 = @views da[Y(1:2), X(1)]
v2 = @views da[Y=1:2, X=1]
@test v1 == v2 == [1, 2]
end
end

@testset "setindex!" begin
Expand Down Expand Up @@ -395,6 +401,11 @@ end
@test view(s, X(1), Y(1))[:two] == view(da2, X(1), Y(1))
@test view(s, X(2), Y(3))[:three] == view(da3, X(2), Y(3))
end
@testset "@views macro and maybeview work even with kw syntax" begin
sv1 = @views s[X(1:2), Y(3:3)]
sv2 = @views s[X=1:2, Y=3:3]
@test parent(sv1) == parent(sv2) == (one=[3.0 6.0]', two=[6.0f0 12.0f0]', three=[9 18]')
end
end

@testset "setindex!" begin
Expand Down

0 comments on commit 24e1b11

Please sign in to comment.