ACE_Unbounded_Stack< T > Class Template Reference

Implement a generic LIFO abstract data type. More...

#include <Containers_T.h>

Collaboration diagram for ACE_Unbounded_Stack< T >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef ACE_Unbounded_Stack_Iterator<
T > 
ITERATOR

Public Member Functions

 ACE_Unbounded_Stack (ACE_Allocator *the_allocator=0)
 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.
int push (const T &new_item)
 Push an element onto the top of stack.
int pop (T &item)
 Pop the top element of the stack.
int top (T &item) const
 Examine the top of the stack.
int is_empty (void) const
 Returns 1 if the container is empty, otherwise returns 0.
int is_full (void) const
 Returns 1 if the container is full, otherwise returns 0.
int insert (const T &new_item)
 Linear Insert of an item.
int remove (const T &item)
int find (const T &item) const
 Finds if item occurs the set. Returns 0 if finds, else -1.
size_t size (void) const
 The number of items in the stack.
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Private Member Functions

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}.

Private Attributes

ACE_Node< T > * head_
 Head of the linked list of Nodes.
size_t cur_size_
 Current size of the stack.
ACE_Allocatorallocator_
 Allocation strategy of the stack.

Friends

class ACE_Unbounded_Stack_Iterator< T >

Detailed Description

template<class T>
class ACE_Unbounded_Stack< T >

Implement a generic LIFO abstract data type.

This implementation of an unbounded Stack uses a linked list. If you use the {insert} or {remove} methods you should keep in mind that duplicate entries aren't allowed. In general, therefore, you should avoid the use of these methods since they aren't really part of the ADT stack. The stack is implemented as a doubly linked list.

Requirements and Performance Characteristics

Definition at line 374 of file Containers_T.h.


Member Typedef Documentation

template<class T>
typedef ACE_Unbounded_Stack_Iterator<T> ACE_Unbounded_Stack< T >::ITERATOR

Definition at line 380 of file Containers_T.h.


Constructor & Destructor Documentation

template<class T>
ACE_Unbounded_Stack< T >::ACE_Unbounded_Stack ( ACE_Allocator the_allocator = 0  ) 

Initialize an empty stack using the user specified allocation strategy if provided.

Definition at line 147 of file Containers_T.cpp.

References ACE_NEW_MALLOC, ACE_Unbounded_Stack< T >::allocator_, ACE_Unbounded_Stack< T >::head_, ACE_Allocator::instance(), and ACE_Node< T, C >::next_.

