3 //=============================================================================
5 * @file Unbounded_Queue.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //=============================================================================
11 #ifndef ACE_UNBOUNDED_QUEUE_H
12 #define ACE_UNBOUNDED_QUEUE_H
13 #include /**/ "ace/pre.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/os_include/os_stddef.h"
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
28 class ACE_Unbounded_Queue
;
31 * @class ACE_Unbounded_Queue_Iterator
33 * @brief Implement an iterator over an unbounded queue.
36 class ACE_Unbounded_Queue_Iterator
39 ACE_Unbounded_Queue_Iterator (ACE_Unbounded_Queue
<T
> &q
, int end
= 0);
41 // = Iteration methods.
43 /// Pass back the @a next_item that hasn't been seen in the queue.
44 /// Returns 0 when all items have been seen, else 1.
45 int next (T
*&next_item
);
47 /// Move forward by one element in the set. Returns 0 when all the
48 /// items in the queue have been seen, else 1.
51 /// Move to the first element in the queue. Returns 0 if the
52 /// queue is empty, else 1.
55 /// Returns 1 when all items have been seen, else 0.
58 /// Dump the state of an object.
61 /// Declare the dynamic allocation hooks.
62 ACE_ALLOC_HOOK_DECLARE
;
65 /// Pointer to the current node in the iteration.
66 ACE_Node
<T
> *current_
;
68 /// Pointer to the queue we're iterating over.
69 ACE_Unbounded_Queue
<T
> &queue_
;
73 * @class ACE_Unbounded_Queue_Const_Iterator
75 * @brief Implement an iterator over an const unbounded queue.
78 class ACE_Unbounded_Queue_Const_Iterator
81 ACE_Unbounded_Queue_Const_Iterator (const ACE_Unbounded_Queue
<T
> &q
, int end
= 0);
83 // = Iteration methods.
85 /// Pass back the @a next_item that hasn't been seen in the queue.
86 /// Returns 0 when all items have been seen, else 1.
87 int next (T
*&next_item
);
89 /// Move forward by one element in the set. Returns 0 when all the
90 /// items in the queue have been seen, else 1.
93 /// Move to the first element in the queue. Returns 0 if the
94 /// queue is empty, else 1.
97 /// Returns 1 when all items have been seen, else 0.
100 /// Dump the state of an object.
103 /// Declare the dynamic allocation hooks.
104 ACE_ALLOC_HOOK_DECLARE
;
107 /// Pointer to the current node in the iteration.
108 ACE_Node
<T
> *current_
;
110 /// Pointer to the queue we're iterating over.
111 const ACE_Unbounded_Queue
<T
> &queue_
;
115 * @class ACE_Unbounded_Queue
117 * @brief A Queue of "infinite" length.
119 * This implementation of an unbounded queue uses a circular
120 * linked list with a dummy node.
122 * <b> Requirements and Performance Characteristics</b>
123 * - Internal Structure
124 * Circular linked list
125 * - Duplicates allowed?
127 * - Random access allowed?
131 * - Insert/replace speed
133 * - Iterator still valid after change to container?
135 * - Frees memory for removed elements?
137 * - Items inserted by
139 * - Requirements for contained type
140 * -# Default constructor
141 * -# Copy constructor
145 class ACE_Unbounded_Queue
148 friend class ACE_Unbounded_Queue_Iterator
<T
>;
149 friend class ACE_Unbounded_Queue_Const_Iterator
<T
>;
152 typedef ACE_Unbounded_Queue_Iterator
<T
> ITERATOR
;
153 typedef ACE_Unbounded_Queue_Const_Iterator
<T
> CONST_ITERATOR
;
155 /// Construction. Use user specified allocation strategy
158 * Initialize an empty queue using the strategy provided.
160 ACE_Unbounded_Queue (ACE_Allocator
*alloc
= nullptr);
162 /// Copy constructor.
164 * Initialize the queue to be a copy of the provided queue.
166 ACE_Unbounded_Queue (const ACE_Unbounded_Queue
<T
> &);
168 /// Assignment operator.
170 * Perform a deep copy of rhs.
172 void operator= (const ACE_Unbounded_Queue
<T
> &);
176 * Clean up the memory for the queue.
178 ~ACE_Unbounded_Queue ();
180 // = Check boundary conditions.
182 /// Returns true if the container is empty, otherwise returns false.
184 * Constant time check to see if the queue is empty.
186 bool is_empty () const;
190 * The queue cannot be full, so it always returns 0.
192 bool is_full () const;
194 // = Classic queue operations.
196 /// Adds @a new_item to the tail of the queue. Returns 0 on success,
199 * Insert an item at the end of the queue.
201 int enqueue_tail (const T
&new_item
);
203 /// Adds @a new_item to the head of the queue. Returns 0 on success,
206 * Insert an item at the head of the queue.
208 int enqueue_head (const T
&new_item
);
210 /// Removes and returns the first @a item on the queue. Returns 0 on
211 /// success, -1 if the queue was empty.
213 * Remove an item from the head of the queue.
215 int dequeue_head (T
&item
);
217 // = Additional utility methods.
219 /// Reset the ACE_Unbounded_Queue to be empty and release all its
220 /// dynamically allocated resources.
222 * Delete the queue nodes.
226 /// Get the @a slot th element in the set. Returns -1 if the element
227 /// isn't in the range {0..#cur_size_ - 1}, else 0.
229 * Find the item in the queue between 0 and the provided index of the
232 int get (T
*&item
, size_t slot
= 0) const;
234 /// Set the @a slot th element of the queue to @a item.
236 * Set the @a slot th element in the set. Will pad out the set with
237 * empty nodes if @a slot is beyond the range {0..#cur_size_ - 1}.
238 * Returns -1 on failure, 0 if @a slot isn't initially in range, and
241 int set (const T
&item
, size_t slot
);
243 /// The number of items in the queue.
245 * Return the size of the queue.
247 size_t size () const;
249 /// Dump the state of an object.
252 // = STL-styled unidirectional iterator factory.
253 ACE_Unbounded_Queue_Iterator
<T
> begin ();
254 ACE_Unbounded_Queue_Iterator
<T
> end ();
256 /// Declare the dynamic allocation hooks.
257 ACE_ALLOC_HOOK_DECLARE
;
260 /// Delete all the nodes in the queue.
261 void delete_nodes ();
263 /// Copy nodes into this queue.
264 void copy_nodes (const ACE_Unbounded_Queue
<T
> &);
266 /// Pointer to the dummy node in the circular linked Queue.
269 /// Current size of the queue.
272 /// Allocation Strategy of the queue.
273 ACE_Allocator
*allocator_
;
276 ACE_END_VERSIONED_NAMESPACE_DECL
278 #if defined (__ACE_INLINE__)
279 #include "ace/Unbounded_Queue.inl"
280 #endif /* __ACE_INLINE__ */
282 #include "ace/Unbounded_Queue.cpp"
284 #include /**/ "ace/post.h"
285 #endif /* ACE_UNBOUNDED_QUEUE_H */