ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GStack.hh
Revision: 248
Committed: Wed May 23 22:10:55 2012 UTC (7 years ago) by gpertea
File size: 1111 byte(s)
Log Message:
put GVec and GPVec in GVec.hh, added GIntervalTree.hh

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