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 >
Wrap
Text File
|
1995-03-15
|
6KB
|
155 lines
/*************************************************************************
IBM C/C++ Tools Version 3.00 - Collection Class Library
(C) Copyright IBM Corporation 1992 ,1995, Licensed Program-Property of
IBM. All Rights Reserved. US Government Users Restricted Rights - Use,
duplication or disclosure restricted by GSA ADP Schedule Contract with
IBM Corp.
*************************************************************************/
/*--------------------------------------------------------------*\
| |
| parcel.CPP - Parcels are handled using a KeySorted Set and |
| a Heap. """"""""""""" |
| """" |
| We maintain two collections that keep track of parcels in |
| circulation and parcels delivered. The collection for the |
| parcels in circulation is a KeySorted Set (key, sorted, |
| unique elements, no element equality). For the delivered |
| parcels we do not care about fast or sorted retrieval. |
| So we select the Heap for this collection (no key, unordered, |
| multiple elements, no element equality). |
| |
| A parcel has as member data two objects of type PlaceTime, |
| which is a point in space and time: one object for its origin, |
| one for its current place and time. It also has as member |
| data two objects of type IString, for the destination and |
| for the ID. |
| |
| Function updateParcels adds parcels that have arrived at |
| their destination to the collection for delivered parcels, |
| and removes them from the collection of circulating parcels. |
| This demonstrates the use of removeAll(). |
| |
\*--------------------------------------------------------------*/
#include <iostream.h>
#include "parcel.h"
// Let's use the default KeySorted Set:
#include <iksset.h>
// Let's use the default Heap:
#include <iheap.h>
typedef IKeySortedSet<Parcel, IString> ParcelSet;
typedef IHeap <Parcel> ParcelHeap;
ostream& operator<<(ostream&, ParcelSet const&);
ostream& operator<<(ostream&, ParcelHeap const&);
void update(ParcelSet&, ParcelHeap&);
main() {
ParcelSet circulating;
ParcelHeap delivered;
int today = 8;
circulating.add(Parcel("London", "Athens",
today, "26LoAt"));
circulating.add(Parcel("Amsterdam", "Toronto",
today += 2, "27AmTo"));
circulating.add(Parcel("Washington", "Stockholm",
today += 5, "25WaSt"));
circulating.add(Parcel("Dublin", "Kairo",
today += 1, "25DuKa"));
update(circulating, delivered);
cout << endl << "The situation at start:" << endl;
cout << "Parcels in circulation:" << endl << circulating;
today ++;
circulating.elementWithKey("27AmTo").arrivedAt(
"Atlanta", today);
circulating.elementWithKey("25WaSt").arrivedAt(
"Amsterdam", today);
circulating.elementWithKey("25DuKa").arrivedAt(
"Paris", today);
update(circulating, delivered);
cout << endl << endl << "The situation at day " << today << ":"
<< endl;
cout << "Parcels in circulation:" << endl << circulating;
today ++; // One day later ...
circulating.elementWithKey("27AmTo").arrivedAt("Chicago", today);
// As in real life, one parcel gets lost:
circulating.removeElementWithKey("26LoAt");
update(circulating, delivered);
cout << endl << endl << "The situation at day " << today << ":"
<< endl;
cout << "Parcels in circulation:" << endl << circulating;
today ++;
circulating.elementWithKey("25WaSt").arrivedAt("Oslo", today);
circulating.elementWithKey("25DuKa").arrivedAt("Kairo", today);
// New parcels are shipped.
circulating.add(Parcel("Dublin", "Rome", today, "27DuRo"));
// Let's try to add one with a key already there.
// The KeySsorted Set should ignore it:
circulating.add(Parcel("Nowhere", "Nirvana", today, "25WaSt"));
update(circulating, delivered);
cout << endl << endl << "The situation at day " << today << ":"
<< endl;
cout << "Parcels in circulation:" << endl << circulating;
cout << "Parcels delivered:" << endl << delivered;
// Now we make all parcels arrive today:
today ++;
ParcelSet::Cursor circulatingcursor(circulating);
forCursor(circulatingcursor) {
circulating.elementAt(circulatingcursor).wasDelivered(today);
}
update(circulating, delivered);
cout << endl << endl << "The situation at day " << today << ":"
<< endl;
cout << "Parcels in circulation:" << endl << circulating;
cout << "Parcels delivered:" << endl << delivered;
if (circulating.isEmpty())
cout << endl << "All parcels were delivered." << endl;
else
cout << endl << "Something very strange happened here." << endl;
return 0;
}
ostream& operator<<(ostream& os, ParcelSet const& parcels) {
ParcelSet::Cursor pcursor(parcels);
forCursor(pcursor) {
os << pcursor.element() << endl;
}
return os;
}
ostream& operator<<(ostream& os, ParcelHeap const& parcels) {
ParcelHeap::Cursor pcursor(parcels);
forCursor(pcursor) {
os << pcursor.element() << endl;
}
return os;
}
IBoolean wasDelivered(Parcel const& p, void* dp) {
if ( p.lastArrival().city() == p.destination() ) {
((ParcelHeap*)dp)->add(p);
return True;
}
else
return False;
}
void update(ParcelSet& p, ParcelHeap& d) {
p.removeAll(wasDelivered, &d);
}