Skip to content

Commit e38b9ce

Browse files
committed
Fill, zero
1 parent 1b9dfe8 commit e38b9ce

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ using SparseArraysBase:
4444
setunstoredindex!,
4545
storedlength,
4646
storedpairs,
47-
storedvalues
47+
storedvalues,
48+
zero!
4849
using Test: @test, @test_throws
4950

5051
a = SparseArrayDOK{Float64}(2, 2)
@@ -120,6 +121,37 @@ b = a[1:2, 2]
120121
@test b isa SparseVectorDOK{Float64}
121122
@test b == [12, 0]
122123
@test storedlength(b) == 1
124+
125+
a = SparseArrayDOK{Float64}(2, 2)
126+
a .= 2
127+
for I in eachindex(a)
128+
@test a[I] == 2
129+
end
130+
@test storedlength(a) == length(a)
131+
132+
a = SparseArrayDOK{Float64}(2, 2)
133+
fill!(a, 2)
134+
for I in eachindex(a)
135+
@test a[I] == 2
136+
end
137+
@test storedlength(a) == length(a)
138+
139+
a = SparseArrayDOK{Float64}(2, 2)
140+
fill!(a, 0)
141+
@test iszero(a)
142+
@test iszero(storedlength(a))
143+
144+
a = SparseArrayDOK{Float64}(2, 2)
145+
a[1, 2] = 12
146+
zero!(a)
147+
@test iszero(a)
148+
@test iszero(storedlength(a))
149+
150+
a = SparseArrayDOK{Float64}(2, 2)
151+
a[1, 2] = 12
152+
b = zero(a)
153+
@test iszero(b)
154+
@test iszero(storedlength(b))
123155
````
124156

125157
---

examples/README.jl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ using SparseArraysBase:
4949
setunstoredindex!,
5050
storedlength,
5151
storedpairs,
52-
storedvalues
52+
storedvalues,
53+
zero!
5354
using Test: @test, @test_throws
5455

5556
a = SparseArrayDOK{Float64}(2, 2)
@@ -116,3 +117,34 @@ b = a[1:2, 2]
116117
@test b isa SparseVectorDOK{Float64}
117118
@test b == [12, 0]
118119
@test storedlength(b) == 1
120+
121+
a = SparseArrayDOK{Float64}(2, 2)
122+
a .= 2
123+
for I in eachindex(a)
124+
@test a[I] == 2
125+
end
126+
@test storedlength(a) == length(a)
127+
128+
a = SparseArrayDOK{Float64}(2, 2)
129+
fill!(a, 2)
130+
for I in eachindex(a)
131+
@test a[I] == 2
132+
end
133+
@test storedlength(a) == length(a)
134+
135+
a = SparseArrayDOK{Float64}(2, 2)
136+
fill!(a, 0)
137+
@test iszero(a)
138+
@test iszero(storedlength(a))
139+
140+
a = SparseArrayDOK{Float64}(2, 2)
141+
a[1, 2] = 12
142+
zero!(a)
143+
@test iszero(a)
144+
@test iszero(storedlength(a))
145+
146+
a = SparseArrayDOK{Float64}(2, 2)
147+
a[1, 2] = 12
148+
b = zero(a)
149+
@test iszero(b)
150+
@test iszero(storedlength(b))

src/abstractsparsearrayinterface.jl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# This is to bring `ArrayLayouts.zero!` into the namespace
2+
# since it is considered part of the sparse array interface.
3+
using ArrayLayouts: zero!
4+
15
# Minimal interface for `SparseArrayInterface`.
26
isstored(a::AbstractArray, I::Int...) = true
37
eachstoredindex(a::AbstractArray) = eachindex(a)
@@ -111,15 +115,31 @@ end
111115
end
112116

113117
@interface ::AbstractSparseArrayInterface function Base.map!(
114-
f, dest::AbstractArray, as::AbstractArray...
118+
f, a_dest::AbstractArray, as::AbstractArray...
115119
)
116-
# Check `f` preserves zeros.
117-
# Define as `map_stored!`.
120+
# TODO: Define a function `preserves_unstored(a_dest, f, as...)`
121+
# to determine if a function preserves the stored values
122+
# of the destination sparse array.
123+
# The current code may be inefficient since it actually
124+
# accesses an unstored element, which in the case of a
125+
# sparse array of arrays can allocate an array.
126+
# Sparse arrays could be expected to define a cheap
127+
# unstored element allocator, for example
128+
# `get_prototypical_unstored(a::AbstractArray)`.
129+
I = first(eachindex(as...))
130+
preserves_unstored = iszero(f(map(a -> getunstoredindex(a, I), as)...))
131+
if !preserves_unstored
132+
# Doesn't preserve unstored values, loop over all elements.
133+
for I in eachindex(as...)
134+
a_dest[I] = map(f, map(a -> a[I], as)...)
135+
end
136+
return a_dest
137+
end
118138
# Define `eachstoredindex` promotion.
119139
for I in eachstoredindex(as...)
120-
dest[I] = f(map(a -> a[I], as)...)
140+
a_dest[I] = f(map(a -> a[I], as)...)
121141
end
122-
return dest
142+
return a_dest
123143
end
124144

125145
# `f::typeof(norm)`, `op::typeof(max)` used by `norm`.
@@ -175,6 +195,7 @@ function mul_indices(I1::CartesianIndex{2}, I2::CartesianIndex{2})
175195
return CartesianIndex(I1[1], I2[2])
176196
end
177197

198+
using LinearAlgebra: mul!
178199
function default_mul!!(
179200
a_dest::AbstractMatrix,
180201
a1::AbstractMatrix,

src/sparsearraydok.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ end
6363

6464
# Optional, but faster than the default.
6565
storedpairs(a::SparseArrayDOK) = pairs(storage(a))
66+
67+
function ArrayLayouts.zero!(a::SparseArrayDOK)
68+
empty!(storage(a))
69+
return a
70+
end

0 commit comments

Comments
 (0)