Package org.parboiled.support
Class DefaultValueStack<V>
java.lang.Object
org.parboiled.support.DefaultValueStack<V>
- Type Parameters:
V
- the type of the value objects
- All Implemented Interfaces:
Iterable<V>
,ValueStack<V>
- Direct Known Subclasses:
DebuggingValueStack
An implementation of a stack of value objects providing an efficient snapshot capability and a number of convenience
methods. The current state of the stack can be saved and restored in small constant time with the methods
takeSnapshot()
and restoreSnapshot(Object)
()}. The implementation also serves as an Iterable
over the current stack values (the values are being provided with the last value (on top of the stack) first).-
Constructor Summary
ConstructorDescriptionInitializes an empty value stack.DefaultValueStack
(Iterable<V> values) Initializes a value stack containing the given values with the last value being at the top of the stack. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears all values.void
dup()
Duplicates the top value.boolean
isEmpty()
Determines whether the stack is empty.iterator()
peek()
Returns the value at the top of the stack without removing it.peek
(int down) Returns the value the given number of elements below the top of the stack without removing it.void
Replaces the element the given number of elements below the current top of the stack.void
Replaces the current top value with the given value.pop()
Removes the value at the top of the stack and returns it.pop
(int down) Removes the value the given number of elements below the top of the stack.void
Inserts the given value a given number of elements below the current top of the stack.void
Pushes the given value onto the stack.void
Pushes all given elements onto the stack (in the order as given).void
Pushes all given elements onto the stack (in the order as given).void
restoreSnapshot
(Object snapshot) Restores the stack state as previously returned byValueStack.takeSnapshot()
.int
size()
Returns the number of elements currently on the stack.void
swap()
Swaps the top two stack values.void
swap3()
Reverses the order of the top 3 stack values.void
swap4()
Reverses the order of the top 4 stack values.void
swap5()
Reverses the order of the top 5 stack values.void
swap6()
Reverses the order of the top 5 stack values.Returns an object representing the current state of the stack.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DefaultValueStack
public DefaultValueStack()Initializes an empty value stack. -
DefaultValueStack
Initializes a value stack containing the given values with the last value being at the top of the stack.- Parameters:
values
- the initial stack values
-
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from interface:ValueStack
Determines whether the stack is empty.- Specified by:
isEmpty
in interfaceValueStack<V>
- Returns:
- true if empty
-
size
public int size()Description copied from interface:ValueStack
Returns the number of elements currently on the stack.- Specified by:
size
in interfaceValueStack<V>
- Returns:
- the number of elements
-
clear
public void clear()Description copied from interface:ValueStack
Clears all values.- Specified by:
clear
in interfaceValueStack<V>
-
takeSnapshot
Description copied from interface:ValueStack
Returns an object representing the current state of the stack. This cost of running this operation is negligible and independent from the size of the stack.- Specified by:
takeSnapshot
in interfaceValueStack<V>
- Returns:
- an object representing the current state of the stack
-
restoreSnapshot
Description copied from interface:ValueStack
Restores the stack state as previously returned byValueStack.takeSnapshot()
. This cost of running this operation is negligible and independent from the size of the stack.- Specified by:
restoreSnapshot
in interfaceValueStack<V>
- Parameters:
snapshot
- a snapshot object previously returned byValueStack.takeSnapshot()
-
push
Description copied from interface:ValueStack
Pushes the given value onto the stack. Equivalent to push(0, value).- Specified by:
push
in interfaceValueStack<V>
- Parameters:
value
- the value
-
push
Description copied from interface:ValueStack
Inserts the given value a given number of elements below the current top of the stack.- Specified by:
push
in interfaceValueStack<V>
- Parameters:
down
- the number of elements to skip before inserting the value (0 being equivalent to push(value))value
- the value
-
pushAll
Description copied from interface:ValueStack
Pushes all given elements onto the stack (in the order as given).- Specified by:
pushAll
in interfaceValueStack<V>
- Parameters:
firstValue
- the first valuemoreValues
- the other values
-
pushAll
Description copied from interface:ValueStack
Pushes all given elements onto the stack (in the order as given).- Specified by:
pushAll
in interfaceValueStack<V>
- Parameters:
values
- the values
-
pop
Description copied from interface:ValueStack
Removes the value at the top of the stack and returns it.- Specified by:
pop
in interfaceValueStack<V>
- Returns:
- the current top value
-
pop
Description copied from interface:ValueStack
Removes the value the given number of elements below the top of the stack.- Specified by:
pop
in interfaceValueStack<V>
- Parameters:
down
- the number of elements to skip before removing the value (0 being equivalent to pop())- Returns:
- the value
-
peek
Description copied from interface:ValueStack
Returns the value at the top of the stack without removing it.- Specified by:
peek
in interfaceValueStack<V>
- Returns:
- the current top value
-
peek
Description copied from interface:ValueStack
Returns the value the given number of elements below the top of the stack without removing it.- Specified by:
peek
in interfaceValueStack<V>
- Parameters:
down
- the number of elements to skip (0 being equivalent to peek())- Returns:
- the value
-
poke
Description copied from interface:ValueStack
Replaces the current top value with the given value. Equivalent to poke(0, value).- Specified by:
poke
in interfaceValueStack<V>
- Parameters:
value
- the value
-
poke
Description copied from interface:ValueStack
Replaces the element the given number of elements below the current top of the stack.- Specified by:
poke
in interfaceValueStack<V>
- Parameters:
down
- the number of elements to skip before replacing the value (0 being equivalent to poke(value))value
- the value to replace with
-
dup
public void dup()Description copied from interface:ValueStack
Duplicates the top value. Equivalent to push(peek()).- Specified by:
dup
in interfaceValueStack<V>
-
swap
public void swap()Description copied from interface:ValueStack
Swaps the top two stack values.- Specified by:
swap
in interfaceValueStack<V>
-
swap3
public void swap3()Description copied from interface:ValueStack
Reverses the order of the top 3 stack values.- Specified by:
swap3
in interfaceValueStack<V>
-
swap4
public void swap4()Description copied from interface:ValueStack
Reverses the order of the top 4 stack values.- Specified by:
swap4
in interfaceValueStack<V>
-
swap5
public void swap5()Description copied from interface:ValueStack
Reverses the order of the top 5 stack values.- Specified by:
swap5
in interfaceValueStack<V>
-
swap6
public void swap6()Description copied from interface:ValueStack
Reverses the order of the top 5 stack values.- Specified by:
swap6
in interfaceValueStack<V>
-
iterator
-