Skip to content

Commit 8deab51

Browse files
committed
Change sentinel in find(first|next|prev|last) to nothing
1 parent 64c3fbd commit 8deab51

37 files changed

+220
-215
lines changed

base/array.jl

+29-27
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
14951495
"""
14961496
findnext(A, i::Integer)
14971497
1498-
Find the next linear index >= `i` of a `true` element of `A`, or `0` if not found.
1498+
Find the next linear index >= `i` of a `true` element of `A`, or `nothing` if not found.
14991499
15001500
# Examples
15011501
```jldoctest
@@ -1508,7 +1508,6 @@ julia> findnext(A, 1)
15081508
2
15091509
15101510
julia> findnext(A, 3)
1511-
0
15121511
```
15131512
"""
15141513
function findnext(A, start::Integer)
@@ -1526,14 +1525,14 @@ function findnext(A, start::Integer)
15261525
end
15271526
i = nextind(A, i)
15281527
end
1529-
return 0
1528+
return nothing
15301529
end
15311530

15321531
"""
15331532
findfirst(A)
15341533
15351534
Return the linear index of the first `true` value in `A`.
1536-
Return `0` if no such value is found.
1535+
Return `nothing` if no such value is found.
15371536
To search for other kinds of values, pass a predicate as the first argument.
15381537
15391538
# Examples
@@ -1546,16 +1545,17 @@ julia> A = [false false; true false]
15461545
julia> findfirst(A)
15471546
2
15481547
1549-
julia> findfirst(falses(3))
1550-
0
1548+
julia> findfirst(falses(3)) == nothing
1549+
true
15511550
```
15521551
"""
15531552
findfirst(A) = findnext(A, 1)
15541553

15551554
"""
15561555
findnext(predicate::Function, A, i::Integer)
15571556
1558-
Find the next linear index >= `i` of an element of `A` for which `predicate` returns `true`, or `0` if not found.
1557+
Find the next linear index >= `i` of an element of `A` for which `predicate` returns `true`,
1558+
or `nothing` if not found.
15591559
15601560
# Examples
15611561
```jldoctest
@@ -1567,8 +1567,8 @@ julia> A = [1 4; 2 2]
15671567
julia> findnext(isodd, A, 1)
15681568
1
15691569
1570-
julia> findnext(isodd, A, 2)
1571-
0
1570+
julia> findnext(isodd, A, 2) == nothing
1571+
true
15721572
```
15731573
"""
15741574
function findnext(testf::Function, A, start::Integer)
@@ -1580,14 +1580,14 @@ function findnext(testf::Function, A, start::Integer)
15801580
end
15811581
i = nextind(A, i)
15821582
end
1583-
return 0
1583+
return nothing
15841584
end
15851585

15861586
"""
15871587
findfirst(predicate::Function, A)
15881588
15891589
Return the linear index of the first element of `A` for which `predicate` returns `true`.
1590-
Return `0` if there is no such element.
1590+
Return `nothing` if there is no such element.
15911591
15921592
# Examples
15931593
```jldoctest
@@ -1599,8 +1599,8 @@ julia> A = [1 4; 2 2]
15991599
julia> findfirst(iseven, A)
16001600
2
16011601
1602-
julia> findfirst(x -> x>10, A)
1603-
0
1602+
julia> findfirst(x -> x>10, A) == nothing
1603+
true
16041604
16051605
julia> findfirst(equalto(4), A)
16061606
3
@@ -1611,7 +1611,7 @@ findfirst(testf::Function, A) = findnext(testf, A, 1)
16111611
"""
16121612
findprev(A, i::Integer)
16131613
1614-
Find the previous linear index <= `i` of a `true` element of `A`, or `0` if not found.
1614+
Find the previous linear index <= `i` of a `true` element of `A`, or `nothing` if not found.
16151615
16161616
# Examples
16171617
```jldoctest
@@ -1623,8 +1623,8 @@ julia> A = [false false; true true]
16231623
julia> findprev(A,2)
16241624
2
16251625
1626-
julia> findprev(A,1)
1627-
0
1626+
julia> findprev(A,1) == nothing
1627+
true
16281628
```
16291629
"""
16301630
function findprev(A, start::Integer)
@@ -1639,14 +1639,14 @@ function findprev(A, start::Integer)
16391639
a != 0 && return i
16401640
i = prevind(A, i)
16411641
end
1642-
return 0
1642+
return nothing
16431643
end
16441644

16451645
"""
16461646
findlast(A)
16471647
16481648
Return the linear index of the last `true` value in `A`.
1649-
Return `0` if there is no `true` value in `A`.
1649+
Return `nothing` if there is no `true` value in `A`.
16501650
16511651
# Examples
16521652
```jldoctest
@@ -1660,8 +1660,8 @@ julia> findlast(A)
16601660
16611661
julia> A = falses(2,2);
16621662
1663-
julia> findlast(A)
1664-
0
1663+
julia> findlast(A) == nothing
1664+
true
16651665
```
16661666
"""
16671667
findlast(A) = findprev(A, endof(A))
@@ -1670,7 +1670,7 @@ findlast(A) = findprev(A, endof(A))
16701670
findprev(predicate::Function, A, i::Integer)
16711671
16721672
Find the previous linear index <= `i` of an element of `A` for which `predicate` returns `true`, or
1673-
`0` if not found.
1673+
`nothing` if not found.
16741674
16751675
# Examples
16761676
```jldoctest
@@ -1679,8 +1679,8 @@ julia> A = [4 6; 1 2]
16791679
4 6
16801680
1 2
16811681
1682-
julia> findprev(isodd, A, 1)
1683-
0
1682+
julia> findprev(isodd, A, 1) == nothing
1683+
true
16841684
16851685
julia> findprev(isodd, A, 3)
16861686
2
@@ -1692,14 +1692,14 @@ function findprev(testf::Function, A, start::Integer)
16921692
testf(A[i]) && return i
16931693
i = prevind(A, i)
16941694
end
1695-
return 0
1695+
return nothing
16961696
end
16971697

16981698
"""
16991699
findlast(predicate::Function, A)
17001700
17011701
Return the linear index of the last element of `A` for which `predicate` returns `true`.
1702-
Return `0` if there is no such element.
1702+
Return `nothing` if there is no such element.
17031703
17041704
# Examples
17051705
```jldoctest
@@ -1711,12 +1711,14 @@ julia> A = [1 2; 3 4]
17111711
julia> findlast(isodd, A)
17121712
2
17131713
1714-
julia> findlast(x -> x > 5, A)
1715-
0
1714+
julia> findlast(x -> x > 5, A) == nothing
1715+
true
17161716
```
17171717
"""
17181718
findlast(testf::Function, A) = findprev(testf, A, endof(A))
17191719

1720+
nothing_sentinel(i) = i == 0 ? nothing : i
1721+
17201722
"""
17211723
find(f::Function, A)
17221724

