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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ jobs:
- uses: julia-actions/setup-julia@latest
with:
version: '1.2'
- run: julia --color=yes -e 'using Pkg; VERSION >= v"1.5-" && !isdir(joinpath(DEPOT_PATH[1], "registries", "General")) && Pkg.Registry.add("General")'
shell: bash
env:
JULIA_PKG_SERVER: ""
- run: julia --project=docs -e '
using Pkg;
Pkg.develop(PackageSpec(; path=pwd()));
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ArrayInterface"
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
version = "4.0.4"
version = "5.0.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand All @@ -14,5 +14,5 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
Compat = "3.37"
IfElse = "0.1"
Requires = "0.5, 1.0"
Static = "0.5"
Static = "0.6"
julia = "1.2"
14 changes: 7 additions & 7 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ julia> ArrayInterface.size(a)
(static(1), 3)

julia> ArrayInterface.known_size(typeof(a))
(1, missing)
(1, nothing)

```

Expand All @@ -62,8 +62,8 @@ Methods should avoid forcing conversion to static sizes when dynamic sizes could
Fore example, `fxn(x) = _fxn(Static.static(ArrayInterface.size(x)), x)` would result in dynamic dispatch if `x` is an instance of `Matrix`.
Additionally, `ArrayInterface.size` should only be used outside of generated functions to avoid possible world age issues.

Generally, `ArrayInterface.size` uses the return of `known_size` to form a static value for those dimensions with known length and only queries dimensions corresponding to `missing`.
For example, the previous example had a known size of `(1, missing)`.
Generally, `ArrayInterface.size` uses the return of `known_size` to form a static value for those dimensions with known length and only queries dimensions corresponding to `nothing`.
For example, the previous example had a known size of `(1, nothing)`.
Therefore, `ArrayInterface.size` would have compile time information about the first dimension returned as `static(1)` and would only look up the size of the second dimension at run time.
This means the above example `ArrayInterface.size(a)` would lower to code similar to this at compile time: `Static.StaticInt(1), Base.arraysize(x, 1)`.
Generic support for `ArrayInterface.known_size` relies on calling `known_length` for each type returned from `axes_types`.
Expand Down Expand Up @@ -108,13 +108,13 @@ ArrayInterface.dimnames(::StaticDimnames{dnames}) where {dnames} = static(dnames
struct DynamicDimnames{N}
dimnames::NTuple{N,Symbol}
end
ArrayInterface.known_dimnames(::Type{DynamicDimnames{N}}) where {N} = ntuple(_-> missing, Val(N))
ArrayInterface.known_dimnames(::Type{DynamicDimnames{N}}) where {N} = ntuple(_-> nothing, Val(N))
ArrayInterface.dimnames(x::DynamicDimnames) = getfield(x, :dimnames)

```

Notice that `DynamicDimnames` returns `missing` instead of a symbol for each dimension.
This indicates dimension names are present for `DynamicDimnames` but that information is missing at compile time.
Notice that `DynamicDimnames` returns `nothing` instead of a symbol for each dimension.
This indicates dimension names are present for `DynamicDimnames` but that information is nothing at compile time.

