Skip to content

Commit

Permalink
Fix documentation for searchsortedfirst/last w/ generalized indexing. (
Browse files Browse the repository at this point in the history
…#43128)

While the code now correctly handles generalized indexing,
documentation for `searchsortedfirst` and `searchsortedlast` still
assume that indices are `1:length(a)`. This PR fixes that.

Also add tests while at it. No behavior is changed.
  • Loading branch information
tpapp authored Nov 19, 2021
1 parent f5e0f9d commit 7afb3b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ julia> searchsorted([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
searchsortedfirst(a, x; by=<transform>, lt=<comparison>, rev=false)
Return the index of the first value in `a` greater than or equal to `x`, according to the
specified order. Return `length(a) + 1` if `x` is greater than all values in `a`.
specified order. Return `lastindex(a) + 1` if `x` is greater than all values in `a`.
`a` is assumed to be sorted.
See also: [`searchsortedlast`](@ref), [`searchsorted`](@ref), [`findfirst`](@ref).
Expand Down Expand Up @@ -360,8 +360,8 @@ julia> searchsortedfirst([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
searchsortedlast(a, x; by=<transform>, lt=<comparison>, rev=false)
Return the index of the last value in `a` less than or equal to `x`, according to the
specified order. Return `0` if `x` is less than all values in `a`. `a` is assumed to
be sorted.
specified order. Return `firstindex(a) - 1` if `x` is less than all values in `a`. `a` is
assumed to be sorted.
# Examples
```jldoctest
Expand Down
8 changes: 8 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -672,4 +672,12 @@ end
end
end

@testset "searchsortedfirst/last with generalized indexing" begin
o = OffsetVector(1:3, -2)
@test searchsortedfirst(o, 4) == lastindex(o) + 1
@test searchsortedfirst(o, 1.5) == 0
@test searchsortedlast(o, 0) == firstindex(o) - 1
@test searchsortedlast(o, 1.5) == -1
end

end

0 comments on commit 7afb3b4

Please sign in to comment.