#priority-queue #priority #queue #keyed #binary-heap #heap #binary

keyed_priority_queue

支持通过键更改优先级或提前删除的优先队列

4个版本

0.4.2 2023年11月15日
0.4.1 2021年10月24日
0.3.2 2021年2月21日
0.3.1 2020年12月21日
0.1.2 2019年11月24日

#93数据结构

Download history 24237/week @ 2024-03-14 23514/week @ 2024-03-21 23837/week @ 2024-03-28 23388/week @ 2024-04-04 22906/week @ 2024-04-11 22370/week @ 2024-04-18 22116/week @ 2024-04-25 22087/week @ 2024-05-02 24338/week @ 2024-05-09 21851/week @ 2024-05-16 21934/week @ 2024-05-23 21514/week @ 2024-05-30 19427/week @ 2024-06-06 22252/week @ 2024-06-13 21529/week @ 2024-06-20 15979/week @ 2024-06-27

83,302 每月下载量
34 crate 中使用 (直接使用10个)

MIT 许可证

75KB
1.5K SLoC

键优先队列

Crates.io tests MIT licensed Average time to resolve an issue Percentage of issues still open

Rust库,支持在队列中更改优先级项或提前删除。要更改优先级,您需要使用某些键。

Cargo.toml强制执行的最低支持的Rust版本。

用法

代码示例

use keyed_priority_queue::{KeyedPriorityQueue, Entry};

let mut queue = KeyedPriorityQueue::new();

// Currently queue is empty
assert_eq!(queue.peek(), None);

queue.push("Second", 4);
queue.push("Third", 3);
queue.push("First", 5);
queue.push("Fourth", 2);
queue.push("Fifth", 1);

// Peek return references to most important pair.
assert_eq!(queue.peek(), Some((&"First", &5)));

assert_eq!(queue.len(), 5);

// We can clone queue if both key and priority is clonable
let mut queue_clone = queue.clone();

// We can run consuming iterator on queue,
// and it will return items in decreasing order
for (key, priority) in queue_clone{
    println!("Priority of key {} is {}", key, priority);
}

// Popping always will return the biggest element
assert_eq!(queue.pop(), Some(("First", 5)));
// We can change priority of item by key:
queue.set_priority(&"Fourth", 10);
// And get it
assert_eq!(queue.get_priority(&"Fourth"), Some(&10));
// Now biggest element is Fourth
assert_eq!(queue.pop(), Some(("Fourth", 10)));
// We can also decrease priority!
queue.set_priority(&"Second", -1);
assert_eq!(queue.pop(), Some(("Third", 3)));
assert_eq!(queue.pop(), Some(("Fifth", 1)));
assert_eq!(queue.pop(), Some(("Second", -1)));
// Now queue is empty
assert_eq!(queue.pop(), None);

// There are Entry API if you want to avoid double hash lookups
match queue.entry("Entry"){
    Entry::Vacant(entry)=>entry.set_priority(10),
    Entry::Occupied(_)=>unreachable!(),
};

match queue.entry("Entry"){
    Entry::Vacant(_)=>unreachable!(),
    Entry::Occupied(entry)=>{
        assert_eq!(entry.get_key(), &"Entry");
        assert_eq!(entry.get_priority(), &10);
        entry.set_priority(5);
    },
};

// We can clear queue
queue.clear();
assert!(queue.is_empty());

依赖项

~1MB
~14K SLoC