Skip to content

Commit 2d3b1e2

Browse files
authored
fix ndims_index(::CartesianIndices{0}) + ndims_index tests (#283)
When indexing empty `CartesianIndices` uniquely map to trailiing dimensions that may be dropped. Therefore, we need to preserve them as an indicator of which dimension to drop.
1 parent c9a5ee6 commit 2d3b1e2

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

lib/ArrayInterfaceCore/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterfaceCore"
22
uuid = "30b0a656-2188-435a-8636-2ec0e6a096e2"
3-
version = "0.1.5"
3+
version = "0.1.6"
44

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

lib/ArrayInterfaceCore/src/ArrayInterfaceCore.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ A scalar `setindex!` which is always allowed.
389389
"""
390390
allowed_setindex!(x, v, i...) = Base.setindex!(x, v, i...)
391391

392-
393392
"""
394393
ArrayIndex{N}
395394
@@ -514,14 +513,14 @@ julia> ArrayInterface.known_step(typeof(1:4))
514513
"""
515514
known_step(x) = known_step(typeof(x))
516515
known_step(::Type{T}) where {T} = parent_type(T) <: T ? nothing : known_step(parent_type(T))
517-
known_step(::Type{<:AbstractUnitRange}) = 1
516+
known_step(@nospecialize T::Type{<:AbstractUnitRange}) = 1
518517

519518
"""
520519
is_splat_index(::Type{T}) -> Bool
521520
Returns `static(true)` if `T` is a type that splats across multiple dimensions.
522521
"""
523-
is_splat_index(@nospecialize(x)) = is_splat_index(typeof(x))
524522
is_splat_index(T::Type) = false
523+
is_splat_index(@nospecialize(x)) = is_splat_index(typeof(x))
525524

526525
"""
527526
ndims_index(::Type{I}) -> Int
@@ -530,11 +529,13 @@ Returns the number of dimension that an instance of `I` maps to when indexing. F
530529
`CartesianIndex{3}` maps to 3 dimensions. If this method is not explicitly defined, then `1`
531530
is returned.
532531
"""
533-
ndims_index(@nospecialize(i)) = ndims_index(typeof(i))
534-
ndims_index(::Type{I}) where {I} = 1
535532
ndims_index(::Type{<:Base.AbstractCartesianIndex{N}}) where {N} = N
536-
ndims_index(::Type{<:AbstractArray{T}}) where {T} = ndims_index(T)
537-
ndims_index(::Type{<:AbstractArray{Bool,N}}) where {N} = N
538-
ndims_index(::Type{<:Base.LogicalIndex{<:Any,<:AbstractArray{Bool,N}}}) where {N} = N
533+
# preserve CartesianIndices{0} as they consume a dimension.
534+
ndims_index(::Type{CartesianIndices{0,Tuple{}}}) = 1
535+
ndims_index(@nospecialize T::Type{<:AbstractArray{Bool}}) = ndims(T)
536+
ndims_index(@nospecialize T::Type{<:AbstractArray}) = ndims_index(eltype(T))
537+
ndims_index(@nospecialize T::Type{<:Base.LogicalIndex}) = ndims(fieldtype(T, :mask))
538+
ndims_index(T::DataType) = 1
539+
ndims_index(@nospecialize(i)) = ndims_index(typeof(i))
539540

540541
end # module

lib/ArrayInterfaceCore/test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,10 @@ end
261261
@test isone(@inferred(ArrayInterfaceCore.known_step(typeof(view(1:4, 1:2)))))
262262
end
263263

264+
@testset "ndims_index" begin
265+
@test @inferred(ArrayInterfaceCore.ndims_index(CartesianIndices(()))) == 1
266+
@test @inferred(ArrayInterfaceCore.ndims_index(trues(2, 2))) == 2
267+
@test @inferred(ArrayInterfaceCore.ndims_index(CartesianIndex(2,2))) == 2
268+
@test @inferred(ArrayInterfaceCore.ndims_index(1)) == 1
269+
end
270+

0 commit comments

Comments
 (0)