-
Notifications
You must be signed in to change notification settings - Fork 27
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 function #108
Comments
It seems the second argument of help?> searchsorted
search: searchsorted searchsortedlast searchsortedfirst
searchsorted(a, x; by=<transform>, lt=<comparison>, rev=false)
Return the range of indices of a which compare as equal to x (using binary search) according to the order specified by the by, lt and rev keywords,
assuming that a is already sorted in that order. Return an empty range located at the insertion point if a does not contain values equal to x.
See also: insorted, searchsortedfirst, sort, findall.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> searchsorted([1, 2, 4, 5, 5, 7], 4) # single match
3:3
julia> searchsorted([1, 2, 4, 5, 5, 7], 5) # multiple matches
4:5
julia> searchsorted([1, 2, 4, 5, 5, 7], 3) # no match, insert in the middle
3:2
julia> searchsorted([1, 2, 4, 5, 5, 7], 9) # no match, insert at end
7:6
julia> searchsorted([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
1:0 |
Sure, naming and interface can be different. For example:
My main point is that this is not a trivial function to implement correctly for users. So, wouldn't it make sense to add to the package? |
The name Just curious, but under what circumstances would the function |
I don't really see how this I needed such a |
Sorry for my confusing comment. I was totally misunderstanding. ( I think adding the function
I think the following error should be avoided with an empty range, just like julia> searchsorted_interval([1,2,3,4,4],1..3)
1:3
julia> searchsorted_interval([1,2,3,4,4],3..1)
ERROR: ArgumentError: Infimum not defined for empty intervals
Stacktrace:
[1] infimum
@ ~/.julia/dev/IntervalSets/src/IntervalSets.jl:83 [inlined]
[2] minimum
@ ~/.julia/dev/IntervalSets/src/IntervalSets.jl:172 [inlined]
[3] searchsorted_interval(arr::Vector{Int64}, int::ClosedInterval{Int64})
@ Main ./REPL[3]:1
[4] top-level scope
@ REPL[18]:1 This can be fixed by replacing julia> searchsorted_interval(arr, int::Interval{:closed, :closed}) = searchsortedfirst(arr, leftendpoint(int)):searchsortedlast(arr, rightendpoint(int))
searchsorted_interval (generic function with 1 method)
julia> searchsorted_interval([1,2,3,4,4],3..1)
3:2
julia> searchsorted_interval([1,2,3,4,4],1..3)
1:3 |
I'm looking for a way to find elements in a sorted array that fall into a given interval. There's nothing ready-made in IntervalSets, so here's my solution attempt:
These functions seem correct from a bit of manual testing, but I'm not sure if I missed some cases somewhere.
Would it be useful to add these methods into IntervalSets itself?
The text was updated successfully, but these errors were encountered: