Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created August 13, 2019 21:37
Show Gist options
  • Save GoldsteinE/c4066a256339bf495904d1ee8b4db254 to your computer and use it in GitHub Desktop.
Save GoldsteinE/c4066a256339bf495904d1ee8b4db254 to your computer and use it in GitHub Desktop.
const WINDOW_SIZE: usize = 10;
struct FloatingWindow<'a> {
data: [i64; WINDOW_SIZE],
start_index: usize,
current_size: usize,
}
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 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