ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GStack.hh
Revision: 264
Committed: Mon Aug 27 18:21:11 2012 UTC (7 years, 1 month ago) by gpertea
File size: 1131 byte(s)
Log Message:
minor refactoring etc

Line File contents
1 //basic template for a Stack of object pointers (implemented as a linked list)
2 #include "GBase.h"
3
4 template <class OBJ> class GStack {
5 protected:
6 struct StackOBJ {
7 OBJ* obj;
8 StackOBJ* prev;
9 };
10 int fCount; //total number of elements in stack
11 StackOBJ* base;
12 StackOBJ* top;
13 public:
14 GStack(OBJ* po=NULL) {
15 base=NULL;
16 top=NULL;
17 fCount=0;
18 if (po!=NULL) Push(po);
19 }
20 ~GStack() {
21 while (fCount>0) Pop();
22 }
23 bool isEmpty() { return fCount==0; }
24 int Size() { return fCount; }
25 int Count() { return fCount; }
26 OBJ* Pop() {
27 if (top==NULL) return NULL;
28 fCount--;
29 StackOBJ* ctop=top;
30 if (top==base) base=NULL;
31 OBJ* r=top->obj;
32 top=top->prev;
33 GFREE(ctop);
34 return r;
35 }
36 OBJ* Push(OBJ* o) {
37 fCount++;
38 StackOBJ* ctop=top; //could be NULL
39 GMALLOC(top, sizeof(StackOBJ));
40 top->obj=o;
41 top->prev=ctop;
42 if (base==NULL) base=top;
43 return o;
44 }
45 OBJ* Top() { return ((top==NULL)? NULL : top->obj); }
46 OBJ* Base() { return ((base==NULL)? NULL : base->obj); }
47 };