@@ -5,10 +5,14 @@ storedvalues(a) = error()
55isstored (a, I:: Int... ) = error ()
66eachstoredindex (a) = error ()
77getstoredindex (a, I:: Int... ) = error ()
8- getunstoredindex (a, I:: Int... ) = error ()
98setstoredindex! (a, value, I:: Int... ) = error ()
109setunstoredindex! (a, value, I:: Int... ) = error ()
1110
11+ # Interface defaults.
12+ # TODO : Have a fallback that handles element types
13+ # that don't define `zero(::Type)`.
14+ getunstoredindex (a, I:: Int... ) = zero (eltype (a))
15+
1216# Derived interface.
1317storedlength (a) = length (storedvalues (a))
1418storedpairs (a) = map (I -> I => getstoredindex (a, I), eachstoredindex (a))
@@ -19,22 +23,28 @@ function eachstoredindex(a1, a2, a_rest...)
1923 return union (eachstoredindex .((a1, a2, a_rest... ))... )
2024end
2125
22- # TODO : Add `ndims` type parameter.
23- # TODO : Define `AbstractSparseArrayInterface`, make this a subtype.
2426using Derive: Derive, @interface , AbstractArrayInterface
25- struct SparseArrayInterface <: AbstractArrayInterface end
27+
28+ # TODO : Add `ndims` type parameter.
29+ # TODO : This isn't used to define interface functions right now.
30+ # Currently, `@interface` expects an instance, probably it should take a
31+ # type instead so fallback functions can use abstract types.
32+ abstract type AbstractSparseArrayInterface <: AbstractArrayInterface end
33+
34+ struct SparseArrayInterface <: AbstractSparseArrayInterface end
2635
2736# Convenient shorthand to refer to the sparse interface.
37+ # TODO : Define this as `InterfaceFunction(AbstractSparseArrayInterface)`
2838const sparse = SparseArrayInterface ()
2939
3040# TODO : Use `ArrayLayouts.layout_getindex`, `ArrayLayouts.sub_materialize`
3141# to handle slicing (implemented by copying SubArray).
32- @interface sparse function Base. getindex (a, I:: Int... )
42+ @interface AbstractSparseArrayInterface function Base. getindex (a, I:: Int... )
3343 ! isstored (a, I... ) && return getunstoredindex (a, I... )
3444 return getstoredindex (a, I... )
3545end
3646
37- @interface sparse function Base. setindex! (a, value, I:: Int... )
47+ @interface AbstractSparseArrayInterface function Base. setindex! (a, value, I:: Int... )
3848 iszero (value) && return a
3949 if ! isstored (a, I... )
4050 setunstoredindex! (a, value, I... )
4656
4757# TODO : This may need to be defined in `sparsearraydok.jl`, after `SparseArrayDOK`
4858# is defined. And/or define `default_type(::SparseArrayStyle, T::Type) = SparseArrayDOK{T}`.
49- @interface sparse function Base. similar (a, T:: Type , size:: Tuple{Vararg{Int}} )
59+ @interface AbstractSparseArrayInterface function Base. similar (
60+ a, T:: Type , size:: Tuple{Vararg{Int}}
61+ )
5062 return SparseArrayDOK {T} (size... )
5163end
5264
5365# # TODO : Make this more general, handle mixtures of integers and ranges.
5466# # TODO : Make this logic generic to all `similar(::AbstractInterface, ...)`.
55- # # @interface sparse function Base.similar(a, T::Type, dims::Tuple{Vararg{Base.OneTo}})
67+ # # @interface AbstractSparseArrayInterface function Base.similar(a, T::Type, dims::Tuple{Vararg{Base.OneTo}})
5668# # return sparse(similar)(interface, a, T, Base.to_shape(dims))
5769# # end
5870
59- @interface sparse function Base. map (f, as... )
71+ @interface AbstractSparseArrayInterface function Base. map (f, as... )
6072 # This is defined in this way so we can rely on the Broadcast logic
6173 # for determining the destination of the operation (element type, shape, etc.).
6274 return f .(as... )
6375end
6476
65- @interface sparse function Base. map! (f, dest, as... )
77+ @interface AbstractSparseArrayInterface function Base. map! (f, dest, as... )
6678 # Check `f` preserves zeros.
6779 # Define as `map_stored!`.
6880 # Define `eachstoredindex` promotion.
7284 return dest
7385end
7486
75- # TODO : Define `AbstractSparseArrayStyle`, make this a subtype.
76- struct SparseArrayStyle{N} <: Broadcast.AbstractArrayStyle{N} end
87+ abstract type AbstractSparseArrayStyle{N} <: Broadcast.AbstractArrayStyle{N} end
88+
89+ struct SparseArrayStyle{N} <: AbstractSparseArrayStyle{N} end
7790
7891SparseArrayStyle {M} (:: Val{N} ) where {M,N} = SparseArrayStyle {N} ()
7992
80- @interface sparse function Broadcast. BroadcastStyle (type:: Type )
93+ @interface AbstractSparseArrayInterface function Broadcast. BroadcastStyle (type:: Type )
8194 return SparseArrayStyle {ndims(type)} ()
8295end
8396
@@ -100,12 +113,12 @@ abstract type AbstractSparseLayout <: ArrayLayouts.MemoryLayout end
100113
101114struct SparseLayout <: AbstractSparseLayout end
102115
103- @interface sparse function ArrayLayouts. MemoryLayout (type:: Type )
116+ @interface AbstractSparseArrayInterface function ArrayLayouts. MemoryLayout (type:: Type )
104117 return SparseLayout ()
105118end
106119
107120using LinearAlgebra: LinearAlgebra
108- @interface sparse function LinearAlgebra. mul! (a_dest, a1, a2, α, β)
121+ @interface AbstractSparseArrayInterface function LinearAlgebra. mul! (a_dest, a1, a2, α, β)
109122 return ArrayLayouts. mul! (a_dest, a1, a2, α, β)
110123end
111124
0 commit comments