00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef LEGOHASH_H
00014 #define LEGOHASH_H
00015
00016
00017
00018 #include <iostream.h>
00019 #include <stl.h>
00020 #include "legoErr.H"
00021
00022
00023
00024
00025
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 }
00049
00050 int NotFound (void) { return notfound; }
00051
00052 void Set (T1 key, T2 value)
00053 {
00054 (*table)[key] = value;
00055 }
00056
00057 void Delete (T1 key)
00058 {
00059 table->erase (key);
00060 }
00061
00062 void DeleteAll (void)
00063 {
00064 table->erase (table->begin(), table->end());
00065 }
00066
00067
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 };
00079
00080
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