diff --git a/src/chainedvector.jl b/src/chainedvector.jl index c85f4b5..a5a1cb5 100644 --- a/src/chainedvector.jl +++ b/src/chainedvector.jl @@ -825,8 +825,9 @@ function findXwithfirst(comp, f, x, y, i) for A in x.arrays for y′ in A y′′ = f(y′) - y = ifelse(comp(y′′, y), y′′, y) - i = ifelse(comp(y′′, y), i′, i) + c = comp(y′′, y) # store this before y changes + y = ifelse(c, y′′, y) + i = ifelse(c, i′, i) i′ += 1 end end @@ -835,8 +836,8 @@ end Base.findmax(x::ChainedVector) = findmax(identity, x) Base.findmin(x::ChainedVector) = findmin(identity, x) -Base.argmax(x::ChainedVector) = findmax(identity, x)[1] -Base.argmin(x::ChainedVector) = findmin(identity, x)[1] +Base.argmax(x::ChainedVector) = findmax(identity, x)[2] +Base.argmin(x::ChainedVector) = findmin(identity, x)[2] Base.argmax(f::F, x::ChainedVector) where {F} = x[findmax(f, x)[2]] Base.argmin(f::F, x::ChainedVector) where {F} = x[findmin(f, x)[2]] diff --git a/test/chainedvector.jl b/test/chainedvector.jl index ef7cbfd..3aeaf97 100644 --- a/test/chainedvector.jl +++ b/test/chainedvector.jl @@ -284,6 +284,17 @@ @test all(==(2), x) @test length(x) == 5 + # https://github.com/JuliaData/SentinelArrays.jl/issues/97 + x = ChainedVector([[18, 70, 92, 15, 65], [25, 14, 95, 54, 57]]) + @test findmax(x) == (95, 8) + @test findmin(x) == (14, 7) + @test argmax(x) == 8 + @test argmin(x) == 7 + @test findmax(inv, x) == (inv(14), 7) + @test findmin(inv, x) == (inv(95), 8) + @test argmax(inv, x) == 14 + @test argmin(inv, x) == 95 + x = ChainedVector(Vector{Float64}[]) @test !any(x -> iseven(x), x) @test !any(map(x -> iseven(x), x))