summaryrefslogtreecommitdiff
path: root/ace/Stack.h
blob: 3b8e85a6ecd5abced3dc1867aeda7ba32d1215a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
// 
// = FILENAME
//    Stack.h
//
// = AUTHOR
//    Doug Schmidt 
// 
// ============================================================================

#if !defined (ACE_STACK_H)
#define ACE_STACK_H

#include "ace/ACE.h"

template <class T>
class ACE_Bounded_Stack 
  // = TITLE
  //     Implement a generic LIFO abstract data type.
  //     
  // = DESCRIPTION
  //     This implementation of a Stack uses a bounded array
  //     that is allocated dynamically.
{
public:
  // = Initialization, assignemnt, and termination methods.
  ACE_Bounded_Stack (size_t size);
    // Initialize a new stack so that it is empty. 

  ACE_Bounded_Stack (const ACE_Bounded_Stack<T> &s);
    // The copy constructor (performs initialization). 

  void operator= (const ACE_Bounded_Stack<T> &s);
    // Assignment operator (performs assignment). 

 ~ACE_Bounded_Stack (void); 
    // Perform actions needed when stack goes out of scope. 

  // = Classic Stack operations.

  void push (const T &new_item);
    // Place a new item on top of the stack. Does not check if the
    // stack is full.  

  void pop (T &item);
    // Remove and return the top stack item. Does not check if stack
    // is full. 

  void top (T &item) const; 
    // Return top stack item without removing it. Does not check if
    // stack is empty. 

  // = Check boundary conditions for Stack operations. 

  int is_empty (void) const;
    // Returns 1 if the stack is empty, otherwise returns 0. 

  int is_full (void) const;
    // Returns 1 if the stack is full, otherwise returns 0. 

  void dump (void) const;
  // Dump the state of an object.

  ACE_ALLOC_HOOK_DECLARE;
  // Declare the dynamic allocation hooks.

private:
  size_t size_;
  // Size of the dynamically allocated data.

  size_t top_; 
  // Keeps track of the current top of stack. 

  T *stack_; 
  // Holds the stack's contents. 
};

//----------------------------------------

template <class T, size_t SIZE>
class ACE_Fixed_Stack 
  // = TITLE
  //     Implement a generic LIFO abstract data type.
  //     
  // = DESCRIPTION
  //     This implementation of a Stack uses a fixed array
  //     with the size fixed at instantiation time.
{
public:
  // = Initialization, assignemnt, and termination methods.
  ACE_Fixed_Stack (void);
    // Initialize a new stack so that it is empty. 

  ACE_Fixed_Stack (const ACE_Fixed_Stack<T, SIZE> &s);
    // The copy constructor (performs initialization). 

  void operator= (const ACE_Fixed_Stack<T, SIZE> &s);
    // Assignment operator (performs assignment). 

 ~ACE_Fixed_Stack (void); 
    // Perform actions needed when stack goes out of scope. 

  // = Classic Stack operations.

  void push (const T &new_item);
    // Place a new item on top of the stack. Does not check if the
    // stack is full.  

  void pop (T &item);
    // Remove and return the top stack item. Does not check if stack
    // is full. 

  void top (T &item) const; 
    // Return top stack item without removing it. Does not check if
    // stack is empty. 

  // = Check boundary conditions for Stack operations. 

  int is_empty (void) const;
    // Returns 1 if the stack is empty, otherwise returns 0. 

  int is_full (void) const;
    // Returns 1 if the stack is full, otherwise returns 0. 

  void dump (void) const;
  // Dump the state of an object.

  ACE_ALLOC_HOOK_DECLARE;
  // Declare the dynamic allocation hooks.

private:
  size_t size_;
  // Size of the dynamically allocated data.

  size_t top_; 
  // Keeps track of the current top of stack. 

  T stack_[SIZE];
  // Holds the stack's contents. 
};

//----------------------------------------

// Forward declaration (use the "Cheshire Cat" approach to information
// hiding). 
template <class T>
class ACE_Stack_Node;

template <class T>
class ACE_Unbounded_Stack 
  // = TITLE
  //     Implement a generic LIFO abstract data type.
  //     
  // = DESCRIPTION
  //     This implementation of an unbounded Stack uses a linked list.
{
public:
  // = Initialization, assignemnt, and termination methods.
  ACE_Unbounded_Stack (void);
    // Initialize a new stack so that it is empty. 

  ACE_Unbounded_Stack (const ACE_Unbounded_Stack<T> &s);
    // The copy constructor (performs initialization). 

  void operator= (const ACE_Unbounded_Stack<T> &s);
    // Assignment operator (performs assignment). 

 ~ACE_Unbounded_Stack (void); 
    // Perform actions needed when stack goes out of scope. 

  // = Classic Stack operations.

  void push (const T &new_item);
    // Place a new item on top of the stack. Does not check if the
    // stack is full.  

  void pop (T &item);
    // Remove and return the top stack item. Does not check if stack
    // is full. 

  void top (T &item) const;
    // Return top stack item without removing it. Does not check if
    // stack is empty. 

  // = Check boundary conditions for Stack operations. 

  int is_empty (void) const;
    // Returns 1 if the stack is empty, otherwise returns 0. 

  int is_full (void) const;
    // Returns 1 if the stack is full, otherwise returns 0. 

   static void delete_free_list (void);
   // Returns all dynamic memory on the free list to the free store.

  void dump (void) const;
  // Dump the state of an object.

  ACE_ALLOC_HOOK_DECLARE;
  // Declare the dynamic allocation hooks.

private:
  void delete_all_nodes (void);
  // Delete all the nodes in the stack.

  void copy_all_nodes (const ACE_Unbounded_Stack<T> &s);
  // Copy all nodes from <s> to <this>.

  ACE_Stack_Node<T> *head_;
   // Head of the linked list of Nodes.

  ACE_Stack_Node<T> *last_resort_;
  // Use this node when all memory is exhausted... 
};

// Forward declaration (use the "Cheshire Cat" approach to information
// hiding). 
template <class T>
class ACE_Queue_Node;

template <class TYPE>
class ACE_Unbounded_Queue
  // = TITLE
  //     A Queue of "infinite" length.

  // = DESCRIPTION
  //     Implemented using dynamic memory...
{
public:
  ACE_Unbounded_Queue (void);
  // construction.

  ~ACE_Unbounded_Queue (void);
  // construction.

  int enqueue (const TYPE &new_item);
  // Addes <new_item> to the queue.  Returns 0 on success -1 on failure.

  int dequeue (TYPE &item);
  // Removes and returns the first <item> on the queue.  Returns 0 on
  // success -1 if nothing was found.

  int peek (TYPE &item);
  // Returns the first <item> on the queue without removing it.
  // Returns 0 on success -1 if nothing was found.

  int size (void) const;
  // The size of the queue

  void dump (void) const;
  // Dump the state of an object.

  ACE_ALLOC_HOOK_DECLARE;
  // Declare the dynamic allocation hooks.

protected:
  ACE_Queue_Node<TYPE> *head_;
  // Head of the Queue.

  ACE_Queue_Node<TYPE> *tail_;
  // Tail of the Queue.

  size_t cur_size_;
  // Current size of the queue.
};

#if defined (__ACE_INLINE__)
#include "ace/Stack.i"
#endif /* __ACE_INLINE__ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Stack.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Stack.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#endif /* ACE_STACK_H */