1
1
# 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
15
16
16
17
# TODO : Use `Base.to_indices`?
17
18
isstored (a:: AbstractArray , I:: CartesianIndex ) = isstored (a, Tuple (I)... )
19
+ # TODO : Use `Base.to_indices`?
18
20
getstoredindex (a:: AbstractArray , I:: CartesianIndex ) = getstoredindex (a, Tuple (I)... )
21
+ # TODO : Use `Base.to_indices`?
19
22
getunstoredindex (a:: AbstractArray , I:: CartesianIndex ) = getunstoredindex (a, Tuple (I)... )
23
+ # TODO : Use `Base.to_indices`?
20
24
function setstoredindex! (a:: AbstractArray , value, I:: CartesianIndex )
21
25
return setstoredindex! (a, value, Tuple (I)... )
22
26
end
27
+ # TODO : Use `Base.to_indices`?
23
28
function setunstoredindex! (a:: AbstractArray , value, I:: CartesianIndex )
24
29
return setunstoredindex! (a, value, Tuple (I)... )
25
30
end
@@ -33,6 +38,9 @@ getunstoredindex(a::AbstractArray, I::Int...) = zero(eltype(a))
33
38
storedlength (a:: AbstractArray ) = length (storedvalues (a))
34
39
storedpairs (a:: AbstractArray ) = map (I -> I => getstoredindex (a, I), eachstoredindex (a))
35
40
41
+ to_vec (x) = vec (collect (x))
42
+ to_vec (x:: AbstractArray ) = vec (x)
43
+
36
44
# A view of the stored values of an array.
37
45
# Similar to: `@view a[collect(eachstoredindex(a))]`, but the issue
38
46
# 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}
47
55
array:: A
48
56
storedindices:: I
49
57
end
50
- StoredValues (a:: AbstractArray ) = StoredValues (a, collect (eachstoredindex (a)))
58
+ StoredValues (a:: AbstractArray ) = StoredValues (a, to_vec (eachstoredindex (a)))
51
59
Base. size (a:: StoredValues ) = size (a. storedindices)
52
60
Base. getindex (a:: StoredValues , I:: Int ) = getstoredindex (a. array, a. storedindices[I])
53
61
function Base. setindex! (a:: StoredValues , value, I:: Int )
0 commit comments