Restructured project for separate plugins
This commit is contained in:
38
dsp/src/ring_buffer.rs
Normal file
38
dsp/src/ring_buffer.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
pub struct RingBuffer<T, const N: usize> {
|
||||
buffer: [T; N],
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<T, const N: usize> RingBuffer<T, N>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
pub const fn new(value: T) -> Self {
|
||||
Self {
|
||||
buffer: [value; N],
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
pub const fn len(&self) -> usize {
|
||||
N
|
||||
}
|
||||
pub fn shift(&mut self) {
|
||||
self.index = (self.index + 1) % N;
|
||||
}
|
||||
|
||||
}
|
||||
impl<T, const N: usize> Index<usize> for RingBuffer<T, N> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
&self.buffer[(self.index + index) % N]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const N: usize> IndexMut<usize> for RingBuffer<T, N> {
|
||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||
&mut self.buffer[(self.index + index) % N]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user