base/bitarray.jl

+17-17
Original file line numberDiff line numberDiff line change
@@ -1459,13 +1459,13 @@ function unsafe_bitfindnext(Bc::Vector{UInt64}, start::Integer)
14591459
end
14601460
end
14611461
end
1462-
return 0
1462+
return nothing
14631463
end
14641464

1465-
# returns the index of the next non-zero element, or 0 if all zeros
1465+
# returns the index of the next true element, or nothing if all false
14661466
function findnext(B::BitArray, start::Integer)
14671467
start > 0 || throw(BoundsError(B, start))
1468-
start > length(B) && return 0
1468+
start > length(B) && return nothing
14691469
unsafe_bitfindnext(B.chunks, start)
14701470
end
14711471

@@ -1474,11 +1474,11 @@ end
14741474
# aux function: same as findnext(~B, start), but performed without temporaries
14751475
function findnextnot(B::BitArray, start::Integer)
14761476
start > 0 || throw(BoundsError(B, start))
1477-
start > length(B) && return 0
1477+
start > length(B) && return nothing
14781478

14791479
Bc = B.chunks
14801480
l = length(Bc)
1481-
l == 0 && return 0
1481+
l == 0 && return nothing
14821482

14831483
chunk_start = _div64(start-1)+1
14841484
within_chunk_start = _mod64(start-1)
@@ -1499,7 +1499,7 @@ function findnextnot(B::BitArray, start::Integer)
14991499
elseif Bc[l] | mask != _msk_end(B)
15001500
return (l-1) << 6 + trailing_ones(Bc[l] | mask) + 1
15011501
end
1502-
return 0
1502+
return nothing
15031503
end
15041504
findfirstnot(B::BitArray) = findnextnot(B,1)
15051505

@@ -1508,7 +1508,7 @@ function findnext(pred::EqualTo, B::BitArray, start::Integer)
15081508
v = pred.x
15091509
v == false && return findnextnot(B, start)
15101510
v == true && return findnext(B, start)
1511-
return 0
1511+
return nothing
15121512
end
15131513
#findfirst(B::BitArray, v) = findnext(B, 1, v) ## defined in array.jl
15141514

@@ -1520,9 +1520,9 @@ function findnext(testf::Function, B::BitArray, start::Integer)
15201520
f0 && !f1 && return findnextnot(B, start)
15211521

15221522
start > 0 || throw(BoundsError(B, start))
1523-
start > length(B) && return 0
1523+
start > length(B) && return nothing
15241524
f0 && f1 && return Int(start)
1525-
return 0 # last case: !f0 && !f1
1525+
return nothing # last case: !f0 && !f1
15261526
end
15271527
#findfirst(testf::Function, B::BitArray) = findnext(testf, B, 1) ## defined in array.jl
15281528

@@ -1541,18 +1541,18 @@ function unsafe_bitfindprev(Bc::Vector{UInt64}, start::Integer)
15411541
end
15421542
end
15431543
end
1544-
return 0
1544+
return nothing
15451545
end
15461546

1547-
# returns the index of the previous non-zero element, or 0 if all zeros
1547+
# returns the index of the previous true element, or nothing if all false
15481548
function findprev(B::BitArray, start::Integer)
1549-
start > 0 || return 0
1549+
start > 0 || return nothing
15501550
start > length(B) && throw(BoundsError(B, start))
15511551
unsafe_bitfindprev(B.chunks, start)
15521552
end
15531553

