Added a MakeEmpty() method.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10402 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2004-12-12 20:48:40 +00:00
parent 805cd92d90
commit 62986df7c4
+17 -12
View File
@@ -1,10 +1,10 @@
/* Stack - a template stack class
*
* Copyright 2001-2004, Axel Dörfler, [email protected].
* This file may be used under the terms of the MIT License.
*/
#ifndef STACK_H #ifndef STACK_H
#define STACK_H #define STACK_H
/* Stack - a template stack class
**
** Copyright 2001 pinc Software. All Rights Reserved.
** This file may be used under the terms of the OpenBeOS License.
*/
#include <SupportDefs.h> #include <SupportDefs.h>
@@ -19,18 +19,23 @@ template<class T> class Stack {
fMax(0) fMax(0)
{ {
} }
~Stack() ~Stack()
{ {
if (fArray) free(fArray);
free(fArray);
} }
void MakeEmpty()
{
// could also free the memory
fUsed = 0;
}
status_t Push(T value) status_t Push(T value)
{ {
if (fUsed >= fMax) { if (fUsed >= fMax) {
fMax += 16; fMax += 16;
T *newArray = (T *)realloc(fArray,fMax * sizeof(T)); T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
if (newArray == NULL) if (newArray == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
@@ -39,7 +44,7 @@ template<class T> class Stack {
fArray[fUsed++] = value; fArray[fUsed++] = value;
return B_OK; return B_OK;
} }
bool Pop(T *value) bool Pop(T *value)
{ {
if (fUsed == 0) if (fUsed == 0)
@@ -48,7 +53,7 @@ template<class T> class Stack {
*value = fArray[--fUsed]; *value = fArray[--fUsed];
return true; return true;
} }
private: private:
T *fArray; T *fArray;
int32 fUsed; int32 fUsed;