Skip to content

Commit 69306cf

Browse files
Merge pull request #25 from Tokazama/master
ArrayInterface integration
2 parents 9ceec71 + fbaf3a8 commit 69306cf

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
language: julia
33
os:
44
- linux
5-
- osx
5+
# - osx
66
julia:
7-
- 1.0
87
- 1
9-
# - nightly
8+
# - nightly
109
matrix:
1110
allow_failures:
1211
- julia: nightly

Project.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
name = "EllipsisNotation"
22
uuid = "da5c29d0-fa7d-589e-88eb-ea29b0a81949"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "1.0.0"
4+
version = "1.1.0"
5+
6+
[deps]
7+
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
58

69
[compat]
7-
julia = "1"
10+
julia = "1.5"
811

912
[extras]
1013
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

src/EllipsisNotation.jl

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ true
4242
"""
4343
module EllipsisNotation
4444

45+
using ArrayInterface
46+
using ArrayInterface: indices
47+
48+
4549
import Base: to_indices, tail
4650

4751
struct Ellipsis end
@@ -53,9 +57,24 @@ const .. = Ellipsis()
5357
to_indices(A, inds, (colons..., tail(I)...))
5458
end
5559

60+
Base.@propagate_inbounds function ArrayInterface.to_indices(A, inds::Tuple{Vararg{Any,M}}, I::Tuple{Ellipsis,Vararg{Any, N}}) where {M,N}
61+
return ArrayInterface.to_indices(A, inds, (ntuple(i -> indices(inds[i]), Val(M-N))..., tail(I)...))
62+
end
63+
ArrayInterface.to_indices(A, inds::Tuple{}, I::Tuple{Ellipsis}) = ()
64+
ArrayInterface.is_linear_indexing(A, args::Tuple{Arg}) where {Arg<:Ellipsis} = false
65+
66+
67+
#=
68+
ArrayInterface.can_flatten(::Type{A}, ::Type{T}) where {A,T<:Ellipsis} = true
69+
@inline function ArrayInterface.flatten_args(A, args::Tuple{Arg,Vararg{Any,N}}) where {Arg<:Ellipsis,N}
70+
return (ntuple(i -> indices(axes(A, i)), Val(ndims(A) - N))..., flatten_args(A, tail(args))...)
71+
end
72+
=#
73+
5674
# avoid copying if indexing with .. alone, see
5775
# https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/issues/214
5876
@inline Base.getindex(A::AbstractArray, ::Ellipsis) = A
77+
@inline ArrayInterface.getindex(A, ::Ellipsis) = A
5978

6079
export ..
6180

test/Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/basic.jl

+39
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,42 @@ C[..] = B[..]
4444
@test B == C
4545
C[1,1] += 1
4646
@test B != C
47+
48+
@testset "ArrayInterface" begin
49+
A = Array{Int}(undef,2,4,2)
50+
ArrayInterface.setindex!(A, [2 1 4 5; 2 2 3 6], .., 1)
51+
ArrayInterface.setindex!(A, [3 2 6 5; 3 2 6 6], .., 2)
52+
53+
@test ArrayInterface.getindex(A, :, :, 1) == [2 1 4 5; 2 2 3 6]
54+
@test ArrayInterface.getindex(A, :, :, 2) == [3 2 6 5; 3 2 6 6]
55+
56+
57+
@test ArrayInterface.getindex(A, :, .., 1) == [2 1 4 5; 2 2 3 6]
58+
@test ArrayInterface.getindex(A, :, .., 2) == [3 2 6 5; 3 2 6 6]
59+
60+
ArrayInterface.setindex!(A, reshape([3 4; 5 6; 4 5; 6 7],1,4,2), 1, ..)
61+
62+
B = [3 4
63+
5 6
64+
4 5
65+
6 7]
66+
67+
@test B == reshape(ArrayInterface.getindex(A, 1, ..),4,2) == reshape(view(A, 1,..), 4, 2)
68+
69+
@test A[:,1,2] == ArrayInterface.getindex(A,..,1,2)
70+
71+
# [..]
72+
C = zero(B)
73+
74+
C[:] = ArrayInterface.getindex(B, ..)
75+
@test B == C
76+
C[1,1] += 1
77+
@test B != C
78+
79+
ArrayInterface.setindex!(C, ArrayInterface.getindex(B, ..), ..)
80+
@test B == C
81+
C[1,1] += 1
82+
@test B != C
83+
84+
end
85+

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using ArrayInterface
12
using EllipsisNotation
23
using Test
34

0 commit comments

Comments
 (0)