Skip to content

Commit 41ca636

Browse files
authored
Specialize fill! (#253)
1 parent e2fe9a2 commit 41ca636

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
2323
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
2424
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2525
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
26+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2627
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2728
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2829
TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
2930
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
3031

3132
[targets]
32-
test = ["Test", "JLArrays", "StaticArrays", "OffsetArrays", "PooledArrays", "TypedTables", "WeakRefStrings", "Documenter"]
33+
test = ["Test", "JLArrays", "StaticArrays", "OffsetArrays", "PooledArrays", "TypedTables", "WeakRefStrings", "Documenter", "SparseArrays"]

src/structarray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,12 @@ function Base.copyto!(I::StructArray, doffs::Integer, J::StructArray, soffs::Int
425425
return I
426426
end
427427

428+
function Base.fill!(s::StructArray{T}, x) where {T}
429+
xT = maybe_convert_elt(T, x)
430+
foreachfield(fill!, s, xT)
431+
return s
432+
end
433+
428434
function Base.resize!(s::StructArray, i::Integer)
429435
for a in components(s)
430436
resize!(a, i)

test/runtests.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using DataAPI: refarray, refvalue
88
using Adapt: adapt, Adapt
99
using JLArrays
1010
using Test
11+
using SparseArrays
1112

1213
using Documenter: doctest
1314
if Base.VERSION >= v"1.6" && Int === Int64
@@ -569,6 +570,54 @@ end
569570
@test t[5] == p
570571
end
571572

573+
@testset "fill!" begin
574+
@testset "dense array, complex" begin
575+
A = zeros(3,3)
576+
B = zeros(3,3)
577+
S = StructArray{Complex{eltype(A)}}((A, B))
578+
fill!(S, 2+3im)
579+
@test all(==(2), A)
580+
@test all(==(3), B)
581+
end
582+
583+
@testset "offset array, custom struct" begin
584+
struct Vec3D{T} <: FieldVector{3, T}
585+
x :: T
586+
y :: T
587+
z :: T
588+
end
589+
590+
A = zeros(3:6, 3:6)
591+
B = zeros(3:6, 3:6)
592+
C = zeros(3:6, 3:6)
593+
S = StructArray{Vec3D{eltype(A)}}((A, B, C))
594+
fill!(S, Vec3D(1,2,3))
595+
@test all(==(1), A)
596+
@test all(==(2), B)
597+
@test all(==(3), C)
598+
end
599+
600+
@testset "Tuple" begin
601+
S = StructArray{Tuple{Int,Int}}(([1,2], [3,4]))
602+
fill!(S, (4,5))
603+
@test all(==((4,5)), S)
604+
605+
S = StructArray{@NamedTuple{a::Int,b::Int}}(([1,2], [3,4]))
606+
fill!(S, (a=10.0, b=20.0))
607+
@test all(==(10), S.a)
608+
@test all(==(20), S.b)
609+
end
610+
611+
@testset "sparse matrix, complex" begin
612+
A = spzeros(3)
613+
B = spzeros(3)
614+
S = StructArray{Complex{eltype(A)}}((A,B))
615+
fill!(S, 0)
616+
@test all(iszero, A)
617+
@test all(iszero, B)
618+
end
619+
end
620+
572621
@testset "concat" begin
573622
t = StructArray{Pair{Int, String}}(([3, 5], ["a", "b"]))
574623
push!(t, (2 => "d"))

0 commit comments

Comments
 (0)