Symbols are unique objects with a name stored in a hash table.
More...
#include <symbol.hh>
|
| ostream & | print (ostream &fout) const |
| | print a symbol on a stream More...
|
| |
|
| | Symbol (const char *str, unsigned int hsh, Symbol *nxt) |
| | Constructs a new symbol ready to be placed in the hash table. More...
|
| |
| | ~Symbol () |
| | The Destructor is never used. More...
|
| |
| bool | equiv (unsigned int hash, const char *str) const |
| | Check if the name of the symbol is equal to string str. More...
|
| |
|
| static unsigned int | calcHashKey (const char *str) |
| | Compute the 32-bits hash key of string str. More...
|
| |
| static Symbol * | get (const string &str) |
| | Get the symbol of name str. More...
|
| |
| static Symbol * | get (const char *str) |
| | Get the symbol of name str. More...
|
| |
| static Symbol * | prefix (const char *str) |
| | Creates a new symbol of name prefixed by str. More...
|
| |
| static bool | isnew (const char *str) |
| | Returns true if no symbol of name str exists. More...
|
| |
|
| char * | fName |
| | Name of the symbol. More...
|
| |
| unsigned int | fHash |
| | Hash key computed from the name and used to determine the hash table entry. More...
|
| |
| Symbol * | fNext |
| | Next symbol in the hash table entry. More...
|
| |
| void * | fData |
| | Field to user disposal to store additional data. More...
|
| |
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.
- Parameters
-
| 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 len().
160 int len = (int)strlen(str);
162 fName =
new char [len+1];
163 memcpy(
fName, str, len+1);
void * fData
Field to user disposal to store additional data.
Symbol * fNext
Next symbol in the hash table entry.
char * fName
Name of the symbol.
unsigned int fHash
Hash key computed from the name and used to determine the hash table entry.
The Destructor is never used.
Definition at line 169 of file symbol.cpp.
char * fName
Name of the symbol.
| unsigned int Symbol::calcHashKey |
( |
const char * |
str | ) |
|
|
staticprivate |
Compute the 32-bits hash key of string str.
- Parameters
-
- Returns
- a 32-bits hash key
Definition at line 140 of file symbol.cpp.
144 while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
| 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.
- Parameters
-
| hash | the hash key of the string (used to speedup the comparison) |
| str | the string to compare |
- Returns
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().
129 return (
fHash == hash) && (strcmp(
fName,str) == 0);
char * fName
Name of the symbol.
unsigned int fHash
Hash key computed from the name and used to determine the hash table entry.
| Symbol * Symbol::get |
( |
const string & |
str | ) |
|
|
staticprivate |
Get the symbol of name str.
Search the hash table for the symbol of name str or returns a new one.
- Parameters
-
| str | the name of the symbol |
- Returns
- a symbol of name str
Definition at line 46 of file symbol.cpp.
Referenced by symbol().
50 int n = (int)str.length();
53 for (i = 0; i < n; i++) { buf[i] = str[i]; }
static Symbol * get(const string &str)
Get the symbol of name str.
| Symbol * Symbol::get |
( |
const char * |
str | ) |
|
|
staticprivate |
Get the symbol of name str.
Search the hash table for the symbol of name str or returns a new one.
- Parameters
-
| str | the name of the symbol |
- Returns
- a symbol of name str
Definition at line 67 of file symbol.cpp.
References equiv(), and fNext.
73 while ( item && !item->
equiv(hsh,str) ) item = item->
fNext;
Symbol * fNext
Next symbol in the hash table entry.
static const int kHashTableSize
Size of the hash table (a prime number is recommended)
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
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.
| bool Symbol::isnew |
( |
const char * |
str | ) |
|
|
staticprivate |
Returns true if no symbol of name str exists.
Static method that searches the symbol table for a string.
- Parameters
-
- Returns
- true if the string is NOT in the table (it is a new string)
Definition at line 85 of file symbol.cpp.
References equiv(), and fNext.
91 while ( item && !item->
equiv(hsh,str) ) item = item->
fNext;
Symbol * fNext
Next symbol in the hash table entry.
static const int kHashTableSize
Size of the hash table (a prime number is recommended)
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
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.
| Symbol * Symbol::prefix |
( |
const char * |
str | ) |
|
|
staticprivate |
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.
- Parameters
-
| str | the prefix of the name |
- Returns
- a symbol of name
prefix++n
Definition at line 102 of file symbol.cpp.
References name().
Referenced by unique().
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]++);
113 return get(
"UNIQUEOVERFLOW");
static bool isnew(const char *str)
Returns true if no symbol of name str exists.
friend const char * name(Symbol *sym)
Returns the name of a symbol.
| 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<<().
176 return fout <<
fName;
char * fName
Name of the symbol.
| void* getUserData |
( |
Symbol * |
sym | ) |
|
|
friend |
Returns user data.
Definition at line 100 of file symbol.hh.
| const char* name |
( |
Symbol * |
sym | ) |
|
|
friend |
Returns the name of a symbol.
Definition at line 98 of file symbol.hh.
| void setUserData |
( |
Symbol * |
sym, |
|
|
void * |
d |
|
) |
| |
|
friend |
| Symbol* symbol |
( |
const char * |
str | ) |
|
|
friend |
Returns (and creates if new) the symbol of name str.
Definition at line 95 of file symbol.hh.
| Symbol* symbol |
( |
const string & |
str | ) |
|
|
friend |
Returns (and creates if new) the symbol of name str.
Definition at line 96 of file symbol.hh.
| Symbol* unique |
( |
const char * |
str | ) |
|
|
friend |
Returns a new unique symbol of name strxxx.
Definition at line 97 of file symbol.hh.
| unsigned int Symbol::fHash |
|
private |
Hash key computed from the name and used to determine the hash table entry.
Definition at line 64 of file symbol.hh.
Hash table used to store the symbols.
Definition at line 59 of file symbol.hh.
| const int Symbol::kHashTableSize = 511 |
|
staticprivate |
Size of the hash table (a prime number is recommended)
Definition at line 58 of file symbol.hh.
The documentation for this class was generated from the following files: