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

Deprecate a couple more find(next|prev) methods, and remove ::Integer on start #25468

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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: 4 additions & 4 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ julia> findnext(A, 3)
0
```
"""
function findnext(A, start::Integer)
function findnext(A, start)
l = endof(A)
i = start
warned = false
Expand Down Expand Up @@ -1571,7 +1571,7 @@ julia> findnext(isodd, A, 2)
0
```
"""
function findnext(testf::Function, A, start::Integer)
function findnext(testf::Function, A, start)
l = endof(A)
i = start
while i <= l
Expand Down Expand Up @@ -1627,7 +1627,7 @@ julia> findprev(A,1)
0
```
"""
function findprev(A, start::Integer)
function findprev(A, start)
i = start
warned = false
while i >= 1
Expand Down Expand Up @@ -1686,7 +1686,7 @@ julia> findprev(isodd, A, 3)
2
```
"""
function findprev(testf::Function, A, start::Integer)
function findprev(testf::Function, A, start)
i = start
while i >= 1
testf(A[i]) && return i
Expand Down
26 changes: 5 additions & 21 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ function unsafe_bitfindnext(Bc::Vector{UInt64}, start::Integer)
end

# returns the index of the next non-zero element, or 0 if all zeros
function findnext(B::BitArray, start::Integer)
function findnext(B::BitArray, start)
start > 0 || throw(BoundsError(B, start))
start > length(B) && return 0
unsafe_bitfindnext(B.chunks, start)
Expand All @@ -1472,7 +1472,7 @@ end
#findfirst(B::BitArray) = findnext(B, 1) ## defined in array.jl

# aux function: same as findnext(~B, start), but performed without temporaries
function findnextnot(B::BitArray, start::Integer)
function findnextnot(B::BitArray, start)
start > 0 || throw(BoundsError(B, start))
start > length(B) && return 0

Expand Down Expand Up @@ -1503,16 +1503,8 @@ function findnextnot(B::BitArray, start::Integer)
end
findfirstnot(B::BitArray) = findnextnot(B,1)

# returns the index of the first matching element
function findnext(B::BitArray, v, start::Integer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we should keep these for efficiency, but change the signature to findnext(pred::EqualTo, B::BitArray, start). Then inside the function you need to do v = pred.x.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that mostly covered by this method? The extra code is O(1).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe. So you think there was no reason for the existence of these methods in the first place?

v == false && return findnextnot(B, start)
v == true && return findnext(B, start)
return 0
end
#findfirst(B::BitArray, v) = findnext(B, 1, v) ## defined in array.jl

# returns the index of the first element for which the function returns true
function findnext(testf::Function, B::BitArray, start::Integer)
function findnext(testf::Function, B::BitArray, start)
f0::Bool = testf(false)
f1::Bool = testf(true)
!f0 && f1 && return findnext(B, start)
Expand Down Expand Up @@ -1544,7 +1536,7 @@ function unsafe_bitfindprev(Bc::Vector{UInt64}, start::Integer)
end

# returns the index of the previous non-zero element, or 0 if all zeros
function findprev(B::BitArray, start::Integer)
function findprev(B::BitArray, start)
start > 0 || return 0
start > length(B) && throw(BoundsError(B, start))
unsafe_bitfindprev(B.chunks, start)
Expand Down Expand Up @@ -1574,16 +1566,8 @@ function findprevnot(B::BitArray, start::Integer)
end
findlastnot(B::BitArray) = findprevnot(B, length(B))

# returns the index of the previous matching element
function findprev(B::BitArray, v, start::Integer)
v == false && return findprevnot(B, start)
v == true && return findprev(B, start)
return 0
end
#findlast(B::BitArray, v) = findprev(B, 1, v) ## defined in array.jl

# returns the index of the previous element for which the function returns true
function findprev(testf::Function, B::BitArray, start::Integer)
function findprev(testf::Function, B::BitArray, start)
f0::Bool = testf(false)
f1::Bool = testf(true)
!f0 && f1 && return findprev(B, start)
Expand Down
12 changes: 7 additions & 5 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1269,11 +1269,13 @@ end
# (2) base/linalg/qr.jl
# (3) base/linalg/lq.jl

@deprecate find(x::Number) find(!iszero, x)
@deprecate findnext(A, v, i::Integer) findnext(equalto(v), A, i)
@deprecate findfirst(A, v) findfirst(equalto(v), A)
@deprecate findprev(A, v, i::Integer) findprev(equalto(v), A, i)
@deprecate findlast(A, v) findlast(equalto(v), A)
@deprecate find(x::Number) find(!iszero, x)
@deprecate findnext(A, v, i) findnext(equalto(v), A, i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably keep ::Integer for deprecations, else they are called incorrectly and make tests fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was what I was worried about. I'm abandoning the idea of providing a sentinel argument, so keeping this ::Integer isn't so bad anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that these are just deprecations, we don't need to support new features for them anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about deprecations causing ambiguities.

@deprecate findfirst(A, v) findfirst(equalto(v), A)
@deprecate findprev(A, v, i) findprev(equalto(v), A, i)
@deprecate findlast(A, v) findlast(equalto(v), A)
@deprecate findnext(A::BitArray, v, i) findnext(equalto(v), A, i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these two deprecations really needed, given that we have more general versions above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, though I added them to ensure (mostly) the same specificity we have now. Sort of like your argument above about not removing ::Integer from start.

@deprecate findprev(A::BitArray, v, i) findprev(equalto(v), A, i)
# also remove deprecation warnings in find* functions in array.jl, sparse/sparsematrix.jl,
# and sparse/sparsevector.jl.

Expand Down
4 changes: 2 additions & 2 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ matchall(re::Regex, str::SubString, overlap::Bool=false) =
matchall(re, String(str), overlap)

# TODO: return only start index and update deprecation
function findnext(re::Regex, str::Union{String,SubString}, idx::Integer)
function findnext(re::Regex, str::Union{String,SubString}, idx)
if idx > nextind(str,endof(str))
throw(BoundsError())
end
Expand All @@ -275,7 +275,7 @@ function findnext(re::Regex, str::Union{String,SubString}, idx::Integer)
PCRE.exec(re.regex, str, idx-1, opts, re.match_data) ?
((Int(re.ovec[1])+1):prevind(str,Int(re.ovec[2])+1)) : (0:-1)
end
findnext(r::Regex, s::AbstractString, idx::Integer) = throw(ArgumentError(
findnext(r::Regex, s::AbstractString, idx) = throw(ArgumentError(
"regex search is only available for the String type; use String(s) to convert"
))
findfirst(r::Regex, s::AbstractString) = findnext(r,s,start(s))
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ end
_sparse_findnextnz(v::AbstractSparseArray, i::Integer) = (I = find(!iszero, v); n = searchsortedfirst(I, i); n<=length(I) ? I[n] : zero(indtype(v)))
_sparse_findprevnz(v::AbstractSparseArray, i::Integer) = (I = find(!iszero, v); n = searchsortedlast(I, i); !iszero(n) ? I[n] : zero(indtype(v)))

function findnext(f::typeof(!iszero), v::AbstractSparseArray, i::Integer)
function findnext(f::typeof(!iszero), v::AbstractSparseArray, i)
j = _sparse_findnextnz(v, i)
while !iszero(j) && !f(v[j])
j = _sparse_findnextnz(v, j+1)
end
return j
end

function findprev(f::typeof(!iszero), v::AbstractSparseArray, i::Integer)
function findprev(f::typeof(!iszero), v::AbstractSparseArray, i)
j = _sparse_findprevnz(v, i)
while !iszero(j) && !f(v[j])
j = _sparse_findprevnz(v, j-1)
Expand Down
16 changes: 8 additions & 8 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function findnext(pred::EqualTo{Char}, s::String, i::Integer)
function findnext(pred::EqualTo{Char}, s::String, i)
if i < 1 || i > sizeof(s)
i == sizeof(s) + 1 && return 0
throw(BoundsError(s, i))
Expand All @@ -17,7 +17,7 @@ end

findfirst(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray) = _search(a, pred.x)

findnext(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray, i::Integer) =
findnext(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray, i) =
_search(a, pred.x, i)

function _search(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = 1)
Expand All @@ -41,7 +41,7 @@ function _search(a::ByteArray, b::Char, i::Integer = 1)
end
end

function findprev(pred::EqualTo{Char}, s::String, i::Integer)
function findprev(pred::EqualTo{Char}, s::String, i)
c = pred.x
c ≤ '\x7f' && return _rsearch(s, c % UInt8, i)
b = first_utf8_byte(c)
Expand All @@ -54,7 +54,7 @@ end

findlast(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray) = _rsearch(a, pred.x)

findprev(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray, i::Integer) =
findprev(pred::EqualTo{<:Union{Int8,UInt8}}, a::ByteArray, i) =
_rsearch(a, pred.x, i)

function _rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = sizeof(a))
Expand Down Expand Up @@ -98,7 +98,7 @@ findfirst(pattern::AbstractString, string::AbstractString) =
findnext(pattern, string, start(string))

# AbstractString implementation of the generic findnext interface
function findnext(testf::Function, s::AbstractString, i::Integer)
function findnext(testf::Function, s::AbstractString, i)
z = ncodeunits(s) + 1
1 ≤ i ≤ z || throw(BoundsError(s, i))
@inbounds i == z || isvalid(s, i) || string_index_err(s, i)
Expand Down Expand Up @@ -260,7 +260,7 @@ julia> findnext("Julia", "JuliaLang", 2)
1:5
```
"""
findnext(t::AbstractString, s::AbstractString, i::Integer) = _search(s, t, i)
findnext(t::AbstractString, s::AbstractString, i) = _search(s, t, i)

"""
findlast(pattern::AbstractString, string::AbstractString)
Expand All @@ -282,7 +282,7 @@ findlast(pattern::AbstractString, string::AbstractString) =
findprev(pattern, string, endof(string))

# AbstractString implementation of the generic findprev interface
function findprev(testf::Function, s::AbstractString, i::Integer)
function findprev(testf::Function, s::AbstractString, i)
if i < 1
return i == 0 ? 0 : throw(BoundsError(s, i))
end
Expand Down Expand Up @@ -445,7 +445,7 @@ julia> findprev("Julia", "JuliaLang", 6)
1:5
```
"""
findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i)
findprev(t::AbstractString, s::AbstractString, i) = _rsearch(s, t, i)

"""
contains(haystack::AbstractString, needle::Union{AbstractString,Regex,Char})
Expand Down