3 //=============================================================================
5 * @file Unbounded_Queue.h
7 * $Id: Unbounded_Queue.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
11 //=============================================================================
13 #ifndef ACE_UNBOUNDED_QUEUE_H
14 #define ACE_UNBOUNDED_QUEUE_H
15 #include /**/ "ace/pre.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 #include "ace/os_include/os_stddef.h"
25 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 class ACE_Unbounded_Queue
;
33 * @class ACE_Unbounded_Queue_Iterator
35 * @brief Implement an iterator over an unbounded queue.
38 class ACE_Unbounded_Queue_Iterator
41 // = Initialization method.
42 ACE_Unbounded_Queue_Iterator (ACE_Unbounded_Queue
<T
> &q
, int end
= 0);
44 // = Iteration methods.
46 /// Pass back the @a next_item that hasn't been seen in the queue.
47 /// Returns 0 when all items have been seen, else 1.
48 int next (T
*&next_item
);
50 /// Move forward by one element in the set. Returns 0 when all the
51 /// items in the queue have been seen, else 1.
54 /// Move to the first element in the queue. Returns 0 if the
55 /// queue is empty, else 1.
58 /// Returns 1 when all items have been seen, else 0.
59 int done (void) const;
61 /// Dump the state of an object.
62 void dump (void) const;
64 /// Declare the dynamic allocation hooks.
65 ACE_ALLOC_HOOK_DECLARE
;
68 /// Pointer to the current node in the iteration.
69 ACE_Node
<T
> *current_
;
71 /// Pointer to the queue we're iterating over.
72 ACE_Unbounded_Queue
<T
> &queue_
;
76 * @class ACE_Unbounded_Queue_Const_Iterator
78 * @brief Implement an iterator over an const unbounded queue.
81 class ACE_Unbounded_Queue_Const_Iterator
84 // = Initialization method.
85 ACE_Unbounded_Queue_Const_Iterator (const ACE_Unbounded_Queue
<T
> &q
, int end
= 0);
87 // = Iteration methods.
89 /// Pass back the @a next_item that hasn't been seen in the queue.
90 /// Returns 0 when all items have been seen, else 1.
91 int next (T
*&next_item
);
93 /// Move forward by one element in the set. Returns 0 when all the
94 /// items in the queue have been seen, else 1.
97 /// Move to the first element in the queue. Returns 0 if the
98 /// queue is empty, else 1.
101 /// Returns 1 when all items have been seen, else 0.
102 int done (void) const;
104 /// Dump the state of an object.
105 void dump (void) const;
107 /// Declare the dynamic allocation hooks.
108 ACE_ALLOC_HOOK_DECLARE
;
111 /// Pointer to the current node in the iteration.
112 ACE_Node
<T
> *current_
;
114 /// Pointer to the queue we're iterating over.
115 const ACE_Unbounded_Queue
<T
> &queue_
;
119 * @class ACE_Unbounded_Queue
121 * @brief A Queue of "infinite" length.
123 * This implementation of an unbounded queue uses a circular
124 * linked list with a dummy node.
126 * <b> Requirements and Performance Characteristics</b>
127 * - Internal Structure
128 * Circular linked list
129 * - Duplicates allowed?
131 * - Random access allowed?
135 * - Insert/replace speed
137 * - Iterator still valid after change to container?
139 * - Frees memory for removed elements?
141 * - Items inserted by
143 * - Requirements for contained type
144 * -# Default constructor
145 * -# Copy constructor
150 class ACE_Unbounded_Queue
153 friend class ACE_Unbounded_Queue_Iterator
<T
>;
154 friend class ACE_Unbounded_Queue_Const_Iterator
<T
>;
157 typedef ACE_Unbounded_Queue_Iterator
<T
> ITERATOR
;
158 typedef ACE_Unbounded_Queue_Const_Iterator
<T
> CONST_ITERATOR
;
160 // = Initialization and termination methods.
161 /// Construction. Use user specified allocation strategy
164 * Initialize an empty queue using the strategy provided.
166 ACE_Unbounded_Queue (ACE_Allocator
*alloc
= 0);
168 /// Copy constructor.
170 * Initialize the queue to be a copy of the provided queue.
172 ACE_Unbounded_Queue (const ACE_Unbounded_Queue
<T
> &);
174 /// Assignment operator.
176 * Perform a deep copy of rhs.
178 void operator= (const ACE_Unbounded_Queue
<T
> &);
182 * Clean up the memory for the queue.
184 ~ACE_Unbounded_Queue (void);
186 // = Check boundary conditions.
188 /// Returns 1 if the container is empty, otherwise returns 0.
190 * Constant time check to see if the queue is empty.
192 int is_empty (void) const;
196 * The queue cannot be full, so it always returns 0.
198 int is_full (void) const;
200 // = Classic queue operations.
202 /// Adds @a new_item to the tail of the queue. Returns 0 on success,
205 * Insert an item at the end of the queue.
207 int enqueue_tail (const T
&new_item
);
209 /// Adds @a new_item to the head of the queue. Returns 0 on success,
212 * Insert an item at the head of the queue.
214 int enqueue_head (const T
&new_item
);
216 /// Removes and returns the first @a item on the queue. Returns 0 on
217 /// success, -1 if the queue was empty.
219 * Remove an item from the head of the queue.
221 int dequeue_head (T
&item
);
223 // = Additional utility methods.
225 /// Reset the ACE_Unbounded_Queue to be empty and release all its
226 /// dynamically allocated resources.
228 * Delete the queue nodes.
232 /// Get the @a slot th element in the set. Returns -1 if the element
233 /// isn't in the range {0..#cur_size_ - 1}, else 0.
235 * Find the item in the queue between 0 and the provided index of the
238 int get (T
*&item
, size_t slot
= 0) const;
240 /// Set the @a slot th element of the queue to @a item.
242 * Set the @a slot th element in the set. Will pad out the set with
243 * empty nodes if @a slot is beyond the range {0..#cur_size_ - 1}.
244 * Returns -1 on failure, 0 if @a slot isn't initially in range, and
247 int set (const T
&item
, size_t slot
);
249 /// The number of items in the queue.
251 * Return the size of the queue.
253 size_t size (void) const;
255 /// Dump the state of an object.
256 void dump (void) const;
258 // = STL-styled unidirectional iterator factory.
259 ACE_Unbounded_Queue_Iterator
<T
> begin (void);
260 ACE_Unbounded_Queue_Iterator
<T
> end (void);
262 /// Declare the dynamic allocation hooks.
263 ACE_ALLOC_HOOK_DECLARE
;
266 /// Delete all the nodes in the queue.
267 void delete_nodes (void);
269 /// Copy nodes into this queue.
270 void copy_nodes (const ACE_Unbounded_Queue
<T
> &);
272 /// Pointer to the dummy node in the circular linked Queue.
275 /// Current size of the queue.
278 /// Allocation Strategy of the queue.
279 ACE_Allocator
*allocator_
;
282 ACE_END_VERSIONED_NAMESPACE_DECL
284 #if defined (__ACE_INLINE__)
285 #include "ace/Unbounded_Queue.inl"
286 #endif /* __ACE_INLINE__ */
288 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
289 #include "ace/Unbounded_Queue.cpp"
290 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
292 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
293 #pragma implementation ("Unbounded_Queue.cpp")
294 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
296 #include /**/ "ace/post.h"
297 #endif /* ACE_UNBOUNDED_QUEUE_H */