/// A queue that always contains the same amount of elements.
///
/// It intuitively record the history of `length` values of a value
/// This implementations allows to get a single contiguous slice view on the history
pub struct HistoryBuf<T>
{
    buffer: Box<[T]>,
    start: usize,
    length: usize,
}

impl<T: Clone> HistoryBuf<T>
{
    pub fn new(default: T, length: usize) -> Self
    {
        Self
        {
            buffer: vec![default; 2 * length].into_boxed_slice(),
            start: 0,
            length
        }
    }

    pub fn from_fn(length: usize, mut f: impl FnMut(usize) -> T) -> Self
    {
        Self
        {
            buffer: (0..(2 * length)).map(|i| f(i)).collect(),
            start: 0,
            length
        }
    }

    pub fn push(&mut self, data: T)
    {
        self.buffer[self.start] = data.clone();
        self.buffer[self.start + self.length] = data.clone();

        self.start += 1;
        self.start %= self.length;
    }

    pub fn as_slice(&self) -> &[T]
    {
        &self.buffer[self.start..(self.start + self.length)]
    }
}

