From 6de263b12e5faa5c7e1666f9dddfc1a97ca795c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 18 Jan 2005 02:31:56 +0000 Subject: [PATCH] This can be useful at other places as well (originally came from the BFS implementation). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10819 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/Stack.h | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 headers/private/kernel/util/Stack.h diff --git a/headers/private/kernel/util/Stack.h b/headers/private/kernel/util/Stack.h new file mode 100644 index 0000000000..1773ac4686 --- /dev/null +++ b/headers/private/kernel/util/Stack.h @@ -0,0 +1,68 @@ +/* Stack - a template stack class + * + * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de. + * This file may be used under the terms of the MIT License. + */ +#ifndef KERNEL_UTIL_STACK_H +#define KERNEL_UTIL_STACK_H + + +#include + + +template class Stack { + public: + Stack() + : + fArray(NULL), + fUsed(0), + fMax(0) + { + } + + ~Stack() + { + free(fArray); + } + + bool IsEmpty() const + { + return fUsed == 0; + } + + void MakeEmpty() + { + // could also free the memory + fUsed = 0; + } + + status_t Push(T value) + { + if (fUsed >= fMax) { + fMax += 16; + T *newArray = (T *)realloc(fArray, fMax * sizeof(T)); + if (newArray == NULL) + return B_NO_MEMORY; + + fArray = newArray; + } + fArray[fUsed++] = value; + return B_OK; + } + + bool Pop(T *value) + { + if (fUsed == 0) + return false; + + *value = fArray[--fUsed]; + return true; + } + + private: + T *fArray; + int32 fUsed; + int32 fMax; +}; + +#endif /* KERNEL_UTIL_STACK_H */