Skip to content

Commit

Permalink
searchsorted overrides, resolves #85 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty authored and fredrikekre committed Nov 29, 2019
1 parent 0a68fac commit d50463f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
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

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

2 comments on commit d50463f

@fredrikekre
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/6012

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.2 -m "<description of version>" d50463f9149dbe238a64be7cf8a58d904f662166
git push origin v0.11.2

Please sign in to comment.