1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef variable_sized_stack_h
26 #define variable_sized_stack_h
28 ///////////////////////////////////////////////////////////////////////
29 // Class Stack<Element> is a variable sized stack
30 // parameterized by its element type.
31 ///////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////
34 // We'll derive a Stack from an Array
35 ///////////////////////////////////////////////////////////////////////
37 #include <AD/contain/vararray.h> // Dynamic array
40 class VarStack
: public VarArray
<T
> {
41 int n
; // size of the stack
44 /////////////////////////////////////////////////////////////
45 // Constructor and destructor
46 /////////////////////////////////////////////////////////////
48 VarStack(int def_size
,int growth
=32)
49 : VarArray
<T
>(0,def_size
-1,growth
), n(0) {}
52 /////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////
55 inline int size() const { return n
; }
56 // int capacity() const // inherited
57 inline Bool
is_empty() const { return n
== 0; }
58 inline Bool
is_full() const { return n
== capacity(); }
59 inline const T
& top() const { return (*this)[n
-1]; }
60 inline T
& top() { return (*this)[n
-1]; }
62 /////////////////////////////////////////////////////////////
64 /////////////////////////////////////////////////////////////
65 inline void clear() { n
= 0; }
66 inline void push(const T
& e
) { (*this)[n
++] = e
; }
67 inline T
& pop() { return (*this)[--n
]; }
68 inline void pop(int m
) { n
-= m
; }
70 /////////////////////////////////////////////////////////////
72 /////////////////////////////////////////////////////////////
73 inline Ix
first() const { return Ix(n
); }
74 inline Ix
last() const { return Ix(n
> 0 ? 1 : 0); }
75 inline Ix
next(Ix i
) const { return Ix((int)i
- 1); }
76 inline Ix
prev(Ix i
) const { return Ix((int)i
< n
? (int)i
+1 : 0); }
77 inline const T
& operator () (Ix i
) const { return (*this)[(int)i
-1]; }
78 inline T
& operator () (Ix i
) { return (*this)[(int)i
-1]; }