-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.c
268 lines (232 loc) · 6.6 KB
/
add.c
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
/*
* Copyright 2014 Romain Brenguier
* Author: Romain Brenguier <[email protected]>
*
* This file is part of Ocaml-cudd.
*
* Ocaml-cudd is a 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "util.h"
#include "cudd.h"
#include "cuddInt.h"
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
#include <caml/callback.h>
/*static struct custom_operations manager_ops = {
custom_finalize_default,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
*/
/* Accessing the DdManager * part of an OCaml custom block */
#define Manager_val(v) (*((DdManager **) Data_custom_val(v)))
/* Allocating an OCaml custom block to hold the given WINDOW * */
/*static value alloc_manager(DdManager * d)
{
value v = alloc_custom(&manager_ops, sizeof(DdManager *), 0, 1);
Manager_val(v) = d;
return v;
}
*/
static struct custom_operations node_ops = {
custom_finalize_default,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
/* Accessing the DdNode * part of an OCaml custom block */
#define DdNode_val(v) (*((DdNode **) Data_custom_val(v)))
//#define AddNode_val(v) (*((DdNode ***) Data_custom_val(v)))
#define AddNode_val(v) (DdNode_val(v))
/* Allocating an OCaml custom block to hold the given WINDOW * */
static value alloc_node(DdNode * d)
{
value v = alloc_custom(&node_ops, sizeof(DdNode *), 0, 1);
DdNode_val(v) = d;
return v;
}
/*
static value alloc_add_node(DdNode ** d)
{
value v = alloc_custom(&node_ops, sizeof(DdNode **), 0, 1);
AddNode_val(v) = d;
return v;
}*/
/*
value caml_Cudd_RecursiveDeref(value manager, value bdd)
{
CAMLparam2(manager,bdd);
Cudd_RecursiveDeref(Manager_val(manager),DdNode_val(bdd));
CAMLreturn(Val_unit);
}
value caml_Cudd_IsConstant(value add)
{
CAMLparam1(add);
if(Cudd_IsConstant(DdNode_val(add)))
CAMLreturn(Val_true);
else
CAMLreturn(Val_false);
}
value caml_Cudd_E(value add)
{
CAMLparam1(add);
CAMLreturn(alloc_node(Cudd_E(DdNode_val(add))));
}
value caml_Cudd_T(value add)
{
CAMLparam1(add);
CAMLreturn(alloc_node(Cudd_T(DdNode_val(add))));
}
*/
value caml_Cudd_V(value add)
{
CAMLparam1(add);
double d = Cudd_V(AddNode_val(add));
CAMLreturn(caml_copy_double(d));
}
/*
value caml_Cudd_NodeReadIndex(value add)
{
CAMLparam1(add);
CAMLreturn(Val_int(Cudd_NodeReadIndex(*AddNode_val(add))));
}
*/
value caml_Cudd_addConst(value dd, value a)
{
CAMLparam2(dd,a);
DdNode *c = Cudd_addConst(Manager_val(dd),Double_val(a));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addIthVar(value dd, value i)
{
CAMLparam2(dd,i);
DdNode * r = Cudd_addIthVar(Manager_val(dd),Int_val(i));
//Cudd_Ref(r);
CAMLreturn(alloc_node(r));
}
value caml_Cudd_addConst_int(value dd, value a)
{
CAMLparam2(dd,a);
DdNode * c = Cudd_addConst(Manager_val(dd),Int_val(a));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addPlus(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode *r = Cudd_addApply(Manager_val(dd),Cudd_addPlus,AddNode_val(f),AddNode_val(g));
Cudd_Ref(r);
CAMLreturn(alloc_node(r));
}
value caml_Cudd_addMinus(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addMinus,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addTimes(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addTimes,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addDivide(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addDivide,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addMinimum(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addMinimum,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addMaximum(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addMaximum,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addAgreement(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addAgreement,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addDiff(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addDiff,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addThreshold(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addThreshold,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addSetNZ(value dd, value f, value g)
{
CAMLparam3(dd,f,g);
DdNode * c = Cudd_addApply(Manager_val(dd),Cudd_addSetNZ,AddNode_val(f),AddNode_val(g));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addLog(value dd, value f)
{
CAMLparam2(dd,f);
DdNode * c = Cudd_addLog(Manager_val(dd),DdNode_val(f));
Cudd_Ref(c);
CAMLreturn(alloc_node(c));
}
value caml_Cudd_addDumpDot(value manager, value file_name, value add)
{
CAMLparam3(manager,file_name,add);
FILE* f = fopen(String_val(file_name), "w");
Cudd_DumpDot(Manager_val(manager), 1 , &(AddNode_val(add)) , NULL, NULL, f);
fclose(f);
CAMLreturn(Val_unit);
}
value caml_setRing(value sum, value product, value zero)
{
CAMLparam3(sum,product,zero);
setRing(Int_val(sum),Int_val(product),Double_val(zero));
CAMLreturn(Val_unit);
}
value caml_Cudd_addMatrixMultiply(value dd,value a,value b,value vector)
{
CAMLparam4(dd,a,b,vector);
DdNode ** tab = (DdNode **) (malloc(sizeof(DdNode*)*Wosize_val(vector)));
int i;
for(i = 0; i< Wosize_val(vector); i++)
tab[i] = DdNode_val(Field(vector,i));
DdNode * result = Cudd_addMatrixMultiply(Manager_val(dd),DdNode_val(a),DdNode_val(b),tab,i);
Cudd_Ref(result);
free(tab);
CAMLreturn(alloc_node(result));
}