Skip to content

Commit

Permalink
Change indexin sentinel to nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
garrison committed Jan 28, 2018
1 parent 8aca8ce commit 412dffa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 9 additions & 6 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ indmin(a) = findmin(a)[2]
Return an array containing the highest index in `b` for
each value in `a` that is a member of `b`. The output
array contains 0 wherever `a` is not a member of `b`.
array contains `nothing` wherever `a` is not a member of `b`.
# Examples
```jldoctest
Expand All @@ -2173,24 +2173,27 @@ julia> a = ['a', 'b', 'c', 'b', 'd', 'a']
julia> b = ['a', 'b', 'c']
julia> indexin(a, b)
6-element Array{Int64,1}:
6-element Array{Union{Nothing, Int64},1}:
1
2
3
2
0
nothing
1
julia> indexin(b, a)
3-element Array{Int64,1}:
3-element Array{Union{Nothing, Int64},1}:
6
4
3
```
"""
function indexin(a, b::AbstractArray)
bdict = Dict(zip(b, 1:length(b)))
map(i -> get(bdict, i, 0), a)
indexes = keys(b)
bdict = Dict(zip(b, indexes))
return Union{eltype(indexes), Nothing}[
get(bdict, i, nothing) for i in a
]
end

function _findin(a, b)
Expand Down
12 changes: 6 additions & 6 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1394,13 +1394,13 @@ end
@test i7197() == (2,2)

# PR #8622 and general indexin tests
@test indexin([1,3,5,7], [5,4,3]) == [0,3,1,0]
@test indexin([1 3; 5 7], [5 4; 3 2]) == [0 2; 1 0]
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [0,3,4,0]
@test indexin(6, [1,3,6,6,2]) == 4
@test indexin([1,3,5,7], [5,4,3]) == [nothing,3,1,nothing]
@test indexin([1 3; 5 7], [5 4; 3 2]) == [nothing CartesianIndex(2, 1); CartesianIndex(1, 1) nothing]
@test indexin((2 * x + 1 for x in 0:3), [5,4,3,5,6]) == [nothing,3,4,nothing]
@test indexin(6, [1,3,6,6,2]) == fill(4, ())
@test indexin([6], [1,3,6,6,2]) == [4]
@test indexin(3, 2:5) == 2
@test indexin(3.0, 2:5) == 2
@test indexin([3], 2:5) == [2]
@test indexin([3.0], 2:5) == [2]

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

0 comments on commit 412dffa

Please sign in to comment.