@@ -5,10 +5,14 @@ storedvalues(a) = error()
5
5
isstored (a, I:: Int... ) = error ()
6
6
eachstoredindex (a) = error ()
7
7
getstoredindex (a, I:: Int... ) = error ()
8
- getunstoredindex (a, I:: Int... ) = error ()
9
8
setstoredindex! (a, value, I:: Int... ) = error ()
10
9
setunstoredindex! (a, value, I:: Int... ) = error ()
11
10
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
+
12
16
# Derived interface.
13
17
storedlength (a) = length (storedvalues (a))
14
18
storedpairs (a) = map (I -> I => getstoredindex (a, I), eachstoredindex (a))
@@ -19,22 +23,28 @@ function eachstoredindex(a1, a2, a_rest...)
19
23
return union (eachstoredindex .((a1, a2, a_rest... ))... )
20
24
end
21
25
22
- # TODO : Add `ndims` type parameter.
23
- # TODO : Define `AbstractSparseArrayInterface`, make this a subtype.
24
26
using 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
26
35
27
36
# Convenient shorthand to refer to the sparse interface.
37
+ # TODO : Define this as `InterfaceFunction(AbstractSparseArrayInterface)`
28
38
const sparse = SparseArrayInterface ()
29
39
30
40
# TODO : Use `ArrayLayouts.layout_getindex`, `ArrayLayouts.sub_materialize`
31
41
# 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... )
33
43
! isstored (a, I... ) && return getunstoredindex (a, I... )
34
44
return getstoredindex (a, I... )
35
45
end
36
46
37
- @interface sparse function Base. setindex! (a, value, I:: Int... )
47
+ @interface AbstractSparseArrayInterface function Base. setindex! (a, value, I:: Int... )
38
48
iszero (value) && return a
39
49
if ! isstored (a, I... )
40
50
setunstoredindex! (a, value, I... )
46
56
47
57
# TODO : This may need to be defined in `sparsearraydok.jl`, after `SparseArrayDOK`
48
58
# 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
+ )
50
62
return SparseArrayDOK {T} (size... )
51
63
end
52
64
53
65
# # TODO : Make this more general, handle mixtures of integers and ranges.
54
66
# # 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}})
56
68
# # return sparse(similar)(interface, a, T, Base.to_shape(dims))
57
69
# # end
58
70
59
- @interface sparse function Base. map (f, as... )
71
+ @interface AbstractSparseArrayInterface function Base. map (f, as... )
60
72
# This is defined in this way so we can rely on the Broadcast logic
61
73
# for determining the destination of the operation (element type, shape, etc.).
62
74
return f .(as... )
63
75
end
64
76
65
- @interface sparse function Base. map! (f, dest, as... )
77
+ @interface AbstractSparseArrayInterface function Base. map! (f, dest, as... )
66
78
# Check `f` preserves zeros.
67
79
# Define as `map_stored!`.
68
80
# Define `eachstoredindex` promotion.
72
84
return dest
73
85
end
74
86
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
77
90
78
91
SparseArrayStyle {M} (:: Val{N} ) where {M,N} = SparseArrayStyle {N} ()
79
92
80
- @interface sparse function Broadcast. BroadcastStyle (type:: Type )
93
+ @interface AbstractSparseArrayInterface function Broadcast. BroadcastStyle (type:: Type )
81
94
return SparseArrayStyle {ndims(type)} ()
82
95
end
83
96
@@ -100,12 +113,12 @@ abstract type AbstractSparseLayout <: ArrayLayouts.MemoryLayout end
100
113
101
114
struct SparseLayout <: AbstractSparseLayout end
102
115
103
- @interface sparse function ArrayLayouts. MemoryLayout (type:: Type )
116
+ @interface AbstractSparseArrayInterface function ArrayLayouts. MemoryLayout (type:: Type )
104
117
return SparseLayout ()
105
118
end
106
119
107
120
using LinearAlgebra: LinearAlgebra
108
- @interface sparse function LinearAlgebra. mul! (a_dest, a1, a2, α, β)
121
+ @interface AbstractSparseArrayInterface function LinearAlgebra. mul! (a_dest, a1, a2, α, β)
109
122
return ArrayLayouts. mul! (a_dest, a1, a2, α, β)
110
123
end
111
124
0 commit comments