You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
use std::{
|
|
|
|
collections::{
|
|
|
|
LinkedList,
|
|
|
|
linked_list,
|
|
|
|
},
|
|
|
|
iter::Rev,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct FixedStack<T>(LinkedList<T>, usize);
|
|
|
|
|
|
|
|
pub type Iter<'a, T> = Rev<linked_list::Iter<'a, T>>;
|
|
|
|
pub type IterMut<'a, T> = Rev<linked_list::IterMut<'a, T>>;
|
|
|
|
pub type IntoIter<T> = Rev<linked_list::IntoIter<T>>;
|
|
|
|
|
|
|
|
impl<T> FixedStack<T>
|
|
|
|
{
|
|
|
|
#[inline] pub fn new(sz: usize) -> Self
|
|
|
|
{
|
|
|
|
Self (
|
|
|
|
LinkedList::new(),
|
|
|
|
sz
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to push an item, returns Ok(()) if successful
|
|
|
|
pub fn push(&mut self, item: T)
|
|
|
|
{
|
|
|
|
if self.0.len() >= self.1 {
|
|
|
|
self.0.pop_back();
|
|
|
|
}
|
|
|
|
self.0.push_front(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self)
|
|
|
|
{
|
|
|
|
self.0.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop(&mut self) -> Option<T>
|
|
|
|
{
|
|
|
|
self.0.pop_back()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize
|
|
|
|
{
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cap(&self) -> usize
|
|
|
|
{
|
|
|
|
self.1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> Iter<T>
|
|
|
|
{
|
|
|
|
self.0.iter().rev()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter_mut(&mut self)-> IterMut<T>
|
|
|
|
{
|
|
|
|
self.0.iter_mut().rev()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoIterator for FixedStack<T>
|
|
|
|
{
|
|
|
|
type Item=T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
|
|
|
|
fn into_iter(self)-> Self::IntoIter
|
|
|
|
{
|
|
|
|
self.0.into_iter().rev()
|
|
|
|
}
|
|
|
|
}
|