#[derive(Debug, Clone)]
pub(crate) struct CircularBuffer<T: Default> {
data: Vec<T>,
}
impl<T: Default> CircularBuffer<T> {
pub(crate) fn new(len: usize) -> Self {
Self {
data: (0..len).map(|_| T::default()).collect::<Vec<_>>(),
}
}
pub(crate) fn take(&mut self, index: usize) -> T {
let idx = index % self.data.len();
core::mem::take(&mut self.data[idx])
}
pub(crate) fn replace(&mut self, index: usize, data: T) -> &T {
let idx = index % self.data.len();
self.data[idx] = data;
&self.data[idx]
}
}
impl<T: Default> core::ops::Index<usize> for CircularBuffer<T> {
type Output = T;
#[inline]
fn index(&self, node_id: usize) -> &Self::Output {
let idx = node_id % self.data.len();
&self.data[idx]
}
}
impl<T: Default> core::ops::IndexMut<usize> for CircularBuffer<T> {
#[inline]
fn index_mut(&mut self, node_id: usize) -> &mut Self::Output {
let idx = node_id % self.data.len();
&mut self.data[idx]
}
}
impl<T: Default> core::ops::Index<isize> for CircularBuffer<T> {
type Output = T;
#[inline]
fn index(&self, node_id: isize) -> &Self::Output {
let idx = node_id.rem_euclid(self.data.len() as isize) as usize;
&self.data[idx]
}
}
impl<T: Default> core::ops::IndexMut<isize> for CircularBuffer<T> {
#[inline]
fn index_mut(&mut self, node_id: isize) -> &mut Self::Output {
let idx = node_id.rem_euclid(self.data.len() as isize) as usize;
&mut self.data[idx]
}
}