Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find(in(b), a) to return cartesian indices for matrix a #30226

Merged
merged 3 commits into from
Dec 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -20,6 +20,14 @@ Language changes
to the `Core` module ([#29968]).
* Using the same name for both a local variable and a static parameter is now an error instead
of a warning ([#29429]).
* `findall(in(b), a)` now returns a `CartesianIndex` when `a` is a matrix or a higher-dimensional array,
for consistency with other `findall` methods. Use `LinearIndices(a)[findall(in(b), a)]` to get
the old behavior, or `CartesianIndices(a)[findall(in(b), a)]` to get the new behavior
on previous Julia versions ([#30226]).
* `findmin(::BitArray)` and `findmax(::BitArray)` now return a `CartesianIndex` when `a` is a matrix
or a higher-dimensional array, for consistency with for other array types.
Use `LinearIndices(a)[findmin(a)[2]]` to get the old behavior, or `CartesianIndices(a)[findmin(a)[2]]`
to get the new behavior on previous Julia versions ([#30102]).
* Method signatures such as
`f(::Type{T}, ::T) where {T <: X}` and
`f(::Type{X}, ::Any)`
10 changes: 5 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
@@ -2195,10 +2195,10 @@ function indexin(a, b::AbstractArray)
]
end

function _findin(a, b)
ind = Int[]
function _findin(a::Union{AbstractArray, Tuple}, b)
ind = Vector{eltype(keys(a))}()
bset = Set(b)
@inbounds for (i,ai) in enumerate(a)
@inbounds for (i,ai) in pairs(a)
ai in bset && push!(ind, i)
end
ind
@@ -2207,8 +2207,8 @@ end
# If two collections are already sorted, _findin can be computed with
# a single traversal of the two collections. This is much faster than
# using a hash table (although it has the same complexity).
function _sortedfindin(v, w)
viter, witer = eachindex(v), eachindex(w)
function _sortedfindin(v::Union{AbstractArray, Tuple}, w)
viter, witer = keys(v), eachindex(w)
out = eltype(viter)[]
vy, wy = iterate(viter), iterate(witer)
if vy === nothing || wy === nothing
18 changes: 13 additions & 5 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
@@ -343,20 +343,27 @@ end
@test B == [0 23 1 24 0; 11 12 13 14 15; 0 21 3 22 0; 0 7 7 0 0]

@test isequal(reshape(reshape(1:27, 3, 3, 3), Val(2))[1,:], [1, 4, 7, 10, 13, 16, 19, 22, 25])

end
@testset "find(in(b), a)" begin
# unsorted inputs
a = [3, 5, -7, 6]
b = [4, 6, 2, -7, 1]
ind = findall(in(b), a)
@test ind == [3,4]
@test findall(in(b), a) == [3,4]
@test findall(in(Int[]), a) == Int[]
@test findall(in(a), Int[]) == Int[]
@test findall(in(b), reshape(a, 2, 2)) == [CartesianIndex(1, 2), CartesianIndex(2, 2)]

a = [1,2,3,4,5]
# sorted inputs
a = [1,2,3,4,5,10]
b = [2,3,4,6]
@test findall(in(b), a) == [2,3,4]
@test findall(in(a), b) == [1,2,3]
@test findall(in(Int[]), a) == Int[]
@test findall(in(a), Int[]) == Int[]
@test findall(in(b), reshape(a, 3, 2)) ==
[CartesianIndex(2, 1), CartesianIndex(3, 1), CartesianIndex(1, 2)]
@test findall(in(a), reshape(b, 2, 2)) ==
[CartesianIndex(1, 1), CartesianIndex(2, 1), CartesianIndex(1, 2)]

a = Vector(1:3:15)
b = Vector(2:4:10)
@@ -374,7 +381,8 @@ end

@test findall(in([1, 2]), 2) == [1]
@test findall(in([1, 2]), 3) == []

end
@testset "setindex! return type" begin
rt = Base.return_types(setindex!, Tuple{Array{Int32, 3}, Vector{UInt8}, Vector{Int}, Int16, UnitRange{Int}})
@test length(rt) == 1 && rt[1] === Array{Int32, 3}
end