Skip to content

Commit 412dffa

Browse files
committed
Change indexin sentinel to nothing
1 parent 8aca8ce commit 412dffa

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

base/array.jl

+9-6
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ indmin(a) = findmin(a)[2]
21642164
21652165
Return an array containing the highest index in `b` for
21662166
each value in `a` that is a member of `b`. The output
2167-
array contains 0 wherever `a` is not a member of `b`.
2167+
array contains `nothing` wherever `a` is not a member of `b`.
21682168
21692169
# Examples
21702170
```jldoctest
@@ -2173,24 +2173,27 @@ julia> a = ['a', 'b', 'c', 'b', 'd', 'a']
21732173
julia> b = ['a', 'b', 'c']
21742174
21752175
julia> indexin(a, b)
2176-
6-element Array{Int64,1}:
2176+
6-element Array{Union{Nothing, Int64},1}:
21772177
1
21782178
2
21792179
3
21802180
2
2181-
0
2181+
nothing
21822182
1
21832183
21842184
julia> indexin(b, a)
2185-
3-element Array{Int64,1}:
2185+
3-element Array{Union{Nothing, Int64},1}:
21862186
6
21872187
4
21882188
3
21892189
```
21902190
"""
21912191
function indexin(a, b::AbstractArray)
2192-
bdict = Dict(zip(b, 1:length(b)))
2193-
map(i -> get(bdict, i, 0), a)
2192+
indexes = keys(b)
2193+
bdict = Dict(zip(b, indexes))
2194+
return Union{eltype(indexes), Nothing}[
2195+
get(bdict, i, nothing) for i in a
2196+
]
21942197
end
21952198

21962199
function _findin(a, b)

test/arrayops.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -1394,13 +1394,13 @@ end
13941394
@test i7197() == (2,2)
13951395

13961396
# PR #8622 and general indexin tests
1397-
@test indexin([1,3,5,7], [5,4,3]) == [0,3,1,0]
1398-
@test indexin([1 3; 5 7], [5 4; 3 2]) == [0 2; 1 0]
1399-
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [0,3,4,0]
1400-
@test indexin(6, [1,3,6,6,2]) == 4
1397+
@test indexin([1,3,5,7], [5,4,3]) == [nothing,3,1,nothing]
1398+
@test indexin([1 3; 5 7], [5 4; 3 2]) == [nothing CartesianIndex(2, 1); CartesianIndex(1, 1) nothing]
1399+
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [nothing,3,4,nothing]
1400+
@test indexin(6, [1,3,6,6,2]) == fill(4, ())
14011401
@test indexin([6], [1,3,6,6,2]) == [4]
1402-
@test indexin(3, 2:5) == 2
1403-
@test indexin(3.0, 2:5) == 2
1402+
@test indexin([3], 2:5) == [2]
1403+
@test indexin([3.0], 2:5) == [2]
14041404

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

0 commit comments

Comments
 (0)