50 int n = (int)str.length();
53 for (i = 0; i < n; i++) { buf[i] = str[i]; }
69 unsigned int hsh = calcHashKey(str);
70 int bckt = hsh % kHashTableSize;
71 Symbol* item = gSymbolTable[bckt];
73 while ( item && !item->
equiv(hsh,str) ) item = item->
fNext;
74 Symbol* r = item ? item : gSymbolTable[bckt] =
new Symbol(str, hsh, gSymbolTable[bckt]);
87 unsigned int hsh = calcHashKey(str);
88 int bckt = hsh % kHashTableSize;
89 Symbol* item = gSymbolTable[bckt];
91 while ( item && !item->
equiv(hsh,str) ) item = item->
fNext;
106 static map<const char*, unsigned int> gPrefixCounters;
108 for (
int n = 0; n<10000; n++) {
109 snprintf(name, 256,
"%s%d", str, gPrefixCounters[str]++);
110 if (isnew(name))
return get(
name);
113 return get(
"UNIQUEOVERFLOW");
129 return (fHash == hash) && (strcmp(fName,str) == 0);
144 while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
160 int len = (int)strlen(str);
162 fName =
new char [len+1];
163 memcpy(fName, str, len+1);
176 return fout << fName;
~Symbol()
The Destructor is never used.
ostream & print(ostream &fout) const
print a symbol on a stream
static Symbol * get(const string &str)
Get the symbol of name str.
A library to create and manipulate symbols with a unique name.
static bool isnew(const char *str)
Returns true if no symbol of name str exists.
Symbol(const char *str, unsigned int hsh, Symbol *nxt)
Constructs a new symbol ready to be placed in the hash table.
Symbol * fNext
Next symbol in the hash table entry.
static Symbol * prefix(const char *str)
Creates a new symbol of name prefixed by str.
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
const char * name(Symbol *sym)
Returns the name of a symbol.
Symbols are unique objects with a name stored in a hash table.
static unsigned int calcHashKey(const char *str)
Compute the 32-bits hash key of string str.
bool equiv(unsigned int hash, const char *str) const
Check if the name of the symbol is equal to string str.