Skip to content

Commit

Permalink
Merge pull request #25 from Tokazama/master
Browse files Browse the repository at this point in the history
ArrayInterface integration
  • Loading branch information
ChrisRackauckas authored Nov 9, 2020
2 parents 9ceec71 + fbaf3a8 commit 69306cf
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
language: julia
os:
- linux
- osx
# - osx
julia:
- 1.0
- 1
# - nightly
# - nightly
matrix:
allow_failures:
- julia: nightly
Expand Down
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
name = "EllipsisNotation"
uuid = "da5c29d0-fa7d-589e-88eb-ea29b0a81949"
authors = ["Chris Rackauckas <[email protected]>"]
version = "1.0.0"
version = "1.1.0"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

[compat]
julia = "1"
julia = "1.5"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
19 changes: 19 additions & 0 deletions src/EllipsisNotation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ true
"""
module EllipsisNotation

using ArrayInterface
using ArrayInterface: indices


import Base: to_indices, tail

struct Ellipsis end
Expand All @@ -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 ..

Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
39 changes: 39 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ArrayInterface
using EllipsisNotation
using Test

Expand Down

0 comments on commit 69306cf

Please sign in to comment.