ACE_Unbounded_Queue< T > Class Template Reference

A Queue of "infinite" length. More...

#include <Unbounded_Queue.h>

Collaboration diagram for ACE_Unbounded_Queue< T >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef
ACE_Unbounded_Queue_Iterator
< T > 
ITERATOR
typedef
ACE_Unbounded_Queue_Const_Iterator
< T > 
CONST_ITERATOR

Public Member Functions

 ACE_Unbounded_Queue (ACE_Allocator *alloc=0)
 ACE_Unbounded_Queue (const ACE_Unbounded_Queue< T > &)
 Copy constructor.
void operator= (const ACE_Unbounded_Queue< T > &)
 Assignment operator.
 ~ACE_Unbounded_Queue (void)
 Destructor.
bool is_empty (void) const
 Returns true if the container is empty, otherwise returns false.
bool is_full (void) const
 Returns 0.
int enqueue_tail (const T &new_item)
int enqueue_head (const T &new_item)
int dequeue_head (T &item)
void reset (void)
int get (T *&item, size_t slot=0) const
int set (const T &item, size_t slot)
 Set the slot th element of the queue to item.
size_t size (void) const
 The number of items in the queue.
void dump (void) const
 Dump the state of an object.
ACE_Unbounded_Queue_Iterator< T > begin (void)
ACE_Unbounded_Queue_Iterator< T > end (void)

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Protected Member Functions

void delete_nodes (void)
 Delete all the nodes in the queue.
void copy_nodes (const ACE_Unbounded_Queue< T > &)
 Copy nodes into this queue.

Protected Attributes

ACE_Node< T > * head_
 Pointer to the dummy node in the circular linked Queue.
size_t cur_size_
 Current size of the queue.
ACE_Allocatorallocator_
 Allocation Strategy of the queue.

Friends

class ACE_Unbounded_Queue_Iterator< T >
class ACE_Unbounded_Queue_Const_Iterator< T >

Detailed Description

template<class T>
class ACE_Unbounded_Queue< T >

A Queue of "infinite" length.

This implementation of an unbounded queue uses a circular linked list with a dummy node.

Requirements and Performance Characteristics

Definition at line 150 of file Unbounded_Queue.h.


Member Typedef Documentation

Definition at line 158 of file Unbounded_Queue.h.

template<class T>
typedef ACE_Unbounded_Queue_Iterator<T> ACE_Unbounded_Queue< T >::ITERATOR

Definition at line 157 of file Unbounded_Queue.h.


Constructor & Destructor Documentation

template<class T >
ACE_Unbounded_Queue< T >::ACE_Unbounded_Queue ( ACE_Allocator alloc = 0  )  [inline]

Construction. Use user specified allocation strategy if specified. Initialize an empty queue using the strategy provided.

Definition at line 25 of file Unbounded_Queue.cpp.

