home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / PARCEL.H < prev    next >
C/C++ Source or Header  |  1993-05-07  |  4KB  |  120 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  parcel.h  -  Class Parcel and its parts for use with the     |
  20. |               example for Key Sorted Set and Heap.            |
  21. \*-------------------------------------------------------------*/
  22.  
  23.                         // For definition of Boolean:
  24. #include <iglobals.h>
  25.                         // Class ToyString:
  26. #include <toystrng.h>
  27.  
  28.  
  29. class PlaceTime {
  30.  
  31.   ToyString cty;
  32.   int   daynum;  // Keeping it simple:  January 9 is day 9
  33.  
  34. public:
  35.  
  36.   PlaceTime(ToyString acity, int aday) : cty(acity), daynum(aday) {}
  37.  
  38.   PlaceTime(ToyString acity) : cty(acity) {daynum = 0;}
  39.  
  40.   ToyString const& city() const {
  41.      return cty;
  42.   }
  43.  
  44.   int const& day() const {
  45.      return daynum;
  46.   }
  47.  
  48.   void operator=(PlaceTime const& pt) {
  49.       cty = pt.cty;
  50.       daynum = pt.daynum;
  51.   }
  52.  
  53.   Boolean operator==(PlaceTime const& pt)  const {
  54.      return ( (cty == pt.cty)
  55.            && (daynum == pt.daynum) );
  56.   }
  57. };
  58.  
  59.  
  60. class Parcel {
  61.  
  62.   PlaceTime org, lstAr;
  63.   ToyString dst, id;
  64.  
  65. public:
  66.  
  67.   Parcel(ToyString orig,  ToyString dest, int day, ToyString ident)
  68.      :  org(orig, day),  lstAr(orig, day),  dst(dest), id(ident) {}
  69.  
  70.   void arrivedAt(ToyString const& acity, int const& day) {
  71.      PlaceTime nowAt(acity, day);
  72.                               // Only if not already there before
  73.      if (nowAt.city() != lstAr.city())
  74.         lstAr = nowAt;
  75.   }
  76.  
  77.   void wasDelivered(int const& day) {arrivedAt(dst, day);  }
  78.  
  79.   PlaceTime const& origin() const {
  80.      return org;
  81.   }
  82.  
  83.   ToyString const& destination() const {
  84.      return dst;
  85.   }
  86.  
  87.   PlaceTime const& lastArrival() const {
  88.      return lstAr;
  89.   }
  90.  
  91.   ToyString const& ID() const {
  92.      return id;
  93.   }
  94.  
  95.   friend ostream& operator<<(ostream& os, Parcel const& p) {
  96.     os << p.id << ": From " << p.org.city()
  97.        << "(day "  << p.org.day() << ") to " << p.dst;
  98.  
  99.     if (p.lstAr.city() != p.dst) {
  100.        os << "\n            is at " << p.lstAr.city()
  101.           << "  since day " << p.lstAr.day() << ".";
  102.     }
  103.     else {
  104.        os << "\n            was delivered on day "
  105.           << p.lstAr.day() << ".";
  106.     }
  107.     return os;
  108.   }
  109. };
  110.  
  111.                         // Key access:
  112.   inline  ToyString const& key( Parcel const&  p) {
  113.   return p.ID();
  114.   }
  115.                         // We need a compare function for the key.
  116.                         // Let's use the default provided for char*:
  117.   inline long compare(ToyString const& TS1, ToyString const& TS2) {
  118.      return compare(TS1.text(), TS2.text());
  119.   }
  120.