Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2188 views
1
/*
2
* Main developer: Nico Van Cleemput
3
* In collaboration with: Craig Larson
4
*
5
* Copyright (C) 2013 Ghent University.
6
* Licensed under the GNU GPL, read the file LICENSE.txt for details.
7
*/
8
9
#include "bintrees.h"
10
#include "util.h"
11
#include "printing.h"
12
13
#define INVARIANT_LABEL 0
14
#define UNARY_LABEL 1
15
#define COMM_BINARY_LABEL 2
16
#define NON_COMM_BINARY_LABEL 3
17
18
void printComparator(int id, FILE *f){
19
if(id==0){
20
fprintf(f, "<=");
21
} else if(id==1){
22
fprintf(f, "<");
23
} else if(id==2){
24
fprintf(f, ">=");
25
} else if(id==3){
26
fprintf(f, ">");
27
} else {
28
BAILOUT("Unknown comparator ID")
29
}
30
}
31
32
void printUnaryOperator_left(int id, FILE *f){
33
if(id==0 || id==1 || id==2 || id==3 || id==4){
34
fprintf(f, "(");
35
} else if(id==5){
36
fprintf(f, "-(");
37
} else if(id==6){
38
fprintf(f, "1 / (");
39
} else if(id==7){
40
fprintf(f, "sqrt(");
41
} else if(id==8){
42
fprintf(f, "ln(");
43
} else if(id==9){
44
fprintf(f, "log10(");
45
} else {
46
BAILOUT("Unknown unary operator ID")
47
}
48
}
49
50
void printUnaryOperator_right(int id, FILE *f){
51
if(id==0){
52
fprintf(f, ") - 1");
53
} else if(id==1){
54
fprintf(f, ") + 1");
55
} else if(id==2){
56
fprintf(f, ") * 2");
57
} else if(id==3){
58
fprintf(f, ") / 2");
59
} else if(id==4){
60
fprintf(f, ") ^ 2");
61
} else if(id==5 || id==6 || id==7 || id==8 || id==9){
62
fprintf(f, ")");
63
} else {
64
BAILOUT("Unknown unary operator ID")
65
}
66
}
67
68
void printCommutativeBinaryOperator_left(int id, FILE *f){
69
if(id==0 || id==1){
70
fprintf(f, "(");
71
} else if(id==2){
72
fprintf(f, "max(");
73
} else if(id==3){
74
fprintf(f, "min(");
75
} else {
76
BAILOUT("Unknown commutative binary operator ID")
77
}
78
}
79
80
void printCommutativeBinaryOperator_middle(int id, FILE *f){
81
if(id==0){
82
fprintf(f, ") + (");
83
} else if(id==1){
84
fprintf(f, ") * (");
85
} else if(id==2 || id==3){
86
fprintf(f, ", ");
87
} else {
88
BAILOUT("Unknown commutative binary operator ID")
89
}
90
}
91
92
void printCommutativeBinaryOperator_right(int id, FILE *f){
93
if(id==0 || id==1){
94
fprintf(f, ")");
95
} else if(id==2 || id==3){
96
fprintf(f, ")");
97
} else {
98
BAILOUT("Unknown commutative binary operator ID")
99
}
100
}
101
102
void printNonCommutativeBinaryOperator_left(int id, FILE *f){
103
if(id==0 || id==1 || id==2){
104
fprintf(f, "(");
105
} else {
106
BAILOUT("Unknown commutative binary operator ID")
107
}
108
}
109
110
void printNonCommutativeBinaryOperator_middle(int id, FILE *f){
111
if(id==0){
112
fprintf(f, ") - (");
113
} else if(id==1){
114
fprintf(f, ") / (");
115
} else if(id==2){
116
fprintf(f, ") ^ (");
117
} else {
118
BAILOUT("Unknown commutative binary operator ID")
119
}
120
}
121
122
void printNonCommutativeBinaryOperator_right(int id, FILE *f){
123
if(id==0 || id==1 || id==2){
124
fprintf(f, ")");
125
} else {
126
BAILOUT("Unknown commutative binary operator ID")
127
}
128
}
129
130
void printNode(NODE *node, FILE *f, char **invariantNamePointers){
131
int type = node->contentLabel[0];
132
int id = node->contentLabel[1];
133
if (type==INVARIANT_LABEL) {
134
if(invariantNamePointers==NULL){
135
fprintf(f, "I%d", id + 1);
136
} else {
137
fprintf(f, "%s", invariantNamePointers[id]);
138
}
139
} else if (type==UNARY_LABEL) {
140
printUnaryOperator_left(id, f);
141
printNode(node->left, f, invariantNamePointers);
142
printUnaryOperator_right(id, f);
143
} else if (type==NON_COMM_BINARY_LABEL){
144
printNonCommutativeBinaryOperator_left(id, f);
145
printNode(node->left, f, invariantNamePointers);
146
printNonCommutativeBinaryOperator_middle(id, f);
147
printNode(node->right, f, invariantNamePointers);
148
printNonCommutativeBinaryOperator_right(id, f);
149
} else if (type==COMM_BINARY_LABEL){
150
printCommutativeBinaryOperator_left(id, f);
151
printNode(node->left, f, invariantNamePointers);
152
printCommutativeBinaryOperator_middle(id, f);
153
printNode(node->right, f, invariantNamePointers);
154
printCommutativeBinaryOperator_right(id, f);
155
} else {
156
BAILOUT("Unknown content label type")
157
}
158
}
159
160
void printUnaryOperator_single(int id, FILE *f){
161
if(id==0){
162
fprintf(f, "-1");
163
} else if(id==1){
164
fprintf(f, "+1");
165
} else if(id==2){
166
fprintf(f, "*2");
167
} else if(id==3){
168
fprintf(f, "/2");
169
} else if(id==4){
170
fprintf(f, "^2");
171
} else if(id==5){
172
fprintf(f, "-()");
173
} else if(id==6){
174
fprintf(f, "1/");
175
} else if(id==7){
176
fprintf(f, "sqrt");
177
} else if(id==8){
178
fprintf(f, "ln");
179
} else if(id==9){
180
fprintf(f, "log10");
181
} else {
182
BAILOUT("Unknown unary operator ID")
183
}
184
}
185
186
void printCommutativeBinaryOperator_single(int id, FILE *f){
187
if(id==0){
188
fprintf(f, "+");
189
} else if(id==1){
190
fprintf(f, "*");
191
} else if(id==2){
192
fprintf(f, "max");
193
} else if(id==3){
194
fprintf(f, "min");
195
} else {
196
BAILOUT("Unknown commutative binary operator ID")
197
}
198
}
199
200
void printNonCommutativeBinaryOperator_single(int id, FILE *f){
201
if(id==0){
202
fprintf(f, "-");
203
} else if(id==1){
204
fprintf(f, "/");
205
} else if(id==2){
206
fprintf(f, "^");
207
} else {
208
BAILOUT("Unknown commutative binary operator ID")
209
}
210
}
211
212
void printSingleNode(NODE *node, FILE *f, char **invariantNamePointers){
213
int type = node->contentLabel[0];
214
int id = node->contentLabel[1];
215
if (type==INVARIANT_LABEL) {
216
if(invariantNamePointers==NULL){
217
fprintf(f, "I%d", id + 1);
218
} else {
219
fprintf(f, "%s", invariantNamePointers[id]);
220
}
221
} else if (type==UNARY_LABEL) {
222
printUnaryOperator_single(id, f);
223
} else if (type==NON_COMM_BINARY_LABEL){
224
printNonCommutativeBinaryOperator_single(id, f);
225
} else if (type==COMM_BINARY_LABEL){
226
printCommutativeBinaryOperator_single(id, f);
227
} else {
228
BAILOUT("Unknown content label type")
229
}
230
}
231
232