Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucidfrontier45/threshold_dict
A data structure to find smallest key that is larger than the query.
https://github.com/lucidfrontier45/threshold_dict
data-structures
Last synced: 2 months ago
JSON representation
A data structure to find smallest key that is larger than the query.
- Host: GitHub
- URL: https://github.com/lucidfrontier45/threshold_dict
- Owner: lucidfrontier45
- License: mit
- Created: 2022-10-15T07:44:27.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-13T12:44:42.000Z (over 1 year ago)
- Last Synced: 2024-10-06T16:18:19.615Z (3 months ago)
- Topics: data-structures
- Language: Rust
- Homepage: https://crates.io/crates/threshold-dict
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# threshold-dict
A data structure to find smallest key that is larger than the query.Suppose we have a following weight dependent price table.
```
weight,price
100,10
200,15
500,30
```The price is decided by the smallest entry whose `weight` key is greater than or equal to the query.
*examples*
- weight=90 -> price=10
- weight=100 -> price=10
- weight=250 -> price=30
- weight=600 -> price=Not Found## Install
```sh
cargo add threshold_dict
```## Usage
A `ThresholdDict` can be created by passing kv pairs. If query is greater than all of the keys, None is returned.
```rust
let kv_pairs = vec![(100, 10), (200, 15), (500, 30)];
let dict = ThresholdDict::from(kv_pairs);assert_eq!(dict.query(&90), Some(&10));
assert_eq!(dict.query(600), None);
```