-
-
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
Make searchsorted*/findnext/findprev return values of keytype #32978
Changes from 12 commits
e392400
5558113
831f607
2f5ef4a
d820a59
f55346e
d3aa626
ee5c898
c666306
61abe1d
57dd806
1e4db52
62d3586
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ using .Base: copymutable, LinearIndices, length, (:), | |
AbstractVector, @inbounds, AbstractRange, @eval, @inline, Vector, @noinline, | ||
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !, | ||
extrema, sub_with_overflow, add_with_overflow, oneunit, div, getindex, setindex!, | ||
length, resize!, fill, Missing, require_one_based_indexing | ||
length, resize!, fill, Missing, require_one_based_indexing, keytype | ||
|
||
using .Base: >>>, !== | ||
|
||
|
@@ -174,7 +174,7 @@ midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...) | |
|
||
# index of the first value of vector a that is greater than or equal to x; | ||
# returns length(v)+1 if x is greater than all values in v. | ||
function searchsortedfirst(v::AbstractVector, x, lo::T, hi::T, o::Ordering) where T<:Integer | ||
function searchsortedfirst(v::AbstractVector, x, lo::T, hi::T, o::Ordering)::keytype(v) where T<:Integer | ||
u = T(1) | ||
lo = lo - u | ||
hi = hi + u | ||
|
@@ -191,7 +191,7 @@ end | |
|
||
# index of the last value of vector a that is less than or equal to x; | ||
# returns 0 if x is less than all values of v. | ||
function searchsortedlast(v::AbstractVector, x, lo::T, hi::T, o::Ordering) where T<:Integer | ||
function searchsortedlast(v::AbstractVector, x, lo::T, hi::T, o::Ordering)::keytype(v) where T<:Integer | ||
u = T(1) | ||
lo = lo - u | ||
hi = hi + u | ||
|
@@ -209,7 +209,7 @@ end | |
# returns the range of indices of v equal to x | ||
# if v does not contain x, returns a 0-length range | ||
# indicating the insertion point of x | ||
function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering) where T<:Integer | ||
function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering)::UnitRange{keytype(v)} where T<:Integer | ||
u = T(1) | ||
lo = ilo - u | ||
hi = ihi + u | ||
|
@@ -228,7 +228,7 @@ function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering) where T | |
return (lo + 1) : (hi - 1) | ||
end | ||
|
||
function searchsortedlast(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering) | ||
function searchsortedlast(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
if step(a) == 0 | ||
lt(o, x, first(a)) ? 0 : length(a) | ||
|
@@ -238,7 +238,7 @@ function searchsortedlast(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering) | |
end | ||
end | ||
|
||
function searchsortedfirst(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering) | ||
function searchsortedfirst(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
if step(a) == 0 | ||
lt(o, first(a), x) ? length(a) + 1 : 1 | ||
|
@@ -248,7 +248,7 @@ function searchsortedfirst(a::AbstractRange{<:Real}, x::Real, o::DirectOrdering) | |
end | ||
end | ||
|
||
function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering) | ||
function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
h = step(a) | ||
if h == 0 | ||
|
@@ -270,7 +270,7 @@ function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderin | |
end | ||
end | ||
|
||
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering) | ||
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
h = step(a) | ||
if h == 0 | ||
|
@@ -285,14 +285,14 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderi | |
lastindex(a) + 1 | ||
else | ||
if o isa ForwardOrdering | ||
-fld(floor(Integer, -x) + first(a), h) + 1 | ||
-fld(floor(Integer, -x) + Signed(first(a)), h) + 1 | ||
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. So the 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. No, it is for the case when 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. Ah right I was off as |
||
else | ||
-fld(ceil(Integer, -x) + first(a), h) + 1 | ||
-fld(ceil(Integer, -x) + Signed(first(a)), h) + 1 | ||
end | ||
end | ||
end | ||
|
||
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering) | ||
function searchsortedfirst(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
if lt(o, first(a), x) | ||
if step(a) == 0 | ||
|
@@ -305,7 +305,7 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOr | |
end | ||
end | ||
|
||
function searchsortedlast(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering) | ||
function searchsortedlast(a::AbstractRange{<:Integer}, x::Unsigned, o::DirectOrdering)::keytype(a) | ||
require_one_based_indexing(a) | ||
if lt(o, x, first(a)) | ||
0 | ||
|
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.
unsafe_bitfindnext
(andprev
, too) accepts astart::Integer
. I was about to make a comment that we should just move this there and/or restrict its signature, but then I realized it's also used byBitSet
.BitSet
demands anInt64
result, whereas its use forBitArray
demands anInt
result. So this seems like the right way to go about it. 👍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.
Maybe it changed, but my impression is that
BitSet
callsunsafe_bitfindnext
by feeding it anInt
, not anInt64
, so I would also favor restrictingunsafe_bitfindnext
toInt
input, as this is an internal function. But the PR is also fine as is!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 have now restricted
unsafe_bitfindnext
toInt
.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.
Ok, sorry for the nitpick, but please change also
unsafe_bitfindprev
accordingly too :)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.
Done :)
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.
That was fast!