Skip to content

Commit 713d6fa

Browse files
authored
Merge pull request #78 from Tokazama/master
Add tests and fix bugs for indexing
2 parents 730592e + 1828509 commit 713d6fa

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.13.5"
3+
version = "2.13.6"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/indexing.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ end
131131
end
132132
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {N,Arg<:AbstractArray{Bool,N}}
133133
if length(args) === 1
134-
if s isa IndexLinear
134+
if IndexStyle(A) isa IndexLinear
135135
return (LogicalIndex{Int}(first(args)),)
136136
else
137137
return (LogicalIndex(first(args)),)
@@ -218,8 +218,13 @@ to_indices(A, axs::Tuple{}, args::Tuple{}) = ()
218218

219219

220220
_multi_check_index(axs::Tuple, arg) = _multi_check_index(axs, axes(arg))
221+
_multi_check_index(axs::Tuple, arg::LogicalIndex) = prod(map(length, axs)) == length(arg)
221222
function _multi_check_index(axs::Tuple, arg::AbstractArray{T}) where {T<:CartesianIndex}
222-
return checkindex(Bool, axs, arg)
223+
b = true
224+
for i in arg
225+
b &= Base.checkbounds_indices(Bool, axs, (i,))
226+
end
227+
return b
223228
end
224229
_multi_check_index(::Tuple{}, ::Tuple{}) = true
225230
function _multi_check_index(axs::Tuple, args::Tuple)
@@ -331,6 +336,8 @@ pair of axes and indices calling [`to_axis`](@ref).
331336
@inline function to_axes(A, inds::Tuple)
332337
if ndims(A) === 1
333338
return (to_axis(axes(A, 1), first(inds)),)
339+
elseif is_linear_indexing(A, inds)
340+
return (to_axis(eachindex(IndexLinear(), A), first(inds)),)
334341
else
335342
return to_axes(A, axes(A), inds)
336343
end

test/indexing.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,27 @@ end
5050
@test @inferred(ArrayInterface.to_indices(a, (1, 1, 1))) == (1,1, 1)
5151
@test @inferred ArrayInterface.to_indices(a, ([CartesianIndex(1,1,1), CartesianIndex(1,2,1)],)) == (CartesianIndex{3}[CartesianIndex(1, 1, 1), CartesianIndex(1, 2, 1)],)
5252
@test @inferred ArrayInterface.to_indices(a, ([CartesianIndex(1,1), CartesianIndex(1,2)],1:1)) == (CartesianIndex{2}[CartesianIndex(1, 1), CartesianIndex(1, 2)], 1:1)
53+
@test @inferred(first(ArrayInterface.to_indices(a, (fill(true, 2, 2, 1),)))) isa Base.LogicalIndex
54+
55+
@test_throws BoundsError ArrayInterface.to_indices(a, (fill(true, 2, 2, 2),))
5356
@test_throws ErrorException ArrayInterface.to_indices(ones(2,2,2), (1, 1))
5457
end
5558

59+
@testset "to_axes" begin
60+
A = ones(3, 3)
61+
axis = StaticInt(1):StaticInt(3)
62+
inds = StaticInt(1):StaticInt(2)
63+
multi_inds = [CartesianIndex(1, 1), CartesianIndex(1, 2)]
64+
65+
@test @inferred(ArrayInterface.to_axes(A, (axis, axis), (inds, inds))) === (inds, inds)
66+
# vector indexing
67+
@test @inferred(ArrayInterface.to_axes(ones(3), (axis,), (inds,))) === (inds,)
68+
# linear indexing
69+
@test @inferred(ArrayInterface.to_axes(A, (axis, axis), (inds,))) === (inds,)
70+
# multidim arg
71+
@test @inferred(ArrayInterface.to_axes(A, (axis, axis), (multi_inds,))) === (Base.OneTo(2),)
72+
end
73+
5674
@testset "0-dimensional" begin
5775
x = Array{Int,0}(undef)
5876
ArrayInterface.setindex!(x, 1)

0 commit comments

Comments
 (0)