Skip to content

Commit ff8b17d

Browse files
committed
Deprecate findin(a, b) in favor of find(occursin(b), a)
1 parent 6ca43fc commit ff8b17d

12 files changed

+56
-90
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,8 @@ Deprecated or removed
906906

907907
* `ismatch(regex, str)` has been deprecated in favor of `contains(str, regex)` ([#24673]).
908908

909+
* `findin(a, b)` has been deprecated in favor of `find(occursin(b), a)` ([#24673]).
910+
909911
Command-line option changes
910912
---------------------------
911913

base/abstractarray.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ get(A::AbstractArray, I::Dims, default) = checkbounds(Bool, A, I...) ? A[I...] :
10551055

10561056
function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T
10571057
# 1d is not linear indexing
1058-
ind = findin(I, indices1(A))
1058+
ind = find(occursin(indices1(A)), I)
10591059
X[ind] = A[I[ind]]
10601060
Xind = indices1(X)
10611061
X[first(Xind):first(ind)-1] = default
@@ -1064,7 +1064,7 @@ function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{AbstractRange,Ab
10641064
end
10651065
function get!(X::AbstractArray{T}, A::AbstractArray, I::Union{AbstractRange,AbstractVector{Int}}, default::T) where T
10661066
# Linear indexing
1067-
ind = findin(I, 1:length(A))
1067+
ind = find(occursin(1:length(A)), I)
10681068
X[ind] = A[I[ind]]
10691069
X[1:first(ind)-1] = default
10701070
X[last(ind)+1:length(X)] = default

base/array.jl

+11-36
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ end
17991799

18001800
find(x::Bool) = x ? [1] : Vector{Int}()
18011801
find(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]
1802+
find(p::OccursIn, x::Number) = x in p.x ? Vector{Int}() : [1]
18021803

18031804
"""
18041805
findnz(A)
@@ -2008,7 +2009,7 @@ function _findin(a, b)
20082009
ind
20092010
end
20102011

2011-
# If two collections are already sorted, findin can be computed with
2012+
# If two collections are already sorted, _findin can be computed with
20122013
# a single traversal of the two collections. This is much faster than
20132014
# using a hash table (although it has the same complexity).
20142015
function _sortedfindin(v, w)
@@ -2050,42 +2051,16 @@ function _sortedfindin(v, w)
20502051
return out
20512052
end
20522053

2053-
"""
2054-
findin(a, b)
2055-
2056-
Return the indices of elements in collection `a` that appear in collection `b`.
2057-
2058-
# Examples
2059-
```jldoctest
2060-
julia> a = collect(1:3:15)
2061-
5-element Array{Int64,1}:
2062-
1
2063-
4
2064-
7
2065-
10
2066-
13
2067-
2068-
julia> b = collect(2:4:10)
2069-
3-element Array{Int64,1}:
2070-
2
2071-
6
2072-
10
2073-
2074-
julia> findin(a,b) # 10 is the only common element
2075-
1-element Array{Int64,1}:
2076-
4
2077-
```
2078-
"""
2079-
function findin(a::Array{<:Real}, b::Union{Array{<:Real},Real})
2080-
if issorted(a, Sort.Forward) && issorted(b, Sort.Forward)
2081-
return _sortedfindin(a, b)
2054+
function find(pred::OccursIn{<:Union{Array{<:Real},Real}}, x::Array{<:Real})
2055+
if issorted(x, Sort.Forward) && issorted(pred.x, Sort.Forward)
2056+
return _sortedfindin(x, pred.x)
20822057
else
2083-
return _findin(a, b)
2058+
return _findin(x, pred.x)
20842059
end
20852060
end
20862061
# issorted fails for some element types so the method above has to be restricted
20872062
# to element with isless/< defined.
2088-
findin(a, b) = _findin(a, b)
2063+
find(pred::OccursIn, x::Union{AbstractArray, Tuple}) = _findin(x, pred.x)
20892064

20902065
# Copying subregions
20912066
function indcopy(sz::Dims, I::Vector)
@@ -2094,8 +2069,8 @@ function indcopy(sz::Dims, I::Vector)
20942069
for i = n+1:length(sz)
20952070
s *= sz[i]
20962071
end
2097-
dst = eltype(I)[findin(I[i], i < n ? (1:sz[i]) : (1:s)) for i = 1:n]
2098-
src = eltype(I)[I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))] for i = 1:n]
2072+
dst = eltype(I)[_findin(I[i], i < n ? (1:sz[i]) : (1:s)) for i = 1:n]
2073+
src = eltype(I)[I[i][_findin(I[i], i < n ? (1:sz[i]) : (1:s))] for i = 1:n]
20992074
dst, src
21002075
end
21012076

@@ -2105,8 +2080,8 @@ function indcopy(sz::Dims, I::Tuple{Vararg{RangeIndex}})
21052080
for i = n+1:length(sz)
21062081
s *= sz[i]
21072082
end
2108-
dst::typeof(I) = ntuple(i-> findin(I[i], i < n ? (1:sz[i]) : (1:s)), n)::typeof(I)
2109-
src::typeof(I) = ntuple(i-> I[i][findin(I[i], i < n ? (1:sz[i]) : (1:s))], n)::typeof(I)
2083+
dst::typeof(I) = ntuple(i-> _findin(I[i], i < n ? (1:sz[i]) : (1:s)), n)::typeof(I)
2084+
src::typeof(I) = ntuple(i-> I[i][_findin(I[i], i < n ? (1:sz[i]) : (1:s))], n)::typeof(I)
21102085
dst, src
21112086
end
21122087

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -3862,6 +3862,8 @@ end
38623862

38633863
@deprecate ismatch(r::Regex, s::AbstractString) contains(s, r)
38643864

3865+
@deprecate findin(a, b) find(occursin(b), a)
3866+
38653867
# END 0.7 deprecations
38663868
# BEGIN 1.0 deprecations
38673869

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ export
494494
find,
495495
findfirst,
496496
findlast,
497-
findin,
498497
findmax,
499498
findmin,
500499
findmin!,

base/range.jl

+2-12
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function intersect(r1::AbstractRange, r2::AbstractRange, r3::AbstractRange, r::A
705705
i
706706
end
707707

708-
# findin (the index of intersection)
708+
# _findin (the index of intersection)
709709
function _findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer})
710710
local ifirst
711711
local ilast
@@ -724,17 +724,7 @@ function _findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer}
724724
ifirst = fr >= fspan ? 1 : length(r)+1
725725
ilast = fr <= lspan ? length(r) : 0
726726
end
727-
ifirst, ilast
728-
end
729-
730-
function findin(r::AbstractUnitRange{<:Integer}, span::AbstractUnitRange{<:Integer})
731-
ifirst, ilast = _findin(r, span)
732-
ifirst:ilast
733-
end
734-
735-
function findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer})
736-
ifirst, ilast = _findin(r, span)
737-
ifirst:1:ilast
727+
r isa AbstractUnitRange ? (ifirst:ilast) : (ifirst:1:ilast)
738728
end
739729

740730
## linear operations on ranges ##

base/sparse/sparsematrix.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,8 @@ function find(p::Function, S::SparseMatrixCSC)
12761276
I, J = _findn(p, S)
12771277
return Base._sub2ind(sz, I, J)
12781278
end
1279+
find(p::Base.OccursIn, x::SparseMatrixCSC) =
1280+
invoke(find, Tuple{Base.OccursIn, AbstractArray}, p, x)
12791281

12801282
findn(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = _findn(x->true, S)
12811283

base/sparse/sparsevector.jl

+2
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ function find(p::Function, x::SparseVector{<:Any,Ti}) where Ti
705705

706706
return I
707707
end
708+
find(p::Base.OccursIn, x::SparseVector{<:Any,Ti}) where {Ti} =
709+
invoke(find, Tuple{Base.OccursIn, AbstractArray}, p, x)
708710

709711
function findnz(x::SparseVector{Tv,Ti}) where {Tv,Ti}
710712
numnz = nnz(x)

doc/src/stdlib/collections.md

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ Fully implemented by:
7777
Base.in
7878
Base.eltype
7979
Base.indexin
80-
Base.findin
8180
Base.unique
8281
Base.unique!
8382
Base.allunique

stdlib/Dates/test/ranges.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let
2626
@test_throws ArgumentError minimum(dr)
2727
@test_throws ArgumentError maximum(dr)
2828
@test_throws BoundsError dr[1]
29-
@test findin(dr, dr) == Int64[]
29+
@test find(occursin(dr), dr) == Int64[]
3030
@test [dr;] == T[]
3131
@test isempty(reverse(dr))
3232
@test length(reverse(dr)) == 0
@@ -58,7 +58,7 @@ let
5858
if len < 10000
5959
dr1 = [i for i in dr]
6060
@test length(dr1) == len
61-
@test findin(dr, dr) == [1:len;]
61+
@test find(occursin(dr), dr) == [1:len;]
6262
@test length([dr;]) == len
6363
@test dr == dr1
6464
@test hash(dr) == hash(dr1)
@@ -84,7 +84,7 @@ let
8484
@test_throws ArgumentError minimum(dr)
8585
@test_throws ArgumentError maximum(dr)
8686
@test_throws BoundsError dr[1]
87-
@test findin(dr, dr) == Int64[]
87+
@test find(occursin(dr), dr) == Int64[]
8888
@test [dr;] == T[]
8989
@test isempty(reverse(dr))
9090
@test length(reverse(dr)) == 0
@@ -116,7 +116,7 @@ let
116116
if len < 10000
117117
dr1 = [i for i in dr]
118118
@test length(dr1) == len
119-
@test findin(dr, dr) == [1:len;]
119+
@test find(occursin(dr), dr) == [1:len;]
120120
@test length([dr;]) == len
121121
@test dr == dr1
122122
@test hash(dr) == hash(dr1)
@@ -144,7 +144,7 @@ let
144144
@test_throws ArgumentError minimum(dr)
145145
@test_throws ArgumentError maximum(dr)
146146
@test_throws BoundsError dr[1]
147-
@test findin(dr, dr) == Int64[]
147+
@test find(occursin(dr), dr) == Int64[]
148148
@test [dr;] == T[]
149149
@test isempty(reverse(dr))
150150
@test length(reverse(dr)) == 0
@@ -176,7 +176,7 @@ let
176176
if len < 10000
177177
dr1 = [i for i in dr]
178178
@test length(dr1) == len
179-
@test findin(dr, dr) == [1:len;]
179+
@test find(occursin(dr), dr) == [1:len;]
180180
@test length([dr;]) == len
181181
@test dr == dr1
182182
@test hash(dr) == hash(dr1)
@@ -202,7 +202,7 @@ let
202202
@test_throws ArgumentError minimum(dr)
203203
@test_throws ArgumentError maximum(dr)
204204
@test_throws BoundsError dr[1]
205-
@test findin(dr, dr) == Int64[]
205+
@test find(occursin(dr), dr) == Int64[]
206206
@test [dr;] == T[]
207207
@test isempty(reverse(dr))
208208
@test length(reverse(dr)) == 0
@@ -234,7 +234,7 @@ let
234234
if len < 10000
235235
dr1 = [i for i in dr]
236236
@test length(dr1) == len
237-
@test findin(dr, dr) == [1:len;]
237+
@test find(occursin(dr), dr) == [1:len;]
238238
@test length([dr;]) == len
239239
@test dr == dr1
240240
@test hash(dr) == hash(dr1)
@@ -293,7 +293,7 @@ drs2 = map(x->Dates.Date(first(x)):step(x):Dates.Date(last(x)), drs)
293293
@test map(length, drs) == map(x->size(x)[1], drs)
294294
@test map(length, drs) == map(x->length(Dates.Date(first(x)):step(x):Dates.Date(last(x))), drs)
295295
@test map(length, drs) == map(x->length(reverse(x)), drs)
296-
@test all(x->findin(x, x)==[1:length(x);], drs[1:4])
296+
@test all(x->find(occursin(x), x)==[1:length(x);], drs[1:4])
297297
@test isempty(dr2)
298298
@test all(x->reverse(x) == range(last(x), -step(x), length(x)), drs)
299299
@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end])
@@ -371,7 +371,7 @@ drs = Any[dr, dr1, dr2, dr3, dr4, dr5, dr6, dr7, dr8, dr9, dr10,
371371
dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20]
372372

373373
@test map(length, drs) == map(x->size(x)[1], drs)
374-
@test all(x->findin(x, x) == [1:length(x);], drs[1:4])
374+
@test all(x->find(occursin(x), x) == [1:length(x);], drs[1:4])
375375
@test isempty(dr2)
376376
@test all(x->reverse(x) == last(x): - step(x):first(x), drs)
377377
@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end])
@@ -557,7 +557,7 @@ drs = Any[dr, dr1, dr2, dr3, dr8, dr9, dr10,
557557
dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20]
558558

559559
@test map(length, drs) == map(x->size(x)[1], drs)
560-
@test all(x->findin(x, x) == [1:length(x);], drs[1:4])
560+
@test all(x->find(occursin(x), x) == [1:length(x);], drs[1:4])
561561
@test isempty(dr2)
562562
@test all(x->reverse(x) == last(x): - step(x):first(x), drs)
563563
@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end])

test/arrayops.jl

+14-14
Original file line numberDiff line numberDiff line change
@@ -265,30 +265,30 @@ end
265265

266266
a = [3, 5, -7, 6]
267267
b = [4, 6, 2, -7, 1]
268-
ind = findin(a, b)
268+
ind = find(occursin(b), a)
269269
@test ind == [3,4]
270-
@test findin(a, Int[]) == Int[]
271-
@test findin(Int[], a) == Int[]
270+
@test find(occursin(Int[]), a) == Int[]
271+
@test find(occursin(a), Int[]) == Int[]
272272

273273
a = [1,2,3,4,5]
274274
b = [2,3,4,6]
275-
@test findin(a, b) == [2,3,4]
276-
@test findin(b, a) == [1,2,3]
277-
@test findin(a, Int[]) == Int[]
278-
@test findin(Int[], a) == Int[]
275+
@test find(occursin(b), a) == [2,3,4]
276+
@test find(occursin(a), b) == [1,2,3]
277+
@test find(occursin(Int[]), a) == Int[]
278+
@test find(occursin(a), Int[]) == Int[]
279279

280280
a = collect(1:3:15)
281281
b = collect(2:4:10)
282-
@test findin(a, b) == [4]
283-
@test findin([a[1:4]; a[4:end]], b) == [4,5]
282+
@test find(occursin(b), a) == [4]
283+
@test find(occursin(b), [a[1:4]; a[4:end]]) == [4,5]
284284

285-
@test findin([1.0, NaN, 2.0], NaN) == [2]
286-
@test findin([1.0, 2.0, NaN], NaN) == [3]
285+
@test find(occursin(NaN), [1.0, NaN, 2.0]) == [2]
286+
@test find(occursin(NaN), [1.0, 2.0, NaN]) == [3]
287287

288-
@testset "findin for uncomparable element types" begin
288+
@testset "find(::OccursIn, b) for uncomparable element types" begin
289289
a = [1 + 1im, 1 - 1im]
290-
@test findin(a, 1 + 1im) == [1]
291-
@test findin(a, a) == [1,2]
290+
@test find(occursin(1 + 1im), a) == [1]
291+
@test find(occursin(a), a) == [1,2]
292292
end
293293

294294
rt = Base.return_types(setindex!, Tuple{Array{Int32, 3}, UInt8, Vector{Int}, Int16, UnitRange{Int}})

test/ranges.jl

+8-13
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,15 @@ end
249249
@test length(0.0:-0.5) == 0
250250
@test length(1:2:0) == 0
251251
end
252-
@testset "findin" begin
253-
@test findin([5.2, 3.3], 3:20) == findin([5.2, 3.3], collect(3:20))
252+
@testset "find(::OccursIn, ::Array)" begin
253+
@test find(occursin(3:20), [5.2, 3.3]) == find(occursin(collect(3:20)), [5.2, 3.3])
254254

255255
let span = 5:20,
256256
r = -7:3:42
257-
@test findin(r, span) == 5:10
257+
@test find(occursin(span), r) == 5:10
258258
r = 15:-2:-38
259-
@test findin(r, span) == 1:6
259+
@test find(occursin(span), r) == 1:6
260260
end
261-
#@test isempty(findin(5+0*(1:6), 2:4))
262-
#@test findin(5+0*(1:6), 2:5) == 1:6
263-
#@test findin(5+0*(1:6), 2:7) == 1:6
264-
#@test findin(5+0*(1:6), 5:7) == 1:6
265-
#@test isempty(findin(5+0*(1:6), 6:7))
266-
#@test findin(5+0*(1:6), 5:5) == 1:6
267261
end
268262
@testset "reverse" begin
269263
@test reverse(reverse(1:10)) == 1:10
@@ -1115,15 +1109,16 @@ end
11151109
@test intersect(r, Base.OneTo(2)) == Base.OneTo(2)
11161110
@test intersect(r, 0:5) == 1:3
11171111
@test intersect(r, 2) === intersect(2, r) === 2:2
1118-
@test findin(r, r) === findin(r, 1:length(r)) === findin(1:length(r), r) === 1:length(r)
1112+
@test find(occursin(r), r) === find(occursin(1:length(r)), r) ===
1113+
find(occursin(r), 1:length(r)) === 1:length(r)
11191114
io = IOBuffer()
11201115
show(io, r)
11211116
str = String(take!(io))
11221117
@test str == "Base.OneTo(3)"
11231118
end
11241119
let r = Base.OneTo(7)
1125-
@test findin(r, 2:(length(r) - 1)) === 2:(length(r) - 1)
1126-
@test findin(2:(length(r) - 1), r) === 1:(length(r) - 2)
1120+
@test find(occursin(2:(length(r) - 1)), r) === 2:(length(r) - 1)
1121+
@test find(occursin(r), 2:(length(r) - 1)) === 1:(length(r) - 2)
11271122
end
11281123
end
11291124

0 commit comments

Comments
 (0)