Skip to content
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

ArrayInterface integration #25

Merged
merged 7 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ os:
- linux
- osx
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
julia:
- 1.0
- 1
# - nightly
- 1.5
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
- nightly
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
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 <accounts@chrisrackauckas.com>"]
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
3 changes: 3 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Copy link
Member

Choose a reason for hiding this comment

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

this isn't needed here since it's in the project

Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
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