#iterator #sliding #adaptor #elements #backing #non-copying #iter-ext

sliding_windows

a non-copying implementation of a sliding windows iterator adaptor

7 stable releases

Uses old Rust 2015

3.0.0 Jun 16, 2017
2.0.4 Jun 7, 2017
2.0.3 Mar 17, 2016
2.0.2 Feb 10, 2016
1.0.0 Nov 17, 2015

#1148 in Algorithms

Download history 63/week @ 2024-07-21 86/week @ 2024-07-28 108/week @ 2024-08-04 62/week @ 2024-08-11 98/week @ 2024-08-18 86/week @ 2024-08-25 88/week @ 2024-09-01 82/week @ 2024-09-08 72/week @ 2024-09-15 91/week @ 2024-09-22 115/week @ 2024-09-29 62/week @ 2024-10-06 78/week @ 2024-10-13 74/week @ 2024-10-20 84/week @ 2024-10-27 64/week @ 2024-11-03

304 downloads per month
Used in fuzzy_match

MIT license

19KB
325 lines

sliding_windows

This crate offers an Iterator adaptor, which yields "sliding windows" over the elements returned by the wrapped iterator.

It's worth to note that it does not copy elements, which makes the code relatively performant.

As a consequence it violates the Iterator protocol slightly. It is not possible to have two Windows into the data available at the same time. This is checked during runtime.

The backing storage is a Vec, so this Iterator adaptor is not ideal for very large windows (>20 elements or very huge elements).

I'd happily accept a PR to implement the same functionality with a VecDeque or similar, see this issue.

Install

Add this to your Cargo.toml.

[dependencies]
sliding_windows = "2.0"

Example

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]

For more examples please consult the Documentation.

This functionality in other languages/crates

No runtime deps