Skip to content

Commit d90fe3f

Browse files
committed
use a boolean operator for any/all, not bitwise
and add more comprehensive tests
1 parent 07dbfc6 commit d90fe3f

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

@@ -994,7 +994,7 @@ _all(a, ::Colon) = _all(identity, a, :)
994994

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

test/reducedim.jl

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

0 commit comments

Comments
 (0)