15541554
function findprevnot(B::BitArray, start::Integer)
1555-
start > 0 || return 0
1555+
start > 0 || return nothing
15561556
start > length(B) && throw(BoundsError(B, start))
15571557

15581558
Bc = B.chunks
@@ -1571,7 +1571,7 @@ function findprevnot(B::BitArray, start::Integer)
15711571
end
15721572
end
15731573
end
1574-
return 0
1574+
return nothing
15751575
end
15761576
findlastnot(B::BitArray) = findprevnot(B, length(B))
15771577

@@ -1580,7 +1580,7 @@ function findprev(pred::EqualTo, B::BitArray, start::Integer)
15801580
v = pred.x
15811581
v == false && return findprevnot(B, start)
15821582
v == true && return findprev(B, start)
1583-
return 0
1583+
return nothing
15841584
end
15851585
#findlast(B::BitArray, v) = findprev(B, 1, v) ## defined in array.jl
15861586

@@ -1591,10 +1591,10 @@ function findprev(testf::Function, B::BitArray, start::Integer)
15911591
!f0 && f1 && return findprev(B, start)
15921592
f0 && !f1 && return findprevnot(B, start)
15931593

1594-
start > 0 || return 0
1594+
start > 0 || return nothing
15951595
start > length(B) && throw(BoundsError(B, start))
15961596
f0 && f1 && return Int(start)
1597-
return 0 # last case: !f0 && !f1
1597+
return nothing # last case: !f0 && !f1
15981598
end
15991599
#findlast(testf::Function, B::BitArray) = findprev(testf, B, 1) ## defined in array.jl
16001600

base/bitset.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ function _bits_findnext(b::Bits, start::Int)
6868
# start is 0-based
6969
# @assert start >= 0
7070
_div64(start) + 1 > length(b) && return -1
71-
unsafe_bitfindnext(b, start+1) - 1
71+
ind = unsafe_bitfindnext(b, start+1)
72+
ind === nothing ? -1 : ind - 1
7273
end
7374

7475
function _bits_findprev(b::Bits, start::Int)
7576
# start is 0-based
7677
# @assert start <= 64 * length(b) - 1
7778
start >= 0 || return -1
78-
unsafe_bitfindprev(b, start+1) - 1
79+
ind = unsafe_bitfindprev(b, start+1)
80+
ind === nothing ? -1 : ind - 1
7981
end
8082

8183
# An internal function for setting the inclusion bit for a given integer

base/client.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function display_error(io::IO, er, bt)
149149
print_with_color(Base.error_color(), io, "ERROR: "; bold = true)
150150
# remove REPL-related frames from interactive printing
151151
eval_ind = findlast(addr->Base.REPL.ip_matches_func(addr, :eval), bt)
152-
if eval_ind != 0
152+
if eval_ind !== nothing
153153
bt = bt[1:eval_ind-1]
154154
end
155155
showerror(IOContext(io, :limit => true), er, bt)

base/combinatorics.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function permute!!(a, p::AbstractVector{<:Integer})
7575
count = 0
7676
start = 0
7777
while count < length(a)
78-
ptr = start = findnext(!iszero, p, start+1)
78+
ptr = start = findnext(!iszero, p, start+1)::Int
7979
temp = a[start]
8080
next = p[start]
8181
count += 1
@@ -125,7 +125,7 @@ function invpermute!!(a, p::AbstractVector{<:Integer})
125125
count = 0
126126
start = 0
127127
while count < length(a)
128-
start = findnext(!iszero, p, start+1)
128+
start = findnext(!iszero, p, start+1)::Int
129129
temp = a[start]
130130
next = p[start]
131131
count += 1

base/event.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function ensure_rescheduled(othertask::Task)
231231
# also need to return it to the runnable state
232232
# before throwing an error
233233
i = findfirst(t->t===ct, Workqueue)
234-
i == 0 || deleteat!(Workqueue, i)
234+
i === nothing || deleteat!(Workqueue, i)
235235
ct.state = :runnable
236236
end
237237
nothing

base/file.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function tempname(temppath::AbstractString,uunique::UInt32)
271271
tempp = cwstring(temppath)
272272
tname = Vector{UInt16}(uninitialized, 32767)
273273
uunique = ccall(:GetTempFileNameW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32,Ptr{UInt16}), tempp,temp_prefix,uunique,tname)
274-
lentname = findfirst(iszero,tname)-1
274+
lentname = coalesce(findfirst(iszero,tname), 0)-1
275275
if uunique == 0 || lentname <= 0
276276
error("GetTempFileName failed: $(Libc.FormatMessage())")
277277
end

base/filesystem.jl

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Base:
4343
nb_available, position, read, read!, readavailable, seek, seekend, show,
4444
skip, stat, unsafe_read, unsafe_write, transcode, uv_error, uvhandle,
4545
uvtype, write
46+
using Base: coalesce
4647

4748
if Sys.iswindows()
4849
import Base: cwstring

0 commit comments

Comments
 (0)