stack.h
Go to the documentation of this file.
#ifndef STACK_H
#define STACK_H
#include <itpp/base/itassert.h>
namespace itpp
{
template<class T>
class Stack
{
public:
Stack();
Stack(int n);
Stack(const Stack<T> &s);
virtual ~Stack();
T pop();
T peek() const;
void push(T v);
void clear();
void operator=(const Stack<T> &s);
int size() const { return ndata; }
int no_elements() const { return valptr; }
void set_size(int n, bool copy = false);
private:
int valptr;
int ndata;
T *data;
private:
void alloc(int n);
void free();
};
// --------------------------- Implementation starts here ----------------------------------
template<class T>
Stack<T>::Stack()
{
data = 0;
ndata = 0;
valptr = 0;
}
template<class T>
Stack<T>::Stack(int n)
{
alloc(n);
valptr = 0;
}
template<class T>
Stack<T>::Stack(const Stack<T> &s)
{
data = NULL;
ndata = 0;
valptr = s.valptr;
alloc(s.ndata);
for (int i = 0; i < s.ndata; i++)
data[i] = s.data[i];
}
template<class T>
Stack<T>::~Stack()
{
free();
}
template <class T>
T Stack<T>::pop()
{
it_error_if(valptr == 0, "Stack<T>::pop: Empty stack");
valptr--;
return data[valptr];
}
template <class T>
T Stack<T>::peek() const
{
it_error_if(valptr == 0, "Stack<T>::peek: Empty stack");
return data[valptr-1];
}
template <class T>
void Stack<T>::push(T v)
{
it_error_if(valptr >= ndata, "Stack<T>::push: Full stack");
data[valptr] = v;
valptr++;
}
template <class T>
void Stack<T>::clear()
{
valptr = 0;
}
template<class T>
void Stack<T>::alloc(int n)
{
if (n == 0) {
data = NULL;
ndata = 0;
}
else {
data = new T[n];
it_assert_debug(data != 0, "Out of memory in Stack::alloc");
}
ndata = n;
}
template<class T>
void Stack<T>::free()
{
delete [] data;
data = 0;
ndata = 0;
}
template<class T>
void Stack<T>::operator=(const Stack<T> &s)
{
set_size(s.ndata);
for (int i = 0; i < ndata; i++)
data[i] = s.data[i];
valptr = 0;
}
template<class T>
void Stack<T>::set_size(int sz, bool copy)
{
int i, min;
T *tmp;
if (ndata == sz)
return;
if (copy) {
tmp = data;
min = ndata < sz ? ndata : sz;
alloc(sz);
for (i = 0; i < min; i++)
data[i] = tmp[i];
delete [] tmp;
}
else {
free();
alloc(sz);
}
ndata = sz;
}
} // namespace itpp
#endif // #ifndef STACK_H
itpp::Array
General array class.
Definition array.h:105
itpp::Array::alloc
void alloc(int n)
Allocate storage for an array of length n.
Definition array.h:197
itpp::Array::free
void free()
Free the storage space allocated by the array.
Definition array.h:210
itpp::Array::ndata
int ndata
The current number of elements in the Array.
Definition array.h:187
itpp::Array::data
T * data
A pointer to the data area.
Definition array.h:189
itpp::Array::set_size
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Definition array.h:257
itpp::Stack
General stack class.
Definition stack.h:67
itpp::Stack::~Stack
virtual ~Stack()
Default destructor.
Definition stack.h:136
itpp::Stack::size
int size() const
Returns the maximum number of data elements the stack can store.
Definition stack.h:91
itpp::Stack::Stack
Stack()
Default constructor.
Definition stack.h:110
itpp::Stack::set_size
void set_size(int n, bool copy=false)
Resizing a Stack<T>.
Definition stack.h:204
itpp::Stack::push
void push(T v)
Push an element at top of stack.
Definition stack.h:157
itpp::Stack::peek
T peek() const
Peek at the topmost element of the stack, without removing it.
Definition stack.h:150
itpp::Stack::operator=
void operator=(const Stack< T > &s)
Assignment operator.
Definition stack.h:195
itpp::Stack::no_elements
int no_elements() const
Returns the number of data elements currently in the stack.
Definition stack.h:93
itpp::Stack::pop
T pop()
Pop the topmost element of the stack.
Definition stack.h:142
itpp::Stack::clear
void clear()
Empty the stack.
Definition stack.h:165
it_error_if
#define it_error_if(t, s)
Abort if t is true.
Definition itassert.h:117
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
itpp::min
T min(const Vec< T > &in)
Minimum value of vector.
Definition min_max.h:125
itassert.h
Error handling functions - header file.
itpp
itpp namespace
Definition itmex.h:37

Generated on Mon Jun 10 2024 11:49:19 for IT++ by Doxygen 1.9.8