From 51be6ac1dbf47e2cb601f657c5b3b59b66caa7cb Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 21 Jul 2017 16:46:43 -0400 Subject: [PATCH] use `pairs` in `findmin` and `findmax`, supporting all indexable collections --- base/array.jl | 24 ++++++++++++------------ test/arrayops.jl | 5 +++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/base/array.jl b/base/array.jl index bd5cda891903a..1e2d0469c3858 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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 @@ -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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 66715a1bd3308..9518e63974d47 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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)