Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vorpalblade/prefix-range
Compute bounds for prefix string queries for BTreeSet/BTreeMap::range
https://github.com/vorpalblade/prefix-range
Last synced: 9 days ago
JSON representation
Compute bounds for prefix string queries for BTreeSet/BTreeMap::range
- Host: GitHub
- URL: https://github.com/vorpalblade/prefix-range
- Owner: VorpalBlade
- License: mit
- Created: 2024-01-14T08:39:14.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-10-14T11:16:23.000Z (about 1 month ago)
- Last Synced: 2024-10-20T00:55:53.591Z (27 days ago)
- Language: Rust
- Size: 30.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Compute bounds for prefix string queries for BTreeSet/BTreeMap::range
[ [crates.io] ] [ [lib.rs] ] [ [docs.rs] ]
If you have a BTreeSet or BTreeMap with string keys and want to find all entries
with a given prefix, the standard library (as of Rust 1.75) doesn't offer any
built in methods to do so.You could use something like the following:
```rust
let iterator = mymap.range(Bounds::Included("myprefix"),
Bounds::Excluded("myprefiy"));
```This issue is finding the upper bound `myprefiy`. You have to deal with UTF-8
encoding, invalid code points etc. That is what the code in this library solves.The code is taken from [a blog post by Jimmy Hartzell](https://www.thecodedmessage.com/posts/prefix-ranges/),
and slightly tweaked:* To work in no-std (still needs alloc though)
* To work with BTreeMaps (not just BTreeSets).A huge thanks to Jimmy Hartzell for solving the problem already (though they
never published it as a crate reusable by others).See the [crate documentation][docs.rs] for usage examples.
## MSRV
The code is simple, and will likely compile on quite old versions, though
nothing older than 1.65 is being actively tested.[crates.io]: https://crates.io/crates/prefix-range
[docs.rs]: https://docs.rs/prefix_range
[lib.rs]: https://lib.rs/crates/prefix-range