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

A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches. More...

#include <tree.hh>

Collaboration diagram for CTree:
Collaboration graph
[legend]

Public Member Functions

 ~CTree ()
 
const Nodenode () const
 return the content of the tree More...
 
int arity () const
 return the number of branches (subtrees) of a tree More...
 
Tree branch (int i) const
 return the ith branch (subtree) of a tree More...
 
const tvecbranches () const
 return all branches (subtrees) of a tree More...
 
unsigned int hashkey () const
 return the hashkey of the tree More...
 
int aperture () const
 return how "open" is a tree in terms of free variables More...
 
void setAperture (int a)
 modify the aperture of a tree More...
 
ostream & print (ostream &fout) const
 print recursively the content of a tree on a stream More...
 
void setType (void *t)
 
void * getType ()
 
bool isAlreadyVisited ()
 
void setVisited ()
 
void setProperty (Tree key, Tree value)
 
void clearProperty (Tree key)
 
void clearProperties ()
 
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 More...
 
Tree getProperty (Tree key)
 

Static Public Member Functions

static Tree make (const Node &n, int ar, Tree br[])
 return a new tree or an existing equivalent one More...
 
static Tree make (const Node &n, const tvec &br)
 return a new tree or an existing equivalent one More...
 
static void control ()
 print the hash table content (for debug purpose) More...
 
static void startNewVisit ()
 

Static Public Attributes

static bool gDetails = false
 Ctree::print() print with more details when true. More...
 
static unsigned int gVisitTime = 0
 Should be incremented for each new visit to keep track of visited tree. More...
 

Private Member Functions

 CTree (unsigned int hk, const Node &n, const tvec &br)
 construction is private, uses tree::make instead More...
 
bool equiv (const Node &n, const tvec &br) const
 used to check if an equivalent tree already exists More...
 

Static Private Member Functions

static unsigned int calcTreeHash (const Node &n, const tvec &br)
 compute the hash key of a tree according to its node and branches More...
 
static int calcTreeAperture (const Node &n, const tvec &br)
 compute how open is a tree More...
 

Private Attributes

Tree fNext
 next tree in the same hashtable entry More...
 
Node fNode
 the node content of the tree More...
 
void * fType
 the type of a tree More...
 
plist fProperties
 the properties list attached to the tree More...
 
unsigned int fHashKey
 the hashtable key More...
 
int fAperture
 how "open" is a tree (synthezised field) More...
 
unsigned int fVisitTime
 keep track of visits More...
 
tvec fBranch
 the subtrees More...
 

Static Private Attributes

static const int kHashTableSize = 2000000
 
static Tree gHashTable [kHashTableSize]
 hash table used for "hash consing" More...
 

Detailed Description

A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.

A CTree = (Node x [CTree]) is the association of a content Node and a list of subtrees called branches. In order to maximize the sharing of trees, hashconsing techniques are used. Ctrees at different addresses always have a different content. A first consequence of this approach is that a fast equality test on pointers can be used as an equality test on CTrees. A second consequence is that a CTree can NEVER be modified. But a property list is associated to each CTree that can be used to attach arbitrary information to it. Due to the maximal sharing property it is therefore easy to do memoization using these property lists.

Means are also provided to do maximal sharing on recursive trees. The idea is to start from a deBruijn representation and progressively build a classical representation such that alpha-equivalent recursive CTrees are necesseraly identical (and therefore shared).

WARNING : in the current implementation CTrees are allocated but never deleted

Definition at line 109 of file tree.hh.

Constructor & Destructor Documentation

CTree::CTree ( unsigned int  hk,
const Node n,
const tvec br 
)
private

construction is private, uses tree::make instead

Definition at line 104 of file tree.cpp.

References fNext, gHashTable, and kHashTableSize.

Referenced by make().

105  : fNode(n),
106  fType(0),
107  fHashKey(hk),
108  fAperture(calcTreeAperture(n,br)),
109  fVisitTime(0),
110  fBranch(br)
111 {
112  // link dans la hash table
113  int j = hk % kHashTableSize;
114  fNext = gHashTable[j];
115  gHashTable[j] = this;
116 
117 }
int fAperture
how "open" is a tree (synthezised field)
Definition: tree.hh:126
static Tree gHashTable[kHashTableSize]
hash table used for "hash consing"
Definition: tree.hh:113
static const int kHashTableSize
Definition: tree.hh:112
unsigned int fVisitTime
keep track of visits
Definition: tree.hh:127
tvec fBranch
the subtrees
Definition: tree.hh:128
void * fType
the type of a tree
Definition: tree.hh:123
unsigned int fHashKey
the hashtable key
Definition: tree.hh:125
Tree fNext
next tree in the same hashtable entry
Definition: tree.hh:121
static int calcTreeAperture(const Node &n, const tvec &br)
compute how open is a tree
Node fNode
the node content of the tree
Definition: tree.hh:122

