Class MiniStack<E>


  • public class MiniStack<E>
    extends Object
    Extremely light stack implementation. Perfect for inlining.
    Since:
    3.10.0
    Author:
    René Schwietzke
    • 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