00001
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
00013
00014 #if !defined (__ACE_INLINE__)
00015 #include "ace/Unbounded_Set.inl"
00016 #endif
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
00026 return this->cur_size_;
00027 }
00028
00029 template <class T> int
00030 ACE_Unbounded_Set<T>::insert_tail (const T &item)
00031 {
00032
00033 ACE_Node<T> *temp;
00034
00035
00036 this->head_->item_ = item;
00037
00038
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
00045 this->head_->next_ = temp;
00046
00047
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
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
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
00113 this->head_->next_ = this->head_;
00114 }
00115
00116 template <class T>
00117 ACE_Unbounded_Set<T>::~ACE_Unbounded_Set (void)
00118 {
00119
00120
00121 this->delete_nodes ();
00122
00123
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
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
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
00183
00184 this->head_->item_ = item;
00185
00186 ACE_Node<T> *temp = this->head_->next_;
00187
00188
00189 while (!(temp->item_ == item))
00190 temp = temp->next_;
00191
00192
00193
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
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
00211
00212
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;
00222 else
00223 {
00224 ACE_Node<T> *temp = curr->next_;
00225
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
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
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
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
00265 }
00266
00267 template <class T> int
00268 ACE_Unbounded_Set_Iterator<T>::advance (void)
00269 {
00270
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
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
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
00308 ACE_Unbounded_Set_Iterator<T> retv (*this);
00309
00310
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
00320
00321
00322
00323 this->advance ();
00324 return *this;
00325 }
00326
00327 template <class T> T&
00328 ACE_Unbounded_Set_Iterator<T>::operator* (void)
00329 {
00330
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
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
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
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
00368 }
00369
00370 template <class T> int
00371 ACE_Unbounded_Set_Const_Iterator<T>::advance (void)
00372 {
00373
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
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
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
00411 ACE_Unbounded_Set_Const_Iterator<T> retv (*this);
00412
00413
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
00423
00424
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
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