-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
require explicit predicates in find
functions
#23812
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1599,14 +1599,14 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x | |
""" | ||
findnext(A, i::Integer) | ||
|
||
Find the next linear index >= `i` of a non-zero element of `A`, or `0` if not found. | ||
Find the next linear index >= `i` of a `true` element of `A`, or `0` if not found. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [0 0; 1 0] | ||
2×2 Array{Int64,2}: | ||
0 0 | ||
1 0 | ||
julia> A = [false false; true false] | ||
2×2 Array{Bool,2}: | ||
false false | ||
true false | ||
|
||
julia> findnext(A,1) | ||
2 | ||
|
@@ -1618,8 +1618,14 @@ julia> findnext(A,3) | |
function findnext(A, start::Integer) | ||
l = endof(A) | ||
i = start | ||
warned = false | ||
while i <= l | ||
if A[i] != 0 | ||
a = A[i] | ||
if !warned && !(a isa Bool) | ||
depwarn("In the future `findnext` will only work on boolean collections. Use `findnext(x->x!=0, A)` instead.", :findnext) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I've done things like this I've often added breadcrumbs that point to it from deprecated.jl that help make sure they get removed at the appropriate timeframe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should
Similarly for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, it should. |
||
warned = true | ||
end | ||
if a != 0 | ||
return i | ||
end | ||
i = nextind(A, i) | ||
|
@@ -1630,8 +1636,9 @@ end | |
""" | ||
findfirst(A) | ||
|
||
Return the linear index of the first non-zero value in `A` (determined by `A[i]!=0`). | ||
Return the linear index of the first `true` value in `A`. | ||
Returns `0` if no such value is found. | ||
To search for other kinds of values, pass a predicate as the first argument. | ||
|
||
# Examples | ||
```jldoctest | ||
|
@@ -1649,58 +1656,6 @@ julia> findfirst(zeros(3)) | |
""" | ||
findfirst(A) = findnext(A, 1) | ||
|
||
""" | ||
findnext(A, v, i::Integer) | ||
|
||
Find the next linear index >= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [1 4; 2 2] | ||
2×2 Array{Int64,2}: | ||
1 4 | ||
2 2 | ||
|
||
julia> findnext(A,4,4) | ||
0 | ||
|
||
julia> findnext(A,4,3) | ||
3 | ||
``` | ||
""" | ||
function findnext(A, v, start::Integer) | ||
l = endof(A) | ||
i = start | ||
while i <= l | ||
if A[i] == v | ||
return i | ||
end | ||
i = nextind(A, i) | ||
end | ||
return 0 | ||
end | ||
""" | ||
findfirst(A, v) | ||
|
||
Return the linear index of the first element equal to `v` in `A`. | ||
Returns `0` if `v` is not found. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [4 6; 2 2] | ||
2×2 Array{Int64,2}: | ||
4 6 | ||
2 2 | ||
|
||
julia> findfirst(A,2) | ||
2 | ||
|
||
julia> findfirst(A,3) | ||
0 | ||
``` | ||
""" | ||
findfirst(A, v) = findnext(A, v, 1) | ||
|
||
""" | ||
findnext(predicate::Function, A, i::Integer) | ||
|
||
|
@@ -1750,21 +1705,24 @@ julia> findfirst(iseven, A) | |
|
||
julia> findfirst(x -> x>10, A) | ||
0 | ||
|
||
julia> findfirst(equalto(4), A) | ||
3 | ||
``` | ||
""" | ||
findfirst(testf::Function, A) = findnext(testf, A, 1) | ||
|
||
""" | ||
findprev(A, i::Integer) | ||
|
||
Find the previous linear index <= `i` of a non-zero element of `A`, or `0` if not found. | ||
Find the previous linear index <= `i` of a `true` element of `A`, or `0` if not found. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [0 0; 1 2] | ||
2×2 Array{Int64,2}: | ||
0 0 | ||
1 2 | ||
julia> A = [false false; true true] | ||
2×2 Array{Bool,2}: | ||
false false | ||
true true | ||
|
||
julia> findprev(A,2) | ||
2 | ||
|
@@ -1775,8 +1733,14 @@ julia> findprev(A,1) | |
""" | ||
function findprev(A, start::Integer) | ||
i = start | ||
warned = false | ||
while i >= 1 | ||
A[i] != 0 && return i | ||
a = A[i] | ||
if !warned && !(a isa Bool) | ||
depwarn("In the future `findprev` will only work on boolean collections. Use `findprev(x->x!=0, A)` instead.", :findprev) | ||
warned = true | ||
end | ||
a != 0 && return i | ||
i = prevind(A, i) | ||
end | ||
return 0 | ||
|
@@ -1785,8 +1749,8 @@ end | |
""" | ||
findlast(A) | ||
|
||
Return the linear index of the last non-zero value in `A` (determined by `A[i]!=0`). | ||
Returns `0` if there is no non-zero value in `A`. | ||
Return the linear index of the last `true` value in `A`. | ||
Returns `0` if there is no `true` value in `A`. | ||
|
||
# Examples | ||
```jldoctest | ||
|
@@ -1809,59 +1773,6 @@ julia> findlast(A) | |
""" | ||
findlast(A) = findprev(A, endof(A)) | ||
|
||
""" | ||
findprev(A, v, i::Integer) | ||
|
||
Find the previous linear index <= `i` of an element of `A` equal to `v` (using `==`), or `0` if not found. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [0 0; 1 2] | ||
2×2 Array{Int64,2}: | ||
0 0 | ||
1 2 | ||
|
||
julia> findprev(A, 1, 4) | ||
2 | ||
|
||
julia> findprev(A, 1, 1) | ||
0 | ||
``` | ||
""" | ||
function findprev(A, v, start::Integer) | ||
i = start | ||
while i >= 1 | ||
A[i] == v && return i | ||
i = prevind(A, i) | ||
end | ||
return 0 | ||
end | ||
|
||
""" | ||
findlast(A, v) | ||
|
||
Return the linear index of the last element equal to `v` in `A`. | ||
Returns `0` if there is no element of `A` equal to `v`. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> A = [1 2; 2 1] | ||
2×2 Array{Int64,2}: | ||
1 2 | ||
2 1 | ||
|
||
julia> findlast(A,1) | ||
4 | ||
|
||
julia> findlast(A,2) | ||
3 | ||
|
||
julia> findlast(A,3) | ||
0 | ||
``` | ||
""" | ||
findlast(A, v) = findprev(A, v, endof(A)) | ||
|
||
""" | ||
findprev(predicate::Function, A, i::Integer) | ||
|
||
|
@@ -1921,16 +1832,23 @@ If there are no such elements of `A`, find returns an empty array. | |
|
||
# Examples | ||
```jldoctest | ||
julia> A = [1 2; 3 4] | ||
2×2 Array{Int64,2}: | ||
1 2 | ||
3 4 | ||
julia> A = [1 2 0; 3 4 0] | ||
2×3 Array{Int64,2}: | ||
1 2 0 | ||
3 4 0 | ||
|
||
julia> find(isodd,A) | ||
julia> find(isodd, A) | ||
2-element Array{Int64,1}: | ||
1 | ||
2 | ||
|
||
julia> find(!iszero, A) | ||
4-element Array{Int64,1}: | ||
1 | ||
2 | ||
3 | ||
4 | ||
|
||
julia> find(isodd, [2, 4]) | ||
0-element Array{Int64,1} | ||
``` | ||
|
@@ -1955,9 +1873,8 @@ _index_remapper(iter) = OneTo(typemax(Int)) # safe for objects that don't imple | |
""" | ||
find(A) | ||
|
||
Return a vector of the linear indexes of the non-zeros in `A` (determined by `A[i]!=0`). A | ||
common use of this is to convert a boolean array to an array of indexes of the `true` | ||
elements. If there are no non-zero elements of `A`, `find` returns an empty array. | ||
Return a vector of the linear indices of the `true` values in `A`. | ||
To search for other kinds of values, pass a predicate as the first argument. | ||
|
||
# Examples | ||
```jldoctest | ||
|
@@ -1971,7 +1888,7 @@ julia> find(A) | |
1 | ||
4 | ||
|
||
julia> find(zeros(3)) | ||
julia> find(falses(3)) | ||
0-element Array{Int64,1} | ||
``` | ||
""" | ||
|
@@ -1980,7 +1897,12 @@ function find(A) | |
I = Vector{Int}(nnzA) | ||
cnt = 1 | ||
inds = _index_remapper(A) | ||
warned = false | ||
for (i,a) in enumerate(A) | ||
if !warned && !(a isa Bool) | ||
depwarn("In the future `find(A)` will only work on boolean collections. Use `find(x->x!=0, A)` instead.", :find) | ||
warned = true | ||
end | ||
if a != 0 | ||
I[cnt] = inds[i] | ||
cnt += 1 | ||
|
@@ -1989,7 +1911,7 @@ function find(A) | |
return I | ||
end | ||
|
||
find(x::Number) = x == 0 ? Array{Int,1}(0) : [1] | ||
find(x::Bool) = x ? [1] : Array{Int,1}(0) | ||
find(testf::Function, x::Number) = !testf(x) ? Array{Int,1}(0) : [1] | ||
|
||
findn(A::AbstractVector) = find(A) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -859,6 +859,7 @@ export | |
identity, | ||
isbits, | ||
isequal, | ||
equalto, | ||
isimmutable, | ||
isless, | ||
ifelse, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this warn everytime the function is called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was counting on
depwarn
for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this looks good to me — the
warned
makes sure the slowerdepwarn
is only called once for each invocation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry for the noise.