Package net.sf.saxon.tree.util
Class IndexedStack<T>
- java.lang.Object
-
- net.sf.saxon.tree.util.IndexedStack<T>
-
- All Implemented Interfaces:
java.lang.Iterable<T>
public class IndexedStack<T> extends java.lang.Object implements java.lang.Iterable<T>
This class replicates functionality that is available in a Java Stack, but not in a C# Stack, for example the ability to access elements by direct index (0-based, counting from the bottom of the stack). It is used in place ofStack
in the few places where this functionality is needed.The stack is iterable, and iteration is in bottom-to-top order (like Java, but unlike a C# stack where iteration is top-to-bottom)
-
-
Constructor Summary
Constructors Constructor Description IndexedStack()
Create an empty stack with a default initial space allocationIndexedStack(int size)
Create an empty stack with a specified initial space allocation
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(T value)
Search for an item on the stackT
get(int i)
Get the item at position N in the stack, without changing the state of the stackint
indexOf(T value)
Search for an item on the stack, starting from the bottomboolean
isEmpty()
Ask if the stack is emptyjava.util.Iterator<T>
iterator()
Iterate the stack in bottom to top orderT
peek()
Get the item on the top of the stack, without changing the state of the stackT
pop()
Get the item on the top of the stack, and remove it from the stackvoid
push(T item)
Add an item to the top of the stackvoid
set(int i, T value)
Overwrite the item at position N in the stackint
size()
Get the current height of the stack
-
-
-
Method Detail
-
size
public int size()
Get the current height of the stack- Returns:
- the number of items on the stack
-
isEmpty
public boolean isEmpty()
Ask if the stack is empty- Returns:
- true if the stack contains no items
-
push
public void push(T item)
Add an item to the top of the stack- Parameters:
item
- the item to be added
-
peek
public T peek()
Get the item on the top of the stack, without changing the state of the stack- Returns:
- the topmost item
- Throws:
java.util.EmptyStackException
- if the stack is empty
-
pop
public T pop()
Get the item on the top of the stack, and remove it from the stack- Returns:
- the topmost item
- Throws:
java.util.EmptyStackException
- if the stack is empty
-
get
public T get(int i)
Get the item at position N in the stack, without changing the state of the stack- Parameters:
i
- the position of the required item, where the first item counting from the bottom of the stack is position 0 (zero)- Returns:
- the item at the specified position
- Throws:
java.lang.IndexOutOfBoundsException
- if {code i} is negative, or greater than or equal to the stack size
-
set
public void set(int i, T value)
Overwrite the item at position N in the stack- Parameters:
i
- the position of the required item, where the first item counting from the bottom of the stack is position 0 (zero)value
- the item to be put at the specified position- Throws:
java.lang.IndexOutOfBoundsException
- if {code i} is negative, or greater than or equal to the stack size
-
contains
public boolean contains(T value)
Search for an item on the stack- Parameters:
value
- the item being sought- Returns:
- true if the stack contains an item equal to the sought item
-
indexOf
public int indexOf(T value)
Search for an item on the stack, starting from the bottom- Parameters:
value
- the item being sought- Returns:
- the index position of the first item found that is equal to
value
, or -1 if no such item is found
-
-