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>
impl<T: Copy + Default> CircularBuffer<T>
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Add a new value to the buffer.
If the buffer is full, this overwrites the oldest value.
Sourcepub fn get(&self, index: usize) -> Option<T>
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.
Sourcepub fn iter(&self) -> CircularBufferIter<'_, T> ⓘ
pub fn iter(&self) -> CircularBufferIter<'_, T> ⓘ
Iterate over all values in the buffer (oldest to newest).
Source§impl<T> CircularBuffer<T>
impl<T> CircularBuffer<T>
Trait Implementations§
Source§impl<T: Clone> Clone for CircularBuffer<T>
impl<T: Clone> Clone for CircularBuffer<T>
Source§fn clone(&self) -> CircularBuffer<T>
fn clone(&self) -> CircularBuffer<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> 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
Mutably borrows from an owned value. Read more