Class IntStack


  • public class IntStack
    extends Object
    Growable int stack with type specific access methods. This implementation is unsynchronized in order to provide the best possible performance for typical usage scenarios, so explicit synchronization must be implemented by a wrapper class or directly by the application in cases where instances are modified in a multithreaded environment. See the base classes for other details of the implementation.
    Version:
    1.0
    Author:
    Dennis M. Sosnoski
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_SIZE
      Default initial array size.
      protected int[] m_baseArray
      The underlying array used for storing the data.
      protected int m_countLimit
      Size of the current array.
      protected int m_countPresent
      The number of values currently present in the stack.
      protected int m_maximumGrowth
      Maximum size increment for growing array.
    • Constructor Summary

      Constructors 
      Constructor Description
      IntStack()
      Default constructor.
      IntStack​(int size)
      Constructor with initial size specified.
      IntStack​(int[] ints)
      Constructor from array of ints.
      IntStack​(int size, int growth)
      Constructor with full specification.
      IntStack​(IntStack base)
      Copy (clone) constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Set the stack to the empty state.
      Object clone()
      Duplicates the object with the generic call.
      void ensureCapacity​(int min)
      Ensure that the array has the capacity for at least the specified number of values.
      boolean isEmpty()
      Check if stack is empty.
      int peek()
      Copy top value from the stack.
      int peek​(int depth)
      Copy a value from the stack.
      int pop()
      Pop a value from the stack.
      int pop​(int count)
      Pop multiple values from the stack.
      void push​(int value)
      Push a value on the stack.
      int size()
      Get the number of values currently present in the stack.
      int[] toArray()
      Constructs and returns a simple array containing the same data as held in this stack.
    • Field Detail

      • DEFAULT_SIZE

        public static final int DEFAULT_SIZE
        Default initial array size.
        See Also:
        Constant Field Values
      • m_countLimit

        protected int m_countLimit
        Size of the current array.
      • m_countPresent

        protected int m_countPresent
        The number of values currently present in the stack.
      • m_maximumGrowth

        protected int m_maximumGrowth
        Maximum size increment for growing array.
      • m_baseArray

        protected int[] m_baseArray
        The underlying array used for storing the data.
    • Constructor Detail

      • IntStack

        public IntStack​(int size,
                        int growth)
        Constructor with full specification.
        Parameters:
        size - number of int values initially allowed in stack
        growth - maximum size increment for growing stack
      • IntStack

        public IntStack​(int size)
        Constructor with initial size specified.
        Parameters:
        size - number of int values initially allowed in stack
      • IntStack

        public IntStack()
        Default constructor.
      • IntStack

        public IntStack​(IntStack base)
        Copy (clone) constructor.
        Parameters:
        base - instance being copied
      • IntStack

        public IntStack​(int[] ints)
        Constructor from array of ints.
        Parameters:
        ints - array of ints for initial contents
    • Method Detail

      • ensureCapacity

        public final void ensureCapacity​(int min)
        Ensure that the array has the capacity for at least the specified number of values.
        Parameters:
        min - minimum capacity to be guaranteed
      • push

        public void push​(int value)
        Push a value on the stack.
        Parameters:
        value - value to be added
      • pop

        public int pop()
        Pop a value from the stack.
        Returns:
        value from top of stack
        Throws:
        ArrayIndexOutOfBoundsException - on attempt to pop empty stack
      • pop

        public int pop​(int count)
        Pop multiple values from the stack. The last value popped is the one returned.
        Parameters:
        count - number of values to pop from stack (must be strictly positive)
        Returns:
        value from top of stack
        Throws:
        ArrayIndexOutOfBoundsException - on attempt to pop past end of stack
      • peek

        public int peek​(int depth)
        Copy a value from the stack. This returns a value from within the stack without modifying the stack.
        Parameters:
        depth - depth of value to be returned
        Returns:
        value from stack
        Throws:
        ArrayIndexOutOfBoundsException - on attempt to peek past end of stack
      • peek

        public int peek()
        Copy top value from the stack. This returns the top value without removing it from the stack.
        Returns:
        value at top of stack
        Throws:
        ArrayIndexOutOfBoundsException - on attempt to peek empty stack
      • toArray

        public int[] toArray()
        Constructs and returns a simple array containing the same data as held in this stack. Note that the items will be in reverse pop order, with the last item to be popped from the stack as the first item in the array.
        Returns:
        array containing a copy of the data
      • clone

        public Object clone()
        Duplicates the object with the generic call.
        Overrides:
        clone in class Object
        Returns:
        a copy of the object
      • size

        public int size()
        Get the number of values currently present in the stack.
        Returns:
        count of values present
      • isEmpty

        public boolean isEmpty()
        Check if stack is empty.
        Returns:
        true if stack empty, false if not
      • clear

        public void clear()
        Set the stack to the empty state.