00026   : head_ (0),
00027     cur_size_ (0),
00028     allocator_ (alloc)
00029 {
00030   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::ACE_Unbounded_Queue (void)");
00031 
00032   if (this->allocator_ == 0)
00033     this->allocator_ = ACE_Allocator::instance ();
00034 
00035   ACE_NEW_MALLOC (this->head_,
00036                   (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00037                   ACE_Node<T>);
00038   // Make the list circular by pointing it back to itself.
00039   this->head_->next_ = this->head_;
00040 }

template<class T>
ACE_Unbounded_Queue< T >::ACE_Unbounded_Queue ( const ACE_Unbounded_Queue< T > &  us  )  [inline]

Copy constructor.

Initialize the queue to be a copy of the provided queue.

Definition at line 43 of file Unbounded_Queue.cpp.

00044   : head_ (0),
00045     cur_size_ (0),
00046     allocator_ (us.allocator_)
00047 {
00048   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::ACE_Unbounded_Queue");
00049 
00050   if (this->allocator_ == 0)
00051     this->allocator_ = ACE_Allocator::instance ();
00052 
00053   ACE_NEW_MALLOC (this->head_,
00054                   (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00055                   ACE_Node<T>);
00056   this->head_->next_ = this->head_;
00057   this->copy_nodes (us);
00058 }

template<class T >
ACE_Unbounded_Queue< T >::~ACE_Unbounded_Queue ( void   )  [inline]

Destructor.

Clean up the memory for the queue.

Definition at line 148 of file Unbounded_Queue.cpp.

00149 {
00150   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::~ACE_Unbounded_Queue (void)");
00151 
00152   this->delete_nodes ();
00153   ACE_DES_FREE_TEMPLATE (head_,
00154                          this->allocator_->free,
00155                          ACE_Node,
00156                          <T>);
00157 }


Member Function Documentation

template<class T >
ACE_Unbounded_Queue_Iterator< T > ACE_Unbounded_Queue< T >::begin ( void   )  [inline]

Definition at line 73 of file Unbounded_Queue.cpp.

00074 {
00075   // ACE_TRACE ("ACE_Unbounded_Queue<T>::begin");
00076   return ACE_Unbounded_Queue_Iterator<T> (*this);
00077 }

template<class T>
void ACE_Unbounded_Queue< T >::copy_nodes ( const ACE_Unbounded_Queue< T > &  us  )  [inline, protected]

Copy nodes into this queue.

Definition at line 112 of file Unbounded_Queue.cpp.

00113 {
00114   for (ACE_Node<T> *curr = us.head_->next_;
00115        curr != us.head_;
00116        curr = curr->next_)
00117     if (this->enqueue_tail (curr->item_) == -1)
00118       // @@ What's the right thing to do here?
00119       this->delete_nodes ();
00120 }

template<class T >
void ACE_Unbounded_Queue< T >::delete_nodes ( void   )  [inline, protected]

Delete all the nodes in the queue.

Definition at line 123 of file Unbounded_Queue.cpp.

00124 {
00125   for (ACE_Node<T> *curr = this->head_->next_;
00126        // Keep looking until we've hit the dummy node.
00127        curr != this->head_;
00128        )
00129     {
00130       ACE_Node<T> *temp = curr;
00131       curr = curr->next_;
00132 
00133       ACE_DES_FREE_TEMPLATE (temp,
00134                              this->allocator_->free,
00135                              ACE_Node,
00136                              <T>);
00137       --this->cur_size_;
00138       // @@ Doesnt make sense to have this check since
00139       // this will always be true.
00140       //   ACE_ASSERT (this->cur_size_ >= 0);
00141     }
00142 
00143   // Reset the list to be a circular list with just a dummy node.
00144   this->head_->next_ = this->head_;
00145 }

template<class T>
int ACE_Unbounded_Queue< T >::dequeue_head ( T &  item  )  [inline]

Removes and returns the first item on the queue. Returns 0 on success, -1 if the queue was empty. Remove an item from the head of the queue.

Definition at line 208 of file Unbounded_Queue.cpp.

00209 {
00210   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::dequeue_head");
00211 
00212   // Check for empty queue.
00213   if (this->is_empty ())
00214     return -1;
00215 
00216   ACE_Node<T> *temp = this->head_->next_;
00217 
00218   item = temp->item_;
00219   this->head_->next_ = temp->next_;
00220   ACE_DES_FREE_TEMPLATE (temp,
00221                          this->allocator_->free,
00222                          ACE_Node,
00223                          <T>);
00224   --this->cur_size_;
00225   return 0;
00226 }

template<class T >
void ACE_Unbounded_Queue< T >::dump ( void   )  const [inline]

Dump the state of an object.

Definition at line 87 of file Unbounded_Queue.cpp.

00088 {
00089 #if defined (ACE_HAS_DUMP)
00090   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::dump");
00091 
00092   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00093   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\nhead_ = %u"), this->head_));
00094   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\nhead_->next_ = %u"), this->head_->next_));
00095   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_));
00096 
00097   T *item = 0;
00098 #if !defined (ACE_NLOGGING)
00099   size_t count = 1;
00100 #endif /* ! ACE_NLOGGING */
00101 
00102   for (ACE_Unbounded_Queue_Iterator<T> iter (*(ACE_Unbounded_Queue<T> *) this);
00103        iter.next (item) != 0;
00104        iter.advance ())
00105     ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("count = %d\n"), count++));
00106 
00107   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00108 #endif /* ACE_HAS_DUMP */
00109 }

