home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 6.5 KB | 288 lines | [TEXT/MPS ] |
- // The C++ Booch Components (Version 2.1)
- // (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
- //
- // Restricted Rights Legend
- // Use, duplication, or disclosure is subject to restrictions as set forth
- // in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer
- // Software clause at DFARS 252.227-7013.
- //
- // BCSet.cpp
- //
- // This file contains the definitions for the set abstract base class
- // and its iterators.
-
- #include "BCSet.h"
-
- template<class Item>
- BC_TSet<Item>::BC_TSet() {}
-
- template<class Item>
- BC_TSet<Item>::BC_TSet(const BC_TSet<Item>&) {}
-
- template<class Item>
- BC_TSet<Item>::~BC_TSet() {}
-
- template<class Item>
- BC_TSet<Item>& BC_TSet<Item>::operator=(const BC_TSet<Item>& s)
- {
- if (this == &s)
- return *this;
- ((BC_TSet<Item>&)s).Lock();
- Purge();
- BC_TSetActiveIterator<Item> iter(s);
- while (!iter.IsDone()) {
- Attach(*iter.CurrentItem());
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- return *this;
- }
-
- template<class Item>
- BC_Boolean BC_TSet<Item>::operator==(const BC_TSet<Item>& s) const
- {
- if (this == &s)
- return 1;
- ((BC_TSet<Item>&)s).Lock();
- if (Cardinality() != s.Cardinality()) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- BC_TSetActiveIterator<Item> iter(*this);
- while (!iter.IsDone()) {
- if (!s.Exists(*iter.CurrentItem())) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- return 1;
- }
-
- template<class Item>
- BC_Boolean BC_TSet<Item>::operator!=(const BC_TSet<Item>& s) const
- {
- return !operator==(s);
- }
-
- template<class Item>
- void BC_TSet<Item>::Union(const BC_TSet<Item>& s)
- {
- if (this != &s) {
- ((BC_TSet<Item>&)s).Lock();
- BC_TSetActiveIterator<Item> iter(s);
- while (!iter.IsDone()) {
- Attach(*iter.CurrentItem());
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- }
- }
-
- template<class Item>
- void BC_TSet<Item>::Intersection(const BC_TSet<Item>& s)
- {
- if (this != &s) {
- ((BC_TSet<Item>&)s).Lock();
- BC_TSetActiveIterator<Item> iter(*this);
- while (!iter.IsDone()) {
- if (!s.Exists(*iter.CurrentItem()))
- Detach(*iter.CurrentItem());
- else
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- }
- }
-
- template<class Item>
- void BC_TSet<Item>::Difference(const BC_TSet<Item>& s)
- {
- if (this == &s)
- Purge();
- else {
- ((BC_TSet<Item>&)s).Lock();
- BC_TSetActiveIterator<Item> iter(s);
- while (!iter.IsDone()) {
- Detach(*iter.CurrentItem());
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- }
- }
-
- template<class Item>
- BC_Boolean BC_TSet<Item>::IsSubset(const BC_TSet<Item>& s) const
- {
- if (this == &s)
- return 1;
- ((BC_TSet<Item>&)s).Lock();
- if (Cardinality() > s.Cardinality()) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- BC_TSetActiveIterator<Item> iter(*this);
- while (!iter.IsDone()) {
- if (!s.Exists(*iter.CurrentItem())) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- return 1;
- }
-
- template<class Item>
- BC_Boolean BC_TSet<Item>::IsProperSubset(const BC_TSet<Item>& s) const
- {
- if (this == &s)
- return 0;
- ((BC_TSet<Item>&)s).Lock();
- if (Cardinality() >= s.Cardinality()) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- BC_TSetActiveIterator<Item> iter(*this);
- while (!iter.IsDone()) {
- if (!s.Exists(*iter.CurrentItem())) {
- ((BC_TSet<Item>&)s).Unlock();
- return 0;
- }
- iter.Next();
- }
- ((BC_TSet<Item>&)s).Unlock();
- return 1;
- }
-
- template<class Item>
- BC_Boolean BC_TSet<Item>::Attach(const Item& item, const BC_Boolean&)
- {
- return Attach(item);
- }
-
- template<class Item>
- void BC_TSet<Item>::Lock() {}
-
- template<class Item>
- void BC_TSet<Item>::Unlock() {}
-
- template<class Item>
- BC_TSetActiveIterator<Item>::BC_TSetActiveIterator(const BC_TSet<Item>& s)
- : fSet(s),
- fBucketIndex(s.Cardinality() ? 0 : -1),
- fIndex(-1)
- {
- if (fBucketIndex == 0) {
- for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- return;
- }
- }
- }
-
- template<class Item>
- BC_TSetActiveIterator<Item>::~BC_TSetActiveIterator() {}
-
- template<class Item>
- void BC_TSetActiveIterator<Item>::Reset()
- {
- fBucketIndex = fSet.Cardinality() ? 0 : -1;
- fIndex = -1;
- if (fBucketIndex == 0) {
- for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- return;
- }
- }
- }
-
- template<class Item>
- BC_Boolean BC_TSetActiveIterator<Item>::Next()
- {
- if (fBucketIndex < fSet.NumberOfBuckets()) {
- fIndex++;
- if (fIndex >= fSet.Length(fBucketIndex)) {
- fBucketIndex++;
- fIndex = -1;
- for (; (fBucketIndex < fSet.NumberOfBuckets()); fBucketIndex++) {
- fIndex = fSet.Length(fBucketIndex) ? 0 : -1;
- if (fIndex != -1)
- break;
- }
- }
- return !IsDone();
- } else
- return 0;
- }
-
- template<class Item>
- BC_Boolean BC_TSetActiveIterator<Item>::IsDone() const
- {
- if ((fBucketIndex < 0) || (fBucketIndex >= fSet.NumberOfBuckets()))
- return 1;
- else {
- if (fIndex >= fSet.Length(fBucketIndex)) {
- ((BC_TSetActiveIterator<Item>&)(*this)).fBucketIndex++;
- ((BC_TSetActiveIterator<Item>&)(*this)).fIndex = -1;
- for (; (fBucketIndex < fSet.NumberOfBuckets());
- ((BC_TSetActiveIterator<Item>&)(*this)).fBucketIndex++) {
- ((BC_TSetActiveIterator<Item>&)(*this)).fIndex =
- fSet.Length(fBucketIndex) ? 0 : - 1;
- if (fIndex != -1)
- return 0;
- }
- return 1;
- }
- return 0;
- }
- }
-
- template<class Item>
- const Item* BC_TSetActiveIterator<Item>::CurrentItem() const
- {
- return IsDone() ? 0 : &fSet.ItemAt(fBucketIndex, fIndex);
- }
-
- template<class Item>
- Item* BC_TSetActiveIterator<Item>::CurrentItem()
- {
- return IsDone() ? 0 : &((Item&)(fSet.ItemAt(fBucketIndex, fIndex)));
- }
-
- template<class Item>
- BC_TSetPassiveIterator<Item>::BC_TSetPassiveIterator(const BC_TSet<Item>& s)
- : fSet(s) {}
-
- template<class Item>
- BC_TSetPassiveIterator<Item>::~BC_TSetPassiveIterator() {}
-
- template<class Item>
- BC_Boolean BC_TSetPassiveIterator<Item>::Apply(BC_Boolean (*f)(const Item&))
- {
- BC_TSetActiveIterator<Item> iter(fSet);
- while (!iter.IsDone()) {
- if (!f(*iter.CurrentItem()))
- return 0;
- else
- iter.Next();
- }
- return 1;
- }
-
- template<class Item>
- BC_Boolean BC_TSetPassiveIterator<Item>::Apply(BC_Boolean (*f)(Item&))
- {
- BC_TSetActiveIterator<Item> iter(fSet);
- while (!iter.IsDone()) {
- if (!f(*iter.CurrentItem()))
- return 0;
- else
- iter.Next();
- }
- return 1;
- }
-