initial
[prop.git] / include / AD / contain / varstack.h
blobf744a567e3d48511a9799e76f7ae546034d44fce
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
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
9 // your programs.
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
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
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
39 template<class T>
40 class VarStack : public VarArray<T> {
41 int n; // size of the stack
42 public:
44 /////////////////////////////////////////////////////////////
45 // Constructor and destructor
46 /////////////////////////////////////////////////////////////
47 VarStack() : n(0) {}
48 VarStack(int def_size,int growth=32)
49 : VarArray<T>(0,def_size-1,growth), n(0) {}
50 ~VarStack() {}
52 /////////////////////////////////////////////////////////////
53 // Selectors
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 /////////////////////////////////////////////////////////////
63 // Mutators
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 /////////////////////////////////////////////////////////////
71 // Iterators
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]; }
81 #endif