File: colorize.cpp

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (211 lines) | stat: -rw-r--r-- 5,029 bytes parent folder | download | duplicates (3)
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
/**
 * @file colorize.cpp
 * Uses colors to analyze dependencies among sub expressions
 */


#include "colorize.h"
#include "signals.hh"

using namespace std; 

// static funvtions needed to implement splitDependance

static int allocateColor(Tree exp);							///< allocate a new unique color for exp
static void colorize(Tree exp, int color);					///< add color information to exp and all its subtrees
static void uncolorize(Tree exp);							///< remove color information
static void listMultiColoredExp(Tree exp, set<Tree>& lst);	///< list multicolored subexpressions of exp



/**
 * Analyze a set of expressions to discover its dependencies that is subexpressions
 * common to at least two of these expressions
 * @param exps set of expressions to analyze
 * @param post resulting set of post expressions
 * @param pre resulting set of pre expressions
 */
void splitDependance(const set<Tree>& exps, set<Tree>& post, set<Tree>& pre)
{
 	set<Tree>::const_iterator e;
 	for (e= exps.begin(); e != exps.end(); e++) {
 		colorize(*e, allocateColor(*e));
 	}
 	
 	pre.clear();
 	for (e = exps.begin(); e != exps.end(); e++) {
 		listMultiColoredExp(*e, pre);
 	}
 	
 	post.clear();
 	set_difference(exps.begin(), exps.end(), pre.begin(), pre.end(), inserter(post, post.begin()));
 	
 	for (e = exps.begin(); e != exps.end(); e++) {
 		uncolorize(*e);
 	}
}

//------------------------------------------- IMPLEMENTATION  (level 1)-----------------------------------------------------

static void addColor(Tree exp, int color);					///< a color to the colors of exp
static bool hasColor(Tree exp, int color);					///< true if exp is already colored with color
static int colorsCount(Tree exp);							///< returns the number of colors of exp
static void clearColors(Tree exp);							///< remove the color property of exp

/**
 * Allocate a unique color (an integer) for an expression.
 * by converting its address into an integer
 */
int allocateColor(Tree exp)
{
//	return int(exp); 
	static map<Tree,int> colorMap;
	static int nextFreeColor = 1;
	int& color = colorMap[exp];
	if (!color)
		color = nextFreeColor++;
	return color;
}

/**
 * Add a color to all the expression tree
 */
void colorize(Tree exp, int color)
{
	if (! hasColor(exp, color)) {
		addColor(exp, color);
		vector<Tree> v;
		int n = getSubSignals(exp, v, false);
		for (int i=0; i<n; i++) colorize(v[i], color);
	}	
}

/**
 * Remove the colors of an expression tree
 */
void uncolorize(Tree exp)
{
	if (colorsCount(exp) > 0) {
		clearColors(exp);
		vector<Tree> v;
		int n = getSubSignals(exp, v, false);
		for (int i=0; i<n; i++) uncolorize(v[i]);
	}	
}

/**
 * Make a set of the multicolored sub-expressions
 */
void listMultiColoredExp(Tree exp, set<Tree>& lst)
{
	assert(colorsCount(exp) > 0);
	if (colorsCount(exp) > 1) {
		// we have found a multicolored expression
		lst.insert(exp);
	} else {
		// it is a monocolored expression
		// we search its subexpressions
		vector<Tree> v;
		int n = getSubSignals(exp, v, false);
		for (int i=0; i<n; i++) {
			listMultiColoredExp(v[i], lst);
		}
	}	
}

//------------------------------------------- IMPLEMENTATION  (level 2)-----------------------------------------------------

Tree COLORPROPERTY = tree(symbol("ColorProperty"));

/**
 * set the color-set property of sig
 * @param sig the signal we want to type
 * @param t the type of the signal
 */
void setColorProperty(Tree sig, set<int>* colorset)
{
	setProperty(sig, COLORPROPERTY, tree((void*)colorset));
}


/**
 * retrieve the color-set property of sig
 * @param sig the signal we want to know the color-set property
 */
set<int>* getColorProperty(Tree sig)
{
	Tree tt;
	if (!getProperty(sig, COLORPROPERTY, tt)) {
		return 0;
	} else {
		return (set<int>*)tree2ptr(tt);
	}
}




/**
 * Add a color to the colorset of exp. Create an empty
 * coloset if needed.
 * @param sig the signal we want to color
 * @param color the color used
 */
void addColor(Tree exp, int color)
{
	set<int>* cset = getColorProperty(exp);
	if (cset == 0) {
		cset = new set<int>();
		setColorProperty(exp, cset);
	}
	cset->insert(color);
}


/**
 * Test if exp as color in its colorset
 * @param sig the signal we want to test
 * @param color the color to test
 * @return true if color is member of the colorset of sig
 */
bool hasColor(Tree exp, int color)
{
	set<int>* cset = getColorProperty(exp);
	if (cset==0) {
		return false;
	} else {
		return cset->find(color) != cset->end();
	}
}


/**
 * Count the number of colors of exp
 * @param exp the expression we want to count the colors
 * @return the number of elements in the color set or 0
 */
static int colorsCount(Tree exp)
{
	set<int>* cset = getColorProperty(exp);
	if (cset==0) {
		return 0;
	} else {
		return cset->size();
	}
}


/**
 * Count the number of colors of exp
 * @param exp the expression we want to count the colors
 * @return the number of elements in the color set or 0
 */
static void clearColors(Tree exp)
{
	set<int>* cset = getColorProperty(exp);
	if (cset != 0) {
		cset->clear();
	}
}