Here is the caller graph for this function:

CTree::~CTree ( )

Definition at line 120 of file tree.cpp.

References fHashKey, fNext, gHashTable, and kHashTableSize.

121 {
122  int i = fHashKey % kHashTableSize;
123  Tree t = gHashTable[i];
124 
125  //printf("Delete of "); this->print(); printf("\n");
126  if (t == this) {
127  gHashTable[i] = fNext;
128  } else {
129  Tree p;
130  while (t != this) {
131  p = t;
132  t = t->fNext;
133  }
134  p->fNext = fNext;
135  }
136 }
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static Tree gHashTable[kHashTableSize]
hash table used for "hash consing"
Definition: tree.hh:113
static const int kHashTableSize
Definition: tree.hh:112
unsigned int fHashKey
the hashtable key
Definition: tree.hh:125
Tree fNext
next tree in the same hashtable entry
Definition: tree.hh:121

Member Function Documentation

int CTree::aperture ( ) const
inline

return how "open" is a tree in terms of free variables

Definition at line 148 of file tree.hh.

References fAperture.

Referenced by calcsubstitute(), isClosed(), isOpen(), markOpen(), and recomputeAperture().

Here is the caller graph for this function:

int CTree::arity ( ) const
inline
Tree CTree::branch ( int  i) const
inline
const tvec& CTree::branches ( ) const
inline

return all branches (subtrees) of a tree

Definition at line 146 of file tree.hh.

References fBranch.

Referenced by getSubSignals(), and realPropagate().

Here is the caller graph for this function:

int CTree::calcTreeAperture ( const Node n,
const tvec br 
)
staticprivate

compute how open is a tree

Definition at line 121 of file recursive-tree.cpp.

References isInt(), and node().

122 {
123  int x;
124  if (n == DEBRUIJNREF) {
125 
126  if (isInt(br[0]->node(), &x)) {
127  return x;
128  } else {
129  return 0;
130  }
131 
132  } else if (n == DEBRUIJN) {
133 
134  return br[0]->fAperture - 1;
135 
136  } else {
137  // return max aperture of branches
138  int rc = 0;
139  tvec::const_iterator b = br.begin();
140  tvec::const_iterator z = br.end();
141  while (b != z) {
142  if ((*b)->aperture() > rc) rc = (*b)->aperture();
143  ++b;
144  }
145  return rc;
146  }
147 }
Sym DEBRUIJNREF
const Node & node() const
return the content of the tree
Definition: tree.hh:143
bool isInt(const Node &n)
Definition: node.hh:126
Sym DEBRUIJN

Here is the call graph for this function:

unsigned int CTree::calcTreeHash ( const Node n,
const tvec br 
)
staticprivate

compute the hash key of a tree according to its node and branches

Definition at line 149 of file tree.cpp.

References Node::getInt(), and Node::type().

Referenced by make().

150 {
151  unsigned int hk = n.type() ^ n.getInt();
152  tvec::const_iterator b = br.begin();
153  tvec::const_iterator z = br.end();
154 
155  while (b != z) {
156  hk = (hk << 1) ^ (hk >> 20) ^ ((*b)->fHashKey);
157  ++b;
158  }
159  return hk;
160 }
int type() const
Definition: node.hh:102
int getInt() const
Definition: node.hh:104

Here is the call graph for this function:

Here is the caller graph for this function:

void CTree::clearProperties ( )
inline

Definition at line 169 of file tree.hh.

References fProperties.

169 { fProperties = plist(); }
map< Tree, Tree > plist
Definition: tree.hh:89
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124
void CTree::clearProperty ( Tree  key)
inline

Definition at line 168 of file tree.hh.

References fProperties.

Referenced by property< Loop * >::clear(), property< Tree >::clear(), property< int >::clear(), and property< double >::clear().

168 { fProperties.erase(key); }
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124

Here is the caller graph for this function:

void CTree::control ( )
static

print the hash table content (for debug purpose)

Definition at line 210 of file tree.cpp.

References fNext, gHashTable, and kHashTableSize.

211 {
212  printf("\ngHashTable Content :\n\n");
213  for (int i = 0; i < kHashTableSize; i++) {
214  Tree t = gHashTable[i];
215  if (t) {
216  printf ("%4d = ", i);
217  while (t) {
218  /*t->print();*/
219  printf(" => ");
220  t = t->fNext;
221  }
222  printf("VOID\n");
223  }
224  }
225  printf("\nEnd gHashTable\n");
226 
227 }
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static Tree gHashTable[kHashTableSize]
hash table used for "hash consing"
Definition: tree.hh:113
static const int kHashTableSize
Definition: tree.hh:112
Tree fNext
next tree in the same hashtable entry
Definition: tree.hh:121
bool CTree::equiv ( const Node n,
const tvec br 
) const
private

used to check if an equivalent tree already exists

Definition at line 139 of file tree.cpp.

References fBranch, and fNode.

Referenced by make().

140 {
141  return (fNode == n) && (fBranch == br);
142 }
tvec fBranch
the subtrees
Definition: tree.hh:128
Node fNode
the node content of the tree
Definition: tree.hh:122

Here is the caller graph for this function:

void CTree::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 at line 388 of file tree.cpp.

References fProperties.

Referenced by copyEnvReplaceDefs().

389 {
390  for (plist::const_iterator p = fProperties.begin(); p != fProperties.end(); p++) {
391  keys.push_back(p->first);
392  values.push_back(p->second);
393  }
394 }
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124

Here is the caller graph for this function:

Tree CTree::getProperty ( Tree  key)
inline

Definition at line 173 of file tree.hh.

References fProperties.

Referenced by property< Loop * >::access(), boxComplexity(), deBruijn2Sym(), property< Tree >::get(), property< int >::get(), property< double >::get(), OccMarkup::getOcc(), getProperty(), isRec(), liftn(), and substitute().

173  {
174  plist::iterator i = fProperties.find(key);
175  if (i==fProperties.end()) {
176  return 0;
177  } else {
178  return i->second;
179  }
180  }
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124

Here is the caller graph for this function:

void* CTree::getType ( )
inline

Definition at line 158 of file tree.hh.

References fType.

Referenced by getSigType().

158 { return fType; }
void * fType
the type of a tree
Definition: tree.hh:123

Here is the caller graph for this function:

unsigned int CTree::hashkey ( ) const
inline

return the hashkey of the tree

Definition at line 147 of file tree.hh.

References fHashKey.

bool CTree::isAlreadyVisited ( )
inline

Definition at line 162 of file tree.hh.

References fVisitTime, and gVisitTime.

Referenced by T().

162 { return fVisitTime==gVisitTime; }
unsigned int fVisitTime
keep track of visits
Definition: tree.hh:127
static unsigned int gVisitTime
Should be incremented for each new visit to keep track of visited tree.
Definition: tree.hh:117

Here is the caller graph for this function:

static Tree CTree::make ( const Node n,
int  ar,
Tree  br[] 
)
static

return a new tree or an existing equivalent one

Referenced by calcDeBruijn2Sym(), calcliftn(), calcsubstitute(), codeSimpleType(), codeTupletType(), real_a2sb(), and tree().

Here is the caller graph for this function:

Tree CTree::make ( const Node n,
const tvec br 
)
static

return a new tree or an existing equivalent one

Definition at line 179 of file tree.cpp.

References calcTreeHash(), CTree(), equiv(), fNext, gHashTable, and kHashTableSize.

180 {
181  unsigned int hk = calcTreeHash(n, br);
182  Tree t = gHashTable[hk % kHashTableSize];
183 
184  while (t && !t->equiv(n, br)) {
185  t = t->fNext;
186  }
187  return (t) ? t : new CTree(hk, n, br);
188 }
bool equiv(const Node &n, const tvec &br) const
used to check if an equivalent tree already exists
Definition: tree.cpp:139
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
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static Tree gHashTable[kHashTableSize]
hash table used for "hash consing"
Definition: tree.hh:113
static const int kHashTableSize
Definition: tree.hh:112
Tree fNext
next tree in the same hashtable entry
Definition: tree.hh:121
CTree(unsigned int hk, const Node &n, const tvec &br)
construction is private, uses tree::make instead
Definition: tree.cpp:104

Here is the call graph for this function:

const Node& CTree::node ( ) const
inline
ostream & CTree::print ( ostream &  fout) const

print recursively the content of a tree on a stream

Definition at line 190 of file tree.cpp.

References arity(), branch(), gDetails, node(), and print().

Referenced by operator<<(), and print().

191 {
192  if (gDetails) {
193  // print the adresse of the tree
194  fout << "<" << this << ">@";
195  }
196  fout << node();
197  int a = arity();
198  if (a > 0) {
199  int i; char sep;
200  for (sep = '[', i = 0; i < a; sep = ',', i++) {
201  fout << sep; branch(i)->print(fout);
202  }
203  fout << ']';
204  }
205 
206  return fout;
207 }
const Node & node() const
return the content of the tree
Definition: tree.hh:143
ostream & print(ostream &fout) const
print recursively the content of a tree on a stream
Definition: tree.cpp:190
static bool gDetails
Ctree::print() print with more details when true.
Definition: tree.hh:116
int arity() const
return the number of branches (subtrees) of a tree
Definition: tree.hh:144
Tree branch(int i) const
return the ith branch (subtree) of a tree
Definition: tree.hh:145

Here is the call graph for this function:

Here is the caller graph for this function:

void CTree::setAperture ( int  a)
inline

modify the aperture of a tree

Definition at line 149 of file tree.hh.

References fAperture.

Referenced by markOpen(), and recomputeAperture().

Here is the caller graph for this function:

void CTree::setProperty ( Tree  key,
Tree  value 
)
inline

Definition at line 167 of file tree.hh.

References fProperties.

Referenced by boxComplexity(), deBruijn2Sym(), liftn(), rec(), property< Loop * >::set(), property< Tree >::set(), property< int >::set(), property< double >::set(), OccMarkup::setOcc(), setProperty(), and substitute().

167 { fProperties[key] = value; }
plist fProperties
the properties list attached to the tree
Definition: tree.hh:124

Here is the caller graph for this function:

void CTree::setType ( void *  t)
inline

Definition at line 157 of file tree.hh.

References fType.

Referenced by codeAudioType(), and setSigType().

157 { fType = t; }
void * fType
the type of a tree
Definition: tree.hh:123

Here is the caller graph for this function:

void CTree::setVisited ( )
inline

Definition at line 163 of file tree.hh.

References fVisitTime, and gVisitTime.

Referenced by T().

163 { /*assert(fVisitTime!=gVisitTime);*/ fVisitTime=gVisitTime; }
unsigned int fVisitTime
keep track of visits
Definition: tree.hh:127
static unsigned int gVisitTime
Should be incremented for each new visit to keep track of visited tree.
Definition: tree.hh:117

Here is the caller graph for this function:

static void CTree::startNewVisit ( )
inlinestatic

Definition at line 161 of file tree.hh.

References gVisitTime.

Referenced by typeAnnotation().

161 { ++gVisitTime; }
static unsigned int gVisitTime
Should be incremented for each new visit to keep track of visited tree.
Definition: tree.hh:117

Here is the caller graph for this function:

Member Data Documentation

int CTree::fAperture
private

how "open" is a tree (synthezised field)

Definition at line 126 of file tree.hh.

Referenced by aperture(), and setAperture().

tvec CTree::fBranch
private

the subtrees

Definition at line 128 of file tree.hh.

Referenced by arity(), branch(), branches(), and equiv().

unsigned int CTree::fHashKey
private

the hashtable key

Definition at line 125 of file tree.hh.

Referenced by hashkey(), and ~CTree().

Tree CTree::fNext
private

next tree in the same hashtable entry

Definition at line 121 of file tree.hh.

Referenced by control(), CTree(), make(), and ~CTree().

Node CTree::fNode
private

the node content of the tree

Definition at line 122 of file tree.hh.

Referenced by equiv(), and node().

plist CTree::fProperties
private

the properties list attached to the tree

Definition at line 124 of file tree.hh.

Referenced by clearProperties(), clearProperty(), exportProperties(), getProperty(), and setProperty().

void* CTree::fType
private

the type of a tree

Definition at line 123 of file tree.hh.

Referenced by getType(), and setType().

unsigned int CTree::fVisitTime
private

keep track of visits

Definition at line 127 of file tree.hh.

Referenced by isAlreadyVisited(), and setVisited().

bool CTree::gDetails = false
static

Ctree::print() print with more details when true.

Definition at line 116 of file tree.hh.

Referenced by print().

Tree CTree::gHashTable
staticprivate

hash table used for "hash consing"

Definition at line 113 of file tree.hh.

Referenced by control(), CTree(), make(), and ~CTree().

unsigned int CTree::gVisitTime = 0
static

Should be incremented for each new visit to keep track of visited tree.

Definition at line 117 of file tree.hh.

Referenced by isAlreadyVisited(), setVisited(), and startNewVisit().

const int CTree::kHashTableSize = 2000000
staticprivate

Definition at line 112 of file tree.hh.

Referenced by control(), CTree(), make(), and ~CTree().


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