FPQ
Priority Queue with scoring function
Usage
- Rust Edition: 2024
- MSRV: 1.85
[dependencies]
fpq = "0.3.0"
In the code, you first need to create an instance with fpq::PriorityQueue::new. It takes a boxed closure that converts your item type &T into score type S: Ord. Then you can use push/pop/peek methods in the same way as std::collections::BinaryHeap.
Example
use std::cmp::Reverse;
use fpq::PriorityQueue;
fn main() {
let score_fn = Box::new(|s: &String| s.len());
let mut queue = PriorityQueue::new(score_fn);
assert!(queue.peek().is_none());
queue.push("a".to_string()); queue.push("ccc".to_string()); queue.push("bb".to_string());
queue.push_with_score("b".to_string(), 10);
assert_eq!(queue.peek(), Some(&"b".to_string()));
assert_eq!(queue.pop(), Some("b".to_string()));
assert_eq!(queue.pop(), Some("ccc".to_string()));
assert_eq!(queue.pop(), Some("bb".to_string()));
assert_eq!(queue.pop(), Some("a".to_string()));
assert!(queue.peek().is_none());
let score_fn = Box::new(|s: &String| Reverse(s.len()));
let mut queue = PriorityQueue::new(score_fn);
queue.push("a".to_string()); queue.push("ccc".to_string()); queue.push("bb".to_string());
queue.push_with_score("b".to_string(), Reverse(10));
assert_eq!(queue.peek(), Some(&"a".to_string()));
assert_eq!(queue.pop(), Some("a".to_string()));
assert_eq!(queue.pop(), Some("bb".to_string()));
assert_eq!(queue.pop(), Some("ccc".to_string()));
assert_eq!(queue.pop(), Some("b".to_string()));
assert!(queue.peek().is_none());
}