Skip to content

Commit 09719f5

Browse files
committed
by and lt arguments to nlargest/nsmallest
1 parent 503ba4d commit 09719f5

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/heaps.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,45 @@ function nextreme(ord::Base.Ordering, n::Int, arr::AbstractVector{T}) where T
129129
end
130130

131131
"""
132-
nlargest(n, arr)
132+
nlargest(n, arr; kw...)
133133
134134
Return the `n` largest elements of the array `arr`.
135135
136136
Equivalent to:
137-
sort(arr, order = Base.Reverse)[1:min(n, end)]
137+
sort(arr, kw..., rev=true)[1:min(n, end)]
138138
139139
Note that if `arr` contains floats and is free of NaN values,
140-
then the following alternative may be used to achieve 2x performance.
140+
then the following alternative may be used to achieve 2x performance:
141+
141142
DataStructures.nextreme(DataStructures.FasterReverse(), n, arr)
143+
142144
This faster version is equivalent to:
145+
143146
sort(arr, lt = >)[1:min(n, end)]
144147
"""
145-
function nlargest(n::Int, arr::AbstractVector)
146-
return nextreme(Base.Reverse, n, arr)
148+
function nlargest(n::Int, arr::AbstractVector; lt=isless, by=identity)
149+
order = Base.ReverseOrdering(Base.ord(lt, by, nothing))
150+
return nextreme(order, n, arr)
147151
end
148152

149153
"""
150-
nsmallest(n, arr)
154+
nsmallest(n, arr; kw...)
151155
152156
Return the `n` smallest elements of the array `arr`.
153157
154158
Equivalent to:
155-
sort(arr, order = Base.Forward)[1:min(n, end)]
159+
sort(arr; kw...)[1:min(n, end)]
156160
157161
Note that if `arr` contains floats and is free of NaN values,
158-
then the following alternative may be used to achieve 2x performance.
162+
then the following alternative may be used to achieve 2x performance:
163+
159164
DataStructures.nextreme(DataStructures.FasterForward(), n, arr)
165+
160166
This faster version is equivalent to:
167+
161168
sort(arr, lt = <)[1:min(n, end)]
162169
"""
163-
function nsmallest(n::Int, arr::AbstractVector)
164-
return nextreme(Base.Forward, n, arr)
170+
function nsmallest(n::Int, arr::AbstractVector; lt=isless, by=identity)
171+
order = Base.ord(lt, by, nothing)
172+
return nextreme(order, n, arr)
165173
end

test/test_binheap.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,17 @@
208208
102,-56,-17,-41,25,-30,-84,26,-84,48,49,-5,-38,28,
209209
114,-54,96,-55,67,74,127,-61,124,11,-7,93,-51,110,
210210
-106,-84,-90,-18,-12,-116,21,115,50]
211+
square = x -> x^2
212+
211213
for n = -1:length(ss) + 1
212-
@test sort(ss, lt = >)[1:min(n, end)] == nlargest(n, ss)
213-
@test sort(ss, lt = <)[1:min(n, end)] == nsmallest(n, ss)
214+
r = 1:min(n, length(ss))
215+
216+
@test nlargest(n, ss) == sort(ss, rev=true)[r]
217+
@test nsmallest(n, ss) == sort(ss)[r]
218+
219+
@test nlargest(n, ss, by=square) == sort(ss, by=square, rev=true)[r]
220+
@test nsmallest(n, ss, by=square) == sort(ss, by=square)[r]
221+
214222
@test nlargest(n, ss) == DataStructures.nextreme(DataStructures.FasterReverse(), n, ss)
215223
@test nsmallest(n, ss) == DataStructures.nextreme(DataStructures.FasterForward(), n, ss)
216224
end

0 commit comments

Comments
 (0)