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

searchsorted overrides, resolves #85 #86

Merged
merged 6 commits into from
Nov 29, 2019
Merged
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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ os:
- osx

julia:
- 0.7
- 1.0
- 1.1
- 1.3
- nightly
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved

notifications:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OffsetArrays"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
version = "0.11.1"
version = "0.11.2"

[compat]
julia = "0.7, 1"
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: 1.0
- julia_version: 1.1
- julia_version: 1.3
- julia_version: nightly

platform:
Expand Down
71 changes: 71 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,75 @@ end

no_offset_view(A::OffsetArray) = no_offset_view(parent(A))


####
# work around for segfault in searchsorted*
# https://github.com/JuliaLang/julia/issues/33977
####

function _safe_searchsorted(v::OffsetArray, x, ilo::T, ihi::T, o::Base.Ordering) where T<:Integer
u = T(1)
lo = ilo - u
hi = ihi + u
@inbounds while lo < hi - u
m = (lo + hi) ÷ 2
if Base.lt(o, v[m], x)
lo = m
elseif Base.lt(o, x, v[m])
hi = m
else
a = searchsortedfirst(v, x, max(lo,ilo), m, o)
b = searchsortedlast(v, x, m, min(hi,ihi), o)
return a : b
end
end
return (lo + 1) : (hi - 1)
end
function _safe_searchsortedfirst(v::OffsetArray, x, lo::T, hi::T, o::Base.Ordering) where T<:Integer
u = T(1)
lo = lo - u
hi = hi + u
@inbounds while lo < hi - u
m = (lo + hi) ÷ 2
if Base.lt(o, v[m], x)
lo = m
else
hi = m
end
end
return hi
end
function _safe_searchsortedlast(v::OffsetArray, x, lo::T, hi::T, o::Base.Ordering) where T<:Integer
u = T(1)
lo = lo - u
hi = hi + u
@inbounds while lo < hi - u
m = (lo + hi) ÷ 2
if Base.lt(o, x, v[m])
hi = m
else
lo = m
end
end
return lo
end

if VERSION ≤ v"1.2"
# ambiguity warnings in earlier versions
Base.searchsorted(v::OffsetArray, x, ilo::Int, ihi::Int, o::Base.Ordering) =
_safe_searchsorted(v, x, ilo, ihi, o)
Base.searchsortedfirst(v::OffsetArray, x, lo::Int, hi::Int, o::Base.Ordering) =
_safe_searchsortedfirst(v, x, lo, hi, o)
Base.searchsortedlast(v::OffsetArray, x, lo::Int, hi::Int, o::Base.Ordering) =
_safe_searchsortedlast(v, x, lo, hi, o)
end

Base.searchsorted(v::OffsetArray, x, ilo::T, ihi::T, o::Base.Ordering) where T<:Integer =
_safe_searchsorted(v, x, ilo, ihi, o)
Base.searchsortedfirst(v::OffsetArray, x, lo::T, hi::T, o::Base.Ordering) where T<:Integer =
_safe_searchsortedfirst(v, x, lo, hi, o)
Base.searchsortedlast(v::OffsetArray, x, lo::T, hi::T, o::Base.Ordering) where T<:Integer =
_safe_searchsortedlast(v, x, lo, hi, o)


end # module
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,22 @@ end
@test empty!(o) === o
@test axes(o, 1) == 0:-1
end

@testset "searchsorted (#85)" begin
o = OffsetVector([1,3,4,5],-2)
@test searchsortedfirst(o,-2) == -1
@test searchsortedfirst(o, 1) == -1
@test searchsortedfirst(o, 2) == 0
@test searchsortedfirst(o, 5) == 2
@test searchsortedfirst(o, 6) == 3
@test searchsortedlast(o, -2) == -2
@test searchsortedlast(o, 1) == -1
@test searchsortedlast(o, 2) == -1
@test searchsortedlast(o, 5) == 2
@test searchsortedlast(o, 6) == 2
@test searchsorted(o, -2) == -1:-2
@test searchsorted(o, 1) == -1:-1
@test searchsorted(o, 2) == 0:-1
@test searchsorted(o, 5) == 2:2
@test searchsorted(o, 6) == 3:2
end