Lines Matching full:stack
14 /// Implementation of a `Stack` which maintains popped elements and length of previous states
15 /// in order to rewind the stack to a previous state.
17 pub struct Stack<T: Clone> { struct
18 /// All elements in the stack.
43 impl<T: Clone> Default for Stack<T> { implementation
49 impl<T: Clone> Stack<T> { implementation
50 /// Creates a new `Stack`.
52 Stack { in new()
59 /// Returns `true` if the stack is currently empty.
65 /// Returns the top-most `&T` in the `Stack`.
70 /// Pushes a `T` onto the `Stack`.
75 /// Pops the top-most `T` from the `Stack`.
91 /// Returns the size of the stack
96 /// Takes a snapshot of the current `Stack`.
109 /// Rewinds the `Stack` to the most recent `snapshot()`. If no `snapshot()` has been taken, this
110 /// function return the stack to its initial state.
137 impl<T: Clone> Index<Range<usize>> for Stack<T> { implementation
147 use super::Stack;
151 let mut stack = Stack::new(); in snapshot_with_empty() localVariable
153 stack.snapshot(); in snapshot_with_empty()
155 assert!(stack.is_empty()); in snapshot_with_empty()
157 stack.push(0); in snapshot_with_empty()
158 stack.restore(); in snapshot_with_empty()
159 assert!(stack.is_empty()); in snapshot_with_empty()
164 let mut stack = Stack::new(); in snapshot_twice() localVariable
166 stack.push(0); in snapshot_twice()
168 stack.snapshot(); in snapshot_twice()
169 stack.snapshot(); in snapshot_twice()
170 stack.restore(); in snapshot_twice()
171 stack.restore(); in snapshot_twice()
173 assert_eq!(stack[0..stack.len()], [0]); in snapshot_twice()
177 let mut stack = Stack::new(); in restore_without_snapshot() localVariable
179 stack.push(0); in restore_without_snapshot()
180 stack.restore(); in restore_without_snapshot()
182 assert_eq!(stack[0..stack.len()], [0; 0]); in restore_without_snapshot()
187 let mut stack = Stack::new(); in snapshot_pop_restore() localVariable
189 stack.push(0); in snapshot_pop_restore()
190 stack.snapshot(); in snapshot_pop_restore()
191 stack.pop(); in snapshot_pop_restore()
192 stack.restore(); in snapshot_pop_restore()
194 assert_eq!(stack[0..stack.len()], [0]); in snapshot_pop_restore()
199 let mut stack = Stack::new(); in snapshot_pop_push_restore() localVariable
201 stack.push(0); in snapshot_pop_push_restore()
202 stack.snapshot(); in snapshot_pop_push_restore()
203 stack.pop(); in snapshot_pop_push_restore()
204 stack.push(1); in snapshot_pop_push_restore()
205 stack.restore(); in snapshot_pop_push_restore()
207 assert_eq!(stack[0..stack.len()], [0]); in snapshot_pop_push_restore()
212 let mut stack = Stack::new(); in snapshot_push_pop_restore() localVariable
214 stack.push(0); in snapshot_push_pop_restore()
215 stack.snapshot(); in snapshot_push_pop_restore()
216 stack.push(1); in snapshot_push_pop_restore()
217 stack.push(2); in snapshot_push_pop_restore()
218 stack.pop(); in snapshot_push_pop_restore()
219 stack.restore(); in snapshot_push_pop_restore()
221 assert_eq!(stack[0..stack.len()], [0]); in snapshot_push_pop_restore()
226 let mut stack = Stack::new(); in snapshot_push_clear() localVariable
228 stack.push(0); in snapshot_push_clear()
229 stack.snapshot(); in snapshot_push_clear()
230 stack.push(1); in snapshot_push_clear()
231 stack.clear_snapshot(); in snapshot_push_clear()
233 assert_eq!(stack[0..stack.len()], [0, 1]); in snapshot_push_clear()
238 let mut stack = Stack::new(); in snapshot_pop_clear() localVariable
240 stack.push(0); in snapshot_pop_clear()
241 stack.push(1); in snapshot_pop_clear()
242 stack.snapshot(); in snapshot_pop_clear()
243 stack.pop(); in snapshot_pop_clear()
244 stack.clear_snapshot(); in snapshot_pop_clear()
246 assert_eq!(stack[0..stack.len()], [0]); in snapshot_pop_clear()
251 let mut stack = Stack::new(); in stack_ops() localVariable
254 assert!(stack.is_empty()); in stack_ops()
255 assert_eq!(stack.peek(), None); in stack_ops()
256 assert_eq!(stack.pop(), None); in stack_ops()
259 stack.push(0); in stack_ops()
260 assert!(!stack.is_empty()); in stack_ops()
261 assert_eq!(stack.peek(), Some(&0)); in stack_ops()
264 stack.push(1); in stack_ops()
265 assert!(!stack.is_empty()); in stack_ops()
266 assert_eq!(stack.peek(), Some(&1)); in stack_ops()
269 assert_eq!(stack.pop(), Some(1)); in stack_ops()
270 assert!(!stack.is_empty()); in stack_ops()
271 assert_eq!(stack.peek(), Some(&0)); in stack_ops()
274 stack.push(2); in stack_ops()
275 assert!(!stack.is_empty()); in stack_ops()
276 assert_eq!(stack.peek(), Some(&2)); in stack_ops()
279 stack.push(3); in stack_ops()
280 assert!(!stack.is_empty()); in stack_ops()
281 assert_eq!(stack.peek(), Some(&3)); in stack_ops()
283 // Take a snapshot of the current stack in stack_ops()
285 stack.snapshot(); in stack_ops()
288 assert_eq!(stack.pop(), Some(3)); in stack_ops()
289 assert!(!stack.is_empty()); in stack_ops()
290 assert_eq!(stack.peek(), Some(&2)); in stack_ops()
292 // Take a snapshot of the current stack in stack_ops()
294 stack.snapshot(); in stack_ops()
297 assert_eq!(stack.pop(), Some(2)); in stack_ops()
298 assert!(!stack.is_empty()); in stack_ops()
299 assert_eq!(stack.peek(), Some(&0)); in stack_ops()
302 assert_eq!(stack.pop(), Some(0)); in stack_ops()
303 assert!(stack.is_empty()); in stack_ops()
307 stack.restore(); in stack_ops()
308 assert_eq!(stack.pop(), Some(2)); in stack_ops()
309 assert_eq!(stack.pop(), Some(0)); in stack_ops()
310 assert_eq!(stack.pop(), None); in stack_ops()
314 stack.restore(); in stack_ops()
315 assert_eq!(stack.pop(), Some(3)); in stack_ops()
316 assert_eq!(stack.pop(), Some(2)); in stack_ops()
317 assert_eq!(stack.pop(), Some(0)); in stack_ops()
318 assert_eq!(stack.pop(), None); in stack_ops()