Skip to content

Commit 1e30cee

Browse files
committed
Update indexing for SparseArrayDOK
1 parent f369653 commit 1e30cee

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/sparsearraydok.jl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,36 @@ storage(a::SparseArrayDOK) = a.storage
7474
Base.size(a::SparseArrayDOK) = a.size
7575

7676
storedvalues(a::SparseArrayDOK) = values(storage(a))
77-
function isstored(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
78-
return @interface interface(a) isstored(a, I...)
77+
@inline function isstored(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
78+
@boundscheck checkbounds(a, I...)
79+
return haskey(storage(a), CartesianIndex(I))
7980
end
80-
function eachstoredindex(a::SparseArrayDOK)
81+
function eachstoredindex(::IndexCartesian, a::SparseArrayDOK)
8182
return keys(storage(a))
8283
end
83-
function getstoredindex(a::SparseArrayDOK, I::Int...)
84+
@inline function getstoredindex(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
85+
@boundscheck checkbounds(a, I...)
8486
return storage(a)[CartesianIndex(I)]
8587
end
86-
function getunstoredindex(a::SparseArrayDOK, I::Int...)
88+
@inline function getunstoredindex(a::SparseArrayDOK{<:Any,N}, I::Vararg{Int,N}) where {N}
89+
@boundscheck checkbounds(a, I...)
8790
return a.getunstored(a, I...)
8891
end
89-
function setstoredindex!(a::SparseArrayDOK, value, I::Int...)
90-
# TODO: Have a way to disable this check, analogous to `checkbounds`,
91-
# since this is already checked in `setindex!`.
92-
isstored(a, I...) || throw(IndexError("key $(CartesianIndex(I)) not found"))
92+
@inline function setstoredindex!(
93+
a::SparseArrayDOK{<:Any,N}, value, I::Vararg{Int,N}
94+
) where {N}
95+
# `isstored` includes a boundscheck as well
96+
@boundscheck isstored(a, I...) ||
97+
throw(IndexError(lazy"key $(CartesianIndex(I...)) not found"))
9398
# TODO: If `iszero(value)`, unstore the index.
9499
storage(a)[CartesianIndex(I)] = value
95100
return a
96101
end
97-
function setunstoredindex!(a::SparseArrayDOK, value, I::Int...)
98-
set!(storage(a), CartesianIndex(I), value)
102+
@inline function setunstoredindex!(
103+
a::SparseArrayDOK{<:Any,N}, value, I::Vararg{Int,N}
104+
) where {N}
105+
@boundscheck checkbounds(a, I...)
106+
insert!(storage(a), CartesianIndex(I), value)
99107
return a
100108
end
101109

0 commit comments

Comments
 (0)