|
| 1 | +using Base: @_propagate_inbounds_meta |
1 | 2 | using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArrayInterface
|
2 | 3 |
|
3 | 4 | # This is to bring `ArrayLayouts.zero!` into the namespace
|
|
34 | 35 |
|
35 | 36 | # Minimal interface for `SparseArrayInterface`.
|
36 | 37 | # Fallbacks for dense/non-sparse arrays.
|
37 |
| -@interface ::AbstractArrayInterface isstored(a::AbstractArray, I::Int...) = true |
| 38 | +@interface ::AbstractArrayInterface function isstored( |
| 39 | + a::AbstractArray{<:Any,N}, I::Vararg{Int,N} |
| 40 | +) where {N} |
| 41 | + @_propagate_inbounds_meta |
| 42 | + @boundscheck checkbounds(a, I...) |
| 43 | + return true |
| 44 | +end |
| 45 | +@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int) |
| 46 | + @_propagate_inbounds_meta |
| 47 | + return @interface interface isstored(a, Tuple(CartesianIndices(a)[I])...) |
| 48 | +end |
| 49 | +@interface interface::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...) |
| 50 | + @_propagate_inbounds_meta |
| 51 | + @boundscheck checkbounds(a, I...) |
| 52 | + I′ = ntuple(i -> I[i], ndims(a)) |
| 53 | + return @inbounds @interface interface isstored(a, I′...) |
| 54 | +end |
38 | 55 | @interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
|
39 | 56 | @interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
|
40 | 57 | getindex(a, I...)
|
@@ -153,6 +170,12 @@ function Base.setindex!(a::StoredValues, value, I::Int)
|
153 | 170 | return setstoredindex!(a.array, value, a.storedindices[I])
|
154 | 171 | end
|
155 | 172 |
|
| 173 | +@interface ::AbstractSparseArrayInterface function isstored( |
| 174 | + a::AbstractArray{<:Any,N}, I::Vararg{Int,N} |
| 175 | +) where {N} |
| 176 | + return CartesianIndex(I) in eachstoredindex(a) |
| 177 | +end |
| 178 | + |
156 | 179 | @interface ::AbstractSparseArrayInterface storedvalues(a::AbstractArray) = StoredValues(a)
|
157 | 180 |
|
158 | 181 | @interface ::AbstractSparseArrayInterface function eachstoredindex(
|
|
0 commit comments