initial
[prop.git] / include / AD / contain / stackcol.h
blobccc6f3a33e2d6718c6696316edb10eb3324edfff
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 stackable_collection_h
26 #define stackable_collection_h
28 ////////////////////////////////////////////////////////////////////////////
29 // Stackable collection
30 ////////////////////////////////////////////////////////////////////////////
31 #include <AD/collect/seqcol.h>
33 template <class T, class S>
34 class StackableCollection : SequenceableCollection<T> {
36 S stack; // implementation
38 public:
39 ////////////////////////////////////////////////////////////////
40 // Inherit types
41 ////////////////////////////////////////////////////////////////
42 typedef SequenceableCollection<T> Super;
43 typedef Super::Element Element;
45 ////////////////////////////////////////////////////////////////
46 // Constructor and destructor
47 ////////////////////////////////////////////////////////////////
48 StackableCollection(int initial_size) : stack(initial_size) {}
49 StackableCollection(const Collection<T>& C)
50 : stack(initial_size) { *this = C; }
51 StackableCollection(const StackableCollection& C)
52 : stack(initial_size) { *this = C; }
53 ~StackableCollection() {}
55 ////////////////////////////////////////////////////////////////
56 // Assignment
57 ////////////////////////////////////////////////////////////////
58 // void operator = (const Collection<T>&); // inherited
60 ////////////////////////////////////////////////////////////////
61 // Selectors
62 ////////////////////////////////////////////////////////////////
63 int size() const { return stack.size(); }
64 int capacity() const { return stack.capacity(); }
65 // virtual Bool is_empty() const; // inherited
66 // virtual Bool is_full() const; // inherited
67 // virtual Bool contains(const T&) const; // inherited
68 virtual T& top() const { return stack.top(); }
70 ////////////////////////////////////////////////////////////////
71 // Mutators
72 ////////////////////////////////////////////////////////////////
73 virtual void clear() { return stack.clear(); }
74 virtual Ix insert(const T& e) { stack.push(e); return &top(); }
75 virtual Bool remove(const T& e) { should_not_implement("remove"); }
76 virtual void push(const T& e) { stack.push(e); }
77 virtual T& pop() { return stack.pop(e); }
79 ////////////////////////////////////////////////////////////////
80 // Iteration
81 ////////////////////////////////////////////////////////////////
82 Ix first() const { return size() == 0 ? 0 : 0; }
83 Ix next(Ix i) const
84 { return i >= (Ix)((T*)stack + size()) ? 0 : (((T*)i)+1); }
85 T& operator () (Ix i) const { return *(T*)i; }
87 ////////////////////////////////////////////////////////////////
88 // Other
89 ////////////////////////////////////////////////////////////////
90 const char * myName() const { return "StackableCollection"; }
93 #endif