home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / BCCollec / Structs / Collects / BCColl.cpp next >
Encoding:
Text File  |  1994-04-21  |  3.6 KB  |  149 lines  |  [TEXT/MPS ]

  1. //  The C++ Booch Components (Version 2.1)
  2. //  (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
  3. //
  4. //  Restricted Rights Legend
  5. //  Use, duplication, or disclosure is subject to restrictions as set forth 
  6. //  in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer 
  7. //  Software clause at DFARS 252.227-7013. 
  8. //
  9. //  BCColl.cpp
  10. //
  11. //  This file contains the definitions for the collection abstract base class
  12. //  and its iterators.
  13.  
  14. #include "BCColl.h"
  15.  
  16. template<class Item>
  17. BC_TCollection<Item>::BC_TCollection() {}
  18.  
  19. template<class Item>
  20. BC_TCollection<Item>::BC_TCollection(const BC_TCollection<Item>&) {}
  21.  
  22. template<class Item>
  23. BC_TCollection<Item>::~BC_TCollection() {}
  24.  
  25. template<class Item>
  26. BC_TCollection<Item>& BC_TCollection<Item>::operator=(const BC_TCollection<Item>& c)
  27. {
  28.   if (this == &c)
  29.     return *this;
  30.   ((BC_TCollection<Item>&)c).Lock();
  31.   Purge();
  32.   BC_TCollectionActiveIterator<Item> iter(c);
  33.   while (!iter.IsDone()) {
  34.     Add(*iter.CurrentItem());
  35.     iter.Next();
  36.   }
  37.   ((BC_TCollection<Item>&)c).Unlock();
  38.   return *this;
  39. }
  40.  
  41. template<class Item>
  42. BC_Boolean BC_TCollection<Item>::operator==(const BC_TCollection<Item>& c) const 
  43. {
  44.   if (this == &c)
  45.     return 1;
  46.   ((BC_TCollection<Item>&)c).Lock();
  47.   if (Cardinality() != c.Cardinality()) {
  48.     ((BC_TCollection<Item>&)c).Unlock();
  49.     return 0;
  50.   }
  51.   BC_TCollectionActiveIterator<Item> iter_1(*this), iter_2(c);
  52.   while (!iter_1.IsDone()) {
  53.     if (*iter_1.CurrentItem() != *iter_2.CurrentItem()) {
  54.       ((BC_TCollection<Item>&)c).Unlock();
  55.       return 0;
  56.     }
  57.     iter_1.Next();
  58.     iter_2.Next();
  59.   }
  60.   ((BC_TCollection<Item>&)c).Unlock();
  61.   return 1;
  62. }
  63.  
  64. template<class Item>
  65. BC_Boolean BC_TCollection<Item>::operator!=(const BC_TCollection<Item>& c) const
  66. {
  67.   return !operator==(c);
  68. }
  69.  
  70. template<class Item>
  71. void BC_TCollection<Item>::Lock() {}
  72.  
  73. template<class Item>
  74. void BC_TCollection<Item>::Unlock() {}
  75.   
  76. template<class Item>
  77. BC_TCollectionActiveIterator<Item>::
  78.   BC_TCollectionActiveIterator(const BC_TCollection<Item>& c) 
  79.     : fCollection(c),
  80.       fIndex(c.Cardinality() ? 0 : -1) {}
  81.  
  82. template<class Item>
  83. BC_TCollectionActiveIterator<Item>::~BC_TCollectionActiveIterator() {}
  84.   
  85. template<class Item>
  86. void BC_TCollectionActiveIterator<Item>::Reset()
  87. {
  88.   fIndex = fCollection.Cardinality() ? 0 : -1;
  89. }
  90.  
  91. template<class Item>
  92. BC_Boolean BC_TCollectionActiveIterator<Item>::Next()
  93. {
  94.   fIndex++;
  95.   return !IsDone();
  96. }
  97.  
  98. template<class Item>
  99. BC_Boolean BC_TCollectionActiveIterator<Item>::IsDone() const
  100. {
  101.   return ((fIndex < 0) || (fIndex >= fCollection.Cardinality()));
  102. }
  103.  
  104. template<class Item>
  105. const Item* BC_TCollectionActiveIterator<Item>::CurrentItem() const
  106. {
  107.   return IsDone() ? 0 : &fCollection.ItemAt(fIndex);
  108. }
  109.  
  110. template<class Item>
  111. Item* BC_TCollectionActiveIterator<Item>::CurrentItem()
  112. {
  113.   return IsDone() ? 0 : &((Item&)(fCollection.ItemAt(fIndex)));
  114. }
  115.  
  116. template<class Item>
  117. BC_TCollectionPassiveIterator<Item>::
  118.   BC_TCollectionPassiveIterator(const BC_TCollection<Item>& c)
  119.     : fCollection(c) {}
  120.  
  121. template<class Item>
  122. BC_TCollectionPassiveIterator<Item>::~BC_TCollectionPassiveIterator() {}
  123.   
  124. template<class Item>
  125. BC_Boolean BC_TCollectionPassiveIterator<Item>::Apply(BC_Boolean (*f)(const Item&))
  126. {
  127.   BC_TCollectionActiveIterator<Item> iter(fCollection);
  128.   while (!iter.IsDone()) {
  129.     if (!f(*iter.CurrentItem()))
  130.       return 0;
  131.     else
  132.       iter.Next();
  133.   }
  134.   return 1;
  135. }
  136.  
  137. template<class Item>
  138. BC_Boolean BC_TCollectionPassiveIterator<Item>::Apply(BC_Boolean (*f)(Item&))
  139. {
  140.   BC_TCollectionActiveIterator<Item> iter(fCollection);
  141.   while (!iter.IsDone()) {
  142.     if (!f(*iter.CurrentItem()))
  143.       return 0;
  144.     else
  145.       iter.Next();
  146.   }
  147.   return 1;
  148. }
  149.