ghash.h

Go to the documentation of this file.
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     // Public enums
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     // Get methods
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     // Set methods
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     // Specific methods
00054     virtual unsigned HashStrPos (unsigned hashSize, gString& s) ;
00055 
00056     // Save/etc methods
00057     virtual gStorage* NewObject () ;
00058     virtual t_uchar* ToString (t_uchar* uBuf) ;
00059 
00060     // Show methods
00061     virtual void Show (bool doShowAll=true) ;
00062 
00063 protected:
00064     eKeyKind keyKind;
00065 
00066     // Possible key values: int, t_uint64, double, ...
00067     t_uint64* pvalUInt;
00068     double* pvalDouble;
00069     gString pvalStr;
00070 
00071     void thisNewKind (eKeyKind newKind) ;
00072 
00073 private:
00074     // Operators,empty
00075     gKey (gKey& ) ; //empty
00076     gKey& operator= (gKey& ) ; //empty
00077 };
00078 ////////////////////////////////////////////////////////////
00079 class gHashElemGeneric : public gControl {
00080 public:
00081     virtual ~gHashElemGeneric () ;
00082 
00083     // Public members
00084     gKey hKey;
00085     gStorage* myObj;
00086 
00087 protected:
00088     gHashElemGeneric (gStorage* aObj) ;
00089 
00090 private:
00091     // Operators,empty
00092     gHashElemGeneric (gHashElemGeneric& ) ; //empty
00093     gHashElemGeneric& operator= (gHashElemGeneric& ) ; //empty
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     // Public data-members
00103     gString sDesc;
00104     int iVal;
00105 
00106     // Show methods
00107     virtual void Show (bool doShowAll=true) ;
00108 
00109 private:
00110     // Operators,empty
00111     gHashElemTriple (gHashElemTriple& ) ; //empty
00112     gHashElemTriple& operator= (gHashElemTriple& ) ; //empty
00113 };
00114 ////////////////////////////////////////////////////////////
00115 class gHashGeneric : public gStorage {
00116 public:
00117     virtual ~gHashGeneric () ;
00118 
00119     // Get methods
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; //PURE
00129 
00130     // Set methods
00131     virtual unsigned Delete () ;
00132 
00133     // Save/etc methods
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     // Operators,empty
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     // Public data-members
00166     bool doFindBeforeInsert;
00167 
00168     // Get methods
00169     virtual bool FindKey (gKey& hKey) ;
00170     // .
00171 
00172     // Set methods
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     // Save/etc methods
00180     virtual t_uchar* ToString (t_uchar* uBuf) ;
00181     virtual bool SaveGuts (FILE* f) ;
00182     virtual bool RestoreGuts (FILE* f) ;
00183 
00184     // Show methods
00185     virtual void Show (bool doShowAll=true) ;
00186 
00187     // Debugging methods
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     // Operators,empty
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     // Get methods
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     // Set methods
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     // Operators,empty
00231     gHashTriple (gHashTriple& ) ; //empty
00232     gHashTriple& operator= (gHashTriple& ) ; //empty
00233 };
00234 ////////////////////////////////////////////////////////////
00235 #endif //gHASH_X_H
00236 

Generated on Sat Aug 18 02:40:54 2007 for xpfweb_v2x lib by  doxygen 1.4.2