Skip to content

Commit 19249c7

Browse files
committed
use a boolean operator for any/all, not bitwise
and add more comprehensive tests
1 parent eedbf82 commit 19249c7

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

base/reduce.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ mul_prod(x::BitSignedSmall, y::BitSignedSmall) = Int(x) * Int(y)
2929
mul_prod(x::BitUnsignedSmall, y::BitUnsignedSmall) = UInt(x) * UInt(y)
3030
mul_prod(x::Real, y::Real)::Real = x * y
3131

32+
and_all(x,y) = Bool(x && y)
33+
or_any(x,y) = Bool(x || y)
34+
3235
## foldl && mapfoldl
3336

3437
function mapfoldl_impl(f::F, op::OP, nt, itr) where {F,OP}
@@ -336,8 +339,8 @@ reduce_empty(::typeof(+), ::Type{T}) where {T} = zero(T)
336339
reduce_empty(::typeof(+), ::Type{Bool}) = zero(Int)
337340
reduce_empty(::typeof(*), ::Type{T}) where {T} = one(T)
338341
reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
339-
reduce_empty(::typeof(&), ::Type{Bool}) = true
340-
reduce_empty(::typeof(|), ::Type{Bool}) = false
342+
reduce_empty(::typeof(and_all), ::Type{T}) where {T} = true
343+
reduce_empty(::typeof(or_any), ::Type{T}) where {T} = false
341344

342345
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
343346
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:BitSignedSmall} = zero(Int)

base/reducedim.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ end
4545
initarray!(a::AbstractArray{T}, f, ::Union{typeof(min),typeof(max),typeof(_extrema_rf)},
4646
init::Bool, src::AbstractArray) where {T} = (init && mapfirst!(f, a, src); a)
4747

48-
for (Op, initval) in ((:(typeof(&)), true), (:(typeof(|)), false))
48+
for (Op, initval) in ((:(typeof(and_all)), true), (:(typeof(or_any)), false))
4949
@eval initarray!(a::AbstractArray, ::Any, ::$(Op), init::Bool, src::AbstractArray) = (init && fill!(a, $initval); a)
5050
end
5151

@@ -173,8 +173,8 @@ end
173173
reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(max), A::AbstractArray{T}, region) where {T} =
174174
reducedim_initarray(A, region, zero(f(zero(T))), _realtype(f, T))
175175

176-
reducedim_init(f, op::typeof(&), A::AbstractArrayOrBroadcasted, region) = reducedim_initarray(A, region, true)
177-
reducedim_init(f, op::typeof(|), A::AbstractArrayOrBroadcasted, region) = reducedim_initarray(A, region, false)
176+
reducedim_init(f, op::typeof(and_all), A::AbstractArrayOrBroadcasted, region) = reducedim_initarray(A, region, true)
177+
reducedim_init(f, op::typeof(or_any), A::AbstractArrayOrBroadcasted, region) = reducedim_initarray(A, region, false)
178178

179179
# specialize to make initialization more efficient for common cases
180180

@@ -996,7 +996,7 @@ _all(a, ::Colon) = _all(identity, a, :)
996996

997997
for (fname, op) in [(:sum, :add_sum), (:prod, :mul_prod),
998998
(:maximum, :max), (:minimum, :min),
999-
(:all, :&), (:any, :|),
999+
(:all, :and_all), (:any, :or_any),
10001000
(:extrema, :_extrema_rf)]
10011001
fname! = Symbol(fname, '!')
10021002
_fname = Symbol('_', fname)

test/reducedim.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,38 @@ end
731731
@test_broken @inferred(maximum(exp, A; dims = 1))[1] === missing
732732
@test_broken @inferred(extrema(exp, A; dims = 1))[1] === (missing, missing)
733733
end
734+
735+
some_exception(op) = try return (Some(op()), nothing); catch ex; return (nothing, ex); end
736+
reduced_shape(sz, dims) = ntuple(d -> d in dims ? 1 : sz[d], length(sz))
737+
738+
@testset "Ensure that calling, e.g., sum(empty; dims) has the same behavior as sum(empty)" begin
739+
@testset "$r(Array{$T}(undef, $sz); dims=$dims)" for
740+
r in (minimum, maximum, findmin, findmax, extrema, sum, prod, mapreduce, all, any, count),
741+
T in (Int, Union{Missing, Int}, Number, Union{Missing, Number}, Bool, Union{Missing, Bool}, Any),
742+
sz in ((0,), (0,1), (1,0), (0,0), (0,0,1), (1,0,1)),
743+
dims in (1, 2, 3, 4, (1,2), (1,3), (2,3,4), (1,2,3))
744+
745+
A = Array{T}(undef, sz)
746+
rsz = reduced_shape(sz, dims)
747+
748+
v, ex = some_exception() do; r(A); end
749+
if isnothing(v)
750+
@test_throws typeof(ex) r(A; dims)
751+
else
752+
actual = fill(something(v), rsz)
753+
@test isequal(r(A; dims), actual)
754+
@test eltype(r(A; dims)) === eltype(actual)
755+
end
756+
757+
for f in (identity, abs, abs2)
758+
v, ex = some_exception() do; r(f, A); end
759+
if isnothing(v)
760+
@test_throws typeof(ex) r(f, A; dims)
761+
else
762+
actual = fill(something(v), rsz)
763+
@test isequal(r(f, A; dims), actual)
764+
@test eltype(r(f, A; dims)) === eltype(actual)
765+
end
766+
end
767+
end
768+
end

0 commit comments

Comments
 (0)