FAUST compiler  0.9.9.6b8
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends
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]

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

{
    int len = strlen(str);
    
    fName = new char [len+1];
    memcpy(fName, str, len+1);
    fHash = hsh;
    fNext = nxt;
    fData = 0;
}

Here is the call graph for this function:

Symbol::~Symbol ( ) [private]

The Destructor is never used.

Definition at line 169 of file symbol.cpp.

{
    delete [] fName;
}

Member Function Documentation

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

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.

{
    unsigned int h = 0;

    while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
    return h;
}
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().

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

Here is the caller graph for this function:

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

Definition at line 46 of file symbol.cpp.

Referenced by symbol().

{
    char    buf[1024];
    int     i;
    int     n = str.length();
    
    if (n>1023) n = 1023;
    for (i = 0; i < n; i++) { buf[i] = str[i]; }
    buf[i] = 0;
    
    return Symbol::get(buf);
}

Here is the caller graph for this function:

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

Definition at line 67 of file symbol.cpp.

References equiv(), and fNext.

{
    unsigned int            hsh  = calcHashKey(str);
    int             bckt = hsh % kHashTableSize;
    Symbol*         item = gSymbolTable[bckt];

    while ( item && !item->equiv(hsh,str) ) item = item->fNext;
    Symbol* r = item ? item : gSymbolTable[bckt] = new Symbol(str, hsh, gSymbolTable[bckt]);
    return r;
}

Here is the call graph for this function:

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:
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.

{
    unsigned int            hsh  = calcHashKey(str);
    int             bckt = hsh % kHashTableSize;
    Symbol*         item = gSymbolTable[bckt];
    
    while ( item && !item->equiv(hsh,str) ) item = item->fNext;
    return item == 0;
}

Here is the call graph for this function:

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

{
    char    name[256];
    
    static map<const char*, unsigned int> gPrefixCounters;
    
    for (int n = 0; n<10000; n++) {
        snprintf(name, 256, "%s%d", str, gPrefixCounters[str]++);
        if (isnew(name)) return get(name);
    }
    assert(false);
    return get("UNIQUEOVERFLOW");
}   

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

{
    return fout << fName;
}

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 [static, private]

Hash table used to store the symbols.

Definition at line 59 of file symbol.hh.

const int Symbol::kHashTableSize = 511 [static, private]

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: