Skip to content

Commit da3b862

Browse files
garrisonJeffBezanson
authored andcommitted
Change indexin sentinel to nothing (#25662)
* Make indexin first argument accept any iterable
1 parent d8233f9 commit da3b862

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

base/array.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,35 +2127,38 @@ argmin(a) = findmin(a)[2]
21272127
"""
21282128
indexin(a, b)
21292129
2130-
Return a vector containing the highest index in `b` for
2131-
each value in `a` that is a member of `b` . The output
2132-
vector contains 0 wherever `a` is not a member of `b`.
2130+
Return an array containing the highest index in `b` for
2131+
each value in `a` that is a member of `b`. The output
2132+
array contains `nothing` wherever `a` is not a member of `b`.
21332133
21342134
# Examples
21352135
```jldoctest
2136-
julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];
2136+
julia> a = ['a', 'b', 'c', 'b', 'd', 'a']
21372137
2138-
julia> b = ['a','b','c'];
2138+
julia> b = ['a', 'b', 'c']
21392139
2140-
julia> indexin(a,b)
2141-
6-element Array{Int64,1}:
2140+
julia> indexin(a, b)
2141+
6-element Array{Union{Nothing, Int64},1}:
21422142
1
21432143
2
21442144
3
21452145
2
2146-
0
2146+
nothing
21472147
1
21482148
2149-
julia> indexin(b,a)
2150-
3-element Array{Int64,1}:
2149+
julia> indexin(b, a)
2150+
3-element Array{Union{Nothing, Int64},1}:
21512151
6
21522152
4
21532153
3
21542154
```
21552155
"""
2156-
function indexin(a::AbstractArray, b::AbstractArray)
2157-
bdict = Dict(zip(b, 1:length(b)))
2158-
[get(bdict, i, 0) for i in a]
2156+
function indexin(a, b::AbstractArray)
2157+
indexes = keys(b)
2158+
bdict = Dict(zip(b, indexes))
2159+
return Union{eltype(indexes), Nothing}[
2160+
get(bdict, i, nothing) for i in a
2161+
]
21592162
end
21602163

21612164
function _findin(a, b)

test/arrayops.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,13 +1396,14 @@ function i7197()
13961396
end
13971397
@test i7197() == (2,2)
13981398

1399-
# PR #8622 and general indexin test
1400-
function pr8622()
1401-
x=[1,3,5,7]
1402-
y=[5,4,3]
1403-
return indexin(x,y)
1404-
end
1405-
@test pr8622() == [0,3,1,0]
1399+
# PR #8622 and general indexin tests
1400+
@test indexin([1,3,5,7], [5,4,3]) == [nothing,3,1,nothing]
1401+
@test indexin([1 3; 5 7], [5 4; 3 2]) == [nothing CartesianIndex(2, 1); CartesianIndex(1, 1) nothing]
1402+
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [nothing,3,4,nothing]
1403+
@test indexin(6, [1,3,6,6,2]) == fill(4, ())
1404+
@test indexin([6], [1,3,6,6,2]) == [4]
1405+
@test indexin([3], 2:5) == [2]
1406+
@test indexin([3.0], 2:5) == [2]
14061407

14071408
#6828 - size of specific dimensions
14081409
let a = Array{Float64}(uninitialized, 10)

0 commit comments

Comments
 (0)