2个不稳定版本

0.2.0 2020年6月29日
0.1.0 2020年6月24日

#1806 in 嵌入式开发

MIT/Apache

17KB
298

超时优先队列

Documentation

这是一个数据结构,允许您拥有具有超时和优先级级别的多个数据版本。

示例

// Create a timer instance. It must implement the `topq::Timer` trait.
let timer = SomeTimer;

// This is a priority queue that:
//  * Uses `u32` as the data item
//  * Uses a `u8` for the priority level
//  * Uses `SomeTimer` for the timer instance
//  * Supports 4 priority levels at once
let mut q: Topq<u32, u8, SomeTimer, U4> = Topq::new(timer);

// We start at time "0"

// Insert the value "10", at priority level "3", valid for
// "30" time units (the unit depends on the timer)
q.insert(10, 3, 30);

// Get the top priority item
assert_eq!(q.get_data(), Some(&10));

// Insert the value "11", at priority level "4", valid for
// "25" time units (the unit depends on the timer)
q.insert(11, 4, 25);

// Get the top priority item - it is now 11
assert_eq!(q.get_data(), Some(&11));

// Insert the value "12", at priority level "5", valid for
// "20" time units (the unit depends on the timer)
q.insert(12, 5, 20);

// Get the top priority item - it is now 12
assert_eq!(q.get_data(), Some(&12));

// Insert the value "13", at priority level "6", valid for
// "15" time units (the unit depends on the timer)
q.insert(13, 6, 15);
assert_eq!(q.get_data(), Some(&13));

// Fast forward to time "15". 13 is still valid here
assert_eq!(q.get_data(), Some(&13));

// Fast forward to time "16". 13 has expired, so 12 is
// now the highest priority + valid data
assert_eq!(q.get_data(), Some(&12));

// Fast forward to time "21". 12 has expired, so 11 is
// now the highest priority + valid data
assert_eq!(q.get_data(), Some(&11));

// Fast forward to time "26". 11 has expired, so 10 is
// now the highest priority + valid data
assert_eq!(q.get_data(), Some(&10));

// Fast forward to time "31". All items have expired,
// so there is no available data
assert_eq!(q.get_data(), None);

许可证

许可协议为以下之一

任选其一。

贡献

除非您明确声明,否则任何有意提交以包含在作品中的贡献,根据Apache-2.0许可证定义,应以上述双重许可,无需任何额外条款或条件。

依赖项

~245KB