2002-10-04 04:42:45 +04:00
|
|
|
#ifndef _TSTACK_H
|
|
|
|
#define _TSTACK_H
|
|
|
|
/* Stack - a template stack class, does not call any constructors/destructors
|
2002-10-04 01:45:43 +04:00
|
|
|
**
|
|
|
|
** Copyright 2001 pinc Software. All Rights Reserved.
|
|
|
|
** This file may be used under the terms of the OpenBeOS License.
|
2002-10-04 04:42:45 +04:00
|
|
|
**
|
|
|
|
** 2002-03-10 Modified by Marcus Overhagen
|
2002-10-04 01:45:43 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
template<class T> class Stack {
|
|
|
|
public:
|
|
|
|
Stack()
|
|
|
|
:
|
|
|
|
fArray(NULL),
|
|
|
|
fUsed(0),
|
|
|
|
fMax(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Stack()
|
|
|
|
{
|
|
|
|
if (fArray)
|
|
|
|
free(fArray);
|
|
|
|
}
|
|
|
|
|
2002-10-04 04:42:45 +04:00
|
|
|
bool Push(const T & value)
|
2002-10-04 01:45:43 +04:00
|
|
|
{
|
|
|
|
if (fUsed >= fMax) {
|
|
|
|
fMax += 16;
|
2002-10-04 04:42:45 +04:00
|
|
|
T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
|
2002-10-04 01:45:43 +04:00
|
|
|
if (newArray == NULL)
|
2002-10-04 04:42:45 +04:00
|
|
|
return false;
|
2002-10-04 01:45:43 +04:00
|
|
|
|
|
|
|
fArray = newArray;
|
|
|
|
}
|
|
|
|
fArray[fUsed++] = value;
|
2002-10-04 04:42:45 +04:00
|
|
|
return true;
|
2002-10-04 01:45:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T *value)
|
|
|
|
{
|
|
|
|
if (fUsed == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*value = fArray[--fUsed];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-10-04 04:42:45 +04:00
|
|
|
int32 CountItems() const
|
|
|
|
{
|
|
|
|
return fUsed;
|
|
|
|
}
|
|
|
|
|
2002-10-04 01:45:43 +04:00
|
|
|
private:
|
|
|
|
T *fArray;
|
|
|
|
int32 fUsed;
|
|
|
|
int32 fMax;
|
|
|
|
};
|
|
|
|
|
2002-10-04 04:42:45 +04:00
|
|
|
#endif /* TSTACK_H */
|