Struct yata::core::Window

source ·
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

Past

Windows

Implementations§

source§

impl<T> Window<T>

source

pub fn new(size: PeriodType, value: T) -> Self
where 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

source

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.

source

pub fn empty() -> Self

Creates an empty Window instance (no buffer allocated)

source

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).

source

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]);
source

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]);
source

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);
source

pub fn oldest(&self) -> &T

Returns an oldest value

source

pub const fn is_empty(&self) -> bool

Checks if Window is empty (length == 0). Returns true if Window is empty or false otherwise.

source

pub const fn as_slice(&self) -> &[T]

Casts Window as a raw slice of T

§Important!

The sequence of elements is not preserved.

source

pub const fn len(&self) -> PeriodType

Returns the length (elements count) of the Window

source

pub fn get(&self, index: PeriodType) -> Option<&T>

Returns an element at index starting from the newest

Trait Implementations§

source§

impl<T> AsRef<[T]> for Window<T>

source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Clone> Clone for Window<T>

source§

fn clone(&self) -> Window<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Window<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for Window<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> From<Box<[T]>> for Window<T>

source§

fn from(slice: Box<[T]>) -> Self

Converts to this type from the input type.
source§

impl<T> From<Vec<T>> for Window<T>

source§

fn from(v: Vec<T>) -> Self

Converts to this type from the input type.
source§

impl<T> Index<u8> for Window<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: PeriodType) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, T> IntoIterator for &'a Window<T>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = WindowIterator<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> Serialize for Window<T>
where T: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, Q> Sequence<T> for Q
where T: OHLCV, Q: AsRef<[T]>,

source§

fn validate(&self) -> bool

Validates the sequence.
source§

fn call<M>(&self, method: &mut M) -> Vec<M::Output>
where M: Method<Input = T>,

Calls Method over the slice and returns Vec of result values.
source§

fn apply<M>(&mut self, method: &mut M)
where M: Method<Input = T, Output = T>, Self: AsMut<[T]>,

Applies Method on the slice in-place.
source§

fn get_initial_value(&self) -> Option<&T>

Returns a reference to the first value in the sequence or None if it’s empty.
source§

fn get_initial_value_mut(&mut self) -> Option<&mut T>
where Self: AsMut<[T]>,

Returns a reference to the first value in the sequence or None if it’s empty.
source§

fn collapse_timeframe(&self, size: usize, continuous: bool) -> Vec<T>
where T: OHLCV + Clone + Add<Output = T>,

Converts timeframe of the series Read more
source§

impl<Q> Sequence<f64> for Q
where Q: AsRef<[f64]>,

source§

fn validate(&self) -> bool

Validates the sequence.
source§

fn call<M>(&self, method: &mut M) -> Vec<M::Output>
where M: Method<Input = T>,

Calls Method over the slice and returns Vec of result values.
source§

fn apply<M>(&mut self, method: &mut M)
where M: Method<Input = T, Output = T>, Self: AsMut<[T]>,

Applies Method on the slice in-place.
source§

fn get_initial_value(&self) -> Option<&T>

Returns a reference to the first value in the sequence or None if it’s empty.
source§

fn get_initial_value_mut(&mut self) -> Option<&mut T>
where Self: AsMut<[T]>,

Returns a reference to the first value in the sequence or None if it’s empty.
source§

fn collapse_timeframe(&self, size: usize, continuous: bool) -> Vec<T>
where T: OHLCV + Clone + Add<Output = T>,

Converts timeframe of the series Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,