initial
[prop.git] / include / AD / contain / stack.h
blob5796b398592601eb372ab29c64b80035b8332318
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 stack_h
26 #define stack_h
28 ///////////////////////////////////////////////////////////////////////////
29 // Class Stack<T> 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/array.h> // Dynamic array
39 template<class T>
40 class Stack : public Array<T> {
41 int n; // size of the stack
43 Stack(const Stack&); // no copy constructor
44 void operator = (const Stack&); // no assignment
46 public:
48 //////////////////////////////////////////////////////////
49 // Constructor and destructor
50 //////////////////////////////////////////////////////////
51 Stack(int size) : Array<T>(size), n(0) {}
52 ~Stack() {}
54 //////////////////////////////////////////////////////////
55 // Selectors
56 //////////////////////////////////////////////////////////
57 inline int size() const { return n; }
58 inline Bool is_empty() const { return n == 0; }
59 inline Bool is_full() const { return n >= length(); }
60 inline const T& top() const { return (*this)[n-1]; }
61 inline T& top() { return (*this)[n-1]; }
62 // T& operator [] (int i) const; // inherited
64 //////////////////////////////////////////////////////////
65 // Mutators
66 //////////////////////////////////////////////////////////
67 inline void clear() { n = 0; }
68 inline void push(const T& e) { (*this)[n++] = e; }
69 inline void set_size(int s) { n = s; }
70 inline T& pop() { return (*this)[--n]; }
71 inline void pop(int m) { n -= m; }
73 //////////////////////////////////////////////////////////
74 // Iterators
75 //////////////////////////////////////////////////////////
76 inline Ix first() const { return Ix(n); }
77 inline Ix last() const { return Ix(n > 0 ? 1 : 0); }
78 inline Ix next(Ix i) const { return Ix((int)i-1); }
79 inline Ix prev(Ix i) const { return Ix((int)i < n ? (int)i+1 : 0); }
80 inline const T& operator () (Ix i) const { return (*this)[(int)i-1]; }
81 inline T& operator () (Ix i) { return (*this)[(int)i-1]; }
84 #endif