1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use bumpalo::Bump;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Node<'b, T> {
val: T,
next: Option<&'b Node<'b, T>>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Stack<'b, T> {
head: Option<&'b Node<'b, T>>,
}
impl<'b, T> Stack<'b, T> {
pub(crate) fn new() -> Self {
Self { head: None }
}
pub(crate) fn peek(&self) -> Option<&T> {
self.head.map(|n| &n.val)
}
pub(crate) fn pop(&self) -> Option<Stack<'b, T>> {
self.head.map(|n| Self { head: n.next })
}
pub(crate) fn push(&self, v: T, alloc: &'b Bump) -> Stack<'b, T> {
Self {
head: Some(alloc.alloc(Node {
val: v,
next: self.head,
})),
}
}
pub(crate) fn size(&self) -> usize {
let mut count = 0;
let mut node = &self.head;
while let Some(next) = node {
count += 1;
node = &next.next;
}
count
}
pub(crate) fn is_empty(&self) -> bool {
self.head.is_none()
}
}