Stack< T > Class Template Reference

A simple stack class which implements the standard push, pop, and peek methods. More...

#include <rimPhysicsStack.h>

List of all members.

Public Member Functions

 Stack ()
 Create a new empty stack data structure with a default capacity.
 Stack (int capacity)
 Create a new empty stack data structure with an initial capacity.
 ~Stack ()
 Destroy the stack.
void push (T newEntry)
 Push a new element onto the stack.
peek () const
 Return the top element of the stack without removing it.
peek (int depth) const
 Return the element of the stack at the specified depth without removing it.
pop ()
 Return the top element of the stack and remove it.
bool isEmpty () const
 Return whether or not the stack is empty (has no elements).
int getSize () const
 Return the number of elements in the stack.
void clear ()
 Remove all elements from the stack, such that it is empty.

Private Member Functions

void initialize (unsigned int newStackSize)
 Initialize the stack to have an array of the specified size.
void resize ()
 Double the size of the stack array.

Private Attributes

T * stack
 the stack array
int numElements
 the number of elements in the stack
int capacity
 the size of the stack array

Static Private Attributes

static const int DEFAULT_INITIAL_CAPACITY = 8
 the default size of a stack array if no size is specified


Detailed Description

template<typename T>
class Stack< T >

A simple stack class which implements the standard push, pop, and peek methods.

This class is an array-based stack class. It is a first-in-first-out data structure. One places an element on the stack by calling push(), and removes an element by calling pop(). One can return the top value on the stack without changing the stack by calling peek(). Other attributes such as the size of the stack can be queried. This data structure is not thread safe.


Constructor & Destructor Documentation

template<typename T>
Stack< T >::Stack (  )  [inline]

Create a new empty stack data structure with a default capacity.

The size of the stack array is initialized to be DEFAULT_INITIAL_CAPACITY.

template<typename T>
Stack< T >::Stack ( int  capacity  )  [inline]

Create a new empty stack data structure with an initial capacity.

The initial capacity of the stack array can be set by the parameter, which can be used to pad the stack to avoid having to resize the stack often.

Parameters:
capacity - the initial capacity of the stack array

template<typename T>
Stack< T >::~Stack (  )  [inline]

Destroy the stack.


Member Function Documentation

template<typename T>
void Stack< T >::push ( newEntry  )  [inline]

Push a new element onto the stack.

NULL values are allowed.

Parameters:
newEntry - the new stack entry

template<typename T>
T Stack< T >::peek (  )  const [inline]

Return the top element of the stack without removing it.

If there are no elements in the stack, then a rimEmptyStackException is thrown.

Returns:
the top element of the stack

template<typename T>
T Stack< T >::peek ( int  depth  )  const [inline]

Return the element of the stack at the specified depth without removing it.

The depth at which one is peeking is an integer ranging from 0 to the size of the stack minus 1. Depth zero specifies the element at the top of the stack.

Returns:
the element of the stack at the specified depth.

template<typename T>
T Stack< T >::pop (  )  [inline]

Return the top element of the stack and remove it.

If there are no elements in the stack, then a rimEmptyStackException is thrown.

Returns:
the top element of the stack or NULL if stack is empty

template<typename T>
bool Stack< T >::isEmpty (  )  const [inline]

Return whether or not the stack is empty (has no elements).

Returns TRUE if the size of the stack is 0, otherwise it returns FALSE.

Returns:
whether or not the stack is empty

template<typename T>
int Stack< T >::getSize (  )  const [inline]

Return the number of elements in the stack.

Returns:
the number of elements in the stack

template<typename T>
void Stack< T >::clear (  )  [inline]

Remove all elements from the stack, such that it is empty.

template<typename T>
void Stack< T >::initialize ( unsigned int  newStackSize  )  [inline, private]

Initialize the stack to have an array of the specified size.

Parameters:
newStackSize - the new stack size

template<typename T>
void Stack< T >::resize (  )  [inline, private]

Double the size of the stack array.


Member Data Documentation

template<typename T>
T* Stack< T >::stack [private]

the stack array

template<typename T>
int Stack< T >::numElements [private]

the number of elements in the stack

template<typename T>
int Stack< T >::capacity [private]

the size of the stack array

template<typename T>
const int Stack< T >::DEFAULT_INITIAL_CAPACITY = 8 [static, private]

the default size of a stack array if no size is specified


The documentation for this class was generated from the following file:
Generated on Tue Aug 26 00:03:21 2008 for Rim Physics by  doxygen 1.5.4