Class MiniStack<E>


  • public class MiniStack<E>
    extends java.lang.Object
    Extremely light stack implementation. Perfect for inlining.
    Since:
    3.10.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object[] elements_  
      private int pos_  
    • Constructor Summary

      Constructors 
      Constructor Description
      MiniStack()
      Create a new empty stack with 8 elements capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Empties the stack.
      boolean isEmpty()
      Checks if we have any element at all on the stack.
      E peek()
      Returns the top/last element without removing it.
      E pop()
      Removes and returns the last element
      void push​(E element)
      Add a new element on top of the stack.
      void reset()
      Resets the stack to zero but does not clean it.
      int size()
      Returns the current size of the stack.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • elements_

        private java.lang.Object[] elements_
      • pos_

        private int pos_
    • Constructor Detail

      • MiniStack

        public MiniStack()
        Create a new empty stack with 8 elements capacity.

        A reference are typically (less than 32 GB memory) 4 bytes. The overhead of any object is about 24 bytes, give or take, hence 8*4 + 24 fits a cache line of 64 bytes.

    • Method Detail

      • clear

        public void clear()
        Empties the stack. It will not only reset the pointer but empty out the backing array to ensure we are not holding old references.
      • reset

        public void reset()
        Resets the stack to zero but does not clean it. Use with caution to avoid holding old objects and preventing them from GC.
      • pop

        public E pop()
        Removes and returns the last element
        Returns:
        the last element or null of none
      • peek

        public E peek()
        Returns the top/last element without removing it. Will return null if we don't have a last element.
        Returns:
        the top/last element if any, null otherwise
      • push

        public void push​(E element)
        Add a new element on top of the stack. You can add null but the return value of pop and peek will become ambiguous.
        Parameters:
        element - the element to add
      • isEmpty

        public boolean isEmpty()
        Checks if we have any element at all on the stack.
        Returns:
        true if the stack holds elements, false otherwise
      • size

        public int size()
        Returns the current size of the stack.
        Returns:
        the size of the stack, always >= 0