FAUST compiler  0.9.9.6b8
tree.hh
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 
23 /*****************************************************************************
24 ******************************************************************************/
25 
70 /*****************************************************************************
71 ******************************************************************************/
72 
73 
74 
75 #ifndef __TREE__
76 #define __TREE__
77 
78 #include "symbol.hh"
79 #include "node.hh"
80 #include <vector>
81 #include <map>
82 #include <assert.h>
83 
84 //---------------------------------API---------------------------------------
85 
86 class CTree;
87 typedef CTree* Tree;
88 
89 typedef map<Tree, Tree> plist;
90 typedef vector<Tree> tvec;
91 
109 class CTree
110 {
111  private:
112  static const int kHashTableSize = 2000000; //510511; ///< size of the hash table used for "hash consing"
114 
115  public:
116  static bool gDetails;
117  static unsigned int gVisitTime;
118 
119  private:
120  // fields
123  void* fType;
125  unsigned int fHashKey;
126  int fAperture;
127  unsigned int fVisitTime;
129 
130  CTree (unsigned int hk, const Node& n, const tvec& br);
131 
132  bool equiv (const Node& n, const tvec& br) const;
133  static unsigned int calcTreeHash (const Node& n, const tvec& br);
134  static int calcTreeAperture (const Node& n, const tvec& br);
135 
136  public:
137  ~CTree ();
138 
139  static Tree make (const Node& n, int ar, Tree br[]);
140  static Tree make(const Node& n, const tvec& br);
141 
142  // Accessors
143  const Node& node() const { return fNode; }
144  int arity() const { return (int)fBranch.size();}
145  Tree branch(int i) const { return fBranch[i]; }
146  const tvec& branches() const { return fBranch; }
147  unsigned int hashkey() const { return fHashKey; }
148  int aperture() const { return fAperture; }
149  void setAperture(int a) { fAperture=a; }
150 
151 
152  // Print a tree and the hash table (for debugging purposes)
153  ostream& print (ostream& fout) const;
154  static void control ();
155 
156  // type information
157  void setType(void* t) { fType = t; }
158  void* getType() { return fType; }
159 
160  // Keep track of visited trees (WARNING : non reentrant)
161  static void startNewVisit() { ++gVisitTime; }
163  void setVisited() { /*assert(fVisitTime!=gVisitTime);*/ fVisitTime=gVisitTime; }
164 
165 
166  // Property list of a tree
167  void setProperty(Tree key, Tree value) { fProperties[key] = value; }
168  void clearProperty(Tree key) { fProperties.erase(key); }
170 
171  void exportProperties(vector<Tree>& keys, vector<Tree>& values);
172 
174  plist::iterator i = fProperties.find(key);
175  if (i==fProperties.end()) {
176  return 0;
177  } else {
178  return i->second;
179  }
180  }
181 };
182 
183 //---------------------------------API---------------------------------------
184 
185 // to build trees
186 inline Tree tree (const Node& n) { Tree br[1]; return CTree::make(n, 0, br); }
187 inline Tree tree (const Node& n, const Tree& a) { Tree br[]= {a}; return CTree::make(n, 1, br); }
188 inline Tree tree (const Node& n, const Tree& a, const Tree& b) { Tree br[]= {a,b}; return CTree::make(n, 2, br); }
189 inline Tree tree (const Node& n, const Tree& a, const Tree& b, const Tree& c) { Tree br[]= {a,b,c}; return CTree::make(n, 3, br); }
190 inline Tree tree (const Node& n, const Tree& a, const Tree& b, const Tree& c, const Tree& d) { Tree br[]= {a,b,c,d}; return CTree::make(n, 4, br); }
191 
192 inline Tree tree (const Node& n, const Tree& a, const Tree& b, const Tree& c, const Tree& d, const Tree& e) { Tree br[]= {a,b,c,d,e}; return CTree::make(n, 5, br); }
193 inline Tree tree (const Node& n, const tvec& br) { return CTree::make(n, br); }
194 
195 // useful conversions
196 int tree2int (Tree t);
197 double tree2float (Tree t);
198 double tree2double (Tree t);
199 const char* tree2str (Tree t);
200 void* tree2ptr (Tree t);
201 void* getUserData(Tree t);
202 
203 // pattern matching
204 bool isTree (const Tree& t, const Node& n);
205 bool isTree (const Tree& t, const Node& n, Tree& a);
206 bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b);
207 bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c);
208 bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c, Tree& d);
209 bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c, Tree& d, Tree& e);
210 
211 //printing
212 inline ostream& operator << (ostream& s, const CTree& t) { return t.print(s); }
213 
214 
215 //-----------------------------------------------------------------------------
216 // recursive trees
217 //-----------------------------------------------------------------------------
218 
219 // creation a recursive trees
220 
221 Tree rec(Tree body);
222 Tree rec(Tree id, Tree body);
223 
224 bool isRec(Tree t, Tree& body);
225 bool isRec(Tree t, Tree& id, Tree& body);
226 
227 // creation of recursive references
228 
229 Tree ref(int level);
230 Tree ref(Tree id);
231 
232 bool isRef(Tree t, int& level);
233 bool isRef(Tree t, Tree& id);
234 
235 
236 // Open vs Closed regarding de Bruijn references
237 
238 inline bool isOpen(Tree t) { return t->aperture() > 0; }
239 inline bool isClosed(Tree t) { return t->aperture() <= 0;}
240 
241 // lift by 1 the free de Bruijn references
242 
243 Tree lift(Tree t);
244 
245 Tree deBruijn2Sym (Tree t);
246 void updateAperture (Tree t);
247 
248 //---------------------------------------------------------------------------
249 
250 class Tabber
251 {
252  int fIndent;
253  int fPostInc;
254  public:
255  Tabber(int n=0) : fIndent(n), fPostInc(0) {}
256  Tabber& operator++() { fPostInc++; return *this;}
257  Tabber& operator--() { assert(fIndent > 0); fIndent--; return *this; }
258 
259  ostream& print (ostream& fout)
260  { for (int i=0; i<fIndent; i++) fout << '\t'; fIndent+=fPostInc; fPostInc=0; return fout; }
261 };
262 
263 //printing
264 inline ostream& operator << (ostream& s, Tabber& t) { return t.print(s); }
265 
266 extern Tabber TABBER;
267 
268 
269 #endif
Tabber TABBER
Definition: tree.cpp:87
A library to create and manipulate symbols with a unique name.
Class Node = (type x (int + double + Sym + void*))
Definition: node.hh:75
double tree2float(Tree t)
if t has a node of type float, return it otherwise error
Definition: tree.cpp:246
bool equiv(const Node &n, const tvec &br) const
used to check if an equivalent tree already exists
Definition: tree.cpp:139
int fAperture
how "open" is a tree (synthezised field)
Definition: tree.hh:126
void updateAperture(Tree t)
void * tree2ptr(Tree t)
if t has a node of type ptr, return it otherwise error
Definition: tree.cpp:288
CTree * Tree
Definition: tree.hh:86
A Node is a tagged unions of int, double, symbol and void* used in the implementation of CTrees...
bool isOpen(Tree t)
t contains free de Bruijn references
Definition: tree.hh:238
Tabber & operator++()
Definition: tree.hh:256
vector< Tree > tvec
Definition: tree.hh:90
Tree deBruijn2Sym(Tree t)
int tree2int(Tree t)
if t has a node of type int, return it otherwise error
Definition: tree.cpp:230
static unsigned int calcTreeHash(const Node &n, const tvec &br)
compute the hash key of a tree according to its node and branches
Definition: tree.cpp:149
unsigned int hashkey() const
return the hashkey of the tree
Definition: tree.hh:147
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
const Node & node() const
return the content of the tree
Definition: tree.hh:143
static Tree make(const Node &n, int ar, Tree br[])
return a new tree or an existing equivalent one
void setType(void *t)
Definition: tree.hh:157
static Tree gHashTable[kHashTableSize]
hash table used for "hash consing"
Definition: tree.hh:113
void * getUserData(Tree t)
if t has a node of type symbol, return the associated user data
Definition: tree.cpp:372
void * getType()
Definition: tree.hh:158
Tree rec(Tree body)
create a de Bruijn recursive tree
bool isAlreadyVisited()
Definition: tree.hh:162
bool isClosed(Tree t)
t dont contain free de Bruijn ref
Definition: tree.hh:239
static void startNewVisit()
Definition: tree.hh:161
static const int kHashTableSize
Definition: tree.hh:112
int fPostInc
Definition: tree.hh:253
unsigned int fVisitTime
keep track of visits
Definition: tree.hh:127
tvec fBranch
the subtrees
Definition: tree.hh:128
map< Tree, Tree > plist
Definition: tree.hh:89
Tree ref(int level)
create a de Bruijn recursive reference
ostream & print(ostream &fout)
Definition: tree.hh:259
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124
void setVisited()
Definition: tree.hh:163
void setProperty(Tree key, Tree value)
Definition: tree.hh:167
Tree lift(Tree t)
int fIndent
Definition: tree.hh:252
Tabber(int n=0)
Definition: tree.hh:255
Definition: tree.hh:250
bool isRec(Tree t, Tree &body)
is t a de Bruijn recursive tree
void * fType
the type of a tree
Definition: tree.hh:123
void clearProperties()
Definition: tree.hh:169
ostream & print(ostream &fout) const
print recursively the content of a tree on a stream
Definition: tree.cpp:190
unsigned int fHashKey
the hashtable key
Definition: tree.hh:125
static bool gDetails
Ctree::print() print with more details when true.
Definition: tree.hh:116
Tree fNext
next tree in the same hashtable entry
Definition: tree.hh:121
double tree2double(Tree t)
if t has a node of type float, return it otherwise error
Definition: tree.cpp:262
~CTree()
Definition: tree.cpp:120
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
const tvec & branches() const
return all branches (subtrees) of a tree
Definition: tree.hh:146
Tabber & operator--()
Definition: tree.hh:257
bool isRef(Tree t, int &level)
is t a de Bruijn recursive reference
ostream & operator<<(ostream &s, const CTree &t)
Definition: tree.hh:212
Tree tree(const Node &n)
Definition: tree.hh:186
static void control()
print the hash table content (for debug purpose)
Definition: tree.cpp:210
static int calcTreeAperture(const Node &n, const tvec &br)
compute how open is a tree
CTree(unsigned int hk, const Node &n, const tvec &br)
construction is private, uses tree::make instead
Definition: tree.cpp:104
Node fNode
the node content of the tree
Definition: tree.hh:122
int aperture() const
return how "open" is a tree in terms of free variables
Definition: tree.hh:148
static unsigned int gVisitTime
Should be incremented for each new visit to keep track of visited tree.
Definition: tree.hh:117
Tree getProperty(Tree key)
Definition: tree.hh:173
void exportProperties(vector< Tree > &keys, vector< Tree > &values)
export the properties of a CTree as two vectors, one for the keys and one for the associated values ...
Definition: tree.cpp:388
const char * tree2str(Tree t)
if t has a node of type symbol, return its name otherwise error
Definition: tree.cpp:278
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145
void clearProperty(Tree key)
Definition: tree.hh:168
void setAperture(int a)
modify the aperture of a tree
Definition: tree.hh:149
bool isTree(const Tree &t, const Node &n)
Definition: tree.cpp:305