template<class T >
ACE_Unbounded_Queue_Iterator< T > ACE_Unbounded_Queue< T >::end ( void   )  [inline]

Definition at line 80 of file Unbounded_Queue.cpp.

00081 {
00082   // ACE_TRACE ("ACE_Unbounded_Queue<T>::end");
00083   return ACE_Unbounded_Queue_Iterator<T> (*this, 1);
00084 }

template<class T>
int ACE_Unbounded_Queue< T >::enqueue_head ( const T &  new_item  )  [inline]

Adds new_item to the head of the queue. Returns 0 on success, -1 on failure. Insert an item at the head of the queue.

Definition at line 160 of file Unbounded_Queue.cpp.

00161 {
00162   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::enqueue_head");
00163 
00164   ACE_Node<T> *temp = 0;
00165 
00166   // Create a new node that points to the original head.
00167   ACE_NEW_MALLOC_RETURN (temp,
00168                          static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00169                          ACE_Node<T> (new_item, this->head_->next_),
00170                          -1);
00171   // Link this pointer into the front of the list.  Note that the
00172   // "real" head of the queue is <head_->next_>, whereas <head_> is
00173   // just a pointer to the dummy node.
00174   this->head_->next_ = temp;
00175 
00176   ++this->cur_size_;
00177   return 0;
00178 }

template<class T>
int ACE_Unbounded_Queue< T >::enqueue_tail ( const T &  new_item  )  [inline]

Adds new_item to the tail of the queue. Returns 0 on success, -1 on failure. Insert an item at the end of the queue.

Definition at line 181 of file Unbounded_Queue.cpp.

00182 {
00183   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::enqueue_tail");
00184 
00185   // Insert <item> into the old dummy node location.  Note that this
00186   // isn't actually the "head" item in the queue, it's a dummy node at
00187   // the "tail" of the queue...
00188   this->head_->item_ = new_item;
00189 
00190   ACE_Node<T> *temp = 0;
00191 
00192   // Create a new dummy node.
00193   ACE_NEW_MALLOC_RETURN (temp,
00194                          static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00195                          ACE_Node<T> (this->head_->next_),
00196                          -1);
00197   // Link this dummy pointer into the list.
00198   this->head_->next_ = temp;
00199 
00200   // Point the head to the new dummy node.
00201   this->head_ = temp;
00202 
00203   ++this->cur_size_;
00204   return 0;
00205 }

template<class T>
int ACE_Unbounded_Queue< T >::get ( T *&  item,
size_t  slot = 0 
) const [inline]

Get the slot th element in the set. Returns -1 if the element isn't in the range {0..cur_size_ - 1}, else 0. Find the item in the queue between 0 and the provided index of the queue.

Definition at line 237 of file Unbounded_Queue.cpp.

00238 {
00239   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::get");
00240 
00241   ACE_Node<T> *curr = this->head_->next_;
00242 
00243   size_t i;
00244 
00245   for (i = 0; i < this->cur_size_; i++)
00246     {
00247       if (i == slot)
00248         break;
00249 
00250       curr = curr->next_;
00251     }
00252 
00253   if (i < this->cur_size_)
00254     {
00255       item = &curr->item_;
00256       return 0;
00257     }
00258   else
00259     return -1;
00260 }

template<class T >
bool ACE_Unbounded_Queue< T >::is_empty ( void   )  const [inline]

Returns true if the container is empty, otherwise returns false.

Constant time check to see if the queue is empty.

Definition at line 14 of file Unbounded_Queue.inl.

00015 {
00016   //  ACE_TRACE ("ACE_Unbounded_Queue<T>::is_empty");
00017   return this->head_ == this->head_->next_;
00018 }

template<class T >
bool ACE_Unbounded_Queue< T >::is_full ( void   )  const [inline]

Returns 0.

The queue cannot be full, so it always returns 0.

Definition at line 21 of file Unbounded_Queue.inl.

