BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / sudoku / Stack.h
blob02ec2c343e43e22d524cc32e78f3606872d928a3
1 /* Stack - a template stack class (plus some handy methods)
3 * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de.
4 * This file may be used under the terms of the MIT License.
5 */
6 #ifndef KERNEL_UTIL_STACK_H
7 #define KERNEL_UTIL_STACK_H
10 #include <SupportDefs.h>
12 #include <stdlib.h>
14 template<class T> class Stack {
15 public:
16 Stack()
18 fArray(NULL),
19 fUsed(0),
20 fMax(0)
24 ~Stack()
26 free(fArray);
29 bool IsEmpty() const
31 return fUsed == 0;
34 void MakeEmpty()
36 // could also free the memory
37 fUsed = 0;
40 status_t Push(T value)
42 if (fUsed >= fMax) {
43 fMax += 16;
44 T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
45 if (newArray == NULL)
46 return B_NO_MEMORY;
48 fArray = newArray;
50 fArray[fUsed++] = value;
51 return B_OK;
54 bool Pop(T *value)
56 if (fUsed == 0)
57 return false;
59 *value = fArray[--fUsed];
60 return true;
63 T *Array()
65 return fArray;
68 int32 CountItems() const
70 return fUsed;
73 private:
74 T *fArray;
75 int32 fUsed;
76 int32 fMax;
79 #endif /* KERNEL_UTIL_STACK_H */