1 #ifndef ACE_UNBOUNDED_SET_EX_CPP
2 #define ACE_UNBOUNDED_SET_EX_CPP
4 #include "ace/Unbounded_Set.h"
5 #include "ace/Malloc_Base.h"
6 #include "ace/Log_Category.h"
8 #if !defined (ACE_LACKS_PRAGMA_ONCE)
10 #endif /* ACE_LACKS_PRAGMA_ONCE */
12 #if !defined (__ACE_INLINE__)
13 #include "ace/Unbounded_Set_Ex.inl"
14 #endif /* __ACE_INLINE__ */
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 ACE_ALLOC_HOOK_DEFINE_Tcc(ACE_Unbounded_Set_Ex
)
20 template <class T
, class C
> size_t
21 ACE_Unbounded_Set_Ex
<T
, C
>::size () const
23 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::size");
24 return this->cur_size_
;
27 template <class T
, class C
> int
28 ACE_Unbounded_Set_Ex
<T
, C
>::insert_tail (const T
&item
)
30 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::insert_tail");
33 // Insert <item> into the old dummy node location.
34 this->head_
->item_
= item
;
36 // Create a new dummy node.
37 ACE_NEW_MALLOC_RETURN (temp
,
38 static_cast<NODE
*> (this->allocator_
->malloc (sizeof (NODE
))),
39 NODE (this->head_
->next_
),
41 // Link this pointer into the list.
42 this->head_
->next_
= temp
;
44 // Point the head to the new dummy node.
51 template <class T
, class C
> void
52 ACE_Unbounded_Set_Ex
<T
, C
>::reset ()
56 this->delete_nodes ();
59 template <class T
, class C
> void
60 ACE_Unbounded_Set_Ex
<T
, C
>::dump () const
62 #if defined (ACE_HAS_DUMP)
63 ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::dump");
65 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
66 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nhead_ = %u"), this->head_
));
67 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nhead_->next_ = %u"), this->head_
->next_
));
68 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_
));
71 #if !defined (ACE_NLOGGING)
73 #endif /* ! ACE_NLOGGING */
75 const_iterator
const the_end
= this->end ();
76 for (const_iterator
i (this->begin ());
79 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("count = %u\n"), count
++));
81 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
82 #endif /* ACE_HAS_DUMP */
85 template <class T
, class C
> void
86 ACE_Unbounded_Set_Ex
<T
, C
>::copy_nodes (const ACE_Unbounded_Set_Ex
<T
, C
> &us
)
88 for (NODE
*curr
= us
.head_
->next_
;
91 this->insert_tail (curr
->item_
);
94 template <class T
, class C
> void
95 ACE_Unbounded_Set_Ex
<T
, C
>::delete_nodes ()
97 NODE
*curr
= this->head_
->next_
;
99 // Keep looking until we've hit the dummy node.
101 while (curr
!= this->head_
)
105 ACE_DES_FREE_TEMPLATE2 (temp
,
106 this->allocator_
->free
,
112 // Reset the list to be a circular list with just a dummy node.
113 this->head_
->next_
= this->head_
;
116 template <class T
, class C
>
117 ACE_Unbounded_Set_Ex
<T
, C
>::~ACE_Unbounded_Set_Ex ()
119 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::~ACE_Unbounded_Set_Ex");
121 this->delete_nodes ();
123 // Delete the dummy node.
124 ACE_DES_FREE_TEMPLATE2 (head_
,
125 this->allocator_
->free
,
131 template <class T
, class C
>
132 ACE_Unbounded_Set_Ex
<T
, C
>::ACE_Unbounded_Set_Ex (ACE_Allocator
*alloc
)
137 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
139 if (this->allocator_
== 0)
140 this->allocator_
= ACE_Allocator::instance ();
142 ACE_NEW_MALLOC (this->head_
,
143 (NODE
*) this->allocator_
->malloc (sizeof (NODE
)),
145 // Make the list circular by pointing it back to itself.
146 this->head_
->next_
= this->head_
;
149 template <class T
, class C
>
150 ACE_Unbounded_Set_Ex
<T
, C
>::ACE_Unbounded_Set_Ex (const C
&comp
,
151 ACE_Allocator
*alloc
)
157 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
159 if (this->allocator_
== 0)
160 this->allocator_
= ACE_Allocator::instance ();
162 ACE_NEW_MALLOC (this->head_
,
163 (NODE
*) this->allocator_
->malloc (sizeof (NODE
)),
165 // Make the list circular by pointing it back to itself.
166 this->head_
->next_
= this->head_
;
169 template <class T
, class C
>
170 ACE_Unbounded_Set_Ex
<T
, C
>::ACE_Unbounded_Set_Ex (const ACE_Unbounded_Set_Ex
<T
, C
> &us
)
173 allocator_ (us
.allocator_
),
176 ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
178 if (this->allocator_
== 0)
179 this->allocator_
= ACE_Allocator::instance ();
181 ACE_NEW_MALLOC (this->head_
,
182 (NODE
*) this->allocator_
->malloc (sizeof (NODE
)),
184 this->head_
->next_
= this->head_
;
185 this->copy_nodes (us
);
188 template <class T
, class C
> ACE_Unbounded_Set_Ex
<T
, C
> &
189 ACE_Unbounded_Set_Ex
<T
, C
>::operator= (const ACE_Unbounded_Set_Ex
<T
, C
> &us
)
191 ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::operator=");
195 this->delete_nodes ();
196 this->copy_nodes (us
);
202 template <class T
, class C
> int
203 ACE_Unbounded_Set_Ex
<T
, C
>::find (const T
&item
) const
205 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::find");
206 const_iterator
const the_end
= this->end ();
207 for (const_iterator i
= this->begin (); i
!= the_end
; ++i
)
208 if (this->comp_(*i
, item
))
214 template <class T
, class C
> int
215 ACE_Unbounded_Set_Ex
<T
, C
>::insert (const T
&item
)
217 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::insert");
218 if (this->find (item
) == 0)
221 return this->insert_tail (item
);
224 template <class T
, class C
> int
225 ACE_Unbounded_Set_Ex
<T
, C
>::remove (const T
&item
)
227 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::remove");
229 // Insert the item to be found into the dummy node.
230 this->head_
->item_
= item
;
232 NODE
*curr
= this->head_
;
234 while (!(this->comp_ (curr
->next_
->item_
, item
)))
237 // reset the dummy node. This ensures reference counted items are
238 // completely released. Without this, a reference can linger as
239 // the dummy long after it was removed from the list.
240 this->head_
->item_
= T();
242 if (curr
->next_
== this->head_
)
243 return -1; // Item was not found.
246 NODE
*temp
= curr
->next_
;
247 // Skip over the node that we're deleting.
248 curr
->next_
= temp
->next_
;
250 ACE_DES_FREE_TEMPLATE2 (temp
,
251 this->allocator_
->free
,
258 template <class T
, class C
> typename ACE_Unbounded_Set_Ex
<T
, C
>::iterator
259 ACE_Unbounded_Set_Ex
<T
, C
>::begin ()
261 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::begin");
262 return iterator (*this);
265 template <class T
, class C
> typename ACE_Unbounded_Set_Ex
<T
, C
>::iterator
266 ACE_Unbounded_Set_Ex
<T
, C
>::end ()
268 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::end");
269 return iterator (*this, 1);
272 template <class T
, class C
> typename ACE_Unbounded_Set_Ex
<T
, C
>::const_iterator
273 ACE_Unbounded_Set_Ex
<T
, C
>::begin () const
275 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::begin");
276 return const_iterator (*this);
279 template <class T
, class C
> typename ACE_Unbounded_Set_Ex
<T
, C
>::const_iterator
280 ACE_Unbounded_Set_Ex
<T
, C
>::end () const
282 // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::end");
283 return const_iterator (*this, 1);
286 ACE_ALLOC_HOOK_DEFINE_Tcc(ACE_Unbounded_Set_Ex_Iterator
)
288 template <class T
, class C
> void
289 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::dump () const
291 #if defined (ACE_HAS_DUMP)
292 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::dump");
293 #endif /* ACE_HAS_DUMP */
296 template <class T
, class C
>
297 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::ACE_Unbounded_Set_Ex_Iterator (
298 ACE_Unbounded_Set_Ex
<T
, C
> &s
,
300 : current_ (!end
? s
.head_
->next_
: s
.head_
),
303 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::ACE_Unbounded_Set_Ex_Iterator");
306 template <class T
, class C
> int
307 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::advance ()
309 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::advance");
310 this->current_
= this->current_
->next_
;
311 return this->current_
!= this->set_
->head_
;
314 template <class T
, class C
> int
315 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::first ()
317 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::first");
318 this->current_
= this->set_
->head_
->next_
;
319 return this->current_
!= this->set_
->head_
;
322 template <class T
, class C
> int
323 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::done () const
325 ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::done");
327 return this->current_
== this->set_
->head_
;
330 template <class T
, class C
> int
331 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::next (T
*&item
)
333 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::next");
334 if (this->current_
== this->set_
->head_
)
338 item
= &this->current_
->item_
;
343 template <class T
, class C
> ACE_Unbounded_Set_Ex_Iterator
<T
, C
>
344 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::operator++ (int)
346 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ (int)");
347 ACE_Unbounded_Set_Ex_Iterator
<T
, C
> retv (*this);
355 template <class T
, class C
> ACE_Unbounded_Set_Ex_Iterator
<T
, C
>&
356 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::operator++ ()
358 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ ()");
366 template <class T
, class C
> T
&
367 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::operator* ()
369 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator*");
372 int result
= this->next (retv
);
373 ACE_ASSERT (result
!= 0);
374 ACE_UNUSED_ARG (result
);
379 template <class T
, class C
> bool
380 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::operator== (const ACE_Unbounded_Set_Ex_Iterator
<T
, C
> &rhs
) const
382 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator==");
383 return (this->set_
== rhs
.set_
&& this->current_
== rhs
.current_
);
386 template <class T
, class C
> bool
387 ACE_Unbounded_Set_Ex_Iterator
<T
, C
>::operator!= (const ACE_Unbounded_Set_Ex_Iterator
<T
, C
> &rhs
) const
389 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator!=");
390 return (this->set_
!= rhs
.set_
|| this->current_
!= rhs
.current_
);
393 ACE_ALLOC_HOOK_DEFINE_Tcc(ACE_Unbounded_Set_Ex_Const_Iterator
)
395 template <class T
, class C
> void
396 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::dump () const
398 #if defined (ACE_HAS_DUMP)
399 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::dump");
400 #endif /* ACE_HAS_DUMP */
403 template <class T
, class C
>
404 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::ACE_Unbounded_Set_Ex_Const_Iterator (
405 const ACE_Unbounded_Set_Ex
<T
, C
> &s
,
407 : current_ (!end
? s
.head_
->next_
: s
.head_
),
410 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::ACE_Unbounded_Set_Ex_Const_Iterator");
413 template <class T
, class C
> int
414 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::advance ()
416 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::advance");
417 this->current_
= this->current_
->next_
;
418 return this->current_
!= this->set_
->head_
;
421 template <class T
, class C
> int
422 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::first ()
424 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::first");
425 this->current_
= this->set_
->head_
->next_
;
426 return this->current_
!= this->set_
->head_
;
429 template <class T
, class C
> int
430 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::done () const
432 ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::done");
434 return this->current_
== this->set_
->head_
;
437 template <class T
, class C
> int
438 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::next (T
*&item
)
440 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::next");
441 if (this->current_
== this->set_
->head_
)
445 item
= &this->current_
->item_
;
450 template <class T
, class C
> ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>
451 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::operator++ (int)
453 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ (int)");
454 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
> retv (*this);
462 template <class T
, class C
> ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>&
463 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::operator++ ()
465 // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ ()");
473 template <class T
, class C
> T
&
474 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::operator* ()
476 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator*");
479 int const result
= this->next (retv
);
480 ACE_ASSERT (result
!= 0);
481 ACE_UNUSED_ARG (result
);
486 template <class T
, class C
> bool
487 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::operator== (const ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
> &rhs
) const
489 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator==");
490 return (this->set_
== rhs
.set_
&& this->current_
== rhs
.current_
);
493 template <class T
, class C
> bool
494 ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
>::operator!= (const ACE_Unbounded_Set_Ex_Const_Iterator
<T
, C
> &rhs
) const
496 //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator!=");
497 return (this->set_
!= rhs
.set_
|| this->current_
!= rhs
.current_
);
500 ACE_END_VERSIONED_NAMESPACE_DECL
502 #endif /* ACE_UNBOUNDED_SET_EX_CPP */