Dimension names should be appropriately propagated between nested arrays using `ArrayInterface.to_parent_dims`.
This allows types such as `SubArray` and `PermutedDimsArray` to work with named dimensions.
Expand Down Expand Up @@ -166,7 +166,7 @@ using ArrayInterface: axes_types, parent_type, to_dims
for dim in 1:ndims(A)
# offset relative to parent array
O = relative_known_offsets(A, dim)
if O === missing # offset is not known at compile time and is an `Int`
if O === nothing # offset is not known at compile time and is an `Int`
push!(out.args, :(IdOffsetRange{Int, axes_types($P, $(static(dim)))}))
else # offset is known, therefore it is a `StaticInt`
push!(out.args, :(IdOffsetRange{StaticInt{$O}, axes_types($P, $(static(dim))}))
Expand Down
36 changes: 27 additions & 9 deletions src/ArrayInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ buffer(x::SparseMatrixCSC) = getfield(x, :nzval)
buffer(x::SparseVector) = getfield(x, :nzval)

"""
known_length(::Type{T}) -> Union{Int,Missing}
known_length(::Type{T}) -> Union{Int,Nothing}

If `length` of an instance of type `T` is known at compile time, return it.
Otherwise, return `missing`.
Otherwise, return `nothing`.
"""
known_length(x) = known_length(typeof(x))
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
Expand All @@ -93,10 +93,28 @@ known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
known_length(::Type{<:Number}) = 1
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
_maybe_known_length(::Base.HasShape, ::Type{T}) where {T} = prod(known_size(T))
_maybe_known_length(::Base.IteratorSize, ::Type) = missing

@generated function _prod_or_nothing(x::Tuple)
p = 1
for i in eachindex(x.parameters)
x.parameters[i] === Nothing && return nothing
p *= x.parameters[i].parameters[1]
end
StaticInt(p)
end

function _maybe_known_length(::Base.HasShape, ::Type{T}) where {T}
t = map(_static_or_nothing, known_size(T))
_int_or_nothing(_prod_or_nothing(t))
end

_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
_static_or_nothing(::Nothing) = nothing
@inline _static_or_nothing(x::Int) = StaticInt{x}()
_int_or_nothing(::StaticInt{N}) where {N} = N
_int_or_nothing(::Nothing) = nothing
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
known_length(I) * known_length(eltype(I))
_int_or_nothing(_prod_or_nothing((_static_or_nothing(known_length(I)),_static_or_nothing(known_length(eltype(I))))))
end

"""
Expand Down Expand Up @@ -415,10 +433,10 @@ Indicates the most efficient way to access elements from the collection in low-l
For `GPUArrays`, will return `ArrayInterface.GPU()`.
For `AbstractArray` supporting a `pointer` method, returns `ArrayInterface.CPUPointer()`.
For other `AbstractArray`s and `Tuple`s, returns `ArrayInterface.CPUIndex()`.
Otherwise, returns `missing`.
Otherwise, returns `nothing`.
"""
device(A) = device(typeof(A))
device(::Type) = missing
device(::Type) = nothing
device(::Type{<:Tuple}) = CPUTuple()
device(::Type{T}) where {T<:Array} = CPUPointer()
device(::Type{T}) where {T<:AbstractArray} = _device(has_parent(T), T)
Expand Down Expand Up @@ -597,7 +615,7 @@ end

function Base.length(A::AbstractArray2)
len = known_length(A)
if len === missing
if len === nothing
return Int(prod(size(A)))
else
return Int(len)
Expand Down Expand Up @@ -1136,7 +1154,7 @@ function __init__()
Static.eachop_tuple(_offset_axis_type, Static.nstatic(Val(ndims(T))), ArrayInterface.parent_type(T))
end
function ArrayInterface.known_offsets(::Type{A}) where {A<:OffsetArrays.OffsetArray}
ntuple(identity -> missing, Val(ndims(A)))
ntuple(identity -> nothing, Val(ndims(A)))
end
function ArrayInterface.offsets(A::OffsetArrays.OffsetArray)
map(+, ArrayInterface.offsets(parent(A)), relative_offsets(A))
Expand Down
18 changes: 9 additions & 9 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function axes_types(::Type{T}) where {T<:PermutedDimsArray}
eachop_tuple(field_type, to_parent_dims(T), axes_types(parent_type(T)))
end
function axes_types(::Type{T}) where {T<:AbstractRange}
if known_length(T) === missing
if known_length(T) === nothing
return Tuple{OneTo{Int}}
else
return Tuple{SOneTo{known_length(T)}}
Expand All @@ -62,7 +62,7 @@ end
function _non_reshaped_axis_type(::Type{A}, d::StaticInt{D}) where {A,D}
paxis = axes_types(parent_type(A), d)
if D === 1
if known_length(paxis) === missing
if known_length(paxis) === nothing
return paxis
else
return SOneTo{div(known_length(paxis) * sizeof(eltype(parent_type(A))), sizeof(eltype(A)))}
Expand Down Expand Up @@ -191,7 +191,7 @@ end
# For now we just make sure the linear elements are accurate.
parent_type(::Type{LazyAxis{:,P}}) where {P<:Array} = OneTo{Int}
@inline function parent_type(::Type{LazyAxis{:,P}}) where {P}
if known_length(P) === missing
if known_length(P) === nothing
return OptionallyStaticUnitRange{StaticInt{1},Int}
else
return SOneTo{known_length(P)}
Expand All @@ -208,14 +208,14 @@ known_first(::Type{LazyAxis{N,P}}) where {N,P} = known_offsets(P, static(N))
known_first(::Type{LazyAxis{:,P}}) where {P} = 1
Base.firstindex(x::LazyAxis) = first(x)
@inline function Base.first(x::LazyAxis{N})::Int where {N}
if known_first(x) === missing
if known_first(x) === nothing
return Int(offsets(parent(x), static(N)))
else
return Int(known_first(x))
end
end
@inline function Base.first(x::LazyAxis{:})::Int
if known_first(x) === missing
if known_first(x) === nothing
return first(parent(x))
else
return known_first(x)
Expand All @@ -225,20 +225,20 @@ known_last(::Type{LazyAxis{N,P}}) where {N,P} = known_last(axes_types(P, static(
known_last(::Type{LazyAxis{:,P}}) where {P} = known_length(P)
Base.lastindex(x::LazyAxis) = last(x)
Base.last(x::LazyAxis) = _last(known_last(x), x)
_last(::Missing, x) = last(parent(x))
_last(::Nothing, x) = last(parent(x))
_last(N::Int, x) = N

known_length(::Type{LazyAxis{N,P}}) where {N,P} = known_size(P, static(N))
known_length(::Type{LazyAxis{:,P}}) where {P} = known_length(P)
@inline function Base.length(x::LazyAxis{N})::Int where {N}
if known_length(x) === missing
if known_length(x) === nothing
return size(getfield(x, :parent), static(N))
else
return known_length(x)
end
end
@inline function Base.length(x::LazyAxis{:})::Int
if known_length(x) === missing
if known_length(x) === nothing
return length(parent(x))
else
return known_length(x)
Expand All @@ -254,7 +254,7 @@ Base.axes1(x::Slice{<:LazyAxis}) = indices(parent(x.indices))
Base.to_shape(x::LazyAxis) = length(x)

@inline function Base.checkindex(::Type{Bool}, x::LazyAxis, i::Integer)
if known_first(x) === missing || known_last(x) === missing
if known_first(x) === nothing || known_last(x) === nothing
return checkindex(Bool, parent(x), i)
else # everything is static so we don't have to retrieve the axis
return (!(known_first(x) > i) || !(known_last(x) < i))
Expand Down
13 changes: 8 additions & 5 deletions src/dimensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ _is_named(x::NTuple{N,Symbol}) where {N} = x !== _nunderscore(Val(N))
_is_named(::Any) = true

"""
known_dimnames(::Type{T}) -> Tuple{Vararg{Union{Symbol,Missing}}}
known_dimnames(::Type{T}, dim::Union{Int,StaticInt}) -> Union{Symbol,Missing}
known_dimnames(::Type{T}) -> Tuple{Vararg{Union{Symbol,Nothing}}}
known_dimnames(::Type{T}, dim::Union{Int,StaticInt}) -> Union{Symbol,Nothing}

Return the names of the dimensions for `x`. `:_` is used to indicate a dimension does not
have a name.
Expand All @@ -176,7 +176,8 @@ function _known_dimnames(::Type{C}, ::Type{P}) where {C,P}
eachop(_inbounds_known_dimname, to_parent_dims(C), known_dimnames(P))
end
@inline function _known_dimname(x::Tuple{Vararg{Any,N}}, dim::CanonicalInt) where {N}
@boundscheck (dim > N || dim < 1) && return :_
# we cannot have `@boundscheck`, else this will depend on bounds checking being enabled
(dim > N || dim < 1) && return :_
return @inbounds(x[dim])
end
@inline _inbounds_known_dimname(x, dim) = @inbounds(_known_dimname(x, dim))
Expand All @@ -195,7 +196,9 @@ have a name.
end
_dimnames(::False, x) = ntuple(_->static(:_), Val(ndims(x)))
@inline function _dimname(x::Tuple{Vararg{Any,N}}, dim::CanonicalInt) where {N}
@boundscheck (dim > N || dim < 1) && return static(:_)
# we cannot have `@boundscheck`, else this will depend on bounds checking being enabled
# for calls such as `dimnames(view(x, :, 1, :))`
(dim > N || dim < 1) && return static(:_)
return @inbounds(x[dim])
end
@inline _inbounds_dimname(x, dim) = @inbounds(_dimname(x, dim))
Expand Down Expand Up @@ -229,7 +232,7 @@ An error is thrown if any keywords are used which do not occur in `nda`'s names.

1. parse into static dimnension names and key words.
2. find each dimnames in key words
3. if missing is found use Colon()
3. if nothing is found use Colon()
4. if (ndims - ncolon) === nkwargs then all were found, else error
=#
@generated function find_all_dimnames(x::Tuple{Vararg{Any,ND}}, nd::Tuple{Vararg{Any,NI}}, inds::Tuple, default) where {ND,NI}
Expand Down
2 changes: 1 addition & 1 deletion src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ previously executed `to_index(old_axis, arg) -> index`. `to_axis` assumes that
"""
@inline function to_axis(axis, inds)
if !can_change_size(axis) &&
(known_length(inds) !== missing && known_length(axis) === known_length(inds))
(known_length(inds) !== nothing && known_length(axis) === known_length(inds))
return axis
else
return to_axis(IndexStyle(axis), axis, inds)
Expand Down
Loading