Skip to content

Commit

Permalink
Don't use _mapreduce for countnz
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Nov 3, 2015
1 parent 99dc2eb commit 984188a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,17 @@ function count(pred, A::AbstractArray)
return n
end

immutable NotEqZero <: Func{1} end
call(::NotEqZero, x) = x != 0

"""
countnz(A)
Counts the number of nonzero values in array `A` (dense or sparse). Note that this is not a constant-time operation.
For sparse matrices, one should usually use `nnz`, which returns the number of stored values.
"""
countnz(a) = count(NotEqZero(), a)
function countnz(a)
n = 0
@inbounds for i in eachindex(a)
ai = a[i]
n = ifelse(ai == zero(ai), n, n + 1)
end
return n
end

0 comments on commit 984188a

Please sign in to comment.