11# Minimal interface for `SparseArrayInterface`.
2- # TODO : Define default definitions for these based
3- # on the dense case.
4- # TODO : Define as `MethodError`.
5- # # isstored(a::AbstractArray, I::Int...) = true
6- isstored (a:: AbstractArray , I:: Int... ) = error (" Not implemented." )
7- # # eachstoredindex(a::AbstractArray) = eachindex(a)
8- eachstoredindex (a:: AbstractArray ) = error (" Not implemented." )
9- # # getstoredindex(a::AbstractArray, I::Int...) = getindex(a, I...)
10- getstoredindex (a:: AbstractArray , I:: Int... ) = error (" Not implemented." )
11- # # setstoredindex!(a::AbstractArray, value, I::Int...) = setindex!(a, value, I...)
12- setstoredindex! (a:: AbstractArray , value, I:: Int... ) = error (" Not implemented." )
13- # # setunstoredindex!(a::AbstractArray, value, I::Int...) = setindex!(a, value, I...)
14- setunstoredindex! (a:: AbstractArray , value, I:: Int... ) = error (" Not implemented." )
2+ isstored (a:: AbstractArray , I:: Int... ) = true
3+ eachstoredindex (a:: AbstractArray ) = eachindex (a)
4+ getstoredindex (a:: AbstractArray , I:: Int... ) = getindex (a, I... )
5+ function setstoredindex! (a:: AbstractArray , value, I:: Int... )
6+ setindex! (a, value, I... )
7+ return a
8+ end
9+ # TODO : Should this error by default if the value at the index
10+ # is stored? It could be disabled with something analogous
11+ # to `checkbounds`, like `checkstored`/`checkunstored`.
12+ function setunstoredindex! (a:: AbstractArray , value, I:: Int... )
13+ setindex! (a, value, I... )
14+ return a
15+ end
1516
1617# TODO : Use `Base.to_indices`?
1718isstored (a:: AbstractArray , I:: CartesianIndex ) = isstored (a, Tuple (I)... )
19+ # TODO : Use `Base.to_indices`?
1820getstoredindex (a:: AbstractArray , I:: CartesianIndex ) = getstoredindex (a, Tuple (I)... )
21+ # TODO : Use `Base.to_indices`?
1922getunstoredindex (a:: AbstractArray , I:: CartesianIndex ) = getunstoredindex (a, Tuple (I)... )
23+ # TODO : Use `Base.to_indices`?
2024function setstoredindex! (a:: AbstractArray , value, I:: CartesianIndex )
2125 return setstoredindex! (a, value, Tuple (I)... )
2226end
27+ # TODO : Use `Base.to_indices`?
2328function setunstoredindex! (a:: AbstractArray , value, I:: CartesianIndex )
2429 return setunstoredindex! (a, value, Tuple (I)... )
2530end
@@ -33,6 +38,9 @@ getunstoredindex(a::AbstractArray, I::Int...) = zero(eltype(a))
3338storedlength (a:: AbstractArray ) = length (storedvalues (a))
3439storedpairs (a:: AbstractArray ) = map (I -> I => getstoredindex (a, I), eachstoredindex (a))
3540
41+ to_vec (x) = vec (collect (x))
42+ to_vec (x:: AbstractArray ) = vec (x)
43+
3644# A view of the stored values of an array.
3745# Similar to: `@view a[collect(eachstoredindex(a))]`, but the issue
3846# with that is it returns a `SubArray` wrapping a sparse array, which
@@ -47,7 +55,7 @@ struct StoredValues{T,A<:AbstractArray{T},I} <: AbstractVector{T}
4755 array:: A
4856 storedindices:: I
4957end
50- StoredValues (a:: AbstractArray ) = StoredValues (a, collect (eachstoredindex (a)))
58+ StoredValues (a:: AbstractArray ) = StoredValues (a, to_vec (eachstoredindex (a)))
5159Base. size (a:: StoredValues ) = size (a. storedindices)
5260Base. getindex (a:: StoredValues , I:: Int ) = getstoredindex (a. array, a. storedindices[I])
5361function Base. setindex! (a:: StoredValues , value, I:: Int )
0 commit comments