Unbounded_Set.cpp

Go to the documentation of this file.
00001 // $Id: Unbounded_Set.cpp 79134 2007-07-31 18:23:50Z johnnyw $
00002 
00003 #ifndef ACE_UNBOUNDED_SET_CPP
00004 #define ACE_UNBOUNDED_SET_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.inl"
00016 #endif /* __ACE_INLINE__ */
00017 
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set)
00021 
00022   template <class T> size_t
00023 ACE_Unbounded_Set<T>::size (void) const
00024 {
00025   // ACE_TRACE ("ACE_Unbounded_Set<T>::size");
00026   return this->cur_size_;
00027 }
00028 
00029 template <class T> int
00030 ACE_Unbounded_Set<T>::insert_tail (const T &item)
00031 {
00032   // ACE_TRACE ("ACE_Unbounded_Set<T>::insert_tail");
00033   ACE_Node<T> *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<ACE_Node<T>*> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
00041                          ACE_Node<T> (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> void
00054 ACE_Unbounded_Set<T>::reset (void)
00055 {
00056   ACE_TRACE ("reset");
00057 
00058   this->delete_nodes ();
00059 }
00060 
00061 template <class T> void
00062 ACE_Unbounded_Set<T>::dump (void) const
00063 {
00064 #if defined (ACE_HAS_DUMP)
00065   ACE_TRACE ("ACE_Unbounded_Set<T>::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> void
00088 ACE_Unbounded_Set<T>::copy_nodes (const ACE_Unbounded_Set<T> &us)
00089 {
00090   for (ACE_Node<T> *curr = us.head_->next_;
00091        curr != us.head_;
00092        curr = curr->next_)
00093     this->insert_tail (curr->item_);
00094 }
00095 
00096 template <class T> void
00097 ACE_Unbounded_Set<T>::delete_nodes (void)
00098 {
00099   ACE_Node<T> *curr = this->head_->next_;
00100 
00101   // Keep looking until we've hit the dummy node.
00102 
00103   while (curr != this->head_)
00104     {
00105       ACE_Node<T> *temp = curr;
00106       curr = curr->next_;
00107       ACE_DES_FREE_TEMPLATE (temp,
00108                              this->allocator_->free,
00109                              ACE_Node,
00110                              <T>);
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>
00119 ACE_Unbounded_Set<T>::~ACE_Unbounded_Set (void)
00120 {
00121   // ACE_TRACE ("ACE_Unbounded_Set<T>::~ACE_Unbounded_Set");
00122 
00123   this->delete_nodes ();
00124 
00125   // Delete the dummy node.
00126   ACE_DES_FREE_TEMPLATE (head_,
00127                          this->allocator_->free,
00128                          ACE_Node,
00129                          <T>);
00130   this->head_ = 0;
00131 }
00132 
00133 template <class T>
00134 ACE_Unbounded_Set<T>::ACE_Unbounded_Set (ACE_Allocator *alloc)
00135   : head_ (0),
00136     cur_size_ (0),
00137     allocator_ (alloc)
00138 {
00139   // ACE_TRACE ("ACE_Unbounded_Set<T>::ACE_Unbounded_Set");
00140 
00141   if (this->allocator_ == 0)
00142     this->allocator_ = ACE_Allocator::instance ();
00143 
00144   ACE_NEW_MALLOC (this->head_,
00145                   (ACE_Node<T>*) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00146                   ACE_Node<T>);
00147   // Make the list circular by pointing it back to itself.
00148   this->head_->next_ = this->head_;
00149 }
00150 
00151 template <class T>
00152 ACE_Unbounded_Set<T>::ACE_Unbounded_Set (const ACE_Unbounded_Set<T> &us)
00153   : head_ (0),
00154     cur_size_ (0),
00155     allocator_ (us.allocator_)
00156 {
00157   ACE_TRACE ("ACE_Unbounded_Set<T>::ACE_Unbounded_Set");
00158 
00159   if (this->allocator_ == 0)
00160     this->allocator_ = ACE_Allocator::instance ();
00161 
00162   ACE_NEW_MALLOC (this->head_,
00163                   (ACE_Node<T>*) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00164                   ACE_Node<T>);
00165   this->head_->next_ = this->head_;
00166   this->copy_nodes (us);
00167 }
00168 
00169 template <class T> ACE_Unbounded_Set<T> &
00170 ACE_Unbounded_Set<T>::operator= (const ACE_Unbounded_Set<T> &us)
00171 {
00172   ACE_TRACE ("ACE_Unbounded_Set<T>::operator=");
00173 
00174   if (this != &us)
00175     {
00176       this->delete_nodes ();
00177       this->copy_nodes (us);
00178     }
00179 
00180   return *this;
00181 }
00182 
00183 template <class T> int
00184 ACE_Unbounded_Set<T>::find (const T &item) const
00185 {
00186   // ACE_TRACE ("ACE_Unbounded_Set<T>::find");
00187   const_iterator const the_end = this->end ();
00188   for (const_iterator i = this->begin (); i != the_end; ++i)
00189     if ((*i) == item)
00190       return 0;
00191 
00192   return -1;
00193 }
00194 
00195 template <class T> int
00196 ACE_Unbounded_Set<T>::insert (const T &item)
00197 {
00198   // ACE_TRACE ("ACE_Unbounded_Set<T>::insert");
00199   if (this->find (item) == 0)
00200     return 1;
00201   else
00202     return this->insert_tail (item);
00203 }
00204 
00205 template <class T> int
00206 ACE_Unbounded_Set<T>::remove (const T &item)
00207 {
00208   // ACE_TRACE ("ACE_Unbounded_Set<T>::remove");
00209 
00210   // Insert the item to be founded into the dummy node.
00211   this->head_->item_ = item;
00212 
00213   ACE_Node<T> *curr = this->head_;
00214 
00215   while (!(curr->next_->item_ == item))
00216     curr = curr->next_;
00217 
00218   if (curr->next_ == this->head_)
00219     return -1; // Item was not found.
00220   else
00221     {
00222       ACE_Node<T> *temp = curr->next_;
00223       // Skip over the node that we're deleting.
00224       curr->next_ = temp->next_;
00225       --this->cur_size_;
00226       ACE_DES_FREE_TEMPLATE (temp,
00227                              this->allocator_->free,
00228                              ACE_Node,
00229                              <T>);
00230       return 0;
00231     }
00232 }
00233 
00234 template <class T> typename ACE_Unbounded_Set<T>::iterator
00235 ACE_Unbounded_Set<T>::begin (void)
00236 {
00237   // ACE_TRACE ("ACE_Unbounded_Set<T>::begin");
00238   return iterator (*this);
00239 }
00240 
00241 template <class T> typename ACE_Unbounded_Set<T>::iterator
00242 ACE_Unbounded_Set<T>::end (void)
00243 {
00244   // ACE_TRACE ("ACE_Unbounded_Set<T>::end");
00245   return iterator (*this, 1);
00246 }
00247 
00248 template <class T> typename ACE_Unbounded_Set<T>::const_iterator
00249 ACE_Unbounded_Set<T>::begin (void) const
00250 {
00251   // ACE_TRACE ("ACE_Unbounded_Set<T>::begin");
00252   return const_iterator (*this);
00253 }
00254 
00255 template <class T> typename ACE_Unbounded_Set<T>::const_iterator
00256 ACE_Unbounded_Set<T>::end (void) const
00257 {
00258   // ACE_TRACE ("ACE_Unbounded_Set<T>::end");
00259   return const_iterator (*this, 1);
00260 }
00261 
00262 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Iterator)
00263 
00264   template <class T> void
00265 ACE_Unbounded_Set_Iterator<T>::dump (void) const
00266 {
00267 #if defined (ACE_HAS_DUMP)
00268   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::dump");
00269 #endif /* ACE_HAS_DUMP */
00270 }
00271 
00272 template <class T>
00273 ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator (
00274   ACE_Unbounded_Set<T> &s,
00275   bool end)
00276   : current_ (!end ? s.head_->next_ : s.head_ ),
00277     set_ (&s)
00278 {
00279   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator");
00280 }
00281 
00282 template <class T> int
00283 ACE_Unbounded_Set_Iterator<T>::advance (void)
00284 {
00285   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::advance");
00286   this->current_ = this->current_->next_;
00287   return this->current_ != this->set_->head_;
00288 }
00289 
00290 template <class T> int
00291 ACE_Unbounded_Set_Iterator<T>::first (void)
00292 {
00293   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::first");
00294   this->current_ = this->set_->head_->next_;
00295   return this->current_ != this->set_->head_;
00296 }
00297 
00298 template <class T> int
00299 ACE_Unbounded_Set_Iterator<T>::done (void) const
00300 {
00301   ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::done");
00302 
00303   return this->current_ == this->set_->head_;
00304 }
00305 
00306 template <class T> int
00307 ACE_Unbounded_Set_Iterator<T>::next (T *&item)
00308 {
00309   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::next");
00310   if (this->current_ == this->set_->head_)
00311     return 0;
00312   else
00313     {
00314       item = &this->current_->item_;
00315       return 1;
00316     }
00317 }
00318 
00319 template <class T> ACE_Unbounded_Set_Iterator<T>
00320 ACE_Unbounded_Set_Iterator<T>::operator++ (int)
00321 {
00322   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator++ (int)");
00323   ACE_Unbounded_Set_Iterator<T> retv (*this);
00324 
00325   // postfix operator
00326 
00327   this->advance ();
00328   return retv;
00329 }
00330 
00331 template <class T> ACE_Unbounded_Set_Iterator<T>&
00332 ACE_Unbounded_Set_Iterator<T>::operator++ (void)
00333 {
00334   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator++ (void)");
00335 
00336   // prefix operator
00337 
00338   this->advance ();
00339   return *this;
00340 }
00341 
00342 template <class T> T&
00343 ACE_Unbounded_Set_Iterator<T>::operator* (void)
00344 {
00345   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator*");
00346   T *retv = 0;
00347 
00348   int result = this->next (retv);
00349   ACE_ASSERT (result != 0);
00350   ACE_UNUSED_ARG (result);
00351 
00352   return *retv;
00353 }
00354 
00355 template <class T> bool
00356 ACE_Unbounded_Set_Iterator<T>::operator== (const ACE_Unbounded_Set_Iterator<T> &rhs) const
00357 {
00358   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator==");
00359   return (this->set_ == rhs.set_ && this->current_ == rhs.current_);
00360 }
00361 
00362 template <class T> bool
00363 ACE_Unbounded_Set_Iterator<T>::operator!= (const ACE_Unbounded_Set_Iterator<T> &rhs) const
00364 {
00365   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator!=");
00366   return (this->set_ != rhs.set_ || this->current_ != rhs.current_);
00367 }
00368 
00369 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Const_Iterator)
00370 
00371 template <class T> void
00372 ACE_Unbounded_Set_Const_Iterator<T>::dump (void) const
00373 {
00374 #if defined (ACE_HAS_DUMP)
00375   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::dump");
00376 #endif /* ACE_HAS_DUMP */
00377 }
00378 
00379 template <class T>
00380 ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator (
00381   const ACE_Unbounded_Set<T> &s,
00382   bool end)
00383   : current_ (!end ? s.head_->next_ : s.head_ ),
00384     set_ (&s)
00385 {
00386   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator");
00387 }
00388 
00389 template <class T> int
00390 ACE_Unbounded_Set_Const_Iterator<T>::advance (void)
00391 {
00392   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::advance");
00393   this->current_ = this->current_->next_;
00394   return this->current_ != this->set_->head_;
00395 }
00396 
00397 template <class T> int
00398 ACE_Unbounded_Set_Const_Iterator<T>::first (void)
00399 {
00400   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::first");
00401   this->current_ = this->set_->head_->next_;
00402   return this->current_ != this->set_->head_;
00403 }
00404 
00405 template <class T> int
00406 ACE_Unbounded_Set_Const_Iterator<T>::done (void) const
00407 {
00408   ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::done");
00409 
00410   return this->current_ == this->set_->head_;
00411 }
00412 
00413 template <class T> int
00414 ACE_Unbounded_Set_Const_Iterator<T>::next (T *&item)
00415 {
00416   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::next");
00417   if (this->current_ == this->set_->head_)
00418     return 0;
00419   else
00420     {
00421       item = &this->current_->item_;
00422       return 1;
00423     }
00424 }
00425 
00426 template <class T> ACE_Unbounded_Set_Const_Iterator<T>
00427 ACE_Unbounded_Set_Const_Iterator<T>::operator++ (int)
00428 {
00429   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator++ (int)");
00430   ACE_Unbounded_Set_Const_Iterator<T> retv (*this);
00431 
00432   // postfix operator
00433 
00434   this->advance ();
00435   return retv;
00436 }
00437 
00438 template <class T> ACE_Unbounded_Set_Const_Iterator<T>&
00439 ACE_Unbounded_Set_Const_Iterator<T>::operator++ (void)
00440 {
00441   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator++ (void)");
00442 
00443   // prefix operator
00444 
00445   this->advance ();
00446   return *this;
00447 }
00448 
00449 template <class T> T&
00450 ACE_Unbounded_Set_Const_Iterator<T>::operator* (void)
00451 {
00452   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator*");
00453   T *retv = 0;
00454 
00455   int const result = this->next (retv);
00456   ACE_ASSERT (result != 0);
00457   ACE_UNUSED_ARG (result);
00458 
00459   return *retv;
00460 }
00461 
00462 template <class T> bool
00463 ACE_Unbounded_Set_Const_Iterator<T>::operator== (const ACE_Unbounded_Set_Const_Iterator<T> &rhs) const
00464 {
00465   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator==");
00466   return (this->set_ == rhs.set_ && this->current_ == rhs.current_);
00467 }
00468 
00469 template <class T> bool
00470 ACE_Unbounded_Set_Const_Iterator<T>::operator!= (const ACE_Unbounded_Set_Const_Iterator<T> &rhs) const
00471 {
00472   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator!=");
00473   return (this->set_ != rhs.set_ || this->current_ != rhs.current_);
00474 }
00475 
00476 ACE_END_VERSIONED_NAMESPACE_DECL
00477 
00478 #endif /* ACE_UNBOUNDED_SET_CPP */

Generated on Sun Jan 27 12:05:43 2008 for ACE by doxygen 1.3.6