From 0f132e536b67ab602c11a363c0a43e03045173c5 Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Fri, 6 Nov 2020 04:11:26 -0500 Subject: [PATCH 1/7] ArrayInterface integration --- Project.toml | 3 +++ src/EllipsisNotation.jl | 19 +++++++++++++++++++ test/Project.toml | 3 +++ test/basic.jl | 39 +++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 5 files changed, 65 insertions(+) create mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index 1a8126f..9b23e52 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,9 @@ uuid = "da5c29d0-fa7d-589e-88eb-ea29b0a81949" authors = ["Chris Rackauckas "] version = "1.0.0" +[deps] +ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" + [compat] julia = "1" diff --git a/src/EllipsisNotation.jl b/src/EllipsisNotation.jl index f56c508..edd0dde 100644 --- a/src/EllipsisNotation.jl +++ b/src/EllipsisNotation.jl @@ -42,6 +42,10 @@ true """ module EllipsisNotation +using ArrayInterface +using ArrayInterface: indices + + import Base: to_indices, tail struct Ellipsis end @@ -53,9 +57,24 @@ const .. = Ellipsis() to_indices(A, inds, (colons..., tail(I)...)) end +Base.@propagate_inbounds function ArrayInterface.to_indices(A, inds::Tuple{Vararg{Any,M}}, I::Tuple{Ellipsis,Vararg{Any, N}}) where {M,N} + return ArrayInterface.to_indices(A, inds, (ntuple(i -> indices(inds[i]), Val(M-N))..., tail(I)...)) +end +ArrayInterface.to_indices(A, inds::Tuple{}, I::Tuple{Ellipsis}) = () +ArrayInterface.is_linear_indexing(A, args::Tuple{Arg}) where {Arg<:Ellipsis} = false + + +#= +ArrayInterface.can_flatten(::Type{A}, ::Type{T}) where {A,T<:Ellipsis} = true +@inline function ArrayInterface.flatten_args(A, args::Tuple{Arg,Vararg{Any,N}}) where {Arg<:Ellipsis,N} + return (ntuple(i -> indices(axes(A, i)), Val(ndims(A) - N))..., flatten_args(A, tail(args))...) +end +=# + # avoid copying if indexing with .. alone, see # https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/issues/214 @inline Base.getindex(A::AbstractArray, ::Ellipsis) = A +@inline ArrayInterface.getindex(A, ::Ellipsis) = A export .. diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..700a796 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,3 @@ +[deps] +ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/basic.jl b/test/basic.jl index 71f39c8..05b0dcd 100644 --- a/test/basic.jl +++ b/test/basic.jl @@ -44,3 +44,42 @@ C[..] = B[..] @test B == C C[1,1] += 1 @test B != C + +@testset "ArrayInterface" begin + A = Array{Int}(undef,2,4,2) + ArrayInterface.setindex!(A, [2 1 4 5; 2 2 3 6], .., 1) + ArrayInterface.setindex!(A, [3 2 6 5; 3 2 6 6], .., 2) + + @test ArrayInterface.getindex(A, :, :, 1) == [2 1 4 5; 2 2 3 6] + @test ArrayInterface.getindex(A, :, :, 2) == [3 2 6 5; 3 2 6 6] + + + @test ArrayInterface.getindex(A, :, .., 1) == [2 1 4 5; 2 2 3 6] + @test ArrayInterface.getindex(A, :, .., 2) == [3 2 6 5; 3 2 6 6] + + ArrayInterface.setindex!(A, reshape([3 4; 5 6; 4 5; 6 7],1,4,2), 1, ..) + + B = [3 4 + 5 6 + 4 5 + 6 7] + + @test B == reshape(ArrayInterface.getindex(A, 1, ..),4,2) == reshape(view(A, 1,..), 4, 2) + + @test A[:,1,2] == ArrayInterface.getindex(A,..,1,2) + + # [..] + C = zero(B) + + C[:] = ArrayInterface.getindex(B, ..) + @test B == C + C[1,1] += 1 + @test B != C + + ArrayInterface.setindex!(C, ArrayInterface.getindex(B, ..), ..) + @test B == C + C[1,1] += 1 + @test B != C + +end + diff --git a/test/runtests.jl b/test/runtests.jl index 885a2ce..5867729 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,3 +1,4 @@ +using ArrayInterface using EllipsisNotation using Test From c30e704c921f4692e486268e0ab3a184d895d0a9 Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Sun, 8 Nov 2020 14:39:49 -0500 Subject: [PATCH 2/7] Up julia version --- .travis.yml | 5 ++--- Project.toml | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 65c2c89..f0f84d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,8 @@ os: - linux - osx julia: - - 1.0 - - 1 - # - nightly + - 1.5 + - nightly matrix: allow_failures: - julia: nightly diff --git a/Project.toml b/Project.toml index 9b23e52..1ee3348 100644 --- a/Project.toml +++ b/Project.toml @@ -7,7 +7,7 @@ version = "1.0.0" ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" [compat] -julia = "1" +julia = "1.5" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" From 4a8ad21d428fea9b1aa698fa391c5e9d1a2e0b3a Mon Sep 17 00:00:00 2001 From: "Zachary P. Christensen" Date: Sun, 8 Nov 2020 14:44:21 -0500 Subject: [PATCH 3/7] Minor version update --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1ee3348..554e528 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "EllipsisNotation" uuid = "da5c29d0-fa7d-589e-88eb-ea29b0a81949" authors = ["Chris Rackauckas "] -version = "1.0.0" +version = "1.1.0" [deps] ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" From 3ef7392fa47175eaa605f417d29b075bf4220a16 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 8 Nov 2020 19:34:52 -0500 Subject: [PATCH 4/7] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f0f84d1..fa23a38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: - osx julia: - 1.5 - - nightly +# - nightly matrix: allow_failures: - julia: nightly From f5d46d18ca58b440912364eec80a52a41231afd6 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 8 Nov 2020 19:34:57 -0500 Subject: [PATCH 5/7] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fa23a38..64bec43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ os: - linux - osx julia: - - 1.5 + - 1 # - nightly matrix: allow_failures: From f9151023d42439a03bbc33581760207b58804e0e Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 8 Nov 2020 19:35:27 -0500 Subject: [PATCH 6/7] Update test/Project.toml --- test/Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Project.toml b/test/Project.toml index 700a796..0c36332 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,2 @@ [deps] -ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" From fbaf3a84f2a1966a66806bbb46d94765a436b31a Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 8 Nov 2020 19:38:31 -0500 Subject: [PATCH 7/7] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 64bec43..8f5e159 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: julia os: - linux - - osx +# - osx julia: - 1 # - nightly