pub struct SlidingWindow<T> { /* private fields */ }Expand description
A simple sliding window implementation in rust. It uses a vector as ring buffer and a sum counter to prevent the requirement of iterating over the entire buffer for each time the total count is required.
This sliding window implementation is not aware of the window movement
it keeps track of the windows and can be triggered to tick over.
Implementations§
Source§impl<T> SlidingWindow<T>
impl<T> SlidingWindow<T>
Sourcepub fn new(size: usize, max: T) -> Self
pub fn new(size: usize, max: T) -> Self
Creates a new sliding window.
size- is the number of slots in the windowsmax- is the maximum the current window allows
pub fn max(&self) -> T
pub fn set_max(&mut self, max: T) -> &Self
Sourcepub fn inc(&mut self) -> Result<T, T>
pub fn inc(&mut self) -> Result<T, T>
Tries to increment the window by one, this equals add(1). See add
for a description of return values.
§Errors
if we would overshoot the maximum, the error is the number over max.
Sourcepub fn add(&mut self, n: T) -> Result<T, T>
pub fn add(&mut self, n: T) -> Result<T, T>
Tries to add a given number to the windows count. If we remain below
max the result is Ok(<new count>). If we fail and would go over
max the result is Err(<count over max>).
§Errors
if we would overshoot the maximum, the error is the number over max.