Skip to content

Commit 23bbd28

Browse files
committedApr 15, 2021
add test for CartesianIndex idx and support scatter as well
1 parent 8bb569d commit 23bbd28

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed
 

‎src/scatter.jl

+20-20
Original file line numberDiff line numberDiff line change
@@ -102,55 +102,55 @@ function scatter end
102102

103103
for op in [+, -]
104104
@eval function scatter(op::typeof($op),
105-
src::AbstractArray{T,Nsrc},
106-
idx::AbstractArray{<:IntOrIntTuple,Nidx}) where {T,Nsrc,Nidx}
105+
src::AbstractArray{Tsrc,Nsrc},
106+
idx::AbstractArray{Tidx,Nidx}) where {Tsrc,Tidx,Nsrc,Nidx}
107107
dims = Nsrc - Nidx
108108
dstsize = (size(src)[1:dims]..., maximum_dims(idx)...)
109-
dst = similar(src, T, dstsize)
110-
fill!(dst, Base.reduce_empty(+, T))
109+
dst = similar(src, Tsrc, dstsize)
110+
fill!(dst, Base.reduce_empty(+, Tsrc))
111111
scatter!(op, dst, src, idx)
112112
end
113113
end
114114

115115
for op in [*, /]
116116
@eval function scatter(op::typeof($op),
117-
src::AbstractArray{T,Nsrc},
118-
idx::AbstractArray{<:IntOrIntTuple,Nidx}) where {T,Nsrc,Nidx}
117+
src::AbstractArray{Tsrc,Nsrc},
118+
idx::AbstractArray{Tidx,Nidx}) where {Tsrc,Tidx,Nsrc,Nidx}
119119
dims = Nsrc - Nidx
120120
dstsize = (size(src)[1:dims]..., maximum_dims(idx)...)
121-
dst = similar(src, T, dstsize)
122-
fill!(dst, Base.reduce_empty(*, T))
121+
dst = similar(src, Tsrc, dstsize)
122+
fill!(dst, Base.reduce_empty(*, Tsrc))
123123
scatter!(op, dst, src, idx)
124124
end
125125
end
126126

127127
function scatter(op::typeof(max),
128-
src::AbstractArray{T,Nsrc},
129-
idx::AbstractArray{<:IntOrIntTuple,Nidx}) where {T,Nsrc,Nidx}
128+
src::AbstractArray{Tsrc,Nsrc},
129+
idx::AbstractArray{Tidx,Nidx}) where {Tsrc,Tidx,Nsrc,Nidx}
130130
dims = Nsrc - Nidx
131131
dstsize = (size(src)[1:dims]..., maximum_dims(idx)...)
132-
dst = similar(src, T, dstsize)
133-
fill!(dst, typemin(T))
132+
dst = similar(src, Tsrc, dstsize)
133+
fill!(dst, typemin(Tsrc))
134134
scatter!(op, dst, src, idx)
135135
end
136136

137137
function scatter(op::typeof(min),
138-
src::AbstractArray{T,Nsrc},
139-
idx::AbstractArray{<:IntOrIntTuple,Nidx}) where {T,Nsrc,Nidx}
138+
src::AbstractArray{Tsrc,Nsrc},
139+
idx::AbstractArray{Tidx,Nidx}) where {Tsrc,Tidx,Nsrc,Nidx}
140140
dims = Nsrc - Nidx
141141
dstsize = (size(src)[1:dims]..., maximum_dims(idx)...)
142-
dst = similar(src, T, dstsize)
143-
fill!(dst, typemax(T))
142+
dst = similar(src, Tsrc, dstsize)
143+
fill!(dst, typemax(Tsrc))
144144
scatter!(op, dst, src, idx)
145145
end
146146

147147
function scatter(op::typeof(mean),
148-
src::AbstractArray{T,Nsrc},
149-
idx::AbstractArray{<:IntOrIntTuple,Nidx}) where {T,Nsrc,Nidx}
150-
FT = float(T)
148+
src::AbstractArray{Tsrc,Nsrc},
149+
idx::AbstractArray{Tidx,Nidx}) where {Tsrc,Tidx,Nsrc,Nidx}
150+
FT = float(Tsrc)
151151
dims = Nsrc - Nidx
152152
dstsize = (size(src)[1:dims]..., maximum_dims(idx)...)
153-
dst = similar(src, T, dstsize)
153+
dst = similar(src, Tsrc, dstsize)
154154
fill!(dst, Base.reduce_empty(+, FT))
155155
scatter!(op, dst, src, idx)
156156
end

‎test/scatter.jl

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ idxs = Dict(
1818
:tup => [(1,) (2,) (3,) (4,);
1919
(4,) (2,) (1,) (3,);
2020
(3,) (5,) (5,) (3,)],
21+
:car => CartesianIndex.(
22+
[(1,) (2,) (3,) (4,);
23+
(4,) (2,) (1,) (3,);
24+
(3,) (5,) (5,) (3,)]),
2125
)
2226
res = Dict(
2327
(+, 0, true) => [5, 6, 9, 8, 9],

0 commit comments

Comments
 (0)