FAUST compiler  0.9.9.6b8
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Symbol Class Reference

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

#include <symbol.hh>

Collaboration diagram for Symbol:
Collaboration graph
[legend]

Public Member Functions

ostream & print (ostream &fout) const
 print a symbol on a stream More...
 

Private Member Functions

 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 Private Member Functions

static unsigned int calcHashKey (const char *str)
 Compute the 32-bits hash key of string str. More...
 
static Symbolget (const string &str)
 Get the symbol of name str. More...
 
static Symbolget (const char *str)
 Get the symbol of name str. More...
 
static Symbolprefix (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...
 

Private Attributes

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...
 
SymbolfNext
 Next symbol in the hash table entry. More...
 
void * fData
 Field to user disposal to store additional data. More...
 

Static Private Attributes

static const int kHashTableSize = 511
 Size of the hash table (a prime number is recommended) More...
 
static SymbolgSymbolTable [kHashTableSize]
 Hash table used to store the symbols. More...
 

Friends

Symbolsymbol (const char *str)
 Returns (and creates if new) the symbol of name str. More...
 
Symbolsymbol (const string &str)
 Returns (and creates if new) the symbol of name str. More...
 
Symbolunique (const char *str)
 Returns a new unique symbol of name strxxx. More...
 
const char * name (Symbol *sym)
 Returns the name of a symbol. More...
 
void * getUserData (Symbol *sym)
 Returns user data. More...
 
void setUserData (Symbol *sym, void *d)
 Set user data. More...
 

Detailed Description

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

Definition at line 53 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
strthe name of the symbol
hshthe hash key of the symbol
nxta pointer to the next symbol in the hash table entry

Definition at line 158 of file symbol.cpp.

References len().

159 {
160  int len = (int)strlen(str);
161 
162  fName = new char [len+1];
163  memcpy(fName, str, len+1);
164  fHash = hsh;
165  fNext = nxt;
166  fData = 0;
167 }
void * fData
Field to user disposal to store additional data.
Definition: symbol.hh:66
Symbol * fNext
Next symbol in the hash table entry.
Definition: symbol.hh:65
char * fName
Name of the symbol.
Definition: symbol.hh:63
unsigned int fHash
Hash key computed from the name and used to determine the hash table entry.
Definition: symbol.hh:64
int len(Tree l)
Definition: list.cpp:198

Here is the call graph for this function:

Symbol::~Symbol ( )
private

The Destructor is never used.

Definition at line 169 of file symbol.cpp.

170 {
171  delete [] fName;
172 }
char * fName
Name of the symbol.
Definition: symbol.hh:63

Member Function Documentation

unsigned int Symbol::calcHashKey ( const char *  str)
staticprivate

Compute the 32-bits hash key of string str.

Parameters
strthe string
Returns
a 32-bits hash key

Definition at line 140 of file symbol.cpp.

141 {
142  unsigned int h = 0;
143 
144  while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
145  return h;
146 }
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
hashthe hash key of the string (used to speedup the comparison)
strthe 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().

128 {
129  return (fHash == hash) && (strcmp(fName,str) == 0);
130 }
char * fName
Name of the symbol.
Definition: symbol.hh:63
unsigned int fHash
Hash key computed from the name and used to determine the hash table entry.
Definition: symbol.hh:64

Here is the caller graph for this function:

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
strthe name of the symbol
Returns
a symbol of name str

Definition at line 46 of file symbol.cpp.

Referenced by symbol().

47 {
48  char buf[1024];
49  int i;
50  int n = (int)str.length();
51 
52  if (n>1023) n = 1023;
53  for (i = 0; i < n; i++) { buf[i] = str[i]; }
54  buf[i] = 0;
55 
56  return Symbol::get(buf);
57 }
static Symbol * get(const string &str)
Get the symbol of name str.
Definition: symbol.cpp:46

Here is the caller graph for this function:

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
strthe name of the symbol
Returns
a symbol of name str

Definition at line 67 of file symbol.cpp.

References equiv(), and fNext.

68 {
69  unsigned int hsh = calcHashKey(str);
70  int bckt = hsh % kHashTableSize;
71  Symbol* item = gSymbolTable[bckt];
72 
73  while ( item && !item->equiv(hsh,str) ) item = item->fNext;
74  Symbol* r = item ? item : gSymbolTable[bckt] = new Symbol(str, hsh, gSymbolTable[bckt]);
75  return r;
76 }
Symbol * fNext
Next symbol in the hash table entry.
Definition: symbol.hh:65
static const int kHashTableSize
Size of the hash table (a prime number is recommended)
Definition: symbol.hh:58
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
Definition: symbol.hh:59
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
static unsigned int calcHashKey(const char *str)
Compute the 32-bits hash key of string str.
Definition: symbol.cpp:140
bool equiv(unsigned int hash, const char *str) const
Check if the name of the symbol is equal to string str.
Definition: symbol.cpp:127

Here is the call graph for this function:

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
strstring to search
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.

86 {
87  unsigned int hsh = calcHashKey(str);
88  int bckt = hsh % kHashTableSize;
89  Symbol* item = gSymbolTable[bckt];
90 
91  while ( item && !item->equiv(hsh,str) ) item = item->fNext;
92  return item == 0;
93 }
Symbol * fNext
Next symbol in the hash table entry.
Definition: symbol.hh:65
static const int kHashTableSize
Size of the hash table (a prime number is recommended)
Definition: symbol.hh:58
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
Definition: symbol.hh:59
Symbols are unique objects with a name stored in a hash table.
Definition: symbol.hh:53
static unsigned int calcHashKey(const char *str)
Compute the 32-bits hash key of string str.
Definition: symbol.cpp:140
bool equiv(unsigned int hash, const char *str) const
Check if the name of the symbol is equal to string str.
Definition: symbol.cpp:127

Here is the call graph for this function:

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
strthe prefix of the name
Returns
a symbol of name prefix++n

Definition at line 102 of file symbol.cpp.

References name().

Referenced by unique().

103 {
104  char name[256];
105 
106  static map<const char*, unsigned int> gPrefixCounters;
107 
108  for (int n = 0; n<10000; n++) {
109  snprintf(name, 256, "%s%d", str, gPrefixCounters[str]++);
110  if (isnew(name)) return get(name);
111  }
112  assert(false);
113  return get("UNIQUEOVERFLOW");
114 }
static bool isnew(const char *str)
Returns true if no symbol of name str exists.
Definition: symbol.cpp:85
friend const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98

Here is the call graph for this function:

Here is the caller graph for this function:

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<<().

175 {
176  return fout << fName;
177 }
char * fName
Name of the symbol.
Definition: symbol.hh:63

Here is the caller graph for this function:

Friends And Related Function Documentation

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

Set user data.

Definition at line 101 of file symbol.hh.

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.

Member Data Documentation

void* Symbol::fData
private

Field to user disposal to store additional data.

Definition at line 66 of file symbol.hh.

Referenced by getUserData(), and setUserData().

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.

char* Symbol::fName
private

Name of the symbol.

Definition at line 63 of file symbol.hh.

Referenced by name().

Symbol* Symbol::fNext
private

Next symbol in the hash table entry.

Definition at line 65 of file symbol.hh.

Referenced by get(), and isnew().

Symbol * Symbol::gSymbolTable
staticprivate

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: