Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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 = "3.2.1"
version = "4"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand All @@ -14,7 +14,7 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
Compat = "3.37"
IfElse = "0.1"
Requires = "0.5, 1.0"
Static = "0.4"
Static = "0.5"
julia = "1.2"

[extras]
Expand Down
8 changes: 4 additions & 4 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, nothing)
(1, missing)

```

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 `nothing`.
For example, the previous example had a known size of `(1, nothing)`.
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)`.
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 @@ -155,7 +155,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 === nothing # offset is not known at compile time and is an `Int`
if O === missing # 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
18 changes: 8 additions & 10 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,Nothing}
known_length(::Type{T}) -> Union{Int,Missing}

If `length` of an instance of type `T` is known at compile time, return it.
Otherwise, return `nothing`.
Otherwise, return `missing`.
"""
known_length(x) = known_length(typeof(x))
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
Expand All @@ -95,13 +95,11 @@ known_length(::Type{<:Number}) = 1
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
function known_length(::Type{T}) where {T}
if parent_type(T) <: T
return nothing
return missing
else
return _known_length(known_size(T))
return prod(known_size(T))
end
end
_known_length(x::Tuple{Vararg{Union{Nothing,Int}}}) = nothing
_known_length(x::Tuple{Vararg{Int}}) = prod(x)

"""
can_change_size(::Type{T}) -> Bool
Expand Down Expand Up @@ -419,10 +417,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 `nothing`.
Otherwise, returns `missing`.
"""
device(A) = device(typeof(A))
device(::Type) = nothing
device(::Type) = missing
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 @@ -605,7 +603,7 @@ end

function Base.length(A::AbstractArray2)
len = known_length(A)
if len === nothing
if len === missing
return Int(prod(size(A)))
else
return Int(len)
Expand Down Expand Up @@ -940,7 +938,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 -> nothing, Val(ndims(A)))
ntuple(identity -> missing, Val(ndims(A)))
end
function ArrayInterface.offsets(A::OffsetArrays.OffsetArray)
map(+, ArrayInterface.offsets(parent(A)), relative_offsets(A))
Expand Down
1 change: 1 addition & 0 deletions src/array_index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,4 @@ end
end
return Expr(:block, Expr(:meta, :inline), out)
end

18 changes: 9 additions & 9 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function axes_types(::Type{T}) where {T<:PermutedDimsArray}
eachop_tuple(_get_tuple, to_parent_dims(T), axes_types(parent_type(T)))
end
function axes_types(::Type{T}) where {T<:AbstractRange}
if known_length(T) === nothing
if known_length(T) === missing
return Tuple{OneTo{Int}}
else
return Tuple{SOneTo{known_length(T)}}
Expand All @@ -63,7 +63,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) === nothing
if known_length(paxis) === missing
return paxis
else
return SOneTo{div(known_length(paxis) * sizeof(eltype(parent_type(A))), sizeof(eltype(A)))}
Expand Down Expand Up @@ -192,7 +192,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) === nothing
if known_length(P) === missing
return OptionallyStaticUnitRange{StaticInt{1},Int}
else
return SOneTo{known_length(P)}
Expand All @@ -209,14 +209,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) === nothing
if known_first(x) === missing
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) === nothing
if known_first(x) === missing
return first(parent(x))
else
return known_first(x)
Expand All @@ -226,20 +226,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(::Nothing, x) = last(parent(x))
_last(::Missing, 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) === nothing
if known_length(x) === missing
return size(parent(x), static(N))
else
return known_length(x)
end
end
@inline function Base.length(x::LazyAxis{:})::Int
if known_length(x) === nothing
if known_length(x) === missing
return length(parent(x))
else
return known_length(x)
Expand All @@ -255,7 +255,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) === nothing || known_last(x) === nothing
if known_first(x) === missing || known_last(x) === missing
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
10 changes: 5 additions & 5 deletions src/dimensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ to_dims(::Type{T}, dim::Integer) where {T} = Int(dim)
to_dims(::Type{T}, dim::Colon) where {T} = dim
function to_dims(::Type{T}, dim::StaticSymbol) where {T}
i = find_first_eq(dim, dimnames(T))
if i === nothing
if i === missing
throw_dim_error(T, dim)
end
return i
end
Compat.@constprop :aggressive function to_dims(::Type{T}, dim::Symbol) where {T}
i = find_first_eq(dim, map(Symbol, dimnames(T)))
if i === nothing
if i === missing
throw_dim_error(T, dim)
end
return i
Expand All @@ -207,7 +207,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 nothing is found use Colon()
3. if missing is found use Colon()
4. if (ndims - ncolon) === nkwargs then all were found, else error
=#
order_named_inds(x::Tuple, ::NamedTuple{(),Tuple{}}) = ()
Expand All @@ -226,7 +226,7 @@ Compat.@constprop :aggressive function order_named_inds(
end
function order_named_inds(x::Tuple, nd::Tuple, inds::Tuple, ::StaticInt{dim}) where {dim}
index = find_first_eq(getfield(x, dim), nd)
if index === nothing
if index === missing
return Colon()
else
return @inbounds(inds[index])
Expand All @@ -241,6 +241,6 @@ function _order_named_inds_check(inds::Tuple{Vararg{Any,N}}, nkwargs::Int) where
if (N - ncolon(inds, 0)) !== nkwargs
error("Not all keywords matched dimension names.")
end
return nothing
return missing
end

4 changes: 2 additions & 2 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

"""
is_splat_index(::Type{T}) -> StaticBool

Returns `static(true)` if `T` is a type that splats across multiple dimensions.
"""
is_splat_index(@nospecialize(x)) = is_splat_index(typeof(x))
Expand Down 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) !== nothing && known_length(axis) === known_length(inds))
(known_length(inds) !== missing && known_length(axis) === known_length(inds))
return axis
else
return to_axis(IndexStyle(axis), axis, inds)
Expand Down
Loading