use std::collections::VecDeque;
use super::register::RegisterContent;
const DEFAULT_HISTORY_CAPACITY: usize = 256;
#[derive(Debug, Clone)]
pub struct HistoryRing {
entries: VecDeque<RegisterContent>,
capacity: usize,
}
impl HistoryRing {
#[must_use]
pub const fn new() -> Self {
Self {
entries: VecDeque::new(),
capacity: DEFAULT_HISTORY_CAPACITY,
}
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
entries: VecDeque::with_capacity(capacity),
capacity,
}
}
pub fn push(&mut self, content: RegisterContent) {
self.entries.push_front(content);
if self.entries.len() > self.capacity {
self.entries.pop_back();
}
}
#[must_use]
pub fn get(&self, index: usize) -> Option<&RegisterContent> {
self.entries.get(index)
}
#[must_use]
pub fn get_by_index(&self, index: u8) -> Option<&RegisterContent> {
self.entries.get(index as usize)
}
#[must_use]
pub fn get_numbered(&self, n: char) -> Option<RegisterContent> {
if n.is_ascii_digit() {
let index = (n as u8 - b'0') as usize;
self.entries.get(index).cloned()
} else {
None
}
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub const fn capacity(&self) -> usize {
self.capacity
}
pub fn iter(&self) -> impl Iterator<Item = &RegisterContent> {
self.entries.iter()
}
pub fn clear(&mut self) {
self.entries.clear();
}
}
impl Default for HistoryRing {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "tests/history.rs"]
mod tests;