Skip to content

Commit c97446f

Browse files
committed
avoid calling AbstractArrayInterface fallbacks when AbstractSparseArrayInterface is selected
1 parent ed9ef7c commit c97446f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/indexing.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,51 @@ end
322322
return _isstored(style, A, Base.to_indices(A, I)...)
323323
end
324324

325+
@interface ::AbstractSparseArrayInterface function getunstoredindex(
326+
A::AbstractArray, I::Int...
327+
)
328+
@_propagate_inbounds_meta
329+
style = IndexStyle(A)
330+
331+
# canonical linear indexing
332+
if style == IndexLinear() && length(I) == 1
333+
@boundscheck checkbounds(A, I...)
334+
return zero(eltype(A))
335+
end
336+
337+
# canonical cartesian indexing
338+
if style == IndexCartesian() && length(I) == ndims(A)
339+
@boundscheck checkbounds(A, I...)
340+
return zero(eltype(A))
341+
end
342+
343+
# non-canonical indexing
344+
return _getunstoredindex(style, A, Base.to_indices(A, I)...)
345+
end
346+
347+
# make sure we don't call AbstractArrayInterface defaults
348+
@interface ::AbstractSparseArrayInterface function getstoredindex(
349+
A::AbstractArray, I::Int...
350+
)
351+
@_propagate_inbounds_meta
352+
style = IndexStyle(A)
353+
error_if_canonical_getstoredindex(style, A, I...)
354+
return _getstoredindex(style, A, Base.to_indices(A, I)...)
355+
end
356+
357+
for f! in (:setstoredindex!, :setunstoredindex!)
358+
_f! = Symbol(:_, f!)
359+
error_if_canonical_setstoredindex = Symbol(:error_if_canonical_, f!)
360+
@eval begin
361+
@interface ::AbstractSparseArrayInterface function $f!(A::AbstractArray, v, I::Int...)
362+
@_propagate_inbounds_meta
363+
style = IndexStyle(A)
364+
$error_if_canonical_setstoredindex(style, A, I...)
365+
return $_f!(style, A, v, Base.to_indices(A, I)...)
366+
end
367+
end
368+
end
369+
325370
@interface ::AbstractSparseArrayInterface storedlength(A::AbstractArray) =
326371
length(storedvalues(A))
327372
@interface ::AbstractSparseArrayInterface storedpairs(A::AbstractArray) =

0 commit comments

Comments
 (0)