home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / parcel / parcel.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  6.6 KB  |  164 lines

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