Guest User

Untitled

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