00148   : head_ (0),
00149     cur_size_ (0),
00150     allocator_ (alloc)
00151 {
00152   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack");
00153   if (this->allocator_ == 0)
00154     this->allocator_ = ACE_Allocator::instance ();
00155 
00156   ACE_NEW_MALLOC (this->head_,
00157                   (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00158                   ACE_Node<T>);
00159   this->head_->next_ = this->head_;
00160 }

template<class T>
ACE_Unbounded_Stack< T >::ACE_Unbounded_Stack ( const ACE_Unbounded_Stack< T > &  s  ) 

The copy constructor (performs initialization).

Initialize this stack to be an exact copy of {s}.

Definition at line 204 of file Containers_T.cpp.

References ACE_NEW_MALLOC, ACE_Unbounded_Stack< T >::allocator_, ACE_Unbounded_Stack< T >::copy_all_nodes(), ACE_Unbounded_Stack< T >::head_, ACE_Allocator::instance(), and ACE_Node< T, C >::next_.

00205   : head_ (0),
00206     cur_size_ (0),
00207     allocator_ (s.allocator_)
00208 {
00209   if (this->allocator_ == 0)
00210     this->allocator_ = ACE_Allocator::instance ();
00211 
00212   ACE_NEW_MALLOC (this->head_,
00213                   (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00214                   ACE_Node<T>);
00215   this->head_->next_ = this->head_;
00216 
00217   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack");
00218   this->copy_all_nodes (s);
00219 }

template<class T>
ACE_Unbounded_Stack< T >::~ACE_Unbounded_Stack ( void   ) 

Perform actions needed when stack goes out of scope.

Destroy the underlying list for the stack.

Definition at line 234 of file Containers_T.cpp.

References ACE_DES_FREE_TEMPLATE, ACE_Unbounded_Stack< T >::delete_all_nodes(), and ACE_Unbounded_Stack< T >::head_.

00235 {
00236   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::~ACE_Unbounded_Stack");
00237 
00238   this->delete_all_nodes ();
00239   ACE_DES_FREE_TEMPLATE (head_,
00240                          this->allocator_->free,
00241                          ACE_Node,
00242                          <T>);
00243 }


Member Function Documentation

template<class T>
void ACE_Unbounded_Stack< T >::copy_all_nodes ( const ACE_Unbounded_Stack< T > &  s  )  [private]

Copy all nodes from {s} to {this}.

Definition at line 182 of file Containers_T.cpp.

References ACE_ASSERT, ACE_NEW_MALLOC, ACE_Unbounded_Stack< T >::cur_size_, ACE_Unbounded_Stack< T >::head_, and ACE_Node< T, C >::next_.

Referenced by ACE_Unbounded_Stack< T >::ACE_Unbounded_Stack(), and ACE_Unbounded_Stack< T >::operator=().

00183 {
00184   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::copy_all_nodes");
00185 
00186   ACE_ASSERT (this->head_ == this->head_->next_);
00187 
00188   ACE_Node<T> *temp = this->head_;
00189 
00190   for (ACE_Node<T> *s_temp = s.head_->next_;
00191        s_temp != s.head_;
00192        s_temp = s_temp->next_)
00193     {
00194       ACE_Node<T> *nptr = temp->next_;
00195       ACE_NEW_MALLOC (temp->next_,
00196                       (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00197                       ACE_Node<T> (s_temp->item_, nptr));
00198       temp = temp->next_;
00199     }
00200   this->cur_size_ = s.cur_size_;
00201 }

template<class T>
void ACE_Unbounded_Stack< T >::delete_all_nodes ( void   )  [private]

Delete all the nodes in the stack.

Definition at line 163 of file Containers_T.cpp.

References ACE_ASSERT, ACE_DES_FREE_TEMPLATE, and ACE_Unbounded_Stack< T >::cur_size_.

Referenced by ACE_Unbounded_Stack< T >::operator=(), and ACE_Unbounded_Stack< T >::~ACE_Unbounded_Stack().

00164 {
00165   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::delete_all_nodes");
00166 
00167   while (this->is_empty () == 0)
00168     {
00169       ACE_Node<T> *temp = this->head_->next_;
00170       this->head_->next_ = temp->next_;
00171       ACE_DES_FREE_TEMPLATE (temp, this->allocator_->free,
00172                              ACE_Node, <T>);
00173     }
00174 
00175   this->cur_size_ = 0;
00176 
00177   ACE_ASSERT (this->head_ == this->head_->next_
00178               && this->is_empty ());
00179 }

template<class T>
void ACE_Unbounded_Stack< T >::dump ( void   )  const

Dump the state of an object.

Definition at line 139 of file Containers_T.cpp.

00140 {
00141 #if defined (ACE_HAS_DUMP)
00142   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::dump");
00143 #endif /* ACE_HAS_DUMP */
00144 }

template<class T>
int ACE_Unbounded_Stack< T >::find ( const T &  item  )  const

Finds if item occurs the set. Returns 0 if finds, else -1.

Linear find operation.

Definition at line 284 of file Containers_T.cpp.

References ACE_Unbounded_Stack< T >::head_, ACE_Node< T, C >::item_, and ACE_Node< T, C >::next_.

00285 {
00286   // ACE_TRACE ("ACE_Unbounded_Stack<T>::find");
00287   // Set <item> into the dummy node.
00288   this->head_->item_ = item;
00289 
00290   ACE_Node<T> *temp = this->head_->next_;
00291 
00292   // Keep looping until we find the item.
00293   while (!(temp->item_ == item))
00294     temp = temp->next_;
00295 
00296   // If we found the dummy node then it's not really there, otherwise,
00297   // it is there.
00298   return temp == this->head_ ? -1 : 0;
00299 }

template<class T>
int ACE_Unbounded_Stack< T >::insert ( const T &  new_item  ) 

Linear Insert of an item.

Insert {new_item} into the Stack at the head (but doesn't allow duplicates). Returns -1 if failures occur, 1 if item is already present (i.e., no duplicates are allowed), else 0.

Definition at line 302 of file Containers_T.cpp.

References ACE_Unbounded_Stack< T >::push().

00303 {
00304   // ACE_TRACE ("ACE_Unbounded_Stack<T>::insert");
00305 
00306   if (this->find (item) == 0)
00307     return 1;
00308   else
00309     return this->push (item);
00310 }

template<class T>
ACE_INLINE int ACE_Unbounded_Stack< T >::is_empty ( void   )  const

Returns 1 if the container is empty, otherwise returns 0.

Constant time check to see if the stack is empty.

Definition at line 128 of file Containers_T.inl.

References ACE_Unbounded_Stack< T >::head_, and ACE_Node< T, C >::next_.

00129 {
00130   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::is_empty");
00131   return this->head_ == this->head_->next_;
00132 }

template<class T>
ACE_INLINE int ACE_Unbounded_Stack< T >::is_full ( void   )  const

Returns 1 if the container is full, otherwise returns 0.

Always resturns 0 since the stack is unbounded.

Definition at line 148 of file Containers_T.inl.

References ACE_TRACE.

00149 {
00150   ACE_TRACE ("ACE_Unbounded_Stack<T>::is_full");
00151   return 0; // ???
00152 }

template<class T>
void ACE_Unbounded_Stack< T >::operator= ( const ACE_Unbounded_Stack< T > &  s  ) 

Assignment operator (performs assignment).

Perform a deep copy of the rhs into the lhs.

Definition at line 222 of file Containers_T.cpp.

References ACE_Unbounded_Stack< T >::copy_all_nodes(), and ACE_Unbounded_Stack< T >::delete_all_nodes().

00223 {
00224   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::operator=");
00225 
00226   if (this != &s)
00227     {
00228       this->delete_all_nodes ();
00229       this->copy_all_nodes (s);
00230     }
00231 }

template<class T>
int ACE_Unbounded_Stack< T >::pop ( T &  item  ) 

Pop the top element of the stack.

Remove and return the top stack item. Returns -1 if the stack is already empty, 0 if the stack is not already empty, and -1 if failure occurs.

Definition at line 262 of file Containers_T.cpp.

References ACE_DES_FREE_TEMPLATE, ACE_Unbounded_Stack< T >::cur_size_, ACE_Unbounded_Stack< T >::head_, ACE_Node< T, C >::item_, and ACE_Node< T, C >::next_.

00263 {
00264   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::pop");
00265 
00266   if (this->is_empty ())
00267     return -1;
00268   else
00269     {
00270       ACE_Node<T> *temp = this->head_->next_;
00271       item = temp->item_;
00272       this->head_->next_ = temp->next_;
00273 
00274       ACE_DES_FREE_TEMPLATE (temp,
00275                              this->allocator_->free,
00276                              ACE_Node,
00277                              <T>);
00278       --this->cur_size_;
00279       return 0;
00280     }
00281 }

template<class T>
int ACE_Unbounded_Stack< T >::push ( const T &  new_item  ) 

Push an element onto the top of stack.

Place a new item on top of the stack. Returns -1 if the stack is already full, 0 if the stack is not already full, and -1 if failure occurs.

Definition at line 246 of file Containers_T.cpp.

References ACE_NEW_MALLOC_RETURN, ACE_Unbounded_Stack< T >::cur_size_, ACE_Unbounded_Stack< T >::head_, and ACE_Node< T, C >::next_.

Referenced by ACE_Unbounded_Stack< T >::insert().

00247 {
00248   //  ACE_TRACE ("ACE_Unbounded_Stack<T>::push");
00249 
00250   ACE_Node<T> *temp = 0;
00251 
00252   ACE_NEW_MALLOC_RETURN (temp,
00253                          static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00254                          ACE_Node<T> (new_item, this->head_->next_),
00255                          -1);
00256   this->head_->next_ = temp;
00257   ++this->cur_size_;
00258   return 0;
00259 }

template<class T>
int ACE_Unbounded_Stack< T >::remove ( const T &  item  ) 

Linear remove operation.

Definition at line 313 of file Containers_T.cpp.

References ACE_DES_FREE_TEMPLATE, ACE_Unbounded_Stack< T >::cur_size_, ACE_Unbounded_Stack< T >::head_, ACE_Node< T, C >::item_, and ACE_Node< T, C >::next_.

00314 {
00315   // ACE_TRACE ("ACE_Unbounded_Stack<T>::remove");
00316 
00317   // Insert the item to be founded into the dummy node.
00318   this->head_->item_ = item;
00319 
00320   ACE_Node<T> *curr = this->head_;
00321 
00322   while (!(curr->next_->item_ == item))
00323     curr = curr->next_;
00324 
00325   if (curr->next_ == this->head_)
00326     return -1; // Item was not found.
00327   else
00328     {
00329       ACE_Node<T> *temp = curr->next_;
00330       // Skip over the node that we're deleting.
00331       curr->next_ = temp->next_;
00332       --this->cur_size_;
00333       ACE_DES_FREE_TEMPLATE (temp,
00334                              this->allocator_->free,
00335                              ACE_Node,
00336                              <T>);
00337       return 0;
00338     }
00339 }

template<class T>
ACE_INLINE size_t ACE_Unbounded_Stack< T >::size ( void   )  const

The number of items in the stack.

Constant time access to the current stack size.

Definition at line 155 of file Containers_T.inl.

References ACE_Unbounded_Stack< T >::cur_size_.

00156 {
00157   return this->cur_size_;
00158 }

template<class T>
ACE_INLINE int ACE_Unbounded_Stack< T >::top ( T &  item  )  const

Examine the top of the stack.

Return top stack item without removing it. Returns -1 if the stack is already empty, 0 if the stack is not already empty, and -1 if failure occurs.

Definition at line 135 of file Containers_T.inl.

References ACE_TRACE.

00136 {
00137   ACE_TRACE ("ACE_Unbounded_Stack<T>::top");
00138   if (this->is_empty () == 0)
00139     {
00140       item = this->head_->next_->item_;
00141       return 0;
00142     }
00143   else
00144     return -1;
00145 }


Friends And Related Function Documentation

template<class T>
friend class ACE_Unbounded_Stack_Iterator< T > [friend]

Definition at line 377 of file Containers_T.h.


Member Data Documentation

template<class T>
ACE_Unbounded_Stack< T >::ACE_ALLOC_HOOK_DECLARE

Declare the dynamic allocation hooks.

Definition at line 483 of file Containers_T.h.

template<class T>
ACE_Allocator* ACE_Unbounded_Stack< T >::allocator_ [private]

Allocation strategy of the stack.

Definition at line 499 of file Containers_T.h.

Referenced by ACE_Unbounded_Stack< T >::ACE_Unbounded_Stack().

template<class T>
size_t ACE_Unbounded_Stack< T >::cur_size_ [private]

Current size of the stack.

Definition at line 496 of file Containers_T.h.

Referenced by ACE_Unbounded_Stack< T >::copy_all_nodes(), ACE_Unbounded_Stack< T >::delete_all_nodes(), ACE_Unbounded_Stack< T >::pop(), ACE_Unbounded_Stack< T >::push(), ACE_Unbounded_Stack< T >::remove(), and ACE_Unbounded_Stack< T >::size().

template<class T>
ACE_Node<T>* ACE_Unbounded_Stack< T >::head_ [private]

Head of the linked list of Nodes.

Definition at line 493 of file Containers_T.h.

Referenced by ACE_Unbounded_Stack< T >::ACE_Unbounded_Stack(), ACE_Unbounded_Stack< T >::copy_all_nodes(), ACE_Unbounded_Stack< T >::find(), ACE_Unbounded_Stack< T >::is_empty(), ACE_Unbounded_Stack< T >::pop(), ACE_Unbounded_Stack< T >::push(), ACE_Unbounded_Stack< T >::remove(), and ACE_Unbounded_Stack< T >::~ACE_Unbounded_Stack().


The documentation for this class was generated from the following files:
Generated on Tue Feb 2 17:35:51 2010 for ACE by  doxygen 1.4.7