Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/ArrayInterfaceCore/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ArrayInterfaceCore"
uuid = "30b0a656-2188-435a-8636-2ec0e6a096e2"
version = "0.1.5"
version = "0.1.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
17 changes: 9 additions & 8 deletions lib/ArrayInterfaceCore/src/ArrayInterfaceCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ A scalar `setindex!` which is always allowed.
"""
allowed_setindex!(x, v, i...) = Base.setindex!(x, v, i...)


"""
ArrayIndex{N}

Expand Down Expand Up @@ -514,14 +513,14 @@ julia> ArrayInterface.known_step(typeof(1:4))
"""
known_step(x) = known_step(typeof(x))
known_step(::Type{T}) where {T} = parent_type(T) <: T ? nothing : known_step(parent_type(T))
known_step(::Type{<:AbstractUnitRange}) = 1
known_step(@nospecialize T::Type{<:AbstractUnitRange}) = 1

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

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

end # module
7 changes: 7 additions & 0 deletions lib/ArrayInterfaceCore/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,10 @@ end
@test isone(@inferred(ArrayInterfaceCore.known_step(typeof(view(1:4, 1:2)))))
end

@testset "ndims_index" begin
@test @inferred(ArrayInterfaceCore.ndims_index(CartesianIndices(()))) == 1
@test @inferred(ArrayInterfaceCore.ndims_index(trues(2, 2))) == 2
@test @inferred(ArrayInterfaceCore.ndims_index(CartesianIndex(2,2))) == 2
@test @inferred(ArrayInterfaceCore.ndims_index(1)) == 1
end