FAUST compiler  0.9.9.6b8
symbol.cpp
Go to the documentation of this file.
1 /************************************************************************
2  ************************************************************************
3  FAUST compiler
4  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "symbol.hh"
26 #include "compatibility.hh"
27 #include <iostream>
28 #include <cstring>
29 #include <assert.h>
30 
31 using namespace std;
32 
37 Symbol* Symbol::gSymbolTable[kHashTableSize];
38 
39 
46 Symbol* Symbol::get(const string& str)
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 }
58 
59 
60 
67 Symbol* Symbol::get(const char* str)
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 }
77 
78 
85 bool Symbol::isnew(const char* str)
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 }
94 
95 
102 Symbol* Symbol::prefix (const char* str)
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 }
115 
116 
127 bool Symbol::equiv (unsigned int hash, const char *str) const
128 {
129  return (fHash == hash) && (strcmp(fName,str) == 0);
130 }
131 
132 
133 
140 unsigned int Symbol::calcHashKey (const char* str)
141 {
142  unsigned int h = 0;
143 
144  while (*str) h = (h << 1) ^ (h >> 20) ^ (*str++);
145  return h;
146 }
147 
148 
149 
158 Symbol::Symbol(const char* str, unsigned int hsh, Symbol* nxt)
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 }
168 
170 {
171  delete [] fName;
172 }
173 
174 ostream& Symbol::print (ostream& fout) const
175 {
176  return fout << fName;
177 }
~Symbol()
The Destructor is never used.
Definition: symbol.cpp:169
ostream & print(ostream &fout) const
print a symbol on a stream
Definition: symbol.cpp:174
static Symbol * get(const string &str)
Get the symbol of name str.
Definition: symbol.cpp:46
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.
Definition: symbol.cpp:85
Symbol(const char *str, unsigned int hsh, Symbol *nxt)
Constructs a new symbol ready to be placed in the hash table.
Definition: symbol.cpp:158
Symbol * fNext
Next symbol in the hash table entry.
Definition: symbol.hh:65
static Symbol * prefix(const char *str)
Creates a new symbol of name prefixed by str.
Definition: symbol.cpp:102
static Symbol * gSymbolTable[kHashTableSize]
Hash table used to store the symbols.
Definition: symbol.hh:59
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
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
int len(Tree l)
Definition: list.cpp:198