home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / LOOKUP.PAK / LOOKUP.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.4 KB  |  72 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LOOKUP.CPP                                                            */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1993 Borland International                        */
  6. /*  All Rights Reserved.                                                  */
  7. /*                                                                        */
  8. /*  Hash table example file                                               */
  9. /*                                                                        */
  10. /*------------------------------------------------------------------------*/
  11.  
  12. #if !defined(SERVICES_CSTRING_H)
  13. # include <services/cstring.h>
  14. #endif
  15. #if !defined(CLASSLIB_ASSOC_H)
  16. # include <classlib/assoc.h>
  17. #endif
  18. #if !defined(CLASSLIB_DICT_H)
  19. # include <classlib/dict.h>
  20. #endif
  21. #if !defined(__IOSTREAM_H)
  22. # include <iostream.h>    
  23. #endif
  24.  
  25. static char *Entries[] =
  26.     {
  27.    "string",           "manipulates character data",
  28.      "TDate",            "manipulates dates",
  29.      "TTime",            "manipulates times",
  30.      "VectorImp",        "implements a zero-based vector",
  31.      "CVectorImp",       "implements a zero-based counted vector",
  32.      "SVectorImp",       "implements a zero-based sorted vector",
  33.      };
  34.  
  35. #define ArraySize(n) (sizeof(n)/sizeof(*n))
  36.  
  37. class HashString : public string {
  38.   public:
  39.      HashString() : string() {}
  40.      HashString(const char* s) : string(s) {}
  41.      unsigned HashValue() const { return hash(); }
  42. };
  43.                          
  44. typedef TDDAssociation<HashString,HashString> ClassData;
  45. typedef TDictionaryAsHashTable<ClassData> Dictionary;
  46.  
  47. int main( int argc, char *argv[] )
  48. {
  49.      if( argc != 2 )
  50.           {
  51.           cerr << "Usage:  lookup classname\n";
  52.           return 1;
  53.           }
  54.  
  55.      Dictionary ClassDefinitions;
  56.      string::set_case_sensitive(0);
  57.  
  58.      for( int i = 0; i < ArraySize(Entries); i+=2 )
  59.           ClassDefinitions.Add( ClassData( Entries[i], Entries[i+1] ) );
  60.  
  61.      ClassData *definition =
  62.           ClassDefinitions.Find( ClassData( argv[1], (char *)0 ) );
  63.      if( definition == 0 )
  64.           cout << "A definition for " << argv[1]
  65.                  << " was not found in the dictionary.\n";
  66.      else
  67.           cout << definition->Key() << " : " << definition->Value() << endl;
  68.  
  69.    ClassDefinitions.Flush();
  70.      return 0;
  71. }
  72.