Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef SetHPP
00033 #define SetHPP
00034
00035
00036 #include <zeusbase/System/ZObject.h>
00037 #include <zeusbase/System/Interfaces/ISet.hpp>
00038 #include <zeusbase/System/Iterators.hpp>
00039
00040 #ifdef _MSC_VER
00041 #pragma warning(push, 1) // Avoid Visual Studio 6.0 level 4 and 3 compiler warnings.
00042 #endif
00043
00044 #include <set>
00045
00046 #ifdef _MSC_VER
00047 #pragma warning(pop)
00048 #endif
00049
00050
00051 BEGIN_NAMESPACE_Zeus
00052
00053
00056
00057 template <class T> class TSet : public ISet<T>
00058 {
00059 public:
00060
00061
00064
00065 inline TSet()
00066
00067
00068
00069
00070
00071
00072
00073 : m_tEmpty(T())
00074 {
00075 m_pIterator = NULL;
00076 }
00077
00078
00082
00083 inline TSet(const T& EmptyItem)
00084 : m_tEmpty(EmptyItem)
00085 {
00086 m_pIterator = NULL;
00087 }
00088
00089
00093
00094 inline TSet(const TSet<T>& rSet)
00095 : m_tEmpty(rSet.m_tEmpty)
00096 {
00097 m_pIterator = NULL;
00098 rSet.copyToSet(*this);
00099 }
00100
00101
00105
00106 inline TSet(const IList<T>& rList)
00107 : m_tEmpty(T())
00108 {
00109 m_pIterator = NULL;
00110 *this = rList;
00111 }
00112
00113 #ifdef USE_STL_BINDINGS
00114
00118
00119 inline TSet(const std::set<T>& rSet)
00120 : m_Set(rSet),
00121 m_tEmpty(T())
00122 {
00123 m_pIterator = NULL;
00124 }
00125 #endif
00126
00127
00130
00131 virtual ~TSet()
00132 {
00133 clear();
00134
00135
00136 TAbstractSetIterator* pIt = m_pIterator;
00137 while (pIt != NULL)
00138 {
00139 pIt->clearParent();
00140 pIt = pIt->getNextIterator();
00141 }
00142 }
00143
00144
00147
00148 inline virtual bool MQUALIFIER addItem(const T& rItem)
00149 {
00150 return m_Set.insert(rItem).second;
00151 }
00152
00153
00156
00157 inline virtual void MQUALIFIER addAllItems(const ISet<T>& rItems)
00158 {
00159 TConstIterator<T> It = rItems.getConstIterator();
00160 while(It.hasNextItem())
00161 {
00162 addItem(It.getNextItemConst());
00163 }
00164 }
00165
00166
00169
00170 inline virtual void MQUALIFIER clear()
00171 {
00172 m_Set.clear();
00173 }
00174
00175
00178
00179 inline virtual Int MQUALIFIER getCount() const
00180 {
00181 return (Int)m_Set.size();
00182 }
00183
00184
00187
00188 virtual bool MQUALIFIER hasItem(const T& rItem) const
00189 {
00190 bool bRetval = false;
00191 typename std::set<T>::const_iterator it;
00192 it = m_Set.find(rItem);
00193 if (it != m_Set.end())
00194 {
00195 bRetval = true;
00196 }
00197 return bRetval;
00198 }
00199
00200
00203
00204 inline virtual bool MQUALIFIER hasAllItems(const ISet<T>& rItems) const
00205 {
00206 bool bRetval = true;
00207
00208 TConstIterator<T> It = rItems.getConstIterator();
00209 while(bRetval && It.hasNextItem())
00210 {
00211 bRetval &= this->hasItem(It.getNextItemConst());
00212 }
00213 return bRetval;
00214 }
00215
00216
00219
00220 inline virtual IListIterator<T>* MQUALIFIER getIterator()
00221 {
00222 TSetIterator* pIterator = new TSetIterator(*this, m_Set, m_tEmpty);
00223 pIterator->setNextIterator(m_pIterator);
00224 m_pIterator = pIterator;
00225 return m_pIterator;
00226 }
00227
00228
00231
00232 inline virtual const IListIterator<T>* MQUALIFIER getConstIterator() const
00233 {
00234 TConstSetIterator* pIterator = new TConstSetIterator(*this, m_Set, m_tEmpty);
00235 pIterator->setNextIterator(m_pIterator);
00236 m_pIterator = pIterator;
00237 return m_pIterator;
00238 }
00239
00240
00243
00244 inline virtual bool MQUALIFIER isEmpty() const
00245 {
00246 return m_Set.empty();
00247 }
00248
00249
00252
00253 virtual void MQUALIFIER releaseIterator(const IListIterator<T>* pIterator) const
00254 {
00255 TAbstractSetIterator* it = m_pIterator;
00256 TAbstractSetIterator* it_prev = NULL;
00257 while (it != NULL)
00258 {
00259 if (it == pIterator)
00260 {
00261
00262 if (it_prev == NULL)
00263 {
00264 m_pIterator = it->getNextIterator();
00265 delete it;
00266 }
00267 else
00268 {
00269 it_prev->setNextIterator(it->getNextIterator());
00270 delete it;
00271 }
00272 break;
00273 }
00274 it_prev = it;
00275 it = it->getNextIterator();
00276 }
00277 }
00278
00279
00282
00283 inline virtual bool MQUALIFIER removeItem(const T& rItem)
00284 {
00285 return (m_Set.erase(rItem) == 1);
00286 }
00287
00288
00291
00292 inline virtual void MQUALIFIER removeAllItems(const ISet<T>& rItems)
00293 {
00294 TConstIterator<T> It = rItems.getConstIterator();
00295 while(It.hasNextItem())
00296 {
00297 this->removeItem(It.getNextItemConst());
00298 }
00299 }
00300
00301
00304
00305 inline virtual void MQUALIFIER copyToList(IList<T>& rList) const
00306 {
00307 for (typename std::set<T>::const_iterator Iterator = m_Set.begin(); Iterator != m_Set.end(); Iterator++)
00308 {
00309 rList.add(*Iterator);
00310 }
00311 }
00312
00313
00316
00317 inline virtual void MQUALIFIER copyToSet(ISet<T>& rSet) const
00318 {
00319 for (typename std::set<T>::const_iterator Iterator = m_Set.begin(); Iterator != m_Set.end(); Iterator++)
00320 {
00321 rSet.addItem(*Iterator);
00322 }
00323 }
00324
00325
00328
00329 virtual bool MQUALIFIER equals(const ISet<T>& rSet) const
00330 {
00331 bool bRetval = (rSet.getCount() == this->getCount());
00332 typename std::set<T>::const_iterator Iterator = m_Set.begin();
00333 while (bRetval && Iterator != m_Set.end())
00334 {
00335 bRetval = rSet.hasItem(*Iterator);
00336 Iterator++;
00337 }
00338 return bRetval;
00339 }
00340
00341
00344
00345 inline virtual T& MQUALIFIER getItem()
00346 {
00347 typename std::set<T>::iterator Iterator = m_Set.begin();
00348 if (Iterator != m_Set.end())
00349 {
00350 return (T&)*Iterator;
00351 }
00352 else
00353 {
00354 return m_tEmpty;
00355 }
00356 }
00357
00358
00361
00362 inline virtual const T& MQUALIFIER getItemConst() const
00363 {
00364 typename std::set<T>::const_iterator Iterator = m_Set.begin();
00365 if (Iterator != m_Set.end())
00366 {
00367 return *Iterator;
00368 }
00369 else
00370 {
00371 return m_tEmpty;
00372 }
00373 }
00374
00375
00381
00382 inline static void createIntersection(const ISet<T>& rSet1, const ISet<T>& rSet2, ISet<T>& rResult)
00383 {
00384 rResult.clear();
00385 TConstIterator<T> It = rSet1.getConstIterator();
00386 while(It.hasNextItem())
00387 {
00388 const T& rItem = It.getNextItemConst();
00389 if (rSet2.hasItem(rItem))
00390 {
00391 rResult.addItem(rItem);
00392 }
00393 }
00394 }
00395
00396
00402
00403 inline static void createUnion(const ISet<T>& rSet1, const ISet<T>& rSet2, ISet<T>& rResult)
00404 {
00405 rResult.clear();
00406 rSet1.copyToSet(rResult);
00407 rSet2.copyToSet(rResult);
00408 }
00409
00410
00413
00414 inline bool operator==(const ISet<T>& rSet) const
00415 {
00416 return this->equals(rSet);
00417 }
00418
00419
00422
00423 inline bool operator!=(const ISet<T>& rSet) const
00424 {
00425 return !this->equals(rSet);
00426 }
00427
00428
00431
00432 inline TSet<T>& operator=(const ISet<T>& rSet)
00433 {
00434 this->clear();
00435 rSet.copyToSet(*this);
00436 return *this;
00437 }
00438
00439
00442
00443 inline TSet<T>& operator=(const TSet<T>& rSet)
00444 {
00445 this->clear();
00446 rSet.copyToSet(*this);
00447 return *this;
00448 }
00449
00450
00453
00454 TSet<T>& operator=(const IList<T>& rList)
00455 {
00456 this->clear();
00457 const IListIterator<T>* pIt = rList.getConstIterator();
00458 while(pIt->hasNextItem())
00459 {
00460 this->addItem(pIt->getNextItemConst());
00461 }
00462 rList.releaseIterator(pIt);
00463 return *this;
00464 }
00465
00466 private:
00467 class TAbstractSetIterator;
00468
00470 T m_tEmpty;
00472 std::set<T> m_Set;
00474 mutable TAbstractSetIterator* m_pIterator;
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00493
00494 class TAbstractSetIterator : public IListIterator<T>
00495 {
00496 public:
00497
00500
00501 inline TAbstractSetIterator(const TSet<T>& rParent)
00502 {
00503 m_iRefCounter = 1;
00504 m_pNextIterator = NULL;
00505 m_pParent = (TSet<T>*) &rParent;
00506 }
00507
00510
00511 inline virtual ~TAbstractSetIterator()
00512 {}
00513
00514
00517
00518 inline TAbstractSetIterator* getNextIterator()
00519 { return m_pNextIterator; }
00520
00521
00524
00525 inline void setNextIterator(TAbstractSetIterator* pIt)
00526 { m_pNextIterator = pIt; }
00527
00528
00531
00532 inline Retval MQUALIFIER askForInterface(const InterfaceID& , IZUnknown*& )
00533 {
00534 return RET_UNKNOWN_INTERFACE;
00535 }
00536
00537
00540
00541 inline void MQUALIFIER addRef() const
00542 {
00543 ++m_iRefCounter;
00544 }
00545
00546
00549
00550 void MQUALIFIER release() const
00551 {
00552 --m_iRefCounter;
00553 if (m_iRefCounter <= 0)
00554 {
00555 if (m_pParent != NULL)
00556 {
00557 m_pParent->releaseIterator(this);
00558 }
00559 else
00560 {
00561 delete this;
00562 }
00563 }
00564 }
00565
00566
00569
00570 inline void clearParent()
00571 {
00572 m_pParent = NULL;
00573 }
00574
00575 protected:
00577 mutable TAbstractSetIterator* m_pNextIterator;
00579 mutable TSet<T>* m_pParent;
00580
00581 private:
00583 mutable Int m_iRefCounter;
00584 };
00585
00586
00589
00590 class TSetIterator : public TAbstractSetIterator
00591 {
00592 public:
00593
00596
00597 inline TSetIterator(const TSet<T>& rParent,
00598 std::set<T>& tSet,
00599 T& tEmptyItem)
00600 : TAbstractSetIterator(rParent),
00601 m_Set(tSet),
00602 m_tEmpty(tEmptyItem),
00603 m_tRetval(tEmptyItem),
00604 m_Iterator(tSet.begin())
00605 {}
00606
00607
00610
00611 inline virtual ~TSetIterator()
00612 {}
00613
00614
00617
00618 inline virtual void MQUALIFIER reset() const
00619 {
00620 m_Iterator = m_Set.begin();
00621 }
00622
00623
00626
00627 virtual T& MQUALIFIER getNextItem() const
00628 {
00629 T& tRetval = (T&)m_tRetval;
00630 if (this->hasNextItem())
00631 {
00632 tRetval = *m_Iterator;
00633 m_Iterator++;
00634 return tRetval;
00635 }
00636 else
00637 {
00638 return (T&)m_tEmpty;
00639 }
00640 }
00641
00642
00645
00646 inline virtual const T& MQUALIFIER getNextItemConst() const
00647 {
00648 return (const T&)this->getNextItem();
00649 }
00650
00651
00654
00655 inline virtual bool MQUALIFIER hasNextItem() const
00656 {
00657 return (m_Set.end() != m_Iterator);
00658 }
00659
00660 private:
00662 std::set<T>& m_Set;
00664 T m_tEmpty;
00666 T m_tRetval;
00668 mutable typename std::set<T>::iterator m_Iterator;
00669 };
00670
00671
00674
00675 class TConstSetIterator : public TAbstractSetIterator
00676 {
00677 public:
00678
00681
00682 inline TConstSetIterator(const TSet<T>& rParent,
00683 const std::set<T>& tSet,
00684 const T& tEmptyItem)
00685 : TAbstractSetIterator(rParent),
00686 m_Set(tSet),
00687 m_tEmpty(tEmptyItem),
00688 m_tRetval(tEmptyItem),
00689 m_Iterator(tSet.begin())
00690 {}
00691
00692
00695
00696 inline virtual ~TConstSetIterator()
00697 {}
00698
00699
00702
00703 inline virtual void MQUALIFIER reset() const
00704 {
00705 m_Iterator = m_Set.begin();
00706 }
00707
00708
00711
00712 inline virtual T& MQUALIFIER getNextItem() const
00713 {
00714 T& tRetval = (T&)m_tRetval;
00715 if (this->hasNextItem())
00716 {
00717 tRetval = *m_Iterator;
00718 m_Iterator++;
00719 return tRetval;
00720 }
00721 else
00722 {
00723 return (T&)m_tEmpty;
00724 }
00725 }
00726
00727
00730
00731 inline virtual const T& MQUALIFIER getNextItemConst() const
00732 {
00733 return (const T&)this->getNextItem();
00734 }
00735
00736
00739
00740 inline virtual bool MQUALIFIER hasNextItem() const
00741 {
00742 return (m_Set.end() != m_Iterator);
00743 }
00744
00745 private:
00747 const std::set<T>& m_Set;
00749 T m_tEmpty;
00751 T m_tRetval;
00753 mutable typename std::set<T>::const_iterator m_Iterator;
00754 };
00755 };
00756
00757
00758 END_NAMESPACE_Zeus
00759
00760
00761 #endif
00762