GithubHelp home page GithubHelp logo

flo-l / rust-sliding_windows Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 677 KB

This repository contains a non-copying implementation of a sliding windows iterator adaptor.

Home Page: https://flo-l.github.io/rust-sliding_windows/

License: MIT License

Rust 99.20% HTML 0.80%
rust iterator crates

rust-sliding_windows's Introduction

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 several orders of mignitudes faster than naive copying variants.

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.

Links

Install

Add this to your Cargo.toml.

[dependencies]
sliding_windows = "3.0"

Example

extern crate sliding_windows;
use sliding_windows::{IterExt, Storage};

fn main() {
    let mut storage: Storage<u32> = Storage::new(3);

    for x in (0..5).sliding_windows(&mut storage) {
        println!("{:?}", x);
    }
}

// This outputs:
// Window[0, 1, 2]
// Window[1, 2, 3]
// Window[2, 3, 4]

For more examples please consult the Documentation.

This functionality in other languages/crates

rust-sliding_windows's People

Contributors

flo-l avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vringar

rust-sliding_windows's Issues

Implement Storage with VecDeque or similar

Storage is currently implemented using a Vec as a backing storage. This becomes inefficient for huge window sizes, as the whole array needs to be shifted on every call to next() sometimes.

As noted in #1 it's very hard to implement Storage generically over any container.

  • One would lose the ability of Window to deref to a (mut) slice.
  • Window probably has to implement Iterator or Index/IndexMut.

I'd be happy to merge a PR that implements Storage generically over containers if good solutions to the above problems are found and if no functionality gets lost.

Anyway I'd also merge a PR implementing another adaptor, which uses a VecDeque or anything efficient for huge window sizes.

Performance improvements

Instead of using VecDeque as storage it's also possible to improve performance immensely by just using a bigger Vec and shifting all elements just when it's end is reached. Eg. a Vec with size = window_size * 2 would be a lot more CPU efficient but would just use a bit more memory.

Needs polishing

Before announcing this on reddit etc. this repo needs some polishing.

Wishlist

  • better readme
  • shorter/better type names (SlidingWindowStorage is horrible)
  • more storage types (VecDeque), maybe a trait so users can implement their own won't be done
  • macro to make usage even easier Is this really needed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.