Use Vec instead of VecDeque.

This commit is contained in:
Sergey Pepyakin 2018-06-14 16:35:36 +03:00
parent e9f201bde9
commit de27ef3745
2 changed files with 8 additions and 15 deletions

View File

@ -1,5 +1,4 @@
use std::collections::VecDeque;
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -22,7 +21,7 @@ impl error::Error for Error {
#[derive(Debug)] #[derive(Debug)]
pub struct StackWithLimit<T> where T: Clone { pub struct StackWithLimit<T> where T: Clone {
/// Stack values. /// Stack values.
values: VecDeque<T>, values: Vec<T>,
/// Stack limit (maximal stack len). /// Stack limit (maximal stack len).
limit: usize, limit: usize,
} }
@ -30,7 +29,7 @@ pub struct StackWithLimit<T> where T: Clone {
impl<T> StackWithLimit<T> where T: Clone { impl<T> StackWithLimit<T> where T: Clone {
pub fn with_limit(limit: usize) -> Self { pub fn with_limit(limit: usize) -> Self {
StackWithLimit { StackWithLimit {
values: VecDeque::new(), values: Vec::new(),
limit: limit limit: limit
} }
} }
@ -43,19 +42,17 @@ impl<T> StackWithLimit<T> where T: Clone {
self.values.len() self.values.len()
} }
pub fn limit(&self) -> usize {
self.limit
}
pub fn top(&self) -> Result<&T, Error> { pub fn top(&self) -> Result<&T, Error> {
let len = self.values.len();
self.values self.values
.back() .get(len - 1)
.ok_or_else(|| Error("non-empty stack expected".into())) .ok_or_else(|| Error("non-empty stack expected".into()))
} }
pub fn top_mut(&mut self) -> Result<&mut T, Error> { pub fn top_mut(&mut self) -> Result<&mut T, Error> {
let len = self.values.len();
self.values self.values
.back_mut() .get_mut(len - 1)
.ok_or_else(|| Error("non-empty stack expected".into())) .ok_or_else(|| Error("non-empty stack expected".into()))
} }
@ -80,13 +77,13 @@ impl<T> StackWithLimit<T> where T: Clone {
return Err(Error(format!("exceeded stack limit {}", self.limit))); return Err(Error(format!("exceeded stack limit {}", self.limit)));
} }
self.values.push_back(value); self.values.push(value);
Ok(()) Ok(())
} }
pub fn pop(&mut self) -> Result<T, Error> { pub fn pop(&mut self) -> Result<T, Error> {
self.values self.values
.pop_back() .pop()
.ok_or_else(|| Error("non-empty stack expected".into())) .ok_or_else(|| Error("non-empty stack expected".into()))
} }

View File

@ -1216,10 +1216,6 @@ impl ValueStack {
self.stack_with_limit.len() self.stack_with_limit.len()
} }
fn limit(&self) -> usize {
self.stack_with_limit.limit()
}
fn top(&self) -> &RuntimeValue { fn top(&self) -> &RuntimeValue {
self.stack_with_limit.top().expect("Due to validation stack shouldn't be empty") self.stack_with_limit.top().expect("Due to validation stack shouldn't be empty")
} }