Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Unbounded_Set.cpp

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

Generated on Mon Jun 16 11:22:02 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002