2.5 Stack

The Stack class is a templated container acting as a last in, first out (LIFO) array. It can be either static or dynamic. Static means it’s allocated once in memory and will not autoresize. When full it will throw an overflow exception. Dynamic means the container will autoresize as new items are pushed on the stack. An overflow exception will then occure only if available memory is exhausted.


Synopsis


  #include <lyric/Stack.hpp>
  
  template <class SIT>
  class Stack : private List<SIT>
  {
  public:
    ~Stack ();
    Stack ();
    Stack (Size initsize, Size resize = 0)
      throw (Exception::Memory::Alloc);
    Stack (const Stack& stack)
      throw (Exception::Memory::Alloc);
    Stack& operator = (const Stack& stack)
      throw (Exception::Memory::Alloc);
    void create (Size initsize, Size resize = 0)
      throw (Exception::Memory::Alloc);
    void clean ();
    bool empty () const;
    void push (const SIT& item)
      throw (Exception::Stack::Overflow);
    SIT pop ()
      throw (Exception::Stack::Underflow);
    const SIT& top () const
      throw (Exception::Stack::Underflow);
  };


Description


˜Stack ()
Destroys this stack, releasing eventually reserved memory. All stored items are destroyed.

Stack ()
Constucts this stack as dynamic stack with a resize size of 1.

Stack (Size initsize, Size resize = 0)
Constructs this stack with a starting memory size initsize and a given resize factor. If resize is zero (or not set), the stack is defined static and will not grow over the given initsize. If resize is greater than zero, the stack will automatically grow as items are pushed on.
|\ Exception::Memory::Alloc
is thrown if not enough memory is found for the initial size.

Stack (const Stack& stack)
Constructs this from stack. All properties, content, and status of stack are given to this.
|\ Exception::Memory::Alloc
is thrown if not enough memory is found for this to clone stack.

Stack& operator = (const Stack& stack)
Clones stack into this. All properties, content, and status of stack are given to this.
|\ Exception::Memory::Alloc
is thrown if not enough memory is found for this to clone stack.

void create (Size initsize, Size resize = 0)
Creates this stack with a starting memory size initsize and a given resize factor. If resize is zero (or not set), the stack is defined static and will not grow over the given initsize. If resize is greater than zero, the stack will automatically grow as items are pushed on.
Warning  Create is destructive. It will destroy all items previously stored in this stack.
|\ Exception::Memory::Alloc
is thrown if not enough memory is found for the initial size.

void clean ()
Cleans the content of this stack. Destroys all stored items and sets stack pointer to zero.

bool empty () const
Returns true if this stack is empty, false if not.

void push (const SIT& item)
Pushes item on this stack, and increments the stack pointer.
|\ Exception::Stack::Overflow
is thrown if this stack was defined static and was full, or if the stack was defined dynamic and no more memory is left on the system to store the extra item.

SIT pop ()
Pops the top item from this stack, decrements the stack pointer, and returns the item.
|\ Exception::Stack::Underflow
is thrown if this stack was empty before the pop.

const SIT& top () const
Returns the top most item from this stack, without poping it.
|\ Exception::Stack::Underflow
is thrown if this stack is empty.