Unbounded_Set_Ex.cpp

Go to the documentation of this file.
00001 // $Id: Unbounded_Set_Ex.cpp 81702 2008-05-15 10:18:07Z johnnyw $
00002 
00003 #ifndef ACE_UNBOUNDED_SET_EX_CPP
00004 #define ACE_UNBOUNDED_SET_EX_CPP
00005 
00006 #include "ace/Unbounded_Set.h"
00007 #include "ace/Malloc_Base.h"
00008 #include "ace/Log_Msg.h"
00009 
00010 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00011 # pragma once
00012 #endif /* ACE_LACKS_PRAGMA_ONCE */
00013 
00014 #if !defined (__ACE_INLINE__)
00015 #include "ace/Unbounded_Set_Ex.inl"
00016 #endif /* __ACE_INLINE__ */
00017 
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Ex)
00021 
00022 template <class T, class C> size_t
00023 ACE_Unbounded_Set_Ex<T, C>::size (void) const
00024 {
00025   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::size");
00026   return this->cur_size_;
00027 }
00028 
00029 template <class T, class C> int
00030 ACE_Unbounded_Set_Ex<T, C>::insert_tail (const T &item)
00031 {
00032   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::insert_tail");
00033   NODE *temp = 0;
00034 
00035   // Insert <item> into the old dummy node location.
00036   this->head_->item_ = item;
00037 
00038   // Create a new dummy node.
00039   ACE_NEW_MALLOC_RETURN (temp,
00040                          static_cast<NODE*> (this->allocator_->malloc (sizeof (NODE))),
00041                          NODE (this->head_->next_),
00042                          -1);
00043   // Link this pointer into the list.
00044   this->head_->next_ = temp;
00045 
00046   // Point the head to the new dummy node.
00047   this->head_ = temp;
00048 
00049   ++this->cur_size_;
00050   return 0;
00051 }
00052 
00053 template <class T, class C> void
00054 ACE_Unbounded_Set_Ex<T, C>::reset (void)
00055 {
00056   ACE_TRACE ("reset");
00057 
00058   this->delete_nodes ();
00059 }
00060 
00061 template <class T, class C> void
00062 ACE_Unbounded_Set_Ex<T, C>::dump (void) const
00063 {
00064 #if defined (ACE_HAS_DUMP)
00065   ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::dump");
00066 
00067   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00068   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\nhead_ = %u"), this->head_));
00069   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\nhead_->next_ = %u"), this->head_->next_));
00070   ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_));
00071 
00072   T *item = 0;
00073 #if !defined (ACE_NLOGGING)
00074   size_t count = 1;
00075 #endif /* ! ACE_NLOGGING */
00076 
00077   const_iterator const the_end = this->end ();
00078   for (const_iterator i (this->begin ());
00079        i != end;
00080        ++i)
00081     ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("count = %u\n"), count++));
00082 
00083   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00084 #endif /* ACE_HAS_DUMP */
00085 }
00086 
00087 template <class T, class C> void
00088 ACE_Unbounded_Set_Ex<T, C>::copy_nodes (const ACE_Unbounded_Set_Ex<T, C> &us)
00089 {
00090   for (NODE *curr = us.head_->next_;
00091        curr != us.head_;
00092        curr = curr->next_)
00093     this->insert_tail (curr->item_);
00094 }
00095 
00096 template <class T, class C> void
00097 ACE_Unbounded_Set_Ex<T, C>::delete_nodes (void)
00098 {
00099   NODE *curr = this->head_->next_;
00100 
00101   // Keep looking until we've hit the dummy node.
00102 
00103   while (curr != this->head_)
00104     {
00105       NODE *temp = curr;
00106       curr = curr->next_;
00107       ACE_DES_FREE_TEMPLATE2 (temp,
00108                              this->allocator_->free,
00109                              ACE_Node,
00110                              T, C);
00111       --this->cur_size_;
00112     }
00113 
00114   // Reset the list to be a circular list with just a dummy node.
00115   this->head_->next_ = this->head_;
00116 }
00117 
00118 template <class T, class C>
00119 ACE_Unbounded_Set_Ex<T, C>::~ACE_Unbounded_Set_Ex (void)
00120 {
00121   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::~ACE_Unbounded_Set_Ex");
00122 
00123   this->delete_nodes ();
00124 
00125   // Delete the dummy node.
00126   ACE_DES_FREE_TEMPLATE2 (head_,
00127                          this->allocator_->free,
00128                          ACE_Node,
00129                          T, C);
00130   this->head_ = 0;
00131 }
00132 
00133 template <class T, class C>
00134 ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex (ACE_Allocator *alloc)
00135   : head_ (0),
00136     cur_size_ (0),
00137     allocator_ (alloc)
00138 {
00139   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
00140 
00141   if (this->allocator_ == 0)
00142     this->allocator_ = ACE_Allocator::instance ();
00143 
00144   ACE_NEW_MALLOC (this->head_,
00145                   (NODE*) this->allocator_->malloc (sizeof (NODE)),
00146                   NODE);
00147   // Make the list circular by pointing it back to itself.
00148   this->head_->next_ = this->head_;
00149 }
00150 
00151 template <class T, class C>
00152 ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex (const C &comp,
00153                                             ACE_Allocator *alloc)
00154   : head_ (0),
00155     cur_size_ (0),
00156     allocator_ (alloc),
00157     comp_ (comp)
00158 {
00159   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
00160 
00161   if (this->allocator_ == 0)
00162     this->allocator_ = ACE_Allocator::instance ();
00163 
00164   ACE_NEW_MALLOC (this->head_,
00165                   (NODE*) this->allocator_->malloc (sizeof (NODE)),
00166                   NODE);
00167   // Make the list circular by pointing it back to itself.
00168   this->head_->next_ = this->head_;
00169 }
00170 
00171 template <class T, class C>
00172 ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex (const ACE_Unbounded_Set_Ex<T, C> &us)
00173   : head_ (0),
00174     cur_size_ (0),
00175     allocator_ (us.allocator_),
00176     comp_ (us.comp_)
00177 {
00178   ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::ACE_Unbounded_Set_Ex");
00179 
00180   if (this->allocator_ == 0)
00181     this->allocator_ = ACE_Allocator::instance ();
00182 
00183   ACE_NEW_MALLOC (this->head_,
00184                   (NODE*) this->allocator_->malloc (sizeof (NODE)),
00185                   NODE);
00186   this->head_->next_ = this->head_;
00187   this->copy_nodes (us);
00188 }
00189 
00190 template <class T, class C> ACE_Unbounded_Set_Ex<T, C> &
00191 ACE_Unbounded_Set_Ex<T, C>::operator= (const ACE_Unbounded_Set_Ex<T, C> &us)
00192 {
00193   ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::operator=");
00194 
00195   if (this != &us)
00196     {
00197       this->delete_nodes ();
00198       this->copy_nodes (us);
00199     }
00200 
00201   return *this;
00202 }
00203 
00204 template <class T, class C> int
00205 ACE_Unbounded_Set_Ex<T, C>::find (const T &item) const
00206 {
00207   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::find");
00208   const_iterator const the_end = this->end ();
00209   for (const_iterator i = this->begin (); i != the_end; ++i)
00210     if (this->comp_(*i, item))
00211       return 0;
00212 
00213   return -1;
00214 }
00215 
00216 template <class T, class C> int
00217 ACE_Unbounded_Set_Ex<T, C>::insert (const T &item)
00218 {
00219   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::insert");
00220   if (this->find (item) == 0)
00221     return 1;
00222   else
00223     return this->insert_tail (item);
00224 }
00225 
00226 template <class T, class C> int
00227 ACE_Unbounded_Set_Ex<T, C>::remove (const T &item)
00228 {
00229   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::remove");
00230 
00231   // Insert the item to be founded into the dummy node.
00232   this->head_->item_ = item;
00233 
00234   NODE *curr = this->head_;
00235 
00236   while (!(this->comp_ (curr->next_->item_, item)))
00237     curr = curr->next_;
00238 
00239   if (curr->next_ == this->head_)
00240     return -1; // Item was not found.
00241   else
00242     {
00243       NODE *temp = curr->next_;
00244       // Skip over the node that we're deleting.
00245       curr->next_ = temp->next_;
00246       --this->cur_size_;
00247       ACE_DES_FREE_TEMPLATE2 (temp,
00248                              this->allocator_->free,
00249                              ACE_Node,
00250                              T, C);
00251       return 0;
00252     }
00253 }
00254 
00255 template <class T, class C> typename ACE_Unbounded_Set_Ex<T, C>::iterator
00256 ACE_Unbounded_Set_Ex<T, C>::begin (void)
00257 {
00258   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::begin");
00259   return iterator (*this);
00260 }
00261 
00262 template <class T, class C> typename ACE_Unbounded_Set_Ex<T, C>::iterator
00263 ACE_Unbounded_Set_Ex<T, C>::end (void)
00264 {
00265   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::end");
00266   return iterator (*this, 1);
00267 }
00268 
00269 template <class T, class C> typename ACE_Unbounded_Set_Ex<T, C>::const_iterator
00270 ACE_Unbounded_Set_Ex<T, C>::begin (void) const
00271 {
00272   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::begin");
00273   return const_iterator (*this);
00274 }
00275 
00276 template <class T, class C> typename ACE_Unbounded_Set_Ex<T, C>::const_iterator
00277 ACE_Unbounded_Set_Ex<T, C>::end (void) const
00278 {
00279   // ACE_TRACE ("ACE_Unbounded_Set_Ex<T, C>::end");
00280   return const_iterator (*this, 1);
00281 }
00282 
00283 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Ex_Iterator)
00284 
00285 template <class T, class C> void
00286 ACE_Unbounded_Set_Ex_Iterator<T, C>::dump (void) const
00287 {
00288 #if defined (ACE_HAS_DUMP)
00289   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::dump");
00290 #endif /* ACE_HAS_DUMP */
00291 }
00292 
00293 template <class T, class C>
00294 ACE_Unbounded_Set_Ex_Iterator<T, C>::ACE_Unbounded_Set_Ex_Iterator (
00295   ACE_Unbounded_Set_Ex<T, C> &s,
00296   bool end)
00297   : current_ (!end ? s.head_->next_ : s.head_ ),
00298     set_ (&s)
00299 {
00300   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::ACE_Unbounded_Set_Ex_Iterator");
00301 }
00302 
00303 template <class T, class C> int
00304 ACE_Unbounded_Set_Ex_Iterator<T, C>::advance (void)
00305 {
00306   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::advance");
00307   this->current_ = this->current_->next_;
00308   return this->current_ != this->set_->head_;
00309 }
00310 
00311 template <class T, class C> int
00312 ACE_Unbounded_Set_Ex_Iterator<T, C>::first (void)
00313 {
00314   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::first");
00315   this->current_ = this->set_->head_->next_;
00316   return this->current_ != this->set_->head_;
00317 }
00318 
00319 template <class T, class C> int
00320 ACE_Unbounded_Set_Ex_Iterator<T, C>::done (void) const
00321 {
00322   ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::done");
00323 
00324   return this->current_ == this->set_->head_;
00325 }
00326 
00327 template <class T, class C> int
00328 ACE_Unbounded_Set_Ex_Iterator<T, C>::next (T *&item)
00329 {
00330   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::next");
00331   if (this->current_ == this->set_->head_)
00332     return 0;
00333   else
00334     {
00335       item = &this->current_->item_;
00336       return 1;
00337     }
00338 }
00339 
00340 template <class T, class C> ACE_Unbounded_Set_Ex_Iterator<T, C>
00341 ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ (int)
00342 {
00343   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ (int)");
00344   ACE_Unbounded_Set_Ex_Iterator<T, C> retv (*this);
00345 
00346   // postfix operator
00347 
00348   this->advance ();
00349   return retv;
00350 }
00351 
00352 template <class T, class C> ACE_Unbounded_Set_Ex_Iterator<T, C>&
00353 ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ (void)
00354 {
00355   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator++ (void)");
00356 
00357   // prefix operator
00358 
00359   this->advance ();
00360   return *this;
00361 }
00362 
00363 template <class T, class C> T&
00364 ACE_Unbounded_Set_Ex_Iterator<T, C>::operator* (void)
00365 {
00366   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator*");
00367   T *retv = 0;
00368 
00369   int result = this->next (retv);
00370   ACE_ASSERT (result != 0);
00371   ACE_UNUSED_ARG (result);
00372 
00373   return *retv;
00374 }
00375 
00376 template <class T, class C> bool
00377 ACE_Unbounded_Set_Ex_Iterator<T, C>::operator== (const ACE_Unbounded_Set_Ex_Iterator<T, C> &rhs) const
00378 {
00379   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator==");
00380   return (this->set_ == rhs.set_ && this->current_ == rhs.current_);
00381 }
00382 
00383 template <class T, class C> bool
00384 ACE_Unbounded_Set_Ex_Iterator<T, C>::operator!= (const ACE_Unbounded_Set_Ex_Iterator<T, C> &rhs) const
00385 {
00386   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Iterator<T, C>::operator!=");
00387   return (this->set_ != rhs.set_ || this->current_ != rhs.current_);
00388 }
00389 
00390 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Ex_Const_Iterator)
00391 
00392 template <class T, class C> void
00393 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::dump (void) const
00394 {
00395 #if defined (ACE_HAS_DUMP)
00396   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::dump");
00397 #endif /* ACE_HAS_DUMP */
00398 }
00399 
00400 template <class T, class C>
00401 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::ACE_Unbounded_Set_Ex_Const_Iterator (
00402   const ACE_Unbounded_Set_Ex<T, C> &s,
00403   bool end)
00404   : current_ (!end ? s.head_->next_ : s.head_ ),
00405     set_ (&s)
00406 {
00407   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::ACE_Unbounded_Set_Ex_Const_Iterator");
00408 }
00409 
00410 template <class T, class C> int
00411 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::advance (void)
00412 {
00413   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::advance");
00414   this->current_ = this->current_->next_;
00415   return this->current_ != this->set_->head_;
00416 }
00417 
00418 template <class T, class C> int
00419 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::first (void)
00420 {
00421   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::first");
00422   this->current_ = this->set_->head_->next_;
00423   return this->current_ != this->set_->head_;
00424 }
00425 
00426 template <class T, class C> int
00427 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::done (void) const
00428 {
00429   ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::done");
00430 
00431   return this->current_ == this->set_->head_;
00432 }
00433 
00434 template <class T, class C> int
00435 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::next (T *&item)
00436 {
00437   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::next");
00438   if (this->current_ == this->set_->head_)
00439     return 0;
00440   else
00441     {
00442       item = &this->current_->item_;
00443       return 1;
00444     }
00445 }
00446 
00447 template <class T, class C> ACE_Unbounded_Set_Ex_Const_Iterator<T, C>
00448 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ (int)
00449 {
00450   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ (int)");
00451   ACE_Unbounded_Set_Ex_Const_Iterator<T, C> retv (*this);
00452 
00453   // postfix operator
00454 
00455   this->advance ();
00456   return retv;
00457 }
00458 
00459 template <class T, class C> ACE_Unbounded_Set_Ex_Const_Iterator<T, C>&
00460 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ (void)
00461 {
00462   // ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator++ (void)");
00463 
00464   // prefix operator
00465 
00466   this->advance ();
00467   return *this;
00468 }
00469 
00470 template <class T, class C> T&
00471 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator* (void)
00472 {
00473   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator*");
00474   T *retv = 0;
00475 
00476   int const result = this->next (retv);
00477   ACE_ASSERT (result != 0);
00478   ACE_UNUSED_ARG (result);
00479 
00480   return *retv;
00481 }
00482 
00483 template <class T, class C> bool
00484 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator== (const ACE_Unbounded_Set_Ex_Const_Iterator<T, C> &rhs) const
00485 {
00486   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator==");
00487   return (this->set_ == rhs.set_ && this->current_ == rhs.current_);
00488 }
00489 
00490 template <class T, class C> bool
00491 ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator!= (const ACE_Unbounded_Set_Ex_Const_Iterator<T, C> &rhs) const
00492 {
00493   //ACE_TRACE ("ACE_Unbounded_Set_Ex_Const_Iterator<T, C>::operator!=");
00494   return (this->set_ != rhs.set_ || this->current_ != rhs.current_);
00495 }
00496 
00497 ACE_END_VERSIONED_NAMESPACE_DECL
00498 
00499 #endif /* ACE_UNBOUNDED_SET_EX_CPP */

Generated on Tue Feb 2 17:18:43 2010 for ACE by  doxygen 1.4.7