2 个版本
0.1.1 | 2023年6月13日 |
---|---|
0.1.0 | 2023年6月13日 |
#2418 在 算法
用于 n_best
5KB
73 行
OrdBy
方便地为任何类型附加自定义比较函数,以从类似 BinaryHeap 的类型中获得 sort_by() 类型行为
OrdBy 是 [Reverse] 所用模式的泛化。
use std::collections::BinaryHeap;
use ord_by::*;
let numbers = vec![0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
let mut heap = BinaryHeap::with_capacity(10);
numbers.into_iter().ord_by(|a, b| a.cmp(b))
.for_each(|n| heap.push(n));
let mut sorted = Vec::with_capacity(10);
while let Some(n) = heap.pop() {
sorted.push(n.into_inner())
}
assert_eq!(sorted, vec![9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
未来工作:我希望比较函数成为一个关联常量,以提高效率,而不是存储在每个结构实例中的变量。然而,这需要关联常量,其类型依赖于泛型,这是目前不支持的功能。 https://github.com/rust-lang/rust/issues/98210