00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Unbounded_Queue.h 00006 * 00007 * $Id: Unbounded_Queue.h 80826 2008-03-04 14:51:23Z wotte $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_UNBOUNDED_QUEUE_H 00014 #define ACE_UNBOUNDED_QUEUE_H 00015 #include /**/ "ace/pre.h" 00016 00017 #include "ace/Node.h" 00018 00019 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00020 # pragma once 00021 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00022 00023 #include "ace/os_include/os_stddef.h" 00024 00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00026 00027 class ACE_Allocator; 00028 00029 template <class T> 00030 class ACE_Unbounded_Queue; 00031 00032 /** 00033 * @class ACE_Unbounded_Queue_Iterator 00034 * 00035 * @brief Implement an iterator over an unbounded queue. 00036 */ 00037 template <class T> 00038 class ACE_Unbounded_Queue_Iterator 00039 { 00040 public: 00041 // = Initialization method. 00042 ACE_Unbounded_Queue_Iterator (ACE_Unbounded_Queue<T> &q, int end = 0); 00043 00044 // = Iteration methods. 00045 00046 /// Pass back the @a next_item that hasn't been seen in the queue. 00047 /// Returns 0 when all items have been seen, else 1. 00048 int next (T *&next_item); 00049 00050 /// Move forward by one element in the set. Returns 0 when all the 00051 /// items in the queue have been seen, else 1. 00052 int advance (void); 00053 00054 /// Move to the first element in the queue. Returns 0 if the 00055 /// queue is empty, else 1. 00056 int first (void); 00057 00058 /// Returns 1 when all items have been seen, else 0. 00059 int done (void) const; 00060 00061 /// Dump the state of an object. 00062 void dump (void) const; 00063 00064 /// Declare the dynamic allocation hooks. 00065 ACE_ALLOC_HOOK_DECLARE; 00066 00067 private: 00068 /// Pointer to the current node in the iteration. 00069 ACE_Node<T> *current_; 00070 00071 /// Pointer to the queue we're iterating over. 00072 ACE_Unbounded_Queue<T> &queue_; 00073 }; 00074 00075 /** 00076 * @class ACE_Unbounded_Queue_Const_Iterator 00077 * 00078 * @brief Implement an iterator over an const unbounded queue. 00079 */ 00080 template <class T> 00081 class ACE_Unbounded_Queue_Const_Iterator 00082 { 00083 public: 00084 // = Initialization method. 00085 ACE_Unbounded_Queue_Const_Iterator (const ACE_Unbounded_Queue<T> &q, int end = 0); 00086 00087 // = Iteration methods. 00088 00089 /// Pass back the @a next_item that hasn't been seen in the queue. 00090 /// Returns 0 when all items have been seen, else 1. 00091 int next (T *&next_item); 00092 00093 /// Move forward by one element in the set. Returns 0 when all the 00094 /// items in the queue have been seen, else 1. 00095 int advance (void); 00096 00097 /// Move to the first element in the queue. Returns 0 if the 00098 /// queue is empty, else 1. 00099 int first (void); 00100 00101 /// Returns 1 when all items have been seen, else 0. 00102 int done (void) const; 00103 00104 /// Dump the state of an object. 00105 void dump (void) const; 00106 00107 /// Declare the dynamic allocation hooks. 00108 ACE_ALLOC_HOOK_DECLARE; 00109 00110 private: 00111 /// Pointer to the current node in the iteration. 00112 ACE_Node<T> *current_; 00113 00114 /// Pointer to the queue we're iterating over. 00115 const ACE_Unbounded_Queue<T> &queue_; 00116 }; 00117 00118 /** 00119 * @class ACE_Unbounded_Queue 00120 * 00121 * @brief A Queue of "infinite" length. 00122 * 00123 * This implementation of an unbounded queue uses a circular 00124 * linked list with a dummy node. 00125 * 00126 * <b> Requirements and Performance Characteristics</b> 00127 * - Internal Structure 00128 * Circular linked list 00129 * - Duplicates allowed? 00130 * Yes 00131 * - Random access allowed? 00132 * No 00133 * - Search speed 00134 * N/A 00135 * - Insert/replace speed 00136 * N/A 00137 * - Iterator still valid after change to container? 00138 * Yes 00139 * - Frees memory for removed elements? 00140 * Yes 00141 * - Items inserted by 00142 * Value 00143 * - Requirements for contained type 00144 * -# Default constructor 00145 * -# Copy constructor 00146 * -# operator= 00147 * 00148 */ 00149 template <class T> 00150 class ACE_Unbounded_Queue 00151 { 00152 public: 00153 friend class ACE_Unbounded_Queue_Iterator<T>; 00154 friend class ACE_Unbounded_Queue_Const_Iterator<T>; 00155 00156 // Trait definition. 00157 typedef ACE_Unbounded_Queue_Iterator<T> ITERATOR; 00158 typedef ACE_Unbounded_Queue_Const_Iterator<T> CONST_ITERATOR; 00159 00160 // = Initialization and termination methods. 00161 /// Construction. Use user specified allocation strategy 00162 /// if specified. 00163 /** 00164 * Initialize an empty queue using the strategy provided. 00165 */ 00166 ACE_Unbounded_Queue (ACE_Allocator *alloc = 0); 00167 00168 /// Copy constructor. 00169 /** 00170 * Initialize the queue to be a copy of the provided queue. 00171 */ 00172 ACE_Unbounded_Queue (const ACE_Unbounded_Queue<T> &); 00173 00174 /// Assignment operator. 00175 /** 00176 * Perform a deep copy of rhs. 00177 */ 00178 void operator= (const ACE_Unbounded_Queue<T> &); 00179 00180 /// Destructor. 00181 /** 00182 * Clean up the memory for the queue. 00183 */ 00184 ~ACE_Unbounded_Queue (void); 00185 00186 // = Check boundary conditions. 00187 00188 /// Returns 1 if the container is empty, otherwise returns 0. 00189 /** 00190 * Constant time check to see if the queue is empty. 00191 */ 00192 int is_empty (void) const; 00193 00194 /// Returns 0. 00195 /** 00196 * The queue cannot be full, so it always returns 0. 00197 */ 00198 int is_full (void) const; 00199 00200 // = Classic queue operations. 00201 00202 /// Adds @a new_item to the tail of the queue. Returns 0 on success, 00203 /// -1 on failure. 00204 /** 00205 * Insert an item at the end of the queue. 00206 */ 00207 int enqueue_tail (const T &new_item); 00208 00209 /// Adds @a new_item to the head of the queue. Returns 0 on success, 00210 /// -1 on failure. 00211 /** 00212 * Insert an item at the head of the queue. 00213 */ 00214 int enqueue_head (const T &new_item); 00215 00216 /// Removes and returns the first @a item on the queue. Returns 0 on 00217 /// success, -1 if the queue was empty. 00218 /** 00219 * Remove an item from the head of the queue. 00220 */ 00221 int dequeue_head (T &item); 00222 00223 // = Additional utility methods. 00224 00225 /// Reset the ACE_Unbounded_Queue to be empty and release all its 00226 /// dynamically allocated resources. 00227 /** 00228 * Delete the queue nodes. 00229 */ 00230 void reset (void); 00231 00232 /// Get the @a slot th element in the set. Returns -1 if the element 00233 /// isn't in the range {0..#cur_size_ - 1}, else 0. 00234 /** 00235 * Find the item in the queue between 0 and the provided index of the 00236 * queue. 00237 */ 00238 int get (T *&item, size_t slot = 0) const; 00239 00240 /// Set the @a slot th element of the queue to @a item. 00241 /** 00242 * Set the @a slot th element in the set. Will pad out the set with 00243 * empty nodes if @a slot is beyond the range {0..#cur_size_ - 1}. 00244 * Returns -1 on failure, 0 if @a slot isn't initially in range, and 00245 * 0 otherwise. 00246 */ 00247 int set (const T &item, size_t slot); 00248 00249 /// The number of items in the queue. 00250 /** 00251 * Return the size of the queue. 00252 */ 00253 size_t size (void) const; 00254 00255 /// Dump the state of an object. 00256 void dump (void) const; 00257 00258 // = STL-styled unidirectional iterator factory. 00259 ACE_Unbounded_Queue_Iterator<T> begin (void); 00260 ACE_Unbounded_Queue_Iterator<T> end (void); 00261 00262 /// Declare the dynamic allocation hooks. 00263 ACE_ALLOC_HOOK_DECLARE; 00264 00265 protected: 00266 /// Delete all the nodes in the queue. 00267 void delete_nodes (void); 00268 00269 /// Copy nodes into this queue. 00270 void copy_nodes (const ACE_Unbounded_Queue<T> &); 00271 00272 /// Pointer to the dummy node in the circular linked Queue. 00273 ACE_Node<T> *head_; 00274 00275 /// Current size of the queue. 00276 size_t cur_size_; 00277 00278 /// Allocation Strategy of the queue. 00279 ACE_Allocator *allocator_; 00280 }; 00281 00282 ACE_END_VERSIONED_NAMESPACE_DECL 00283 00284 #if defined (__ACE_INLINE__) 00285 #include "ace/Unbounded_Queue.inl" 00286 #endif /* __ACE_INLINE__ */ 00287 00288 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00289 #include "ace/Unbounded_Queue.cpp" 00290 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00291 00292 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00293 #pragma implementation ("Unbounded_Queue.cpp") 00294 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00295 00296 #include /**/ "ace/post.h" 00297 #endif /* ACE_UNBOUNDED_QUEUE_H */