Skip to content

Commit 9ffa695

Browse files
stev47c42f
authored andcommitted
add in method (#646)
This change makes `in` unroll the loop (compared to `Base.in`) and is significantly faster.
1 parent 41728ad commit 9ffa695

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/mapreduce.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ end
203203
@inline any(a::StaticArray{<:Tuple,Bool}; dims=:) = reduce(|, a; dims=dims, init=false) # (benchmarking needed)
204204
@inline any(f::Function, a::StaticArray; dims=:) = mapreduce(x->f(x)::Bool, |, a; dims=dims, init=false) # (benchmarking needed)
205205

206+
@inline Base.in(x, a::StaticArray) = mapreduce(==(x), |, a, init=false)
207+
206208
_mean_denom(a, dims::Colon) = length(a)
207209
_mean_denom(a, dims::Int) = size(a, dims)
208210
_mean_denom(a, ::Val{D}) where {D} = size(a, D)

test/mapreduce.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ using Statistics: mean
8383
@test any(sb, dims=Val(2)) === RSArray2(any(b, dims=2))
8484
@test any(x->x>0, sa, dims=Val(2)) === RSArray2(any(x->x>0, a, dims=2))
8585

86+
@test all(in(x, sa) for x in sa)
87+
@test all(in(x, sa) === in(x, a) for x in randn(10))
88+
8689
@test mean(sa) === mean(a)
8790
@test mean(abs2, sa) === mean(abs2, a)
8891
@test mean(sa, dims=Val(2)) === RSArray2(mean(a, dims=2))

0 commit comments

Comments
 (0)