Skip to content

Commit a9161d9

Browse files
committedNov 28, 2018
Fix findmin/findmax to return cartesian indices for BitMatrix
For consistency with other AbstractArray types. The call to keys is optimized out by the compiler for vectors thanks to @inbounds.
1 parent 40c2d89 commit a9161d9

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed
 

Diff for: ‎base/bitarray.jl

+5-6
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,6 @@ function findprev(testf::Function, B::BitArray, start::Integer)
15101510
end
15111511
#findlast(testf::Function, B::BitArray) = findprev(testf, B, 1) ## defined in array.jl
15121512

1513-
15141513
function findmax(a::BitArray)
15151514
isempty(a) && throw(ArgumentError("BitArray must be non-empty"))
15161515
m, mi = false, 1
@@ -1519,9 +1518,9 @@ function findmax(a::BitArray)
15191518
for i = 1:length(ac)
15201519
@inbounds k = trailing_zeros(ac[i])
15211520
ti += k
1522-
k == 64 || return (true, ti)
1521+
k == 64 || return (true, @inbounds keys(a)[ti])
15231522
end
1524-
return m, mi
1523+
return m, @inbounds keys(a)[mi]
15251524
end
15261525

15271526
function findmin(a::BitArray)
@@ -1532,13 +1531,13 @@ function findmin(a::BitArray)
15321531
for i = 1:length(ac)-1
15331532
@inbounds k = trailing_ones(ac[i])
15341533
ti += k
1535-
k == 64 || return (false, ti)
1534+
k == 64 || return (false, @inbounds keys(a)[ti])
15361535
end
15371536
l = Base._mod64(length(a)-1) + 1
15381537
@inbounds k = trailing_ones(ac[end] & Base._msk_end(l))
15391538
ti += k
1540-
k == l || return (false, ti)
1541-
return m, mi
1539+
k == l || return (false, @inbounds keys(a)[ti])
1540+
return (m, @inbounds keys(a)[mi])
15421541
end
15431542

15441543
# findall helper functions

Diff for: ‎test/arrayops.jl

+5
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ end
567567
@test isnan(findmax([NaN, NaN, 0.0/0.0])[1])
568568
@test findmax([NaN, NaN, 0.0/0.0])[2] == 1
569569

570+
# Check that cartesian indices are returned for matrices
571+
@test argmax([10 12; 9 11]) === CartesianIndex(1, 2)
572+
@test argmin([10 12; 9 11]) === CartesianIndex(2, 1)
573+
@test findmax([10 12; 9 11]) === (12, CartesianIndex(1, 2))
574+
@test findmin([10 12; 9 11]) === (9, CartesianIndex(2, 1))
570575
end
571576

572577
@testset "permutedims" begin

Diff for: ‎test/bitarray.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,8 @@ timesofar("linalg")
15231523
for b1 in [falses(v1), trues(v1),
15241524
BitArray([1,0,1,1,0]),
15251525
BitArray([0,0,1,1,0]),
1526+
BitArray([1 0; 1 1]),
1527+
BitArray([0 0; 1 1]),
15261528
bitrand(v1)]
15271529
@check_bit_operation findmin(b1)
15281530
@check_bit_operation findmax(b1)

0 commit comments

Comments
 (0)
Please sign in to comment.