-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.rs
49 lines (39 loc) · 905 Bytes
/
stack.rs
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
use std::fmt;
use arrayvec::ArrayVec;
use ethnum::U256;
#[derive(Clone, Default, Debug)]
pub struct Stack {
stack: ArrayVec<U256, 1024>,
}
impl Stack {
pub fn new() -> Self {
Self {
stack: ArrayVec::new(),
}
}
pub fn push(&mut self, item: U256) {
self.stack.push(item);
}
pub fn pop(&mut self) -> Option<U256> {
self.stack.pop()
}
pub fn len(&self) -> usize {
self.stack.len()
}
pub fn is_empty(&self) -> bool {
self.stack.len() == 0
}
pub fn get(&self, pos: usize) -> Option<&U256> {
self.stack.get(self.len() - 1 - pos)
}
pub fn swap_top(&mut self, pos: usize) {
let top = self.len() - 1;
let swap_pos = self.len() - 1 - pos;
self.stack.swap(top, swap_pos);
}
}
impl fmt::Display for Stack {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Stack: {:?}", self.stack)
}
}