00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00040 #ifndef __SYMBOL__
00041 #define __SYMBOL__
00042
00043 #include <string>
00044 #include <map>
00045
00046 using namespace std;
00047
00048
00049
00053 class Symbol
00054 {
00055
00056 private:
00057
00058 static const int kHashTableSize = 511;
00059 static Symbol* gSymbolTable[kHashTableSize];
00060
00061
00062
00063 char* fName;
00064 unsigned int fHash;
00065 Symbol* fNext;
00066 void* fData;
00067
00068
00069 Symbol (const char* str, unsigned int hsh, Symbol* nxt);
00070 ~Symbol ();
00071
00072
00073 bool equiv (unsigned int hash, const char* str) const ;
00074 static unsigned int calcHashKey (const char* str);
00075
00076
00077 static Symbol* get (const string& str);
00078 static Symbol* get (const char* str);
00079 static Symbol* prefix (const char* str);
00080 static bool isnew (const char* str);
00081
00082 public:
00083 ostream& print (ostream& fout) const;
00084
00085 friend Symbol* symbol (const char* str);
00086 friend Symbol* symbol (const string& str);
00087 friend Symbol* unique (const char* str);
00088 friend const char* name (Symbol* sym);
00089
00090 friend void* getUserData (Symbol* sym);
00091 friend void setUserData (Symbol* sym, void* d);
00092
00093 };
00094
00095 inline Symbol* symbol (const char* str) { return Symbol::get(str); }
00096 inline Symbol* symbol (const string& str) { return Symbol::get(str); }
00097 inline Symbol* unique (const char* str) { return Symbol::prefix(str);}
00098 inline const char* name (Symbol* sym) { return sym->fName; }
00099
00100 inline void* getUserData (Symbol* sym) { return sym->fData; }
00101 inline void setUserData (Symbol* sym, void* d) { sym->fData=d; }
00102
00103 inline ostream& operator << (ostream& s, const Symbol& n) { return n.print(s); }
00104
00105
00106 typedef Symbol* Sym;
00107
00108 #endif