Symbols are unique objects with a name stored in a hash table. More...
#include <symbol.hh>
Public Member Functions | |
| ostream & | print (ostream &fout) const |
| print a symbol on a stream | |
Private Member Functions | |
| Symbol (const char *str, unsigned int hsh, Symbol *nxt) | |
| Constructs a new symbol ready to be placed in the hash table. | |
| ~Symbol () | |
| The Destructor is never used. | |
| bool | equiv (unsigned int hash, const char *str) const |
Check if the name of the symbol is equal to string str. | |
Static Private Member Functions | |
| static unsigned int | calcHashKey (const char *str) |
Compute the 32-bits hash key of string str. | |
| static Symbol * | get (const string &str) |
Get the symbol of name str. | |
| static Symbol * | get (const char *str) |
Get the symbol of name str. | |
| static Symbol * | prefix (const char *str) |
Creates a new symbol of name prefixed by str. | |
| static bool | isnew (const char *str) |
Returns true if no symbol of name str exists. | |
Private Attributes | |
| char * | fName |
| Name of the symbol. | |
| unsigned int | fHash |
| Hash key computed from the name and used to determine the hash table entry. | |
| Symbol * | fNext |
| Next symbol in the hash table entry. | |
| void * | fData |
| Field to user disposal to store additional data. | |
Static Private Attributes | |
| static const int | kHashTableSize = 511 |
| Size of the hash table (a prime number is recommended). | |
| static Symbol * | gSymbolTable [kHashTableSize] |
| Hash table used to store the symbols. | |
Friends | |
| Symbol * | symbol (const char *str) |
Returns (and creates if new) the symbol of name str. | |
| Symbol * | symbol (const string &str) |
Returns (and creates if new) the symbol of name str. | |
| Symbol * | unique (const char *str) |
| Returns a new unique symbol of name strxxx. | |
| const char * | name (Symbol *sym) |
| Returns the name of a symbol. | |
| void * | getUserData (Symbol *sym) |
| Returns user data. | |
| void | setUserData (Symbol *sym, void *d) |
| Set user data. | |
Symbols are unique objects with a name stored in a hash table.
Definition at line 53 of file symbol.hh.
| Symbol::Symbol | ( | const char * | str, | |
| unsigned int | hsh, | |||
| Symbol * | nxt | |||
| ) | [private] |
Constructs a new symbol ready to be placed in the hash table.
Constructs a symbol ready to be placed in the hash table.
It makes a private copy of its name.
| str | the name of the symbol | |
| hsh | the hash key of the symbol | |
| nxt | a pointer to the next symbol in the hash table entry |
Definition at line 158 of file symbol.cpp.
References fData, fHash, fName, fNext, and len().
00159 { 00160 int len = strlen(str); 00161 00162 fName = new char [len+1]; 00163 memcpy(fName, str, len+1); 00164 fHash = hsh; 00165 fNext = nxt; 00166 fData = 0; 00167 }
| Symbol::~Symbol | ( | ) | [private] |
The Destructor is never used.
Definition at line 169 of file symbol.cpp.
References fName.
00170 { 00171 delete [] fName; 00172 }
| unsigned int Symbol::calcHashKey | ( | const char * | str | ) | [static, private] |
Compute the 32-bits hash key of string str.
| str | the string |
Definition at line 140 of file symbol.cpp.
Referenced by get(), and isnew().
00141 { 00142 unsigned int h = 0; 00143 00144 while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++); 00145 return h; 00146 }
| bool Symbol::equiv | ( | unsigned int | hash, | |
| const char * | str | |||
| ) | const [private] |
Check if the name of the symbol is equal to string str.
Check if the name of the symbol is equal to string str This method is used by isnew() and make() when searching the hashtable for an existing symbol.
| hash | the hash key of the string (used to speedup the comparison) | |
| str | the string to compare |
true if the name of the symbol and str are the same Definition at line 127 of file symbol.cpp.
Referenced by get(), and isnew().
| Symbol * Symbol::get | ( | const char * | str | ) | [static, private] |
Get the symbol of name str.
Search the hash table for the symbol of name str or returns a new one.
| str | the name of the symbol |
Definition at line 67 of file symbol.cpp.
References calcHashKey(), equiv(), fNext, gSymbolTable, and kHashTableSize.
00068 { 00069 unsigned int hsh = calcHashKey(str); 00070 int bckt = hsh % kHashTableSize; 00071 Symbol* item = gSymbolTable[bckt]; 00072 00073 while ( item && !item->equiv(hsh,str) ) item = item->fNext; 00074 Symbol* r = item ? item : gSymbolTable[bckt] = new Symbol(str, hsh, gSymbolTable[bckt]); 00075 return r; 00076 }
| Symbol * Symbol::get | ( | const string & | str | ) | [static, private] |
Get the symbol of name str.
Search the hash table for the symbol of name str or returns a new one.
| str | the name of the symbol |
Definition at line 46 of file symbol.cpp.
Referenced by symbol().
00047 { 00048 char buf[1024]; 00049 int i; 00050 int n = str.length(); 00051 00052 if (n>1023) n = 1023; 00053 for (i = 0; i < n; i++) { buf[i] = str[i]; } 00054 buf[i] = 0; 00055 00056 return Symbol::get(buf); 00057 }
| bool Symbol::isnew | ( | const char * | str | ) | [static, private] |
Returns true if no symbol of name str exists.
Static method that searches the symbol table for a string.
| str | string to search |
Definition at line 85 of file symbol.cpp.
References calcHashKey(), equiv(), fNext, gSymbolTable, and kHashTableSize.
Referenced by prefix().
00086 { 00087 unsigned int hsh = calcHashKey(str); 00088 int bckt = hsh % kHashTableSize; 00089 Symbol* item = gSymbolTable[bckt]; 00090 00091 while ( item && !item->equiv(hsh,str) ) item = item->fNext; 00092 return item == 0; 00093 }
| Symbol * Symbol::prefix | ( | const char * | str | ) | [static, private] |
Creates a new symbol of name prefixed by str.
Creates a new symbol with a name obtained by concatenating the str prefix with a number in order to make it unique.
| str | the prefix of the name |
prefix++n Definition at line 102 of file symbol.cpp.
Referenced by unique().
00103 { 00104 char name[256]; 00105 00106 static map<const char*, unsigned int> gPrefixCounters; 00107 00108 for (int n = 0; n<10000; n++) { 00109 snprintf(name, 256, "%s%d", str, gPrefixCounters[str]++); 00110 if (isnew(name)) return get(name); 00111 } 00112 assert(false); 00113 return get("UNIQUEOVERFLOW"); 00114 }
| ostream & Symbol::print | ( | ostream & | fout | ) | const |
print a symbol on a stream
< print a symbol on a stream
Definition at line 174 of file symbol.cpp.
Referenced by operator<<().
00175 { 00176 return fout << fName; 00177 }
| void* getUserData | ( | Symbol * | sym | ) | [friend] |
| const char* name | ( | Symbol * | sym | ) | [friend] |
| void setUserData | ( | Symbol * | sym, | |
| void * | d | |||
| ) | [friend] |
| Symbol* symbol | ( | const string & | str | ) | [friend] |
| Symbol* symbol | ( | const char * | str | ) | [friend] |
| Symbol* unique | ( | const char * | str | ) | [friend] |
void* Symbol::fData [private] |
Field to user disposal to store additional data.
Definition at line 66 of file symbol.hh.
Referenced by getUserData(), setUserData(), and Symbol().
unsigned int Symbol::fHash [private] |
char* Symbol::fName [private] |
Symbol* Symbol::fNext [private] |
Symbol * Symbol::gSymbolTable [static, private] |
const int Symbol::kHashTableSize = 511 [static, private] |
1.6.3