| 1 | use super::CFFError; |
| 2 | |
| 3 | pub struct ArgumentsStack<'a> { |
| 4 | pub data: &'a mut [f32], |
| 5 | pub len: usize, |
| 6 | pub max_len: usize, |
| 7 | } |
| 8 | |
| 9 | impl<'a> ArgumentsStack<'a> { |
| 10 | #[inline ] |
| 11 | pub fn len(&self) -> usize { |
| 12 | self.len |
| 13 | } |
| 14 | |
| 15 | #[inline ] |
| 16 | pub fn is_empty(&self) -> bool { |
| 17 | self.len == 0 |
| 18 | } |
| 19 | |
| 20 | #[inline ] |
| 21 | pub fn push(&mut self, n: f32) -> Result<(), CFFError> { |
| 22 | if self.len == self.max_len { |
| 23 | Err(CFFError::ArgumentsStackLimitReached) |
| 24 | } else { |
| 25 | self.data[self.len] = n; |
| 26 | self.len += 1; |
| 27 | Ok(()) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | #[inline ] |
| 32 | pub fn at(&self, index: usize) -> f32 { |
| 33 | self.data[index] |
| 34 | } |
| 35 | |
| 36 | #[inline ] |
| 37 | pub fn pop(&mut self) -> f32 { |
| 38 | debug_assert!(!self.is_empty()); |
| 39 | self.len -= 1; |
| 40 | self.data[self.len] |
| 41 | } |
| 42 | |
| 43 | #[inline ] |
| 44 | pub fn reverse(&mut self) { |
| 45 | if self.is_empty() { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Reverse only the actual data and not the whole stack. |
| 50 | let (first, _) = self.data.split_at_mut(self.len); |
| 51 | first.reverse(); |
| 52 | } |
| 53 | |
| 54 | #[inline ] |
| 55 | pub fn clear(&mut self) { |
| 56 | self.len = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl core::fmt::Debug for ArgumentsStack<'_> { |
| 61 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 62 | f.debug_list().entries(&self.data[..self.len]).finish() |
| 63 | } |
| 64 | } |
| 65 | |