Skip to content

add lower_bound and upper_bound to slice to complement binary search #59301

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

Closed
matiu2 opened this issue Mar 19, 2019 · 4 comments
Closed

add lower_bound and upper_bound to slice to complement binary search #59301

matiu2 opened this issue Mar 19, 2019 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@matiu2
Copy link

matiu2 commented Mar 19, 2019

In the case where we do a binary search on an iterator and have a lot of keys the same, I'd like to find the first key and the last key, rather than a random key (what binary_search currently returns).

In c++ they have upper_bound and lower_bound, that will return the first and last of a block of duplicate keys.

Could we add these to slice too please ?

  • upper_bound
  • upper_bound_by_key
  • upper_bound_by
  • upper_bound_by_cached_key

(and all that again for lower bound)

@jonas-schievink jonas-schievink added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Mar 19, 2019
@ExpHP
Copy link
Contributor

ExpHP commented Mar 20, 2019

And here is where it's unfortunate that binary_search returns Result<usize, usize> rather than some kind of expression object BinarySearch<'a, T> that could provide lower_bound() and upper_bound() methods.

@ExpHP
Copy link
Contributor

ExpHP commented Mar 20, 2019

A note: I think upper_bound should return the index after the last match.

@scottmcm
Copy link
Member

This looks like a match for rust-lang/rfcs#2184, so I'm going to close in favour of that.

@AngelicosPhosphoros
Copy link
Contributor

You can easily emulate this behaviour using binary_search_by: https://stackoverflow.com/a/75790348/8195987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants