#price #query #smallest #key #weight #find

threshold-dict

A data structure to find smallest key that is larger than the query

8 releases (breaking)

0.7.0 Jul 13, 2023
0.6.0 Mar 8, 2023
0.5.0 Mar 6, 2023
0.4.1 Mar 1, 2023
0.1.4 Oct 15, 2022

#1769 in Data structures

36 downloads per month

Custom license

5KB
65 lines

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

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.

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);

No runtime deps