-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.rs
More file actions
92 lines (75 loc) · 1.99 KB
/
stack.rs
File metadata and controls
92 lines (75 loc) · 1.99 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use core::iter::Rev;
use core::slice::Iter;
use std::fmt;
use std::ops::{Index, IndexMut};
#[derive(Debug)]
pub struct Stack<T> {
storage: Vec<T>,
}
impl<T> Index<usize> for Stack<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.storage[index]
}
}
impl<T> IndexMut<usize> for Stack<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.storage[index]
}
}
impl<T> Stack<T> {
pub fn new() -> Self {
Stack { storage: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
Stack { storage: Vec::with_capacity(capacity) }
}
pub fn push(&mut self, item: T) {
self.storage.push(item);
}
pub fn pop(&mut self) -> Option<T> {
self.storage.pop()
}
/// Pop top N items if at least N items are present. The top item in
/// the stack will be at the *end* of list of returned items.
pub fn pop_n(&mut self, n: usize) -> Option<Vec<T>> {
let size = self.len();
if size < n {
None
} else {
let items = self.storage.split_off(size - n);
Some(items)
}
}
pub fn peek(&self) -> Option<&T> {
self.storage.last()
}
pub fn peek_mut(&mut self) -> Option<&mut T> {
self.storage.last_mut()
}
pub fn is_empty(&self) -> bool {
self.storage.is_empty()
}
pub fn len(&self) -> usize {
self.storage.len()
}
#[cfg(test)]
pub fn clear(&mut self) {
self.storage.clear()
}
pub fn truncate(&mut self, len: usize) {
self.storage.truncate(len)
}
/// NOTE: This iterates in REVERSE order (from stack top to bottom)
pub fn iter(&self) -> Rev<Iter<T>> {
self.storage.iter().rev()
}
}
impl fmt::Display for Stack<usize> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for index in self.iter() {
write!(f, "{index}")?;
}
write!(f, "")
}
}