Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

legoHash.H

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * File:         legoHash.H
00004  * Author:       Bill Havanki
00005  * Description:  Describes a generic hash table.
00006  *
00007  * 10/17/97 WAH: Added notfound field and associated methods.
00008  * 10/24/97 WAH: Converted implementation to use STL map container.
00009  * 10/30/97 WAH: Added DeleteAll method.
00010  * 1/6/98 WAH:   Removed old implementation.
00011  **************************************************************************/
00012 
00013 #ifndef LEGOHASH_H
00014 #define LEGOHASH_H
00015 
00016 //#define LEGOHASH_DEBUG
00017 
00018 #include <iostream.h>
00019 #include <stl.h>
00020 #include "legoErr.H"
00021 
00022 /*
00023  * class legoHash
00024  *
00025  * This class describes a single hash table of arbitrary size.
00026  */
00027 template <class T1, class T2, class T3>
00028 class legoHash
00029 {
00030   map <T1, T2, T3> *table;
00031   map <T1, T2, T3>::iterator it;
00032   int notfound;
00033 
00034 public:
00035   legoHash() { table = new map <T1, T2, T3>; }
00036   ~legoHash() { delete table; }
00037 
00038   T2 Lookup (T1 key)
00039   {
00040     map<T1, T2, T3>::iterator i;
00041 
00042     i = table->find (key);
00043     if (i == table->end())
00044       notfound = 1;
00045     else
00046       notfound = 0;
00047     return (*i).second;
00048   } // end Lookup
00049 
00050   int NotFound (void) { return notfound; }
00051 
00052   void Set (T1 key, T2 value)
00053   {
00054     (*table)[key] = value;
00055   } // end Set
00056 
00057   void Delete (T1 key)
00058   {
00059     table->erase (key);
00060   } // end Delete
00061 
00062   void DeleteAll (void)
00063   {
00064     table->erase (table->begin(), table->end());
00065   } // end DeleteAll
00066 
00067   // Iterator functions
00068 
00069   void Start (void)
00070   {
00071     it = table->begin();
00072   }
00073   void Next (void) { it++; }
00074   int AtEnd (void) { return (it == table->end()); } 
00075   T1 GetCurrentKey (void) { return (*it).first; }
00076   T2 LookupCurrent (void) { return (*it).second; }
00077 
00078 }; // end class legoHash
00079 
00080 // Comparison functions required for hash table usage.
00081 struct legoHash_lt_string
00082 {
00083   bool operator()(const char* s1, const char* s2) const
00084   {
00085     return strcmp(s1, s2) < 0;
00086   }
00087 };
00088 
00089 struct legoHash_lt_int
00090 {
00091   bool operator()(const int i1, const int i2) const
00092   {
00093     return (i1 < i2);
00094   }
00095 };
00096 
00097 struct legoHash_lt_pointer
00098 {
00099   bool operator()(const void *i1, const void *i2) const
00100   {
00101     return ((unsigned long) i1 < (unsigned long) i2);
00102   }
00103 };
00104 
00105 #endif

Generated on Mon Jul 21 20:27:32 2003 for TINKER LEGO DOC by doxygen 1.3.2