From 280ad5551db5064f8041e52fdd20422001700154 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Wed, 1 Jan 2025 08:20:26 -0500 Subject: [PATCH] improve searchsorted_interval performance (#194) --- src/findall.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/findall.jl b/src/findall.jl index bd41604..b77dd82 100644 --- a/src/findall.jl +++ b/src/findall.jl @@ -66,14 +66,15 @@ julia> searchsorted_interval(Float64[], 1..3) ``` """ function searchsorted_interval(X, i::Interval{L, R}; rev=false) where {L, R} + ord = Base.Order.ord(<, identity, rev) if rev === true - _searchsorted_begin(X, rightendpoint(i), Val(R); rev):_searchsorted_end(X, leftendpoint(i), Val(L); rev) + _searchsorted_begin(X, rightendpoint(i), Val(R), ord):_searchsorted_end(X, leftendpoint(i), Val(L), ord) else - _searchsorted_begin(X, leftendpoint(i), Val(L); rev):_searchsorted_end(X, rightendpoint(i), Val(R); rev) + _searchsorted_begin(X, leftendpoint(i), Val(L), ord):_searchsorted_end(X, rightendpoint(i), Val(R), ord) end end -_searchsorted_begin(X, x, ::Val{:closed}; rev) = searchsortedfirst(X, x; rev, lt=<) -_searchsorted_begin(X, x, ::Val{:open}; rev) = searchsortedlast(X, x; rev, lt=<) + 1 - _searchsorted_end(X, x, ::Val{:closed}; rev) = searchsortedlast(X, x; rev, lt=<) - _searchsorted_end(X, x, ::Val{:open}; rev) = searchsortedfirst(X, x; rev, lt=<) - 1 +_searchsorted_begin(X, x, ::Val{:closed}, ord) = searchsortedfirst(X, x, ord) +_searchsorted_begin(X, x, ::Val{:open}, ord) = searchsortedlast(X, x, ord) + 1 + _searchsorted_end(X, x, ::Val{:closed}, ord) = searchsortedlast(X, x, ord) + _searchsorted_end(X, x, ::Val{:open}, ord) = searchsortedfirst(X, x, ord) - 1