microsat/
stack.rs

1/// A simple stack implementation.
2#[derive(Clone)]
3pub struct Stack<T> {
4    stack: Vec<T>,
5}
6
7impl<T> Stack<T> {
8    /// Initializes a new stack with the given capacity.
9    pub fn new(capacity: usize) -> Stack<T> {
10        Stack {
11            stack: Vec::with_capacity(capacity),
12        }
13    }
14
15    /// Pushes a new value onto the stack.
16    #[inline]
17    pub fn push(&mut self, value: T) {
18        self.stack.push(value);
19    }
20
21    /// Pops the top value from the stack.
22    #[inline]
23    pub fn pop(&mut self) -> Option<T> {
24        self.stack.pop()
25    }
26
27    /// Returns the length of the stack.
28    #[inline]
29    pub fn len(&self) -> usize {
30        self.stack.len()
31    }
32
33    /// Returns true if the stack is empty.
34    #[inline]
35    pub fn is_empty(&self) -> bool {
36        self.stack.is_empty()
37    }
38
39    /// Peeks at the top value of the stack.
40    #[inline]
41    pub fn peek(&self) -> &T {
42        self.stack.last().unwrap()
43    }
44
45    /// Checks if the stack contains a value.
46    #[inline]
47    pub fn contains(&self, value: &T) -> bool
48    where
49        T: PartialEq,
50    {
51        self.stack.contains(value)
52    }
53}
54
55impl Stack<(u16, bool)> {
56    pub fn iter(&self) -> std::slice::Iter<(u16, bool)> {
57        self.stack.iter()
58    }
59}
60
61impl IntoIterator for Stack<(u16, bool)> {
62    type Item = (u16, bool);
63    type IntoIter = std::vec::IntoIter<(u16, bool)>;
64
65    fn into_iter(self) -> Self::IntoIter {
66        self.stack.into_iter()
67    }
68}