FAUST compiler  0.9.9.6b8
colorize.cpp
Go to the documentation of this file.
1 
7 #include "colorize.h"
8 #include "signals.hh"
9 
10 using namespace std;
11 
12 // static funvtions needed to implement splitDependance
13 
14 static int allocateColor(Tree exp);
15 static void colorize(Tree exp, int color);
16 static void uncolorize(Tree exp);
17 static void listMultiColoredExp(Tree exp, set<Tree>& lst);
18 
19 
20 
28 void splitDependance(const set<Tree>& exps, set<Tree>& post, set<Tree>& pre)
29 {
30  set<Tree>::const_iterator e;
31  for (e= exps.begin(); e != exps.end(); e++) {
32  colorize(*e, allocateColor(*e));
33  }
34 
35  pre.clear();
36  for (e = exps.begin(); e != exps.end(); e++) {
37  listMultiColoredExp(*e, pre);
38  }
39 
40  post.clear();
41  set_difference(exps.begin(), exps.end(), pre.begin(), pre.end(), inserter(post, post.begin()));
42 
43  for (e = exps.begin(); e != exps.end(); e++) {
44  uncolorize(*e);
45  }
46 }
47 
48 //------------------------------------------- IMPLEMENTATION (level 1)-----------------------------------------------------
49 
50 static void addColor(Tree exp, int color);
51 static bool hasColor(Tree exp, int color);
52 static int colorsCount(Tree exp);
53 static void clearColors(Tree exp);
54 
60 {
61 // return int(exp);
62  static map<Tree,int> colorMap;
63  static int nextFreeColor = 1;
64  int& color = colorMap[exp];
65  if (!color)
66  color = nextFreeColor++;
67  return color;
68 }
69 
73 void colorize(Tree exp, int color)
74 {
75  if (! hasColor(exp, color)) {
76  addColor(exp, color);
77  vector<Tree> v;
78  int n = getSubSignals(exp, v, false);
79  for (int i=0; i<n; i++) colorize(v[i], color);
80  }
81 }
82 
86 void uncolorize(Tree exp)
87 {
88  if (colorsCount(exp) > 0) {
89  clearColors(exp);
90  vector<Tree> v;
91  int n = getSubSignals(exp, v, false);
92  for (int i=0; i<n; i++) uncolorize(v[i]);
93  }
94 }
95 
99 void listMultiColoredExp(Tree exp, set<Tree>& lst)
100 {
101  assert(colorsCount(exp) > 0);
102  if (colorsCount(exp) > 1) {
103  // we have found a multicolored expression
104  lst.insert(exp);
105  } else {
106  // it is a monocolored expression
107  // we search its subexpressions
108  vector<Tree> v;
109  int n = getSubSignals(exp, v, false);
110  for (int i=0; i<n; i++) {
111  listMultiColoredExp(v[i], lst);
112  }
113  }
114 }
115 
116 //------------------------------------------- IMPLEMENTATION (level 2)-----------------------------------------------------
117 
118 Tree COLORPROPERTY = tree(symbol("ColorProperty"));
119 
125 void setColorProperty(Tree sig, set<int>* colorset)
126 {
127  setProperty(sig, COLORPROPERTY, tree((void*)colorset));
128 }
129 
130 
135 set<int>* getColorProperty(Tree sig)
136 {
137  Tree tt;
138  if (!getProperty(sig, COLORPROPERTY, tt)) {
139  return 0;
140  } else {
141  return (set<int>*)tree2ptr(tt);
142  }
143 }
144 
145 
146 
147 
154 void addColor(Tree exp, int color)
155 {
156  set<int>* cset = getColorProperty(exp);
157  if (cset == 0) {
158  cset = new set<int>();
159  setColorProperty(exp, cset);
160  }
161  cset->insert(color);
162 }
163 
164 
171 bool hasColor(Tree exp, int color)
172 {
173  set<int>* cset = getColorProperty(exp);
174  if (cset==0) {
175  return false;
176  } else {
177  return cset->find(color) != cset->end();
178  }
179 }
180 
181 
187 static int colorsCount(Tree exp)
188 {
189  set<int>* cset = getColorProperty(exp);
190  if (cset==0) {
191  return 0;
192  } else {
193  return (int)cset->size();
194  }
195 }
196 
197 
203 static void clearColors(Tree exp)
204 {
205  set<int>* cset = getColorProperty(exp);
206  if (cset != 0) {
207  cset->clear();
208  }
209 }
210 
211 
static void addColor(Tree exp, int color)
a color to the colors of exp
Definition: colorize.cpp:154
static void uncolorize(Tree exp)
remove color information
Definition: colorize.cpp:86
static int allocateColor(Tree exp)
allocate a new unique color for exp
Definition: colorize.cpp:59
set< int > * getColorProperty(Tree sig)
retrieve the color-set property of sig
Definition: colorize.cpp:135
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
static int colorsCount(Tree exp)
returns the number of colors of exp
Definition: colorize.cpp:187
static void listMultiColoredExp(Tree exp, set< Tree > &lst)
list multicolored subexpressions of exp
Definition: colorize.cpp:99
static void clearColors(Tree exp)
remove the color property of exp
Definition: colorize.cpp:203
#define lst
Definition: faustlexer.cpp:807
Tree tree(const Node &n)
Definition: tree.hh:186
void * tree2ptr(Tree t)
if t has a node of type ptr, return it otherwise error
Definition: tree.cpp:288
static void colorize(Tree exp, int color)
add color information to exp and all its subtrees
Definition: colorize.cpp:73
void setProperty(Tree t, Tree key, Tree val)
Definition: list.cpp:418
Tree COLORPROPERTY
Definition: colorize.cpp:118
void splitDependance(const set< Tree > &exps, set< Tree > &post, set< Tree > &pre)
Analyze a set of expressions to discover its dependencies that is subexpressions common to at least t...
Definition: colorize.cpp:28
Symbol * symbol(const char *str)
Returns (and creates if new) the symbol of name str.
Definition: symbol.hh:95
static bool hasColor(Tree exp, int color)
true if exp is already colored with color
Definition: colorize.cpp:171
void setColorProperty(Tree sig, set< int > *colorset)
set the color-set property of sig
Definition: colorize.cpp:125
int getSubSignals(Tree sig, vector< Tree > &vsigs, bool visitgen=true)
Extract the sub signals of a signal expression, that is not necesseraly all the subtrees.
Definition: subsignals.cpp:12
bool getProperty(Tree t, Tree key, Tree &val)
Definition: list.cpp:423