use std::cell::RefCell;
use std::rc::Rc;
pub type StackState = Rc<RefCell<Vec<i32>>>;
pub fn new_stack() -> StackState {
Rc::new(RefCell::new(Vec::new()))
}
pub fn push(state: &StackState, value: i32) {
state.borrow_mut().push(value);
}
pub fn pop(state: &StackState) -> i32 {
state.borrow_mut().pop().unwrap_or(0)
}
pub fn swap(state: &StackState) {
let mut stack = state.borrow_mut();
let len = stack.len();
if len >= 2 {
stack.swap(len - 1, len - 2);
}
}
pub fn add(state: &StackState) {
let mut stack = state.borrow_mut();
if stack.len() >= 2 {
let a = stack.pop().unwrap();
let b = stack.pop().unwrap();
stack.push(a + b);
}
}
pub fn get(state: &StackState) -> &StackState {
state
}