Skip to content

Commit

Permalink
use pairs in findmin and findmax, supporting all indexable coll…
Browse files Browse the repository at this point in the history
…ections
  • Loading branch information
JeffBezanson committed Jul 21, 2017
1 parent 349592f commit 51be6ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 12 additions & 12 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1717,12 +1717,12 @@ function findmax(a)
if isempty(a)
throw(ArgumentError("collection must be non-empty"))
end
s = start(a)
mi = i = 1
m, s = next(a, s)
while !done(a, s)
ai, s = next(a, s)
i += 1
p = pairs(a)
s = start(p)
(mi, m), s = next(p, s)
i = mi
while !done(p, s)
(i, ai), s = next(p, s)
if ai > m || m!=m
m = ai
mi = i
Expand Down Expand Up @@ -1756,12 +1756,12 @@ function findmin(a)
if isempty(a)
throw(ArgumentError("collection must be non-empty"))
end
s = start(a)
mi = i = 1
m, s = next(a, s)
while !done(a, s)
ai, s = next(a, s)
i += 1
p = pairs(a)
s = start(p)
(mi, m), s = next(p, s)
i = mi
while !done(p, s)
(i, ai), s = next(p, s)
if ai < m || m!=m
m = ai
mi = i
Expand Down
5 changes: 5 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,11 @@ s, si = findmax(S)
@test a == b == s
@test ai == bi == si

for X in (A, B, S)
@test findmin(X) == findmin(Dict(pairs(X)))
@test findmax(X) == findmax(Dict(pairs(X)))
end

fill!(B, 2)
@test all(x->x==2, B)

Expand Down

0 comments on commit 51be6ac

Please sign in to comment.