home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxk / iasrtmap.c__ / IASRTMAP.C
Encoding:
Text File  |  1992-10-26  |  4.6 KB  |  160 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2.  
  3.  
  4. template < class Element, class Key >
  5. INumber IASortedMap < Element, Key >::
  6. numberOfOccurrences (Element const& element) const
  7. { if (contains (element))
  8.     return 1;
  9.   else
  10.     return 0;
  11. }
  12.  
  13. template < class Element, class Key >
  14. Boolean IASortedMap < Element, Key >::
  15. locateNext (Element const&, ICursor& cursor) const
  16. { cursor.invalidate ();
  17.   return False;
  18. }
  19.  
  20. template < class Element, class Key >
  21. INumber IASortedMap < Element, Key >::
  22. removeAllOccurrences (Element const& element)
  23. { if (remove (element))
  24.     return 1;
  25.   else
  26.     return 0;
  27. }
  28.  
  29. template < class Element, class Key >
  30. Boolean IASortedMap < Element, Key >::
  31. operator == (IASortedMap < Element, Key > const& collection) const
  32. { return this->containsAllFrom (collection) &&
  33.      collection.containsAllFrom (*this);
  34. }
  35.  
  36. template < class Element, class Key >
  37. Boolean IASortedMap < Element, Key >::
  38. operator != (IASortedMap < Element, Key > const& collection) const
  39. { return ! operator == (collection);
  40. }
  41.  
  42. template < class Element, class Key >
  43. void IASortedMap < Element, Key >::
  44. unionWith (IASortedMap < Element, Key > const& collection)
  45. { addAllFrom (collection);
  46. }
  47.  
  48. template < class Element, class Key >
  49. void IASortedMap < Element, Key >::
  50. intersectionWith (IASortedMap < Element, Key > const& collection)
  51. { IAEqualityCollection < Element > const* env = &collection;
  52.   removeAll (IAEqualityCollection < Element >::isNotContained, &env);
  53. }
  54.  
  55. template < class Element, class Key >
  56. void IASortedMap < Element, Key >::
  57. differenceWith (IASortedMap < Element, Key > const& collection)
  58. { IAEqualityCollection < Element > const* env = &collection;
  59.   removeAll (IAEqualityCollection < Element >::isContained, &env);
  60. }
  61.  
  62. template < class Element, class Key >
  63. void IASortedMap < Element, Key >::
  64. addUnion (IASortedMap < Element, Key > const& collection1,
  65.           IASortedMap < Element, Key > const& collection2)
  66. { addAllFrom (collection1);
  67.   addAllFrom (collection2);
  68. }
  69.  
  70. template < class Element, class Key >
  71. void IASortedMap < Element, Key >::
  72. addIntersection (IASortedMap < Element, Key > const& collection1,
  73.                  IASortedMap < Element, Key > const& collection2)
  74. { ICursor *cursor = collection1.newCursor ();
  75.   forCursor (*cursor) {
  76.     if (collection2.contains (collection1.elementAt (*cursor)))
  77.       add (collection1.elementAt (*cursor));
  78.   }
  79.   delete cursor;
  80. }
  81.  
  82. template < class Element, class Key >
  83. void IASortedMap < Element, Key >::
  84. addDifference (IASortedMap < Element, Key > const& collection1,
  85.                IASortedMap < Element, Key > const& collection2)
  86. { ICursor *cursor = collection1.newCursor ();
  87.   forCursor (*cursor) {
  88.     if (! collection2.contains (collection1.elementAt (*cursor)))
  89.       add (collection1.elementAt (*cursor));
  90.   }
  91.   delete cursor;
  92. }
  93.  
  94. template < class Element, class Key >
  95. INumber IASortedMap < Element, Key >::
  96. numberOfElementsWithKey (Key const& key) const
  97. { if (containsElementWithKey (key))
  98.     return 1;
  99.   else
  100.     return 0;
  101. }
  102.  
  103. template < class Element, class Key >
  104. Boolean IASortedMap < Element, Key >::
  105. locateNextElementWithKey (Key const&, ICursor& cursor) const
  106. { cursor.invalidate ();
  107.   return False;
  108. }
  109.  
  110. template < class Element, class Key >
  111. INumber IASortedMap < Element, Key >::
  112. removeAllElementsWithKey (Key const& key)
  113. { if (removeElementWithKey (key))
  114.     return 1;
  115.   else
  116.     return 0;
  117. }
  118.  
  119. template < class Element, class Key >
  120. INumber IASortedMap < Element, Key >::
  121. numberOfDifferentKeys () const
  122. { return numberOfElements ();
  123. }
  124.  
  125. template < class Element, class Key >
  126. Boolean IASortedMap < Element, Key >::
  127. setToNextWithDifferentKey (ICursor& cursor) const
  128. { return setToNext (cursor);
  129. }
  130.  
  131. template < class Element, class Key >
  132. long IASortedMap < Element, Key >::
  133. compare (IASortedMap < Element, Key > const& collection,
  134.          long (*comparisonFunction) (Element const&, Element const&)) const
  135. { long result = 0;
  136.   ICursor *thisCursor = newCursor ();
  137.   ICursor *collectionCursor = collection.newCursor ();
  138.  
  139.   for (thisCursor->setToFirst (), collectionCursor->setToFirst ();
  140.        result == 0 && thisCursor->isValid () && collectionCursor->isValid ();
  141.        thisCursor->setToNext (), collectionCursor->setToNext ()) {
  142.     result = (*comparisonFunction) (this->elementAt (*thisCursor),
  143.                       collection.elementAt (*collectionCursor));
  144.   }
  145.  
  146.   if (result == 0) {
  147.     if (thisCursor->isValid () && ! collectionCursor->isValid ())
  148.       result = 1;
  149.     else if (! thisCursor->isValid () && collectionCursor->isValid ())
  150.       result = -1;
  151.     else {
  152.     }
  153.   }
  154.   delete thisCursor;
  155.   delete collectionCursor;
  156.  
  157.   return result;
  158. }
  159.  
  160.