CircularBuffer

Struct CircularBuffer 

Source
pub struct CircularBuffer<T> { /* private fields */ }
Expand description

Fixed-size circular buffer optimized for indicator calculations.

This buffer maintains a fixed capacity and automatically overwrites the oldest data when new data is added. It’s more memory-efficient than keeping all historical data when only a fixed window is needed.

§Example

let mut buffer = CircularBuffer::new(3);
buffer.push(10.0);
buffer.push(20.0);
buffer.push(30.0);  // Buffer is now full: [10, 20, 30]

assert_eq!(buffer.sum(), 60.0);
assert_eq!(buffer.mean(), Some(20.0));

buffer.push(40.0);  // Overwrites 10: [20, 30, 40]
assert_eq!(buffer.sum(), 90.0);

Implementations§

Source§

impl<T: Copy + Default> CircularBuffer<T>

Source

pub fn new(capacity: usize) -> Self

Create a new circular buffer with the specified capacity.

§Arguments
  • capacity - Maximum number of elements the buffer can hold
§Panics

Panics if capacity is 0.

Source

pub fn push(&mut self, value: T)

Add a new value to the buffer.

If the buffer is full, this overwrites the oldest value.

Source

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

Get a value at a specific index.

Index 0 is the oldest value, index size - 1 is the newest. Returns None if the index is out of bounds.

Source

pub fn last(&self) -> Option<T>

Get the most recent value (last pushed).

Source

pub fn first(&self) -> Option<T>

Get the oldest value in the buffer.

Source

pub fn is_full(&self) -> bool

Check if the buffer is full.

Source

pub fn is_empty(&self) -> bool

Check if the buffer is empty.

Source

pub fn len(&self) -> usize

Get the current number of elements in the buffer.

Source

pub fn capacity(&self) -> usize

Get the maximum capacity of the buffer.

Source

pub fn clear(&mut self)

Clear all elements from the buffer.

Source

pub fn iter(&self) -> CircularBufferIter<'_, T>

Iterate over all values in the buffer (oldest to newest).

Source

pub fn as_slice(&self) -> Vec<T>

Get a slice view of the buffer’s data in insertion order.

Note: This returns the values in chronological order (oldest to newest), which may not match the internal storage order.

Source§

impl<T> CircularBuffer<T>
where T: Copy + Default + Add<Output = T> + PartialOrd,

Source

pub fn sum(&self) -> T
where T: Add<Output = T> + Default,

Calculate the sum of all values in the buffer.

Source

pub fn max(&self) -> Option<T>

Find the maximum value in the buffer.

Returns None if the buffer is empty.

Source

pub fn min(&self) -> Option<T>

Find the minimum value in the buffer.

Returns None if the buffer is empty.

Source§

impl CircularBuffer<f64>

Source

pub fn mean(&self) -> Option<f64>

Calculate the mean (average) of all values in the buffer.

Returns None if the buffer is empty.

Source

pub fn variance(&self) -> Option<f64>

Calculate the variance of all values in the buffer.

Returns None if the buffer is empty.

Source

pub fn std_dev(&self) -> Option<f64>

Calculate the standard deviation of all values in the buffer.

Returns None if the buffer is empty.

Trait Implementations§

Source§

impl<T: Clone> Clone for CircularBuffer<T>

Source§

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

Returns a duplicate 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 CircularBuffer<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CircularBuffer<T>

§

impl<T> RefUnwindSafe for CircularBuffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for CircularBuffer<T>
where T: Send,

§

impl<T> Sync for CircularBuffer<T>
where T: Sync,

§

impl<T> Unpin for CircularBuffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for CircularBuffer<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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.