home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 8 < prev    next >
Encoding:
Text File  |  1992-04-01  |  4.6 KB  |  147 lines

  1. /* 
  2.  * Example 5: Hashed dictionaries
  3.  * This program uses RWCollectableStrings as keys and RWCollectableInts as values.
  4.  *
  5.  * $Header:   E:/vcs/toolexam/example5.cpv   1.1   01 Apr 1992 16:51:10   keffer  $
  6.  *
  7.  ****************************************************************************
  8.  *
  9.  * Rogue Wave Software, Inc.
  10.  * P.O. Box 2328
  11.  * Corvallis, OR 97339
  12.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  13.  *
  14.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  15.  * protection under the laws of the United States and other countries.
  16.  *
  17.  ***************************************************************************
  18.  *
  19.  * $Log:   E:/vcs/toolexam/example5.cpv  $
  20.  * 
  21.  *    Rev 1.1   01 Apr 1992 16:51:10   keffer
  22.  * Now includes <rw/xxx.h>
  23.  * 
  24.  */
  25.  
  26.  
  27. /*
  28.  * Declarations for class RWCollectableString:  
  29.  * This class inherits class RWCString and class RWCollectable.  
  30.  * Class RWCollectable has virtual functions hash() and matches() that are 
  31.  * redefined by class RWCollectableString.  These functions are used to 
  32.  * insert and retrieve RWCollectableStrings in the hash table.
  33.  */
  34. #include <rw/collstr.h>
  35.  
  36. // Declarations for class RWCollectableInt:
  37. #include <rw/collint.h>
  38.  
  39. // Declarations for RWHashDictionary:
  40. #include <rw/hashdict.h>
  41. #include <rw/rstream.h>
  42.  
  43. STARTWRAP
  44. #include <stdlib.h>
  45. ENDWRAP
  46.  
  47. // The following can easily be changed to store and retrieve 
  48. // keys and values of other types:
  49. typedef RWCollectableString KeyType;    
  50. typedef RWCollectableInt    ValueType;
  51.  
  52. // The following can be changed to use a different container class; 
  53. // for example, a B-Tree:
  54. typedef RWHashDictionary        DictionaryType;
  55.  
  56. /* Global function to print keys and values.  This function is handed to  
  57.  * RWHashDictionary::apply().
  58.  */
  59. void
  60. printKeyAndValue( RWCollectable* ky, RWCollectable* val, void*)
  61. {
  62.   /* Do the necessary typecast, then print 'em out: */
  63.   cout << *(const KeyType*) ky << "\t" <<  *(const ValueType*)val << NL;
  64. }  
  65.  
  66. main()
  67. {
  68.   cout << "***************Example of Hash Dictionary *****************\n\n";
  69.  
  70.   // Construct a dictionary with no entries:
  71.   DictionaryType dictionary;
  72.  
  73.   // Loop to do various things to the Dictionary:
  74.   // Since the Dictionary stores pointers to keys and values, storage will be 
  75.   // provided from the heap for the actual keys and values.
  76.  
  77.   KeyType*      theKey;      // pointer to key.
  78.   ValueType*    theValue;    // pointer to value.
  79.   KeyType       aKey;        // Test key.
  80.   char          option;      // Options for switch.
  81.  
  82.   do {
  83.     cout << dictionary.entries() << " entries in dictionary.\n";
  84.  
  85.     cout << "(i)nsert (f)ind (d)elete (l)ist (c)lear e(x)it:\t";
  86.  
  87.     // Borland bug necessitates explicit test for .good():
  88.  
  89.     if ( !(cin >> option).good() ) break; // Check for EOF
  90.     
  91.     switch ( option ) {
  92.     case 'i':         // Insert a key-value pair.
  93.     case 'I':
  94.       cout << "Enter word to act as key:\t";
  95.       theKey = new KeyType;   // Allocate from the heap.
  96.       cin >> *theKey;
  97.       cout << "\nEnter int to act as value:\t";
  98.       theValue = new ValueType;  // Allocate from the heap.
  99.       cin >> *theValue;
  100.       dictionary.insertKeyAndValue(theKey, theValue);
  101.       cout << "Key " << *theKey << " with value " << *theValue << " entered.\n";
  102.       break;
  103.     case 'f':        // Find a value, given a test key.
  104.     case 'F':
  105.       cout << "Enter word to find:\t";
  106.       cin >> aKey;
  107.       theValue = (ValueType*)dictionary.findValue(&aKey);
  108.       if ( theValue )
  109.     cout << aKey << " is a member of the collection with value " << *theValue <<NL;
  110.       else
  111.     cout << aKey << " is not a member of the collection.\n";
  112.       break;
  113.     case 'd':       // Delete a key.
  114.     case 'D':
  115.       cout << "Enter word to be deleted:\t";
  116.       cin >> aKey;
  117.       RWCollectable* temp;    // Temporary necessary to calm cfront down
  118.       theKey = (KeyType*)dictionary.removeKeyAndValue(&aKey, temp);
  119.       if(theKey){
  120.     theValue = (ValueType*)temp;
  121.     cout << "Key " << *theKey << " with value " << *theValue << " removed.\n";
  122.     delete theKey;         
  123.     delete theValue;
  124.       }
  125.       else
  126.     cout << "Key " << aKey << " not found.\n";
  127.       break;
  128.     case 'l':    // List contents of Dictionary.
  129.     case 'L':
  130.       dictionary.applyToKeyAndValue(printKeyAndValue, rwnil); // Uses global function defined above.
  131.       break;
  132.     case 'c':   // Clear the Dictionary
  133.     case 'C':
  134.       dictionary.clearAndDestroy();
  135.       break;
  136.     case 'x':
  137.     case 'X':
  138.       break;
  139.     default:
  140.       cerr << "Not recognized.\n";
  141.     }    // end switch
  142.   } while (option != 'x' && option != 'X') ;
  143.  
  144.   return 0;
  145. }
  146.  
  147.