Skip to content

Commit f41f1bc

Browse files
committed
use isless as default value for topk/perm
1 parent aabe6c7 commit f41f1bc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/stat/non_hp_stat.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,12 @@ Base.@propagate_inbounds function topk_perm(x::Union{Vector{T}, SubArray{T, N, V
590590
end
591591

592592
"""
593-
topk(x, k; rev = false, lt = <, by = identity, threads = false)
593+
topk(x, k; rev = false, lt = isless, by = identity, threads = false)
594594
595595
Return upto `k` largest nonmissing elements of `x`. When `rev = true` it returns upto `k` smallest nonmissing elements of `x`. When all elements are missing, the function returns `[missing]`. The `by` keyword lets you provide a function that will be applied to each element before comparison; the `lt` keyword allows providing a custom "less than" function (note that for every x and y, only one of `lt(x,y)` and `lt(y,x)` can return true)
596596
597+
> When it is safe, passing `lt = <` improves the performance.
598+
597599
Also see [`topkperm`](@ref), [`partialsort`](@ref)
598600
599601
# Example
@@ -625,7 +627,7 @@ julia> topk(x, 3, by = abs, rev = true)
625627
10
626628
```
627629
"""
628-
function topk(x::AbstractVector, k::Int; rev::Bool=false, lt=<, by=identity, threads=false)
630+
function topk(x::AbstractVector, k::Int; rev::Bool=false, lt=isless, by=identity, threads=false)
629631
isempty(x) && throw(ArgumentError("empty arrays are not allowed"))
630632
@assert firstindex(x) == 1 "topk only supports 1-based indexing"
631633
if threads && length(x) > Threads.nthreads()
@@ -643,10 +645,12 @@ function topk(x::AbstractVector, k::Int; rev::Bool=false, lt=<, by=identity, thr
643645
end
644646
end
645647
"""
646-
topkperm(x, k; rev = false, lt = <, by = identity, threads = false)
648+
topkperm(x, k; rev = false, lt = isless, by = identity, threads = false)
647649
648650
Return the indices of upto `k` largest nonmissing elements of `x`. When `rev = true` it returns the indices of upto `k` smallest nonmissing elements of `x`. When all elements are missing, the function returns `[missing]`. The `by` keyword lets you provide a function that will be applied to each element before comparison; the `lt` keyword allows providing a custom "less than" function (note that for every x and y, only one of `lt(x,y)` and `lt(y,x)` can return true)
649651
652+
> When it is safe, passing `lt = <` improves the performance.
653+
650654
Also see [`topk`](@ref), [`partialsortperm`](@ref)
651655
652656
# Examples
@@ -693,7 +697,7 @@ julia> topkperm(x, 10, threads = true, rev = true)
693697
648
694698
```
695699
"""
696-
function topkperm(x::AbstractVector, k::Int; rev::Bool=false, lt=<, by=identity, threads=false)
700+
function topkperm(x::AbstractVector, k::Int; rev::Bool=false, lt=isless, by=identity, threads=false)
697701
isempty(x) && throw(ArgumentError("empty arrays are not allowed"))
698702
@assert firstindex(x) == 1 "topkperm only supports 1-based indexing"
699703
if threads && length(x) > Threads.nthreads()

test/stats.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ using Random,PooledArrays,CategoricalArrays
124124
x=CategoricalArray(rand(1000))
125125
# TODO categorical array is not thread safe - fortunately, it throws Errors - however, in future we may need to fix it
126126
@test_throws UndefRefError topk(x,10,lt=isless,threads=true)
127+
128+
@test isequal(topk([NaN,NaN,NaN,3],2,rev=true), [3.0, NaN])
129+
@test isequal(topk([NaN,NaN,NaN,3],2,rev=false), [NaN, NaN])
130+
@test isequal(topk([missing, NaN,NaN,NaN,3, missing],2,rev=true), [3.0, NaN])
131+
@test isequal(topk([NaN,missing, missing, missing,NaN,NaN,3, missing],2,rev=false), [NaN, NaN])
127132
end

0 commit comments

Comments
 (0)