00022 {
00023   //  ACE_TRACE ("ACE_Unbounded_Queue<T>::is_full");
00024   return false; // We should implement a "node of last resort for this..."
00025 }

template<class T>
void ACE_Unbounded_Queue< T >::operator= ( const ACE_Unbounded_Queue< T > &  us  )  [inline]

Assignment operator.

Perform a deep copy of rhs.

Definition at line 61 of file Unbounded_Queue.cpp.

00062 {
00063   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::operator=");
00064 
00065   if (this != &us)
00066     {
00067       this->delete_nodes ();
00068       this->copy_nodes (us);
00069     }
00070 }

template<class T >
void ACE_Unbounded_Queue< T >::reset ( void   )  [inline]

Reset the ACE_Unbounded_Queue to be empty and release all its dynamically allocated resources. Delete the queue nodes.

Definition at line 229 of file Unbounded_Queue.cpp.

00230 {
00231   ACE_TRACE ("reset");
00232 
00233   this->delete_nodes ();
00234 }

template<class T>
int ACE_Unbounded_Queue< T >::set ( const T &  item,
size_t  slot 
) [inline]

Set the slot th element of the queue to item.

Set the slot th element in the set. Will pad out the set with empty nodes if slot is beyond the range {0..cur_size_ - 1}. Returns -1 on failure, 0 if slot isn't initially in range, and 0 otherwise.

Definition at line 263 of file Unbounded_Queue.cpp.

00265 {
00266   //   ACE_TRACE ("ACE_Unbounded_Queue<T>::set");
00267 
00268   ACE_Node<T> *curr = this->head_->next_;
00269 
00270   size_t i;
00271 
00272   for (i = 0;
00273        i < slot && i < this->cur_size_;
00274        ++i)
00275     curr = curr->next_;
00276 
00277   if (i < this->cur_size_)
00278     {
00279       // We're in range, so everything's cool.
00280       curr->item_ = item;
00281       return 0;
00282     }
00283   else
00284     {
00285       // We need to expand the list.
00286 
00287       // A common case will be increasing the set size by 1.
00288       // Therefore, we'll optimize for this case.
00289       if (i == slot)
00290         {
00291           // Try to expand the size of the set by 1.
00292           if (this->enqueue_tail (item) == -1)
00293             return -1;
00294           else
00295             return 0;
00296         }
00297       else
00298         {
00299           T const dummy = T ();
00300 
00301           // We need to expand the list by multiple (dummy) items.
00302           for (; i < slot; ++i)
00303             {
00304               // This head points to the existing dummy node, which is
00305               // about to be overwritten when we add the new dummy
00306               // node.
00307               curr = this->head_;
00308 
00309               // Try to expand the size of the set by 1, but don't
00310               // store anything in the dummy node (yet).
00311               if (this->enqueue_tail (dummy) == -1)
00312                 return -1;
00313             }
00314 
00315           curr->item_ = item;
00316           return 0;
00317         }
00318     }
00319 }

template<class T >
size_t ACE_Unbounded_Queue< T >::size ( void   )  const [inline]

The number of items in the queue.

Return the size of the queue.

Definition at line 8 of file Unbounded_Queue.inl.

00009 {
00010   return this->cur_size_;
00011 }


Friends And Related Function Documentation

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

Definition at line 154 of file Unbounded_Queue.h.

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

Definition at line 153 of file Unbounded_Queue.h.


Member Data Documentation

Declare the dynamic allocation hooks.

Definition at line 263 of file Unbounded_Queue.h.

template<class T>
ACE_Allocator* ACE_Unbounded_Queue< T >::allocator_ [protected]

Allocation Strategy of the queue.

Definition at line 279 of file Unbounded_Queue.h.

template<class T>
size_t ACE_Unbounded_Queue< T >::cur_size_ [protected]

Current size of the queue.

Definition at line 276 of file Unbounded_Queue.h.

template<class T>
ACE_Node<T>* ACE_Unbounded_Queue< T >::head_ [protected]

Pointer to the dummy node in the circular linked Queue.

Definition at line 273 of file Unbounded_Queue.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Sat Nov 21 23:19:16 2009 for ACE by  doxygen 1.6.1