home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / BCCollec / Structs / Maps / BCMap.cpp next >
Encoding:
Text File  |  1994-04-21  |  5.4 KB  |  207 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 bag 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. //  BCMap.cpp
  10. //
  11. //  This file contains the definitions for the map abstract base class
  12. //  and its iterators.
  13.  
  14. #include "BCMap.h"
  15.  
  16. template<class Item, class Value>
  17. BC_TMap<Item, Value>::BC_TMap() {}
  18.  
  19. template<class Item, class Value>
  20. BC_TMap<Item, Value>::BC_TMap(const BC_TMap<Item, Value>&) {}
  21.  
  22. template<class Item, class Value>
  23. BC_TMap<Item, Value>::~BC_TMap() {}
  24.  
  25. template<class Item, class Value>
  26. BC_TMap<Item, Value>& BC_TMap<Item, Value>::operator=(const BC_TMap<Item, Value>& m)
  27. {
  28.   if (this == &m)
  29.     return *this;
  30.   ((BC_TMap<Item, Value>&)m).Lock();
  31.   Purge();
  32.   BC_TMapActiveIterator<Item, Value> iter(m);
  33.   while (!iter.IsDone()) {
  34.     Attach(*iter.CurrentItem(), *iter.CurrentValue());
  35.     iter.Next();
  36.   }
  37.   ((BC_TMap<Item, Value>&)m).Unlock();
  38.   return *this;
  39. }
  40.  
  41. template<class Item, class Value>
  42. BC_Boolean BC_TMap<Item, Value>::operator==(const BC_TMap<Item, Value>& m) const
  43. {
  44.   if (this == &m)
  45.     return 1;
  46.   ((BC_TMap<Item, Value>&)m).Lock();
  47.   if (Cardinality() != m.Cardinality()) {
  48.     ((BC_TMap<Item, Value>&)m).Unlock();
  49.     return 0;
  50.   }
  51.   BC_TMapActiveIterator<Item, Value> iter(*this);
  52.   while (!iter.IsDone()) {
  53.     if (!m.Exists(*iter.CurrentItem())) {
  54.       ((BC_TMap<Item, Value>&)m).Unlock();
  55.       return 0;
  56.     }
  57.     iter.Next();
  58.   }
  59.   ((BC_TMap<Item, Value>&)m).Unlock();
  60.   return 1;
  61. }    
  62.  
  63. template<class Item, class Value>
  64. BC_Boolean BC_TMap<Item, Value>::operator!=(const BC_TMap<Item, Value>& m) const
  65. {
  66.   return !operator==(m);
  67. }
  68.  
  69. template<class Item, class Value>
  70. void BC_TMap<Item, Value>::Lock() {}
  71.  
  72. template<class Item, class Value>
  73. void BC_TMap<Item, Value>::Unlock() {}
  74.  
  75. template<class Item, class Value>
  76. BC_TMapActiveIterator<Item, Value>::
  77.   BC_TMapActiveIterator(const BC_TMap<Item, Value>& m) 
  78.     : fMap(m),
  79.       fBucketIndex(m.Cardinality() ? 0 : -1),
  80.       fIndex(-1)
  81. {
  82.   if (fBucketIndex == 0) {
  83.     for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
  84.       fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
  85.       if (fIndex != -1)
  86.         return;
  87.     }
  88.   }
  89. }
  90.  
  91. template<class Item, class Value>
  92. BC_TMapActiveIterator<Item, Value>::~BC_TMapActiveIterator() {}
  93.   
  94. template<class Item, class Value>
  95. void BC_TMapActiveIterator<Item, Value>::Reset()
  96. {
  97.   fBucketIndex = fMap.Cardinality() ? 0 : -1;
  98.   fIndex = -1;
  99.   if (fBucketIndex == 0) {
  100.     for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
  101.       fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
  102.       if (fIndex != -1)
  103.         return;
  104.     }
  105.   }
  106. }
  107.  
  108. template<class Item, class Value>
  109. BC_Boolean BC_TMapActiveIterator<Item, Value>::Next()
  110. {
  111.   if (fBucketIndex < fMap.NumberOfBuckets()) {
  112.     fIndex++;
  113.     if (fIndex >= fMap.Length(fBucketIndex)) {
  114.       fBucketIndex++;
  115.       fIndex = - 1;
  116.       for (; (fBucketIndex < fMap.NumberOfBuckets()); fBucketIndex++) {
  117.         fIndex = fMap.Length(fBucketIndex) ? 0 : -1;
  118.         if (fIndex != -1)
  119.           break;
  120.       }
  121.     }
  122.     return !IsDone();
  123.   } else
  124.     return 0;
  125. }
  126.  
  127. template<class Item, class Value>
  128. BC_Boolean BC_TMapActiveIterator<Item, Value>::IsDone() const
  129. {
  130.   if ((fBucketIndex < 0) || (fBucketIndex >= fMap.NumberOfBuckets()))
  131.     return 1;
  132.   else {
  133.     if (fIndex >= fMap.Length(fBucketIndex)) {
  134.       ((BC_TMapActiveIterator<Item, Value>&)(*this)).fBucketIndex++;
  135.       ((BC_TMapActiveIterator<Item, Value>&)(*this)).fIndex = -1;
  136.       for (; (fBucketIndex < fMap.NumberOfBuckets());
  137.            ((BC_TMapActiveIterator<Item, Value>&)(*this)).fBucketIndex++) {
  138.         ((BC_TMapActiveIterator<Item, Value>&)(*this)).fIndex =
  139.           fMap.Length(fBucketIndex) ? 0 : - 1;
  140.         if (fIndex != -1)
  141.           return 0;
  142.       }
  143.       return 1;
  144.     }
  145.     return 0;
  146.   }
  147. }
  148.  
  149. template<class Item, class Value>
  150. const Item* BC_TMapActiveIterator<Item, Value>::CurrentItem() const
  151. {
  152.   return IsDone() ? 0 : &fMap.ItemAt(fBucketIndex, fIndex);
  153. }
  154.  
  155. template<class Item, class Value>
  156. Item* BC_TMapActiveIterator<Item, Value>::CurrentItem()
  157. {
  158.   return IsDone() ? 0 : &((Item&)(fMap.ItemAt(fBucketIndex, fIndex)));
  159. }
  160.  
  161. template<class Item, class Value>
  162. const Value* BC_TMapActiveIterator<Item, Value>::CurrentValue() const
  163. {
  164.   return IsDone() ? 0 : &fMap.ValueAt(fBucketIndex, fIndex);
  165. }
  166.  
  167. template<class Item, class Value>
  168. Value* BC_TMapActiveIterator<Item, Value>::CurrentValue()
  169. {
  170.   return IsDone() ? 0 : &((Value&)(fMap.ValueAt(fBucketIndex, fIndex)));
  171. }
  172.  
  173. template<class Item, class Value>
  174. BC_TMapPassiveIterator<Item, Value>::
  175.   BC_TMapPassiveIterator(const BC_TMap<Item, Value>& m)
  176.     : fMap(m) {}
  177.  
  178. template<class Item, class Value>
  179. BC_TMapPassiveIterator<Item, Value>::~BC_TMapPassiveIterator() {}
  180.   
  181. template<class Item, class Value>
  182. BC_Boolean BC_TMapPassiveIterator<Item, Value>::
  183.   Apply(BC_Boolean (*f)(const Item&, const Value&))
  184. {
  185.   BC_TMapActiveIterator<Item, Value> iter(fMap);
  186.   while (!iter.IsDone()) {
  187.     if (!f(*iter.CurrentItem(), *iter.CurrentValue()))
  188.       return 0;
  189.     else
  190.       iter.Next();
  191.   }
  192.   return 1;
  193. }
  194.  
  195. template<class Item, class Value>
  196. BC_Boolean BC_TMapPassiveIterator<Item, Value>::Apply(BC_Boolean (*f)(Item&, Value&))
  197. {
  198.   BC_TMapActiveIterator<Item, Value> iter(fMap);
  199.   while (!iter.IsDone()) {
  200.     if (!f(*iter.CurrentItem(), *iter.CurrentValue()))
  201.       return 0;
  202.     else
  203.       iter.Next();
  204.   }
  205.   return 1;
  206. }
  207.