salad commit

This commit is contained in:
2022-12-01 21:37:44 +00:00
parent f6ae1e19c0
commit 3557107248
12 changed files with 411 additions and 82 deletions

View File

@@ -6,14 +6,16 @@ use std::fmt::Debug;
#[derive(Clone, Copy)]
pub struct Stackframe<'a, T> {
pub item: T,
pub prev: Option<&'a Stackframe<'a, T>>
pub prev: Option<&'a Stackframe<'a, T>>,
pub len: usize
}
impl<'a, T: 'a> Stackframe<'a, T> {
pub fn new(item: T) -> Self {
Self {
item,
prev: None
prev: None,
len: 1
}
}
/// Get the item owned by this listlike, very fast O(1)
@@ -27,9 +29,22 @@ impl<'a, T: 'a> Stackframe<'a, T> {
pub fn push(&self, item: T) -> Stackframe<'_, T> {
Stackframe {
item,
prev: Some(self)
prev: Some(self),
len: self.len + 1
}
}
pub fn opush(prev: &Option<Self>, item: T) -> Option<Self> {
Some(Self {
item,
prev: prev.as_ref(),
len: prev.map_or(1, |s| s.len)
})
}
pub fn len(&self) -> usize { self.len }
pub fn pop(&self, count: usize) -> Option<&Self> {
if count == 0 {Some(self)}
else {self.prev.and_then(|prev| prev.pop(count - 1))}
}
}
impl<'a, T> Debug for Stackframe<'a, T> where T: Debug {
@@ -52,4 +67,4 @@ impl<'a, T> Iterator for StackframeIterator<'a, T> {
self.curr = prev;
Some(item)
}
}
}