pub struct Window<T> { /* private fields */ }
Expand description
Window is a circular buffer where both
start
and end
pointers always point to a single element.
When push new value into it, it remembers that value and returns the oldest pushed value.
Also you can iterate over remembered values inside the Window
§Examples
use yata::core::Window;
let mut w = Window::new(3, 1); // [1, 1, 1]
assert_eq!(w.push(2), 1); // [1, 1, 2]
assert_eq!(w.push(3), 1); // [1, 2, 3]
assert_eq!(w.push(4), 1); // [2, 3, 4]
assert_eq!(w.push(5), 2); // [3, 4, 5]
assert_eq!(w.push(6), 3); // [4, 5, 6]
use yata::core::Window;
let mut w = Window::new(3, 0);
w.push(1);
w.push(2);
assert_eq!(w[0], 2);
assert_eq!(w[1], 1);
assert_eq!(w[2], 0);
w.push(3);
assert_eq!(w[0], 3);
assert_eq!(w[1], 2);
assert_eq!(w[2], 1);
w.push(4);
assert_eq!(w[0], 4);
assert_eq!(w[1], 3);
assert_eq!(w[2], 2);
§See also
Implementations§
source§impl<T> Window<T>
impl<T> Window<T>
sourcepub fn new(size: PeriodType, value: T) -> Selfwhere
T: Clone,
pub fn new(size: PeriodType, value: T) -> Selfwhere
T: Clone,
Creates new Window
object of size size
with filled values value
§Panics
When in development mode, this method may panic if size
is equal to PeriodType::MAX
sourcepub fn from_parts(slice: Box<[T]>, index: PeriodType) -> Self
pub fn from_parts(slice: Box<[T]>, index: PeriodType) -> Self
Creates new Window
object from raw slice and index of the oldest inserted element in that slice.
index
must be an index of the most oldest value in the slice. In most cases it should be zero.
§Panics
This method will panic if length of the slice is greater or equal to PeriodType::MAX
.
This method will also panic if provided index
is greater or equal to slice’s length.
sourcepub fn push(&mut self, value: T) -> T
pub fn push(&mut self, value: T) -> T
Pushes the value
into the Window
.
Returns an oldest pushed value.
§Panics
This method panics if try to push into empty Window
(when size
= 0
).
sourcepub const fn iter(&self) -> WindowIterator<'_, T>
pub const fn iter(&self) -> WindowIterator<'_, T>
Returns an iterator over the Window
’s values (by copy) (from the newest to the oldest).
§Examples
use yata::core::Window;
let mut w = Window::new(3, 1);
w.push(2);
w.push(3);
w.push(4);
w.push(5);
let p: Vec<_> = w.iter().copied().collect();
assert_eq!(p, [5, 4, 3]);
sourcepub const fn iter_rev(&self) -> ReversedWindowIterator<'_, T>
pub const fn iter_rev(&self) -> ReversedWindowIterator<'_, T>
Returns a reversed iterator over the Window
’s values (by copy) (from the oldest value to the newest).
§Examples
use yata::core::Window;
let mut w = Window::new(3, 1);
w.push(2);
w.push(3);
w.push(4);
w.push(5);
let p: Vec<_> = w.iter_rev().copied().collect();
assert_eq!(p, [3, 4, 5]);
sourcepub fn newest(&self) -> &T
pub fn newest(&self) -> &T
Returns a last pushed value
§Examples
use yata::core::Window;
let mut w = Window::new(3, 1);
assert_eq!(w.newest(), &1);
w.push(2);
assert_eq!(w.newest(), &2);
w.push(3);
assert_eq!(w.newest(), &3);
w.push(4);
assert_eq!(w.newest(), &4);
w.push(5);
assert_eq!(w.newest(), &5);
w.push(6);
assert_eq!(w.newest(), &6);
sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Checks if Window
is empty (length
== 0). Returns true
if Window
is empty or false otherwise.
sourcepub const fn len(&self) -> PeriodType
pub const fn len(&self) -> PeriodType
Returns the length (elements count) of the Window
sourcepub fn get(&self, index: PeriodType) -> Option<&T>
pub fn get(&self, index: PeriodType) -> Option<&T>
Returns an element at index
starting from the newest
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for Window<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Window<T>where
T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<'a, T> IntoIterator for &'a Window<T>
impl<'a, T> IntoIterator for &'a Window<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Window<T>where
T: RefUnwindSafe,
impl<T> Send for Window<T>where
T: Send,
impl<T> Sync for Window<T>where
T: Sync,
impl<T> Unpin for Window<T>
impl<T> UnwindSafe for Window<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T, Q> Sequence<T> for Q
impl<T, Q> Sequence<T> for Q
source§fn call<M>(&self, method: &mut M) -> Vec<M::Output>where
M: Method<Input = T>,
fn call<M>(&self, method: &mut M) -> Vec<M::Output>where
M: Method<Input = T>,
Method
over the slice and returns Vec
of result values.source§fn get_initial_value(&self) -> Option<&T>
fn get_initial_value(&self) -> Option<&T>
None
if it’s empty.source§fn get_initial_value_mut(&mut self) -> Option<&mut T>
fn get_initial_value_mut(&mut self) -> Option<&mut T>
None
if it’s empty.source§impl<Q> Sequence<f64> for Q
impl<Q> Sequence<f64> for Q
source§fn call<M>(&self, method: &mut M) -> Vec<M::Output>where
M: Method<Input = T>,
fn call<M>(&self, method: &mut M) -> Vec<M::Output>where
M: Method<Input = T>,
Method
over the slice and returns Vec
of result values.source§fn get_initial_value(&self) -> Option<&T>
fn get_initial_value(&self) -> Option<&T>
None
if it’s empty.source§fn get_initial_value_mut(&mut self) -> Option<&mut T>
fn get_initial_value_mut(&mut self) -> Option<&mut T>
None
if it’s empty.