Skip to content

Commit d9535fc

Browse files
committed
Better fix, add tests
1 parent 6e65672 commit d9535fc

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/abstractsparsearrayinterface.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Base: @_propagate_inbounds_meta
12
using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArrayInterface
23

34
# This is to bring `ArrayLayouts.zero!` into the namespace
@@ -37,13 +38,19 @@ end
3738
@interface ::AbstractArrayInterface function isstored(
3839
a::AbstractArray{<:Any,N}, I::Vararg{Int,N}
3940
) where {N}
41+
@_propagate_inbounds_meta
4042
@boundscheck checkbounds(a, I...)
4143
return true
4244
end
43-
@interface ::AbstractArrayInterface function isstored(a::AbstractArray, I::Int...)
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
4451
@boundscheck checkbounds(a, I...)
4552
I′ = ntuple(i -> I[i], ndims(a))
46-
return isstored(a, I′...)
53+
return @inbounds @interface interface isstored(a, I′...)
4754
end
4855
@interface ::AbstractArrayInterface eachstoredindex(a::AbstractArray) = eachindex(a)
4956
@interface ::AbstractArrayInterface getstoredindex(a::AbstractArray, I::Int...) =
@@ -163,6 +170,12 @@ function Base.setindex!(a::StoredValues, value, I::Int)
163170
return setstoredindex!(a.array, value, a.storedindices[I])
164171
end
165172

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+
166179
@interface ::AbstractSparseArrayInterface storedvalues(a::AbstractArray) = StoredValues(a)
167180

168181
@interface ::AbstractSparseArrayInterface function eachstoredindex(

src/sparsearraydok.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ storage(a::SparseArrayDOK) = a.storage
7373
Base.size(a::SparseArrayDOK) = a.size
7474

7575
storedvalues(a::SparseArrayDOK) = values(storage(a))
76-
function isstored(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
77-
return CartesianIndex(I) in keys(storage(a))
76+
function isstored(a::SparseArrayDOK, I::Int...)
77+
return @interface interface(a) isstored(a, I...)
7878
end
7979
function eachstoredindex(a::SparseArrayDOK)
8080
return keys(storage(a))

test/test_basics.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ arrayts = (Array, JLArray)
2626
for indexstyle in (IndexLinear(), IndexCartesian())
2727
for I in eachindex(indexstyle, a)
2828
@test isstored(a, I)
29+
if indexstyle == IndexCartesian()
30+
@test isstored(a, Tuple(I)..., 1)
31+
end
2932
end
3033
end
3134
@test eachstoredindex(a) == eachindex(a)

test/test_sparsearraydok.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ arrayts = (Array,)
210210
a[1, 2] = 12
211211
@test sprint(show, "text/plain", a) ==
212212
"$(summary(a)):\n$(eltype(a)(12))\n ⋅ ⋅ "
213+
214+
a = SparseArrayDOK{elt}(undef, 2)
215+
a[1] = 1
216+
@test sprint(show, "text/plain", a) == "$(summary(a)):\n $(eltype(a)(1))\n"
213217
end
214218

215219
# Regression test for:

0 commit comments

Comments
 (0)