3个版本
0.1.2 | 2021年5月8日 |
---|---|
0.1.1 | 2018年10月30日 |
0.1.0 | 2018年10月30日 |
1632 在 数据结构 中排名
每月353次下载
在 4 个crate中使用(直接使用3个)
5KB
81 代码行
binary-search
给定一个单调函数,找到最大且不足的数量以及最小且过多的数量。二分查找的第一个参数设置搜索空间的上限和下限。
use binary_search::{binary_search, Direction};
fn main() {
let values =
[0, 4, 5, 6, 7, 9, 456];
let (largest_low, smallest_high) =
binary_search((0, ()), (values.len(), ()), |i|
if values[i] < 6 {
Direction::Low(())
} else {
Direction::High(())
}
);
dbg!(largest_low);
dbg!(smallest_high);
}
您还可以提供关联的'见证',如本示例所示。见证在传递给以及从 binary_search
生成时使用。这些参数作为函数确实在该范围内转换的证明。如果您不知道这种情况,可能需要首先在边界处调用您的函数。
use binary_search::{binary_search, Direction};
fn main() {
let values =
[Ok("foo"), Ok("bar"), Ok("baz"), Err(false), Err(true)];
let (largest_low, smallest_high) =
binary_search((0, "foo"), (values.len() - 1, true), |i|
match values[i] {
Ok(x) => Direction::Low(x),
Err(x) => Direction::High(x),
}
);
dbg!(largest_low); // "baz"
dbg!(smallest_high); // false
}