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 Aug 24, 2017
1 parent 918612a commit 25915e7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
24 changes: 12 additions & 12 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2068,12 +2068,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)
ai != ai && continue # assume x != x => x is a NaN
if m != m || isless(m, ai)
m = ai
Expand Down Expand Up @@ -2108,12 +2108,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)
ai != ai && continue
if m != m || isless(ai, m)
m = ai
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1905,8 +1905,8 @@ end

findmin(A::SparseMatrixCSC{Tv,Ti}, region) where {Tv,Ti} = @_findr(<, A, region, Tv, Ti)
findmax(A::SparseMatrixCSC{Tv,Ti}, region) where {Tv,Ti} = @_findr(>, A, region, Tv, Ti)
findmin(A::SparseMatrixCSC) = (r=findmin(A,(1,2)); (r[1][1], r[2][1]))
findmax(A::SparseMatrixCSC) = (r=findmax(A,(1,2)); (r[1][1], r[2][1]))
findmin(A::SparseMatrixCSC) = (r=findmin(A,(1,2)); (r[1][1], CartesianIndex(ind2sub(A, r[2][1]))))
findmax(A::SparseMatrixCSC) = (r=findmax(A,(1,2)); (r[1][1], CartesianIndex(ind2sub(A, r[2][1]))))

indmin(A::SparseMatrixCSC) = findmin(A)[2]
indmax(A::SparseMatrixCSC) = findmax(A)[2]
Expand Down
7 changes: 6 additions & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ end
@test indmin(5:-2:1) == 3

#23094
@test findmax(Set(["abc"])) === ("abc", 1)
@test_throws MethodError findmax(Set(["abc"]))
@test findmin(["abc", "a"]) === ("a", 2)
@test_throws MethodError findmax([Set([1]), Set([2])])
@test findmin([0.0, -0.0]) === (-0.0, 2)
Expand Down Expand Up @@ -1811,6 +1811,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
4 changes: 2 additions & 2 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,8 @@ end

S = spzeros(10,8)
A = Array(S)
@test indmax(S) == indmax(A) == 1
@test indmin(S) == indmin(A) == 1
@test indmax(S) == indmax(A) == CartesianIndex(1,1)
@test indmin(S) == indmin(A) == CartesianIndex(1,1)

A = Array{Int}(0,0)
S = sparse(A)
Expand Down

0 comments on commit 25915e7

Please sign in to comment.