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

  1. /* Copyright (c) IBM Corp. 1992 */
  2.  
  3.  
  4. template < class Element >
  5. INumber IASortedSet < Element >::
  6. numberOfOccurrences (Element const& element) const
  7. { if (contains (element))
  8.     return 1;
  9.   else
  10.     return 0;
  11. }
  12.  
  13. template < class Element >
  14. Boolean IASortedSet < Element >::
  15. locateNext (Element const&, ICursor& cursor) const
  16. { cursor.invalidate ();
  17.   return False;
  18. }
  19.  
  20. template < class Element >
  21. INumber IASortedSet < Element >::
  22. removeAllOccurrences (Element const& element)
  23. { if (remove (element))
  24.     return 1;
  25.   else
  26.     return 0;
  27. }
  28.  
  29. template < class Element >
  30. INumber IASortedSet < Element >::
  31. numberOfDifferentElements () const
  32. { return numberOfElements ();
  33. }
  34.  
  35. template < class Element >
  36. Boolean IASortedSet < Element >::
  37. setToNextDifferentElement (ICursor& cursor) const
  38. { return setToNext (cursor);
  39. }
  40.  
  41. template < class Element >
  42. Boolean IASortedSet < Element >::
  43. operator == (IASortedSet < Element > const& collection) const
  44. { return this->containsAllFrom (collection) &&
  45.      collection.containsAllFrom (*this);
  46. }
  47.  
  48. template < class Element >
  49. Boolean IASortedSet < Element >::
  50. operator != (IASortedSet < Element > const& collection) const
  51. { return ! operator == (collection);
  52. }
  53.  
  54. template < class Element >
  55. void IASortedSet < Element >::
  56. unionWith (IASortedSet < Element > const& collection)
  57. { addAllFrom (collection);
  58. }
  59.  
  60. template < class Element >
  61. void IASortedSet < Element >::
  62. intersectionWith (IASortedSet < Element > const& collection)
  63. { IAEqualityCollection < Element > const* env = &collection;
  64.   removeAll (IAEqualityCollection < Element >::isNotContained, &env);
  65. }
  66.  
  67. template < class Element >
  68. void IASortedSet < Element >::
  69. differenceWith (IASortedSet < Element > const& collection)
  70. { IAEqualityCollection < Element > const* env = &collection;
  71.   removeAll (IAEqualityCollection < Element >::isContained, &env);
  72. }
  73.  
  74. template < class Element >
  75. void IASortedSet < Element >::
  76. addUnion (IASortedSet < Element > const& collection1,
  77.           IASortedSet < Element > const& collection2)
  78. { addAllFrom (collection1);
  79.   addAllFrom (collection2);
  80. }
  81.  
  82. template < class Element >
  83. void IASortedSet < Element >::
  84. addIntersection (IASortedSet < Element > const& collection1,
  85.                  IASortedSet < Element > 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 >
  95. void IASortedSet < Element >::
  96. addDifference (IASortedSet < Element > const& collection1,
  97.                IASortedSet < Element > const& collection2)
  98. { ICursor *cursor = collection1.newCursor ();
  99.   forCursor (*cursor) {
  100.     if (! collection2.contains (collection1.elementAt (*cursor)))
  101.       add (collection1.elementAt (*cursor));
  102.   }
  103.   delete cursor;
  104. }
  105.  
  106. template < class Element >
  107. long IASortedSet < Element >::
  108. compare (IASortedSet < Element > const& collection,
  109.          long (*comparisonFunction) (Element const&, Element const&)) const
  110. { long result = 0;
  111.   ICursor *thisCursor = newCursor ();
  112.   ICursor *collectionCursor = collection.newCursor ();
  113.  
  114.   for (thisCursor->setToFirst (), collectionCursor->setToFirst ();
  115.        result == 0 && thisCursor->isValid () && collectionCursor->isValid ();
  116.        thisCursor->setToNext (), collectionCursor->setToNext ()) {
  117.     result = (*comparisonFunction) (this->elementAt (*thisCursor),
  118.                       collection.elementAt (*collectionCursor));
  119.   }
  120.  
  121.   if (result == 0) {
  122.     if (thisCursor->isValid () && ! collectionCursor->isValid ())
  123.       result = 1;
  124.     else if (! thisCursor->isValid () && collectionCursor->isValid ())
  125.       result = -1;
  126.     else {
  127.     }
  128.   }
  129.   delete thisCursor;
  130.   delete collectionCursor;
  131.  
  132.   return result;
  133. }
  134.  
  135.