7 个稳定版本
使用旧的 Rust 2015
3.0.0 | 2017年6月16日 |
---|---|
2.0.4 | 2017年6月7日 |
2.0.3 | 2016年3月17日 |
2.0.2 | 2016年2月10日 |
1.0.0 | 2015年11月17日 |
1055 在 算法 中
305 每月下载量
在 fuzzy_match 中使用
19KB
325 行
sliding_windows
此包提供了一种 Iterator
适配器,它产生由包装迭代器返回的元素上的“滑动窗口”。
值得注意的是,它不会复制元素,这使得代码相对高效。
因此,它略微违反了迭代器协议。不能同时有两个窗口访问数据。这在运行时进行检查。
支持存储是 Vec
,因此此迭代器适配器不适用于非常大的窗口(>20个元素或非常大的元素)。
我很乐意接受一个 PR 来实现使用 VecDeque
或类似功能的相同功能,请参阅此 问题。
链接
安装
将其添加到您的 Cargo.toml
。
[dependencies]
sliding_windows = "2.0"
示例
extern crate sliding_windows;
use sliding_windows::{IterExt, Storage};
let mut storage: Storage<u32> = Storage::new(3);
for x in (0..5).sliding_windows(&mut storage) {
println!("{:?}", x);
}
// This outputs:
// [0, 1, 2]
// [1, 2, 3]
// [2, 3, 4]
有关更多示例,请参阅文档。
其他语言/包中的此功能
- Ruby: #each_cons
- Python: window
- Rust (仅适用于切片): .windows()
- Rust (适用于所有迭代器,但会复制): .slide()