Class MiniStack<E>

java.lang.Object
org.htmlunit.cyberneko.util.MiniStack<E>

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

    Fields
    Modifier and Type
    Field
    Description
    private Object[]
     
    private int
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    Empties the stack.
    boolean
    Checks if we have any element at all on the stack.
    Returns the top/last element without removing it.
    pop()
    Removes and returns the last element
    void
    push(E element)
    Add a new element on top of the stack.
    void
    Resets the stack to zero but does not clean it.
    int
    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 Details

    • elements_

      private Object[] elements_
    • pos_

      private int pos_
  • Constructor Details

    • 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 Details

    • 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