SHARE
TWEET

Untitled

a guest Aug 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const WINDOW_SIZE: usize = 10;
  2.  
  3. struct FloatingWindow<'a> {
  4.     data: [i64; WINDOW_SIZE],
  5.     start_index: usize,
  6.     current_size: usize,
  7. }
  8.  
  9. impl<'a> FloatingWindow<'a> {
  10.     pub fn at(&self, idx: usize) -> i64 {
  11.         self.data[(self.start_index + idx) % WINDOW_SIZE]
  12.     }
  13. }
  14.  
  15.  
  16. struct FloatingWindowIterator<'a> {
  17.     window: &'a FloatingWindow<'a>,
  18.     curr_index: usize
  19. }
  20.  
  21. impl<'a> IntoIterator for FloatingWindow<'a> {
  22.     type Item = i64;
  23.     type IntoIter = FloatingWindowIterator<'a>;
  24.  
  25.     fn into_iter(&self) -> Self::IntoIter {
  26.         FloatingWindowIterator::new(&self)
  27.     }
  28. }
  29.  
  30. impl<'a> FloatingWindowIterator<'a> {
  31.     pub fn new(window: &'a FloatingWindow) -> FloatingWindowIterator<'a> {
  32.         FloatingWindowIterator {
  33.             window,
  34.             curr_index: 0
  35.         }
  36.     }
  37. }
  38.  
  39. impl<'a> Iterator for FloatingWindowIterator<'a> {
  40.     type Item = i64;
  41.  
  42.     fn next(&mut self) -> Option<Self::Item> {
  43.         if self.curr_index < self.window.current_size {
  44.             self.curr_index += 1;
  45.             Some(self.window.at(self.curr_index))
  46.         } else {
  47.             None
  48.         }
  49.     }
  50. }
  51.  
  52. fn main() {
  53.     println!("Hello!");
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top