Created
August 13, 2019 21:43
-
-
Save GoldsteinE/e9ea3504a7a992cc5dcd1357a4cacd21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment