12
12
const SmallUnsigned = Union{UInt8,UInt16,UInt32}
13
13
end
14
14
15
+ abstract type AbstractBroadcasted end
16
+ const AbstractArrayOrBroadcasted = Union{AbstractArray, AbstractBroadcasted}
17
+
15
18
"""
16
19
Base.add_sum(x, y)
17
20
@@ -152,7 +155,8 @@ foldr(op, itr; kw...) = mapfoldr(identity, op, itr; kw...)
152
155
153
156
# This is a generic implementation of `mapreduce_impl()`,
154
157
# certain `op` (e.g. `min` and `max`) may have their own specialized versions.
155
- @noinline function mapreduce_impl (f, op, A:: AbstractArray , ifirst:: Integer , ilast:: Integer , blksize:: Int )
158
+ @noinline function mapreduce_impl (f, op, A:: AbstractArrayOrBroadcasted ,
159
+ ifirst:: Integer , ilast:: Integer , blksize:: Int )
156
160
if ifirst == ilast
157
161
@inbounds a1 = A[ifirst]
158
162
return mapreduce_first (f, op, a1)
@@ -175,7 +179,7 @@ foldr(op, itr; kw...) = mapfoldr(identity, op, itr; kw...)
175
179
end
176
180
end
177
181
178
- mapreduce_impl (f, op, A:: AbstractArray , ifirst:: Integer , ilast:: Integer ) =
182
+ mapreduce_impl (f, op, A:: AbstractArrayOrBroadcasted , ifirst:: Integer , ilast:: Integer ) =
179
183
mapreduce_impl (f, op, A, ifirst, ilast, pairwise_blocksize (f, op))
180
184
181
185
"""
@@ -296,13 +300,13 @@ The default is `reduce_first(op, f(x))`.
296
300
"""
297
301
mapreduce_first (f, op, x) = reduce_first (op, f (x))
298
302
299
- _mapreduce (f, op, A:: AbstractArray ) = _mapreduce (f, op, IndexStyle (A), A)
303
+ _mapreduce (f, op, A:: AbstractArrayOrBroadcasted ) = _mapreduce (f, op, IndexStyle (A), A)
300
304
301
- function _mapreduce (f, op, :: IndexLinear , A:: AbstractArray{T} ) where T
305
+ function _mapreduce (f, op, :: IndexLinear , A:: AbstractArrayOrBroadcasted )
302
306
inds = LinearIndices (A)
303
307
n = length (inds)
304
308
if n == 0
305
- return mapreduce_empty (f, op, T )
309
+ return mapreduce_empty_iter (f, op, A, IteratorEltype (A) )
306
310
elseif n == 1
307
311
@inbounds a1 = A[first (inds)]
308
312
return mapreduce_first (f, op, a1)
323
327
324
328
mapreduce (f, op, a:: Number ) = mapreduce_first (f, op, a)
325
329
326
- _mapreduce (f, op, :: IndexCartesian , A:: AbstractArray ) = mapfoldl (f, op, A)
330
+ _mapreduce (f, op, :: IndexCartesian , A:: AbstractArrayOrBroadcasted ) = mapfoldl (f, op, A)
327
331
328
332
"""
329
333
reduce(op, itr; [init])
@@ -473,7 +477,7 @@ isgoodzero(::typeof(max), x) = isbadzero(min, x)
473
477
isgoodzero (:: typeof (min), x) = isbadzero (max, x)
474
478
475
479
function mapreduce_impl (f, op:: Union{typeof(max), typeof(min)} ,
476
- A:: AbstractArray , first:: Int , last:: Int )
480
+ A:: AbstractArrayOrBroadcasted , first:: Int , last:: Int )
477
481
a1 = @inbounds A[first]
478
482
v1 = mapreduce_first (f, op, a1)
479
483
v2 = v3 = v4 = v1
@@ -746,7 +750,7 @@ function count(pred, itr)
746
750
end
747
751
return n
748
752
end
749
- function count (pred, a:: AbstractArray )
753
+ function count (pred, a:: AbstractArrayOrBroadcasted )
750
754
n = 0
751
755
for i in eachindex (a)
752
756
@inbounds n += pred (a[i]):: Bool
0 commit comments