# Pre-RFC: More general binary search

**URL:** <https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154>\
**Category:** language design\
**Created:** [August 9, 2022, 6:00am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154 "2022-08-09T06:00:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![liborty](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/liborty/32/9696_2.png) [@liborty](https://internals.rust-lang.org/u/liborty)\
**Post date:** [August 9, 2022, 6:00am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/1 "2022-08-09T06:00:50Z")

</div>

I have implemented `binsearch` for `&[T]` which has a number of advantages over the existing `binary_search` method in primitive type slice. It can be presently seen in trait [Vecops](https://docs.rs/indxvec/1.3.1/src/indxvec/vecops.rs.html#126) in my crate `indxvec`, version. 1.3.1.

- It only requires PartialOrd instead of Ord.
- It works on both ascending and descending orders, at the cost of a single test of an extra boolean argument specifying the order.
- It always succeeds, i.e. it does not return Err. The `std` version uses `Err` mechanism to deliver a genuine sort-order result, which is arguably not quite right.
- It always returns an unambiguous result: Range from the sort position of the first occurrence of the sought value till the last. Empty range n..n, means that the item was not found and n is its insert position. This mechanism is logically consistent and avoids doing Error processing of useful results.
- It solves the sub-optimality hinted at in the documentation: " If there are multiple matches, then any one of the matches could be returned. The index is chosen deterministically, but is subject to change in future versions of Rust."

I am well aware that there is extreme reluctance to introduce changes to std but a breaking change could be avoided by simply adding this method for some transitional period.

Is this a useful RFC?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 9, 2022, 6:51am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/2 "2022-08-09T06:51:16Z")

</div>

The thing you'd need for this is an [https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html](https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html).

(It's not something with enough impact on the ecosystem to need a full RFC.)

> [@liborty](#):
>
> - It only requires PartialOrd instead of Ord.
> - It solves the sub-optimality hinted at in the documentation: " If there are multiple matches, then any one of the matches could be returned. The index is chosen deterministically, but is subject to change in future versions of Rust."

These has been dealt with by [`partition_point`](https://doc.rust-lang.org/std/primitive.slice.html#method.partition_point), since you can use `|x| x < a` for `PartialOrd` if you want. (Or `|x| x > a` for a descending slice.)

> [@liborty](#):
>
> It always returns an unambiguous result: struct Found{index:usize,count:usize}, where index is the sort position of the first occurrence of the sought value and count is the exact number of its occurrences. When count==0, the item was not found but its insert position index is still returned. This mechanism is logically consistent and avoids doing Error processing of useful results.

The bar for a new type is quite high, and position+count is pretty unusual in rust.

But that has an easy solution: return `Range<usize>` instead. That still lets you return `i..i` if the place to insert it in sorted order is at `i`, and it's an existing type. It could even be used to index the original slice to get all the matching items.

Something like that has long been discussed under the name `equal_range` in [Add `lower_bound`, `upper_bound` and `equal_range` for slices where T: Ord to complement `binary_search` · Issue #2184 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/issues/2184)

---

<div class="post-metadata">

**Author:** ![liborty](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/liborty/32/9696_2.png) [@liborty](https://internals.rust-lang.org/u/liborty)\
**Post date:** [August 9, 2022, 7:00am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/3 "2022-08-09T07:00:09Z")

</div>

Thank you for that suggestion. I can certainly do that without any difficulty. And then retrieve the index from: range.start

Edit: Implemented now in `indxvec` v. 1.3.3 [source](https://docs.rs/indxvec/1.3.3/src/indxvec/vecops.rs.html#126)

---

<div class="post-metadata">

**Author:** ![liborty](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/liborty/32/9696_2.png) [@liborty](https://internals.rust-lang.org/u/liborty)\
**Post date:** [August 9, 2022, 12:23pm UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/4 "2022-08-09T12:23:23Z")

</div>

> [@scottmcm](#):
>

Thanks. Following your advice, I have now submitted it as an API change proposal.

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [August 18, 2022, 1:06am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/5 "2022-08-18T01:06:31Z")

</div>

([Link to the ACP](https://github.com/rust-lang/libs-team/issues/81), if anyone else is curious.)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 16, 2022, 1:06am UTC](https://internals.rust-lang.org/t/pre-rfc-more-general-binary-search/17154/6 "2022-11-16T01:06:34Z")

</div>

Closed on November 16, 2022, 1:06am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
