Unbounded_Set.cpp

Go to the documentation of this file.
00001 // Unbounded_Set.cpp,v 4.16 2006/02/10 10:35:03 jwillemsen Exp
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;
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_LIB_TEXT ("\nhead_ = %u"), this->head_));
00069   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("\nhead_->next_ = %u"), this->head_->next_));
00070   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_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   for (ACE_Unbounded_Set_Iterator<T> iter (*(ACE_Unbounded_Set<T> *) this);
00078        iter.next (item) != 0;
00079        iter.advance ())
00080     ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("count = %d\n"), count++));
00081 
00082   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00083 #endif /* ACE_HAS_DUMP */
00084 }
00085 
00086 template <class T> void
00087 ACE_Unbounded_Set<T>::copy_nodes (const ACE_Unbounded_Set<T> &us)
00088 {
00089   for (ACE_Node<T> *curr = us.head_->next_;
00090        curr != us.head_;
00091        curr = curr->next_)
00092     this->insert_tail (curr->item_);
00093 }
00094 
00095 template <class T> void
00096 ACE_Unbounded_Set<T>::delete_nodes (void)
00097 {
00098   ACE_Node<T> *curr = this->head_->next_;
00099 
00100   // Keep looking until we've hit the dummy node.
00101 
00102   while (curr != this->head_)
00103     {
00104       ACE_Node<T> *temp = curr;
00105       curr = curr->next_;
00106       ACE_DES_FREE_TEMPLATE (temp,
00107                              this->allocator_->free,
00108                              ACE_Node,
00109                              <T>);
00110       --this->cur_size_;
00111     }
00112 
00113   // Reset the list to be a circular list with just a dummy node.
00114   this->head_->next_ = this->head_;
00115 }
00116 
00117 template <class T>
00118 ACE_Unbounded_Set<T>::~ACE_Unbounded_Set (void)
00119 {
00120   // ACE_TRACE ("ACE_Unbounded_Set<T>::~ACE_Unbounded_Set");
00121 
00122   this->delete_nodes ();
00123 
00124   // Delete the dummy node.
00125   ACE_DES_FREE_TEMPLATE (head_,
00126                          this->allocator_->free,
00127                          ACE_Node,
00128                          <T>);
00129   this->head_ = 0;
00130 }
00131 
00132 template <class T>
00133 ACE_Unbounded_Set<T>::ACE_Unbounded_Set (ACE_Allocator *alloc)
00134   : head_ (0),
00135     cur_size_ (0),
00136     allocator_ (alloc)
00137 {
00138   // ACE_TRACE ("ACE_Unbounded_Set<T>::ACE_Unbounded_Set");
00139 
00140   if (this->allocator_ == 0)
00141     this->allocator_ = ACE_Allocator::instance ();
00142 
00143   ACE_NEW_MALLOC (this->head_,
00144                   (ACE_Node<T>*) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00145                   ACE_Node<T>);
00146   // Make the list circular by pointing it back to itself.
00147   this->head_->next_ = this->head_;
00148 }
00149 
00150 template <class T>
00151 ACE_Unbounded_Set<T>::ACE_Unbounded_Set (const ACE_Unbounded_Set<T> &us)
00152   : head_ (0),
00153     cur_size_ (0),
00154     allocator_ (us.allocator_)
00155 {
00156   ACE_TRACE ("ACE_Unbounded_Set<T>::ACE_Unbounded_Set");
00157 
00158   if (this->allocator_ == 0)
00159     this->allocator_ = ACE_Allocator::instance ();
00160 
00161   ACE_NEW_MALLOC (this->head_,
00162                   (ACE_Node<T>*) this->allocator_->malloc (sizeof (ACE_Node<T>)),
00163                   ACE_Node<T>);
00164   this->head_->next_ = this->head_;
00165   this->copy_nodes (us);
00166 }
00167 
00168 template <class T> ACE_Unbounded_Set<T> &
00169 ACE_Unbounded_Set<T>::operator= (const ACE_Unbounded_Set<T> &us)
00170 {
00171   ACE_TRACE ("ACE_Unbounded_Set<T>::operator=");
00172 
00173   if (this != &us)
00174     {
00175       this->delete_nodes ();
00176       this->copy_nodes (us);
00177     }
00178 
00179   return *this;
00180 }
00181 
00182 template <class T> int
00183 ACE_Unbounded_Set<T>::find (const T &item) const
00184 {
00185   // ACE_TRACE ("ACE_Unbounded_Set<T>::find");
00186   // Set <item> into the dummy node.
00187   this->head_->item_ = item;
00188 
00189   ACE_Node<T> *temp = this->head_->next_;
00190 
00191   // Keep looping until we find the item.
00192   while (!(temp->item_ == item))
00193     temp = temp->next_;
00194 
00195   // If we found the dummy node then it's not really there, otherwise,
00196   // it is there.
00197   return temp == this->head_ ? -1 : 0;
00198 }
00199 
00200 template <class T> int
00201 ACE_Unbounded_Set<T>::insert (const T &item)
00202 {
00203   // ACE_TRACE ("ACE_Unbounded_Set<T>::insert");
00204   if (this->find (item) == 0)
00205     return 1;
00206   else
00207     return this->insert_tail (item);
00208 }
00209 
00210 template <class T> int
00211 ACE_Unbounded_Set<T>::remove (const T &item)
00212 {
00213   // ACE_TRACE ("ACE_Unbounded_Set<T>::remove");
00214 
00215   // Insert the item to be founded into the dummy node.
00216   this->head_->item_ = item;
00217 
00218   ACE_Node<T> *curr = this->head_;
00219 
00220   while (!(curr->next_->item_ == item))
00221     curr = curr->next_;
00222 
00223   if (curr->next_ == this->head_)
00224     return -1; // Item was not found.
00225   else
00226     {
00227       ACE_Node<T> *temp = curr->next_;
00228       // Skip over the node that we're deleting.
00229       curr->next_ = temp->next_;
00230       --this->cur_size_;
00231       ACE_DES_FREE_TEMPLATE (temp,
00232                              this->allocator_->free,
00233                              ACE_Node,
00234                              <T>);
00235       return 0;
00236     }
00237 }
00238 
00239 template <class T> ACE_Unbounded_Set_Iterator<T>
00240 ACE_Unbounded_Set<T>::begin (void)
00241 {
00242   // ACE_TRACE ("ACE_Unbounded_Set<T>::begin");
00243   return ACE_Unbounded_Set_Iterator<T> (*this);
00244 }
00245 
00246 template <class T> ACE_Unbounded_Set_Iterator<T>
00247 ACE_Unbounded_Set<T>::end (void)
00248 {
00249   // ACE_TRACE ("ACE_Unbounded_Set<T>::end");
00250   return ACE_Unbounded_Set_Iterator<T> (*this, 1);
00251 }
00252 
00253 
00254 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Iterator)
00255 
00256   template <class T> void
00257 ACE_Unbounded_Set_Iterator<T>::dump (void) const
00258 {
00259 #if defined (ACE_HAS_DUMP)
00260   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::dump");
00261 #endif /* ACE_HAS_DUMP */
00262 }
00263 
00264 template <class T>
00265 ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator (ACE_Unbounded_Set<T> &s, int end)
00266   : current_ (end == 0 ? s.head_->next_ : s.head_ ),
00267     set_ (&s)
00268 {
00269   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator");
00270 }
00271 
00272 template <class T> int
00273 ACE_Unbounded_Set_Iterator<T>::advance (void)
00274 {
00275   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::advance");
00276   this->current_ = this->current_->next_;
00277   return this->current_ != this->set_->head_;
00278 }
00279 
00280 template <class T> int
00281 ACE_Unbounded_Set_Iterator<T>::first (void)
00282 {
00283   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::first");
00284   this->current_ = this->set_->head_->next_;
00285   return this->current_ != this->set_->head_;
00286 }
00287 
00288 template <class T> int
00289 ACE_Unbounded_Set_Iterator<T>::done (void) const
00290 {
00291   ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::done");
00292 
00293   return this->current_ == this->set_->head_;
00294 }
00295 
00296 template <class T> int
00297 ACE_Unbounded_Set_Iterator<T>::next (T *&item)
00298 {
00299   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::next");
00300   if (this->current_ == this->set_->head_)
00301     return 0;
00302   else
00303     {
00304       item = &this->current_->item_;
00305       return 1;
00306     }
00307 }
00308 
00309 template <class T> ACE_Unbounded_Set_Iterator<T>
00310 ACE_Unbounded_Set_Iterator<T>::operator++ (int)
00311 {
00312   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator++ (int)");
00313   ACE_Unbounded_Set_Iterator<T> retv (*this);
00314 
00315   // postfix operator
00316 
00317   this->advance ();
00318   return retv;
00319 }
00320 
00321 template <class T> ACE_Unbounded_Set_Iterator<T>&
00322 ACE_Unbounded_Set_Iterator<T>::operator++ (void)
00323 {
00324   // ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator++ (void)");
00325 
00326   // prefix operator
00327 
00328   this->advance ();
00329   return *this;
00330 }
00331 
00332 template <class T> T&
00333 ACE_Unbounded_Set_Iterator<T>::operator* (void)
00334 {
00335   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator*");
00336   T *retv = 0;
00337 
00338   int result = this->next (retv);
00339   ACE_ASSERT (result != 0);
00340   ACE_UNUSED_ARG (result);
00341 
00342   return *retv;
00343 }
00344 
00345 template <class T> bool
00346 ACE_Unbounded_Set_Iterator<T>::operator== (const ACE_Unbounded_Set_Iterator<T> &rhs) const
00347 {
00348   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator==");
00349   return (this->set_ == rhs.set_ && this->current_ == rhs.current_);
00350 }
00351 
00352 template <class T> bool
00353 ACE_Unbounded_Set_Iterator<T>::operator!= (const ACE_Unbounded_Set_Iterator<T> &rhs) const
00354 {
00355   //ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::operator!=");
00356   return (this->set_ != rhs.set_ || this->current_ != rhs.current_);
00357 }
00358 
00359 ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Const_Iterator)
00360 
00361 template <class T> void
00362 ACE_Unbounded_Set_Const_Iterator<T>::dump (void) const
00363 {
00364 #if defined (ACE_HAS_DUMP)
00365   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::dump");
00366 #endif /* ACE_HAS_DUMP */
00367 }
00368 
00369 template <class T>
00370 ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator (const ACE_Unbounded_Set<T> &s, int end)
00371   : current_ (end == 0 ? s.head_->next_ : s.head_ ),
00372     set_ (&s)
00373 {
00374   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator");
00375 }
00376 
00377 template <class T> int
00378 ACE_Unbounded_Set_Const_Iterator<T>::advance (void)
00379 {
00380   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::advance");
00381   this->current_ = this->current_->next_;
00382   return this->current_ != this->set_->head_;
00383 }
00384 
00385 template <class T> int
00386 ACE_Unbounded_Set_Const_Iterator<T>::first (void)
00387 {
00388   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::first");
00389   this->current_ = this->set_->head_->next_;
00390   return this->current_ != this->set_->head_;
00391 }
00392 
00393 template <class T> int
00394 ACE_Unbounded_Set_Const_Iterator<T>::done (void) const
00395 {
00396   ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::done");
00397 
00398   return this->current_ == this->set_->head_;
00399 }
00400 
00401 template <class T> int
00402 ACE_Unbounded_Set_Const_Iterator<T>::next (T *&item)
00403 {
00404   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::next");
00405   if (this->current_ == this->set_->head_)
00406     return 0;
00407   else
00408     {
00409       item = &this->current_->item_;
00410       return 1;
00411     }
00412 }
00413 
00414 template <class T> ACE_Unbounded_Set_Const_Iterator<T>
00415 ACE_Unbounded_Set_Const_Iterator<T>::operator++ (int)
00416 {
00417   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator++ (int)");
00418   ACE_Unbounded_Set_Const_Iterator<T> retv (*this);
00419 
00420   // postfix operator
00421 
00422   this->advance ();
00423   return retv;
00424 }
00425 
00426 template <class T> ACE_Unbounded_Set_Const_Iterator<T>&
00427 ACE_Unbounded_Set_Const_Iterator<T>::operator++ (void)
00428 {
00429   // ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator++ (void)");
00430 
00431   // prefix operator
00432 
00433   this->advance ();
00434   return *this;
00435 }
00436 
00437 template <class T> T&
00438 ACE_Unbounded_Set_Const_Iterator<T>::operator* (void)
00439 {
00440   //ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::operator*");
00441   T *retv = 0;
00442 
00443   int const result = this->next (retv);
00444   ACE_ASSERT (result != 0);
00445   ACE_UNUSED_ARG (result);
00446 
00447   return *retv;
00448 }
00449 
00450 ACE_END_VERSIONED_NAMESPACE_DECL
00451 
00452 #endif /* ACE_UNBOUNDED_SET_CPP */

Generated on Thu Nov 9 09:42:09 2006 for ACE by doxygen 1.3.6