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

  1. /*------------------------------------------------------------*\
  2. | animals.CPP  -  Example for the use of the Key Bag           |
  3. |                                            """""""           |
  4. | We keep a Key Bag of our observations on animals.  Elements  |
  5. | handled in this Key Bag are of type animal, the key is the   |
  6. | name of the animal.                                          |
  7. | This Key Bag allows us to efficiently access all observations|
  8. | on an animal.                                                |
  9. | We use a Sequence to store the names of all extinct animals. |
  10. | At last we remove all extinct animals from the Key Bag.      |
  11. \*------------------------------------------------------------*/
  12.  
  13. #include <iostream.h>
  14.                // Class Animal:
  15. #include "animal.h"
  16.  
  17.                // Let's use the default Key Bag:
  18. #include <ikb.h>
  19. typedef IKeyBag<Animal, IString> Animals;
  20.  
  21.                // For keys let's use the default Sequence:
  22. #include <iseq.h>
  23. typedef ISequence<IString> Names;
  24.  
  25.  
  26. main() {
  27.  
  28.    Animals animals;
  29.    Animals::Cursor animalsCur1(animals), animalsCur2(animals);
  30.  
  31.    animals.add(Animal("bear", "heavy"));
  32.    animals.add(Animal("bear", "strong"));
  33.    animals.add(Animal("dinosaur", "heavy"));
  34.    animals.add(Animal("dinosaur", "huge"));
  35.    animals.add(Animal("dinosaur", "extinct"));
  36.    animals.add(Animal("eagle", "black"));
  37.    animals.add(Animal("eagle", "strong"));
  38.    animals.add(Animal("lion", "dangerous"));
  39.    animals.add(Animal("lion", "strong"));
  40.    animals.add(Animal("mammoth", "long haired"));
  41.    animals.add(Animal("mammoth", "extinct"));
  42.    animals.add(Animal("sabre tooth tiger", "extinct"));
  43.    animals.add(Animal("zebra", "striped"));
  44.  
  45.                  // Display all elements in animals:
  46.    cout << endl
  47.         << "All our observations on animals:" << endl;
  48.    forICursor(animalsCur1)  cout << "    " << animalsCur1.element();
  49.  
  50.    cout << endl << endl
  51.         << "Number of observations on animals: "
  52.         << animals.numberOfElements() << endl;
  53.  
  54.    cout << endl
  55.         << "Number of different animals: "
  56.         << animals.numberOfDifferentKeys() << endl;
  57.  
  58.    Names namesOfExtinct(animals.numberOfDifferentKeys());
  59.    Names::Cursor extinctCur1(namesOfExtinct);
  60.  
  61.    animalsCur1.setToFirst();
  62.    do {
  63.       IString name = animalsCur1.element().name();
  64.  
  65.       cout << endl
  66.            << "We have " << animals.numberOfElementsWithKey(name)
  67.            << " observations on " << name << ":" << endl;
  68.  
  69.                    // We need to use a separate cursor here
  70.                    // because otherwise animalsCur1 would become
  71.                    // invalid after last locateNextElement...()
  72.       animals.locateElementWithKey(name, animalsCur2);
  73.       do  {
  74.          IString attribute = animalsCur2.element().attribute();
  75.          cout << "    " << attribute << endl;
  76.          if (attribute == "extinct") namesOfExtinct.add(name);
  77.       } while (animals.locateNextElementWithKey(name, animalsCur2));
  78.  
  79.    } while (animals.setToNextWithDifferentKey(animalsCur1));
  80.  
  81.                  // Remove all observations on extinct animals:
  82.    forICursor(extinctCur1)
  83.       animals.removeAllElementsWithKey(extinctCur1.element());
  84.  
  85.                  // Display all elements in animals:
  86.    cout << endl << endl
  87.         << "After removing all observations on extinct animals:"
  88.         << endl;
  89.    forICursor(animalsCur1)  cout << "    " << animalsCur1.element();
  90.  
  91.    cout << endl
  92.         << "Number of observations on animals: "
  93.         << animals.numberOfElements() << endl;
  94.  
  95.    cout << endl
  96.         << "Number of different animals: "
  97.         << animals.numberOfDifferentKeys() << endl;
  98.  
  99.    return 0;
  100. }
  101.