00001 #ifndef gHASH_X_H
00002 #define gHASH_X_H
00003
00004 #include "glist.h"
00005 #include "gcontrol.h"
00006
00007 #define LST_HASH_SIZE 971 // A prime number, default hash size
00008 #ifdef DEBUG_HASH
00009 #define LST_HASH_SIZE_MIN 3 // For easier debugging...
00010 #else
00011 #define LST_HASH_SIZE_MIN 101 // Minimum hash size
00012 #endif //DEBUG_HASH
00013
00014
00015 class gKey : public gInt {
00016 public:
00017
00018 enum eKeyKind {
00019 e_Int,
00020 e_UInt64,
00021 e_Real,
00022 e_String
00023 };
00024
00025 gKey (eKeyKind aKeyKind=e_Int) ;
00026 gKey (int v) ;
00027 gKey (gInt& v) ;
00028 gKey (gString& s) ;
00029 gKey (char* s) ;
00030 virtual ~gKey () ;
00031
00032
00033 virtual bool IsOk () ;
00034 virtual eKeyKind GetKeyKind () {
00035 return keyKind;
00036 }
00037 virtual t_uint64 GetUInt64 () ;
00038 virtual double GetReal () ;
00039 virtual gString& GetString () ;
00040
00041 virtual unsigned HashPos (unsigned hashSize) ;
00042 virtual bool MatchKey (gKey& hKey) ;
00043 virtual bool MatchStorage (gStorage* aObj) ;
00044
00045
00046 virtual void Reset () ;
00047 void Set (int v) ;
00048 void Set (t_uint64 v) ;
00049 void Set (double v) ;
00050 void Set (gString& s) ;
00051 void Copy (gKey& copy) ;
00052
00053
00054 virtual unsigned HashStrPos (unsigned hashSize, gString& s) ;
00055
00056
00057 virtual gStorage* NewObject () ;
00058 virtual t_uchar* ToString (t_uchar* uBuf) ;
00059
00060
00061 virtual void Show (bool doShowAll=true) ;
00062
00063 protected:
00064 eKeyKind keyKind;
00065
00066
00067 t_uint64* pvalUInt;
00068 double* pvalDouble;
00069 gString pvalStr;
00070
00071 void thisNewKind (eKeyKind newKind) ;
00072
00073 private:
00074
00075 gKey (gKey& ) ;
00076 gKey& operator= (gKey& ) ;
00077 };
00078
00079 class gHashElemGeneric : public gControl {
00080 public:
00081 virtual ~gHashElemGeneric () ;
00082
00083
00084 gKey hKey;
00085 gStorage* myObj;
00086
00087 protected:
00088 gHashElemGeneric (gStorage* aObj) ;
00089
00090 private:
00091
00092 gHashElemGeneric (gHashElemGeneric& ) ;
00093 gHashElemGeneric& operator= (gHashElemGeneric& ) ;
00094 };
00095
00096 class gHashElemTriple : public gHashElemGeneric {
00097 public:
00098 gHashElemTriple (gKey& aKey, gString& s, gStorage* aObj) ;
00099 gHashElemTriple (gKey& aKey, int aVal, gStorage* aObj) ;
00100 virtual ~gHashElemTriple () ;
00101
00102
00103 gString sDesc;
00104 int iVal;
00105
00106
00107 virtual void Show (bool doShowAll=true) ;
00108
00109 private:
00110
00111 gHashElemTriple (gHashElemTriple& ) ;
00112 gHashElemTriple& operator= (gHashElemTriple& ) ;
00113 };
00114
00115 class gHashGeneric : public gStorage {
00116 public:
00117 virtual ~gHashGeneric () ;
00118
00119
00120 virtual unsigned N () {
00121 return size;
00122 }
00123 virtual bool IsEmpty () {
00124 return usageCounter==0;
00125 }
00126 virtual bool IsValidIndex (unsigned idx) ;
00127 gList* GetHash (unsigned idx) ;
00128 virtual bool FindKey (gKey& hKey) = 0;
00129
00130
00131 virtual unsigned Delete () ;
00132
00133
00134 virtual gStorage* NewObject () ;
00135 virtual t_uchar* ToString (t_uchar* uBuf) ;
00136 virtual bool SaveGuts (FILE* f) ;
00137 virtual bool RestoreGuts (FILE* f) ;
00138
00139 protected:
00140 gHashGeneric (eStorage aKind, unsigned hashSize) ;
00141
00142 unsigned size;
00143 gList* pLst;
00144
00145 void thisPreAllocate (unsigned toSize) ;
00146 unsigned thisDelete () ;
00147 unsigned thisInsertElement () {
00148 usageCounter++;
00149 return usageCounter;
00150 }
00151
00152 private:
00153 unsigned usageCounter;
00154
00155
00156 gHashGeneric (gHashGeneric& ) ;
00157 gHashGeneric& operator= (gHashGeneric& ) ;
00158 };
00159
00160 class gHash : public gHashGeneric {
00161 public:
00162 gHash (unsigned hashSize=LST_HASH_SIZE) ;
00163 virtual ~gHash () ;
00164
00165
00166 bool doFindBeforeInsert;
00167
00168
00169 virtual bool FindKey (gKey& hKey) ;
00170
00171
00172
00173 unsigned Add (int v) ;
00174 unsigned Add (gInt& v) ;
00175 unsigned Add (gInt& v, gString& s) ;
00176 unsigned Add (gKey& hKey, gString& s) ;
00177
00178
00179
00180 virtual t_uchar* ToString (t_uchar* uBuf) ;
00181 virtual bool SaveGuts (FILE* f) ;
00182 virtual bool RestoreGuts (FILE* f) ;
00183
00184
00185 virtual void Show (bool doShowAll=true) ;
00186
00187
00188 bool AddTriplePos_dbg (unsigned idx, gKey& hKey, char* str) ;
00189
00190 protected:
00191 int thisAddElem (unsigned idx, gHashElemGeneric* pElem) ;
00192 int thisFindKey (gKey& hKey, int& x) ;
00193 int thisKeyMatch (gKey& hKey, gStorage* hashElem) ;
00194
00195 private:
00196
00197 gHash (gHash& ) ;
00198 gHash& operator= (gHash& ) ;
00199 };
00200
00201 class gHashTriple : public gHash {
00202 public:
00203 gHashTriple (unsigned hashSize=LST_HASH_SIZE) ;
00204 virtual ~gHashTriple () ;
00205
00206
00207 virtual bool FindKey (gKey& hKey) ;
00208
00209 gHashElemTriple* Find (gKey& hKey) {
00210 unsigned idx;
00211 int x;
00212 return Find( hKey, idx, x );
00213 }
00214 gHashElemTriple* Find (gKey& hKey, unsigned& idx) {
00215 int x;
00216 return Find( hKey, idx, x );
00217 }
00218 gHashElemTriple* Find (gKey& hKey, unsigned& idx, int& x) ;
00219
00220 virtual gHashElemTriple* GetTriple (unsigned idx, int x) ;
00221
00222
00223
00224 bool AddTriple (gKey& hKey, char* str, gStorage* aObj=nil) ;
00225 bool AddTriple (gKey& hKey, gString& s, gStorage* aObj=nil) ;
00226 bool AddTriple (gKey& hKey, int iVal, gStorage* aObj=nil) ;
00227
00228
00229 private:
00230
00231 gHashTriple (gHashTriple& ) ;
00232 gHashTriple& operator= (gHashTriple& ) ;
00233 };
00234
00235 #endif //gHASH_X_H
00236