V
- the type of the value objectspublic class DefaultValueStack<V> extends java.lang.Object implements ValueStack<V>
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).Modifier and Type | Class and Description |
---|---|
protected static class |
DefaultValueStack.Element |
Modifier and Type | Field and Description |
---|---|
protected DefaultValueStack.Element |
head |
protected V |
tempValue |
Constructor and Description |
---|
DefaultValueStack()
Initializes an empty value stack.
|
DefaultValueStack(java.lang.Iterable<V> values)
Initializes a value stack containing the given values with the last value being at the top of the stack.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all values.
|
void |
dup()
Duplicates the top value.
|
boolean |
isEmpty()
Determines whether the stack is empty.
|
private static boolean |
isSizeGTE(int minSize,
DefaultValueStack.Element head) |
java.util.Iterator<V> |
iterator() |
V |
peek()
Returns the value at the top of the stack without removing it.
|
V |
peek(int down)
Returns the value the given number of elements below the top of the stack without removing it.
|
private static java.lang.Object |
peek(int down,
DefaultValueStack.Element head) |
private static DefaultValueStack.Element |
poke(int down,
java.lang.Object value,
DefaultValueStack.Element head) |
void |
poke(int down,
V value)
Replaces the element the given number of elements below the current top of the stack.
|
void |
poke(V value)
Replaces the current top value with the given value.
|
V |
pop()
Removes the value at the top of the stack and returns it.
|
V |
pop(int down)
Removes the value the given number of elements below the top of the stack.
|
private DefaultValueStack.Element |
pop(int down,
DefaultValueStack.Element head) |
private static DefaultValueStack.Element |
push(int down,
java.lang.Object value,
DefaultValueStack.Element head) |
void |
push(int down,
V value)
Inserts the given value a given number of elements below the current top of the stack.
|
void |
push(V value)
Pushes the given value onto the stack.
|
void |
pushAll(java.lang.Iterable<V> values)
Pushes all given elements onto the stack (in the order as given).
|
void |
pushAll(V firstValue,
V... moreValues)
Pushes all given elements onto the stack (in the order as given).
|
void |
restoreSnapshot(java.lang.Object snapshot)
Restores the stack state as previously returned by
ValueStack.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.
|
java.lang.Object |
takeSnapshot()
Returns an object representing the current state of the stack.
|
protected DefaultValueStack.Element head
protected V tempValue
public DefaultValueStack()
public DefaultValueStack(java.lang.Iterable<V> values)
values
- the initial stack valuespublic boolean isEmpty()
ValueStack
isEmpty
in interface ValueStack<V>
public int size()
ValueStack
size
in interface ValueStack<V>
public void clear()
ValueStack
clear
in interface ValueStack<V>
public java.lang.Object takeSnapshot()
ValueStack
takeSnapshot
in interface ValueStack<V>
public void restoreSnapshot(java.lang.Object snapshot)
ValueStack
ValueStack.takeSnapshot()
.
This cost of running this operation is negligible and independent from the size of the stack.restoreSnapshot
in interface ValueStack<V>
snapshot
- a snapshot object previously returned by ValueStack.takeSnapshot()
public void push(V value)
ValueStack
push
in interface ValueStack<V>
value
- the valuepublic void push(int down, V value)
ValueStack
push
in interface ValueStack<V>
down
- the number of elements to skip before inserting the value (0 being equivalent to push(value))value
- the valueprivate static DefaultValueStack.Element push(int down, java.lang.Object value, DefaultValueStack.Element head)
public void pushAll(V firstValue, V... moreValues)
ValueStack
pushAll
in interface ValueStack<V>
firstValue
- the first valuemoreValues
- the other valuespublic void pushAll(java.lang.Iterable<V> values)
ValueStack
pushAll
in interface ValueStack<V>
values
- the valuespublic V pop()
ValueStack
pop
in interface ValueStack<V>
public V pop(int down)
ValueStack
pop
in interface ValueStack<V>
down
- the number of elements to skip before removing the value (0 being equivalent to pop())private DefaultValueStack.Element pop(int down, DefaultValueStack.Element head)
public V peek()
ValueStack
peek
in interface ValueStack<V>
public V peek(int down)
ValueStack
peek
in interface ValueStack<V>
down
- the number of elements to skip (0 being equivalent to peek())private static java.lang.Object peek(int down, DefaultValueStack.Element head)
public void poke(V value)
ValueStack
poke
in interface ValueStack<V>
value
- the valuepublic void poke(int down, V value)
ValueStack
poke
in interface ValueStack<V>
down
- the number of elements to skip before replacing the value (0 being equivalent to poke(value))value
- the value to replace withprivate static DefaultValueStack.Element poke(int down, java.lang.Object value, DefaultValueStack.Element head)
public void dup()
ValueStack
dup
in interface ValueStack<V>
public void swap()
ValueStack
swap
in interface ValueStack<V>
public void swap3()
ValueStack
swap3
in interface ValueStack<V>
public void swap4()
ValueStack
swap4
in interface ValueStack<V>
public void swap5()
ValueStack
swap5
in interface ValueStack<V>
public void swap6()
ValueStack
swap6
in interface ValueStack<V>
private static boolean isSizeGTE(int minSize, DefaultValueStack.Element head)