use std::collections::VecDeque;
use std::fmt::Debug;
use std::ops::Deref;
#[derive(Clone, Copy)]
pub struct Stackframe<'a, T> {
pub item: T,
pub prev: &'a Substack<'a, T>,
pub len: usize,
}
impl<'a, T> Deref for Stackframe<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target { &self.item }
}
#[derive(Clone, Copy)]
pub enum Substack<'a, T> {
Frame(Stackframe<'a, T>),
Bottom,
}
impl<'a, T> Substack<'a, T> {
pub fn opt(&'a self) -> Option<&'a Stackframe<'a, T>> {
match self {
Self::Frame(f) => Some(f),
Self::Bottom => None,
}
}
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 + 1) }
}
pub fn pop(&'a self, count: usize) -> &'a Self {
match (self, count) {
(_, 0) => self,
(Self::Frame(f), _) => f.prev.pop(count - 1),
(Self::Bottom, _) => panic!("Stack index out of bounds"),
}
}
pub fn len(&self) -> usize {
match self {
Self::Frame(f) => f.len,
Self::Bottom => 0,
}
}
pub fn is_empty(&self) -> bool { self.len() == 0 }
pub fn value(&self) -> Option<&T> { self.opt().map(|f| &f.item) }
pub fn unreverse(&self) -> Vec<T>
where
T: Clone,
{
self.iter().unreverse()
}
}
impl<'a, T: Debug> Debug for Substack<'a, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Substack")?;
f.debug_list().entries(self.iter()).finish()
}
}
pub struct SubstackIterator<'a, T> {
curr: &'a Substack<'a, T>,
}
impl<'a, T> SubstackIterator<'a, T> {
pub fn unreverse(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> Copy for SubstackIterator<'a, T> {}
impl<'a, T> Clone for SubstackIterator<'a, T> {
fn clone(&self) -> Self { *self }
}
impl<'a, T> Iterator for SubstackIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<&'a T> {
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()))
}
}
#[cfg(test)]
mod test {
use crate::Substack;
fn descending_ints(
num: usize,
stack: Substack<usize>,
then: impl FnOnce(Substack<usize>),
) {
match num {
0 => then(stack.push(0)),
n => descending_ints(n - 1, stack.push(n), then),
}
}
#[test]
fn general() {
descending_ints(5, Substack::Bottom, |nums| {
let rev_items = nums.iter().cloned().collect::<Vec<_>>();
assert_eq!(rev_items, [0, 1, 2, 3, 4, 5], "iterator is reversed");
assert_eq!(nums.len(), 6, "length is correct");
assert!(!nums.is_empty(), "is not empty");
assert_eq!(nums.iter().unreverse(), [5, 4, 3, 2, 1, 0], "unreverse");
assert_eq!(nums.pop(0).value(), Some(&0), "popping none");
assert_eq!(nums.pop(2).value(), Some(&2), "popping multiple");
assert!(matches!(nums.pop(6), Substack::Bottom), "popping all");
})
}
#[test]
#[should_panic]
fn out_of_bounds() {
descending_ints(5, Substack::Bottom, |nums| {
nums.pop(7);
})
}
#[test]
fn empty() {
let b = Substack::Bottom::<()>;
assert_eq!(b.len(), 0, "length computes");
assert!(b.is_empty(), "is empty");
assert_eq!(
b.pop(0).value(),
None,
"can pop nothing from empty without panic"
);
}
}