bug fixes and performance improvements
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt::Debug;
|
||||
|
||||
// TODO: extract to crate
|
||||
@@ -8,71 +9,60 @@ use std::fmt::Debug;
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Stackframe<'a, T> {
|
||||
pub item: T,
|
||||
pub prev: Option<&'a Stackframe<'a, T>>,
|
||||
pub prev: &'a Substack<'a, T>,
|
||||
pub len: usize
|
||||
}
|
||||
|
||||
impl<'a, T: 'a> Stackframe<'a, T> {
|
||||
pub fn new(item: T) -> Self {
|
||||
Self {
|
||||
item,
|
||||
prev: None,
|
||||
len: 1
|
||||
}
|
||||
}
|
||||
/// Get the item owned by this listlike, very fast O(1)
|
||||
pub fn item(&self) -> &T { &self.item }
|
||||
/// Get the next link in the list, very fast O(1)
|
||||
pub fn prev(&self) -> Option<&'a Stackframe<T>> { self.prev }
|
||||
/// Construct an iterator over the listlike, very fast O(1)
|
||||
pub fn iter(&self) -> StackframeIterator<T> {
|
||||
StackframeIterator { curr: Some(self) }
|
||||
}
|
||||
pub fn push(&self, item: T) -> Stackframe<'_, T> {
|
||||
Stackframe {
|
||||
item,
|
||||
prev: Some(self),
|
||||
len: self.len + 1
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn opush(prev: Option<&'a Self>, item: T) -> Self {
|
||||
Self {
|
||||
item,
|
||||
prev,
|
||||
len: prev.map_or(1, |s| s.len)
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn len(&self) -> usize { self.len }
|
||||
#[allow(unused)]
|
||||
pub fn pop(&self, count: usize) -> Option<&Self> {
|
||||
if count == 0 {Some(self)}
|
||||
else {self.prev.expect("Index out of range").pop(count - 1)}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn opop(cur: Option<&Self>, count: usize) -> Option<&Self> {
|
||||
if count == 0 {cur}
|
||||
else {Self::opop(cur.expect("Index out of range").prev, count - 1)}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn o_into_iter(curr: Option<&Self>) -> StackframeIterator<T> {
|
||||
StackframeIterator { curr }
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Substack<'a, T> {
|
||||
Frame(Stackframe<'a, T>),
|
||||
Bottom
|
||||
}
|
||||
|
||||
impl<'a, T> Debug for Stackframe<'a, T> where T: Debug {
|
||||
impl<'a, T> Substack<'a, T> {
|
||||
/// Convert the substack into an option of stackframe
|
||||
pub fn opt(&'a self) -> Option<&'a Stackframe<'a, T>> { match self {
|
||||
Self::Frame(f) => Some(f),
|
||||
Self::Bottom => None
|
||||
} }
|
||||
/// Construct an iterator over the listlike, very fast O(1)
|
||||
pub fn iter(&self) -> SubstackIterator<T> {
|
||||
SubstackIterator { curr: self }
|
||||
}
|
||||
pub fn push(&'a self, item: T) -> Self {
|
||||
Self::Frame(self.new_frame(item))
|
||||
}
|
||||
pub fn new_frame(&'a self, item: T) -> Stackframe<'a, T> {
|
||||
Stackframe {
|
||||
item,
|
||||
prev: &self,
|
||||
len: self.opt().map_or(1, |s| s.len)
|
||||
}
|
||||
}
|
||||
pub fn pop(&'a self, count: usize) -> Option<&'a Stackframe<'a, T>> {
|
||||
if let Self::Frame(p) = self {
|
||||
if count == 0 {Some(&p)}
|
||||
else {p.prev.pop(count - 1)}
|
||||
} else {None}
|
||||
}
|
||||
pub fn len(&self) -> usize { match self {
|
||||
Self::Frame(f) => f.len,
|
||||
Self::Bottom => 0
|
||||
} }
|
||||
}
|
||||
|
||||
impl<'a, T> Debug for Substack<'a, T> where T: Debug {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Substack")?;
|
||||
f.debug_list().entries(self.iter()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StackframeIterator<'a, T> {
|
||||
curr: Option<&'a Stackframe<'a, T>>
|
||||
pub struct SubstackIterator<'a, T> {
|
||||
curr: &'a Substack<'a, T>
|
||||
}
|
||||
|
||||
impl<'a, T> StackframeIterator<'a, T> {
|
||||
impl<'a, T> SubstackIterator<'a, T> {
|
||||
#[allow(unused)]
|
||||
pub fn first_some<U, F>(&mut self, f: F) -> Option<U>
|
||||
where F: Fn(&T) -> Option<U> {
|
||||
@@ -83,15 +73,38 @@ impl<'a, T> StackframeIterator<'a, T> {
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns an iterator that starts from the bottom of the stack
|
||||
/// and ends at the current position. This moves all items to the
|
||||
/// heap by copying them to a [Vec]
|
||||
pub fn rev_vec_clone(self) -> Vec<T> where T: Clone {
|
||||
let mut deque = VecDeque::with_capacity(self.curr.len());
|
||||
for item in self { deque.push_front(item.clone()) }
|
||||
deque.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for StackframeIterator<'a, T> {
|
||||
impl<'a, T> Copy for SubstackIterator<'a, T> {}
|
||||
impl<'a, T> Clone for SubstackIterator<'a, T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self { curr: self.curr }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for SubstackIterator<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
let curr = self.curr?;
|
||||
let item = curr.item();
|
||||
let prev = curr.prev();
|
||||
let curr = self.curr.opt()?;
|
||||
let item = &curr.item;
|
||||
let prev = curr.prev;
|
||||
self.curr = prev;
|
||||
Some(item)
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.curr.len(), Some(self.curr.len()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user