Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::marker::PhantomData;
- const WINDOW_SIZE: usize = 10;
- struct FloatingWindow<'a> {
- data: [i64; WINDOW_SIZE],
- start_index: usize,
- current_size: usize,
- phantom: PhantomData<&'a i32>
- }
- impl<'a> FloatingWindow<'a> {
- pub fn at(&self, idx: usize) -> i64 {
- self.data[(self.start_index + idx) % WINDOW_SIZE]
- }
- }
- struct FloatingWindowIterator<'a> {
- window: &'a FloatingWindow<'a>,
- curr_index: usize
- }
- impl<'a> IntoIterator for &'a FloatingWindow<'a> {
- type Item = i64;
- type IntoIter = FloatingWindowIterator<'a>;
- fn into_iter(self) -> Self::IntoIter {
- FloatingWindowIterator::new(self)
- }
- }
- impl<'a> FloatingWindowIterator<'a> {
- pub fn new(window: &'a FloatingWindow) -> FloatingWindowIterator<'a> {
- FloatingWindowIterator {
- window,
- curr_index: 0
- }
- }
- }
- impl<'a> Iterator for FloatingWindowIterator<'a> {
- type Item = i64;
- fn next(&mut self) -> Option<Self::Item> {
- if self.curr_index < self.window.current_size {
- self.curr_index += 1;
- Some(self.window.at(self.curr_index))
- } else {
- None
- }
- }
- }
- fn main() {
- println!("Hello!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement