1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
/*****************************************************************************
******************************************************************************
TREE
Y. Orlarey, (c) Grame 2002
------------------------------------------------------------------------------
Trees are made of a Node associated with a list of branches : (Node x [CTree]).
Up to 4 branches are allowed in this implementation. A hash table is used to
maximize the sharing of trees during construction : trees at different
addresses always have a different content. Reference counting is used for
garbage collection, and smart pointers P<CTree> should be used for permanent
storage of trees.
API:
----
tree (n) : tree of node n with no branch
tree (n, t1) : tree of node n with a branch t
tree (n, t1,...,tm) : tree of node n with m branches t1,...,tm
Pattern matching :
if (isTree (t, n)) ... : t has node n and no branches;
if (isTree (t, n, &t1) ... : t has node n and 1 branch, t1 is set accordingly;
if (isTree (t, n, &t1...&tm)...: t has node n and m branches, ti's are set accordingly;
Accessors :
t->node() : the node of t { return fNode; }
t->arity() : the number of branches of t return fArity; }
t->branch(i) : the ith branch of t
Attributs :
t->attribut() : return the attribut (also a tree) of t
t->attribut(t') : set the attribut of t to t'
Warning :
---------
Since reference counters are used for garbage collecting, one must be careful not to
create cycles in trees The only possible source of cycles is by setting the attribut
of a tree t to a tree t' that contains t as a subtree.
Properties:
-----------
If p and q are two CTree pointers :
p != q <=> *p != *q
History :
---------
2002-02-08 : First version
2002-10-14 : counts for height and recursiveness added
******************************************************************************
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "tree.hh"
#include <fstream>
#include <cstdlib>
Tabber TABBER(1);
extern Tabber TABBER;
static void error(const char * s, Tree t)
{
//fprintf(stderr, "ERROR : %s (%p)\n", s, t);
cerr << "ERROR : " << s << " : " << *t << endl;
}
#define ERROR(s,t) { error(s,t); exit(1); }
Tree CTree::gHashTable[kHashTableSize];
bool CTree::gDetails = false;
unsigned int CTree::gVisitTime = 0;
// Constructor : add the tree to the hash table
CTree::CTree (unsigned int hk, const Node& n, const tvec& br)
: fNode(n),
fType(0),
fHashKey(hk),
fAperture(calcTreeAperture(n,br)),
fVisitTime(0),
fBranch(br)
{
// link dans la hash table
int j = hk % kHashTableSize;
fNext = gHashTable[j];
gHashTable[j] = this;
}
// Destructor : remove the tree form the hash table
CTree::~CTree ()
{
int i = fHashKey % kHashTableSize;
Tree t = gHashTable[i];
//printf("Delete of "); this->print(); printf("\n");
if (t == this) {
gHashTable[i] = fNext;
} else {
Tree p;
while (t != this) {
p = t;
t = t->fNext;
}
p->fNext = fNext;
}
}
// equivalence
bool CTree::equiv (const Node& n, const tvec& br) const
{
return (fNode == n) && (fBranch == br);
}
Sym PROCESS = symbol("process");
unsigned int CTree::calcTreeHash( const Node& n, const tvec& br )
{
unsigned int hk = n.type() ^ n.getInt();
tvec::const_iterator b = br.begin();
tvec::const_iterator z = br.end();
while (b != z) {
hk = (hk << 1) ^ (hk >> 20) ^ ((*b)->fHashKey);
++b;
}
return hk;
}
Tree CTree::make(const Node& n, int ar, Tree* tbl)
{
tvec br(ar);
for (int i=0; i<ar; i++) br[i] = tbl[i];
unsigned int hk = calcTreeHash(n, br);
Tree t = gHashTable[hk % kHashTableSize];
while (t && !t->equiv(n, br)) {
t = t->fNext;
}
return (t) ? t : new CTree(hk, n, br);
}
Tree CTree::make(const Node& n, const tvec& br)
{
unsigned int hk = calcTreeHash(n, br);
Tree t = gHashTable[hk % kHashTableSize];
while (t && !t->equiv(n, br)) {
t = t->fNext;
}
return (t) ? t : new CTree(hk, n, br);
}
ostream& CTree::print (ostream& fout) const
{
if (gDetails) {
// print the adresse of the tree
fout << "<" << this << ">@";
}
fout << node();
int a = arity();
if (a > 0) {
int i; char sep;
for (sep = '[', i = 0; i < a; sep = ',', i++) {
fout << sep; branch(i)->print(fout);
}
fout << ']';
}
return fout;
}
void CTree::control ()
{
printf("\ngHashTable Content :\n\n");
for (int i = 0; i < kHashTableSize; i++) {
Tree t = gHashTable[i];
if (t) {
printf ("%4d = ", i);
while (t) {
/*t->print();*/
printf(" => ");
t = t->fNext;
}
printf("VOID\n");
}
}
printf("\nEnd gHashTable\n");
}
// if t has a node of type int, return it otherwise error
int tree2int (Tree t)
{
double x;
int i;
if (isInt(t->node(), &i)) {
// nothing to do
} else if (isDouble(t->node(), &x)) {
i = int(x);
} else {
ERROR("the node of the tree is not an int nor a float", t);
}
return i;
}
// if t has a node of type float, return it otherwise error
double tree2float (Tree t)
{
double x;
int i;
if (isInt(t->node(), &i)) {
x = double(i);
} else if (isDouble(t->node(), &x)) {
//nothing to do
} else {
ERROR("the node of the tree is not a float nor an int", t);
}
return x;
}
// if t has a node of type float, return it as a double otherwise error
double tree2double (Tree t)
{
double x;
int i;
if (isInt(t->node(), &i)) {
x = double(i);
} else if (isDouble(t->node(), &x)) {
//nothing to do
} else {
ERROR("the node of the tree is not a float nor an int", t);
}
return double(x);
}
// if t has a node of type symbol, return its name otherwise error
const char* tree2str (Tree t)
{
Sym s;
if (!isSym(t->node(), &s)) {
ERROR("the node of the tree is not a symbol", t);
}
return name(s);
}
// if t has a node of type ptr, return it otherwise error
void* tree2ptr (Tree t)
{
void* x;
if (! isPointer(t->node(), &x)) {
ERROR("the node of the tree is not a pointer", t);
}
return x;
}
/*
bool isTree (const Tree& t, const Node& n)
{
return (t->node() == n) && (t->arity() == 0);
}
*/
// Si ca ne pose pas de probl�es, c'est plus pratique
bool isTree (const Tree& t, const Node& n)
{
return (t->node() == n);
}
bool isTree (const Tree& t, const Node& n, Tree& a)
{
if ((t->node() == n) && (t->arity() == 1)) {
a=t->branch(0);
return true;
} else {
return false;
}
}
bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b)
{
if ((t->node() == n) && (t->arity() == 2)) {
a=t->branch(0);
b=t->branch(1);
return true;
} else {
return false;
}
}
bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c)
{
if ((t->node() == n) && (t->arity() == 3)) {
a=t->branch(0);
b=t->branch(1);
c=t->branch(2);
return true;
} else {
return false;
}
}
bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c, Tree& d)
{
if ((t->node() == n) && (t->arity() == 4)) {
a=t->branch(0);
b=t->branch(1);
c=t->branch(2);
d=t->branch(3);
return true;
} else {
return false;
}
}
bool isTree (const Tree& t, const Node& n, Tree& a, Tree& b, Tree& c, Tree& d, Tree& e)
{
if ((t->node() == n) && (t->arity() == 5)) {
a=t->branch(0);
b=t->branch(1);
c=t->branch(2);
d=t->branch(3);
e=t->branch(4);
return true;
} else {
return false;
}
}
// July 2005, support for symbol user data
void* getUserData(Tree t)
{
Sym s;
if (isSym(t->node(), &s)) {
return getUserData(s);
} else {
return 0;
}
}
/**
* export the properties of a CTree as two vectors, one for the keys
* and one for the associated values
*/
void CTree::exportProperties(vector<Tree>& keys, vector<Tree>& values)
{
for (plist::const_iterator p = fProperties.begin(); p != fProperties.end(); p++) {
keys.push_back(p->first);
values.push_back(p->second);
}
}
|