Symbol Class Reference

Symbols are unique objects with a name stored in a hash table. More...

#include <symbol.hh>

List of all members.

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 Symbolget (const string &str)
 Get the symbol of name str.
static Symbolget (const char *str)
 Get the symbol of name str.
static Symbolprefix (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.
SymbolfNext
 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 SymbolgSymbolTable [kHashTableSize]
 Hash table used to store the symbols.

Friends

Symbolsymbol (const char *str)
 Returns (and creates if new) the symbol of name str.
Symbolsymbol (const string &str)
 Returns (and creates if new) the symbol of name str.
Symbolunique (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.


Detailed Description

Symbols are unique objects with a name stored in a hash table.

Definition at line 52 of file symbol.hh.


Constructor & Destructor Documentation

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 151 of file symbol.cpp.

References fData, fHash, fName, and fNext.

00152 {
00153     int len = strlen(str);
00154     
00155     fName = new char [len+1];
00156     memcpy(fName, str, len+1);
00157     fHash = hsh;
00158     fNext = nxt;
00159     fData = 0;
00160 }


Member Function Documentation

unsigned int Symbol::calcHashKey ( const char *  str  )  [static, private]

Compute the 32-bits hash key of string str.

Parameters:
str the string
Returns:
a 32-bits hash key

Definition at line 133 of file symbol.cpp.

Referenced by get(), and isnew().

00134 {
00135     unsigned int h = 0;
00136 
00137     while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
00138     return h;
00139 }

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 120 of file symbol.cpp.

References fHash, and fName.

Referenced by get(), and isnew().

00121 {
00122     return (fHash == hash) && (strcmp(fName,str) == 0);
00123 }

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.

Parameters:
str the name of the symbol
Returns:
a symbol of name str

Definition at line 63 of file symbol.cpp.

References calcHashKey(), equiv(), fNext, gSymbolTable, and kHashTableSize.

00064 {
00065     unsigned int            hsh  = calcHashKey(str);
00066     int             bckt = hsh % kHashTableSize;
00067     Symbol*         item = gSymbolTable[bckt];
00068 
00069     while ( item && !item->equiv(hsh,str) ) item = item->fNext;
00070     Symbol* r = item ? item : gSymbolTable[bckt] = new Symbol(str, hsh, gSymbolTable[bckt]);
00071     return r;
00072 }

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.

Parameters:
str the name of the symbol
Returns:
a symbol of name str

Definition at line 42 of file symbol.cpp.

Referenced by symbol().

00043 {
00044     char    buf[1024];
00045     int     i;
00046     int     n = str.length();
00047     
00048     if (n>1023) n = 1023;
00049     for (i = 0; i < n; i++) { buf[i] = str[i]; }
00050     buf[i] = 0;
00051     
00052     return Symbol::get(buf);
00053 }

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.

Parameters:
str string to search
Returns:
true if the string is NOT in the table (it is a new string)

Definition at line 81 of file symbol.cpp.

References calcHashKey(), equiv(), fNext, gSymbolTable, and kHashTableSize.

Referenced by prefix().

00082 {
00083     unsigned int            hsh  = calcHashKey(str);
00084     int             bckt = hsh % kHashTableSize;
00085     Symbol*         item = gSymbolTable[bckt];
00086     
00087     while ( item && !item->equiv(hsh,str) ) item = item->fNext;
00088     return item == 0;
00089 }

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.

Parameters:
str the prefix of the name
Returns:
a symbol of name prefix++n

Definition at line 98 of file symbol.cpp.

References isnew(), and name.

Referenced by unique().

00099 {
00100     char    name[256];
00101     
00102     for (int n = 0; n<10000; n++) {
00103         snprintf(name, 256, "%s%d", str, n);
00104         if (isnew(name)) return get(name);
00105     }
00106     return get("UNIQUEOVERFLOW");
00107 }   

ostream & Symbol::print ( ostream &  fout  )  const

print a symbol on a stream

< print a symbol on a stream

Definition at line 164 of file symbol.cpp.

00165 {
00166     return fout << fName;
00167 }


The documentation for this class was generated from the following files:

Generated on Sat Jul 25 13:29:15 2009 for FAUST compiler by  doxygen 1.5.9