home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / PARCEL / PARCEL.CPP < prev    next >
Text File  |  1995-03-15  |  6KB  |  155 lines

  1. /*************************************************************************
  2.   IBM C/C++ Tools Version 3.00 - Collection Class Library
  3.  (C) Copyright IBM Corporation 1992 ,1995, Licensed Program-Property of
  4.  IBM.  All Rights Reserved.  US Government Users Restricted Rights - Use,
  5.  duplication or disclosure restricted by GSA ADP Schedule Contract with
  6.  IBM Corp.
  7.  *************************************************************************/
  8.  
  9. /*--------------------------------------------------------------*\
  10. |                                                                |
  11. | parcel.CPP  -  Parcels are handled using  a  KeySorted Set and |
  12. |                a Heap.                       """""""""""""     |
  13. |                  """"                                          |
  14. | We maintain two collections that keep track of parcels in      |
  15. | circulation and parcels delivered.  The collection for the     |
  16. | parcels in circulation is a KeySorted Set (key, sorted,        |
  17. | unique elements, no element equality).  For the delivered      |
  18. | parcels we do not care about fast or sorted retrieval.         |
  19. | So we select the Heap for this collection (no key, unordered,  |
  20. | multiple elements, no element equality).                       |
  21. |                                                                |
  22. | A parcel has as member data two objects of type PlaceTime,     |
  23. | which is a point in space and time: one object for its origin, |
  24. | one for its current place and time. It also has as member      |
  25. | data two objects of type IString, for the destination and      |
  26. | for the ID.                                                    |
  27. |                                                                |
  28. | Function updateParcels adds parcels that have arrived at       |
  29. | their destination to the collection for delivered parcels,     |
  30. | and removes them from the collection of circulating parcels.   |
  31. | This demonstrates the use of removeAll().                      |
  32. |                                                                |
  33. \*--------------------------------------------------------------*/
  34. #include <iostream.h>
  35.  
  36. #include "parcel.h"
  37.                           // Let's use the default KeySorted Set:
  38. #include <iksset.h>
  39.                           // Let's use the default Heap:
  40. #include <iheap.h>
  41.  
  42. typedef IKeySortedSet<Parcel, IString> ParcelSet;
  43. typedef IHeap        <Parcel>            ParcelHeap;
  44.  
  45. ostream& operator<<(ostream&, ParcelSet const&);
  46. ostream& operator<<(ostream&, ParcelHeap const&);
  47.  
  48. void update(ParcelSet&, ParcelHeap&);
  49.  
  50.  
  51. main()  {
  52.  
  53.   ParcelSet circulating;
  54.   ParcelHeap delivered;
  55.  
  56.   int today = 8;
  57.  
  58.   circulating.add(Parcel("London", "Athens",
  59.      today,      "26LoAt"));
  60.   circulating.add(Parcel("Amsterdam", "Toronto",
  61.      today += 2, "27AmTo"));
  62.   circulating.add(Parcel("Washington", "Stockholm",
  63.      today += 5, "25WaSt"));
  64.   circulating.add(Parcel("Dublin", "Kairo",
  65.      today += 1, "25DuKa"));
  66.   update(circulating, delivered);
  67.   cout << endl << "The situation at start:" << endl;
  68.   cout << "Parcels in circulation:" << endl << circulating;
  69.  
  70.   today ++;
  71.   circulating.elementWithKey("27AmTo").arrivedAt(
  72.      "Atlanta",   today);
  73.   circulating.elementWithKey("25WaSt").arrivedAt(
  74.      "Amsterdam", today);
  75.   circulating.elementWithKey("25DuKa").arrivedAt(
  76.      "Paris",     today);
  77.   update(circulating, delivered);
  78.   cout << endl << endl << "The situation at day " << today << ":"
  79.        << endl;
  80.   cout << "Parcels in circulation:" << endl << circulating;
  81.  
  82.   today ++;          // One day later ...
  83.   circulating.elementWithKey("27AmTo").arrivedAt("Chicago", today);
  84.              // As in real life, one parcel gets lost:
  85.   circulating.removeElementWithKey("26LoAt");
  86.   update(circulating, delivered);
  87.   cout << endl << endl << "The situation at day " << today << ":"
  88.        << endl;
  89.   cout << "Parcels in circulation:" << endl << circulating;
  90.  
  91.   today ++;
  92.   circulating.elementWithKey("25WaSt").arrivedAt("Oslo", today);
  93.   circulating.elementWithKey("25DuKa").arrivedAt("Kairo", today);
  94.              // New parcels are shipped.
  95.   circulating.add(Parcel("Dublin", "Rome", today,   "27DuRo"));
  96.              // Let's try to add one with a key already there.
  97.              // The KeySsorted Set should ignore it:
  98.   circulating.add(Parcel("Nowhere", "Nirvana", today, "25WaSt"));
  99.   update(circulating, delivered);
  100.   cout << endl << endl << "The situation at day " << today << ":"
  101.        << endl;
  102.   cout << "Parcels in circulation:" << endl << circulating;
  103.   cout << "Parcels delivered:" << endl << delivered;
  104.  
  105.                    // Now we make all parcels arrive today:
  106.   today ++;
  107.  
  108.   ParcelSet::Cursor circulatingcursor(circulating);
  109.   forCursor(circulatingcursor) {
  110.      circulating.elementAt(circulatingcursor).wasDelivered(today);
  111.   }
  112.   update(circulating, delivered);
  113.   cout << endl << endl << "The situation at day " << today << ":"
  114.        << endl;
  115.   cout << "Parcels in circulation:" << endl << circulating;
  116.   cout << "Parcels delivered:" << endl << delivered;
  117.  
  118.   if (circulating.isEmpty())
  119.      cout << endl << "All parcels were delivered." << endl;
  120.   else
  121.      cout << endl << "Something very strange happened here." << endl;
  122.  
  123.   return  0;
  124. }
  125.  
  126.  
  127. ostream& operator<<(ostream& os, ParcelSet const& parcels)  {
  128.   ParcelSet::Cursor pcursor(parcels);
  129.   forCursor(pcursor) {
  130.      os <<  pcursor.element() << endl;
  131.   }
  132.   return os;
  133. }
  134.  
  135. ostream& operator<<(ostream& os, ParcelHeap const& parcels)  {
  136.   ParcelHeap::Cursor pcursor(parcels);
  137.   forCursor(pcursor) {
  138.      os <<  pcursor.element() << endl;
  139.   }
  140.   return os;
  141. }
  142.  
  143. IBoolean wasDelivered(Parcel const& p, void* dp) {
  144.    if ( p.lastArrival().city() == p.destination() ) {
  145.       ((ParcelHeap*)dp)->add(p);
  146.       return True;
  147.    }
  148.    else
  149.       return False;
  150. }
  151.  
  152. void update(ParcelSet& p, ParcelHeap& d) {
  153.    p.removeAll(wasDelivered, &d);
  154. }
  155.