forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdd.js
438 lines (396 loc) · 9.98 KB
/
bdd.js
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
'use strict';
var Caml_array = require("../../lib/js/caml_array.js");
var Caml_int32 = require("../../lib/js/caml_int32.js");
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
function $$eval(_bdd, vars) {
while(true) {
var bdd = _bdd;
if (typeof bdd === "number") {
if (bdd !== 0) {
return false;
} else {
return true;
}
} else if (Caml_array.caml_array_get(vars, bdd[1])) {
_bdd = bdd[3];
continue ;
} else {
_bdd = bdd[0];
continue ;
}
};
}
function getId(bdd) {
if (typeof bdd === "number") {
if (bdd !== 0) {
return 0;
} else {
return 1;
}
} else {
return bdd[2];
}
}
var nodeC = [1];
var sz_1 = [8191];
var htab = [Caml_array.caml_make_vect(sz_1[0] + 1 | 0, /* [] */0)];
var n_items = [0];
function hashVal(x, y, v) {
return ((x << 1) + y | 0) + (v << 2) | 0;
}
function resize(newSize) {
var arr = htab[0];
var newSz_1 = newSize - 1 | 0;
var newArr = Caml_array.caml_make_vect(newSize, /* [] */0);
var copyBucket = function (_bucket) {
while(true) {
var bucket = _bucket;
if (bucket) {
var n = bucket[0];
if (typeof n === "number") {
throw [
Caml_builtin_exceptions.assert_failure,
[
"bdd.ml",
54,
27
]
];
} else {
var ind = hashVal(getId(n[0]), getId(n[3]), n[1]) & newSz_1;
Caml_array.caml_array_set(newArr, ind, /* :: */[
n,
Caml_array.caml_array_get(newArr, ind)
]);
_bucket = bucket[1];
continue ;
}
} else {
return /* () */0;
}
};
};
for(var n = 0 ,n_finish = sz_1[0]; n <= n_finish; ++n){
copyBucket(Caml_array.caml_array_get(arr, n));
}
htab[0] = newArr;
sz_1[0] = newSz_1;
return /* () */0;
}
function insert(idl, idh, v, ind, bucket, newNode) {
if (n_items[0] <= sz_1[0]) {
Caml_array.caml_array_set(htab[0], ind, /* :: */[
newNode,
bucket
]);
n_items[0] = n_items[0] + 1 | 0;
return /* () */0;
} else {
resize((sz_1[0] + sz_1[0] | 0) + 2 | 0);
var ind$1 = hashVal(idl, idh, v) & sz_1[0];
return Caml_array.caml_array_set(htab[0], ind$1, /* :: */[
newNode,
Caml_array.caml_array_get(htab[0], ind$1)
]);
}
}
function resetUnique() {
sz_1[0] = 8191;
htab[0] = Caml_array.caml_make_vect(sz_1[0] + 1 | 0, /* [] */0);
n_items[0] = 0;
nodeC[0] = 1;
return /* () */0;
}
function mkNode(low, v, high) {
var idl = getId(low);
var idh = getId(high);
if (idl === idh) {
return low;
} else {
var ind = hashVal(idl, idh, v) & sz_1[0];
var bucket = Caml_array.caml_array_get(htab[0], ind);
var _b = bucket;
while(true) {
var b = _b;
if (b) {
var n = b[0];
if (typeof n === "number") {
throw [
Caml_builtin_exceptions.assert_failure,
[
"bdd.ml",
99,
31
]
];
} else if (v === n[1] && idl === getId(n[0]) && idh === getId(n[3])) {
return n;
} else {
_b = b[1];
continue ;
}
} else {
var n_002 = (nodeC[0] = nodeC[0] + 1 | 0, nodeC[0]);
var n$1 = /* Node */[
low,
v,
n_002,
high
];
insert(getId(low), getId(high), v, ind, bucket, n$1);
return n$1;
}
};
}
}
function cmpVar(x, y) {
if (x < y) {
return /* LESS */0;
} else if (x > y) {
return /* GREATER */2;
} else {
return /* EQUAL */1;
}
}
function mkVar(x) {
return mkNode(/* Zero */1, x, /* One */0);
}
var andslot1 = Caml_array.caml_make_vect(1999, 0);
var andslot2 = Caml_array.caml_make_vect(1999, 0);
var andslot3 = Caml_array.caml_make_vect(1999, /* Zero */1);
var xorslot1 = Caml_array.caml_make_vect(1999, 0);
var xorslot2 = Caml_array.caml_make_vect(1999, 0);
var xorslot3 = Caml_array.caml_make_vect(1999, /* Zero */1);
var notslot1 = Caml_array.caml_make_vect(1999, 0);
var notslot2 = Caml_array.caml_make_vect(1999, /* One */0);
function hash(x, y) {
return ((x << 1) + y | 0) % 1999;
}
function not(n) {
if (typeof n === "number") {
if (n !== 0) {
return /* One */0;
} else {
return /* Zero */1;
}
} else {
var id = n[2];
var h = id % 1999;
if (id === Caml_array.caml_array_get(notslot1, h)) {
return Caml_array.caml_array_get(notslot2, h);
} else {
var f = mkNode(not(n[0]), n[1], not(n[3]));
Caml_array.caml_array_set(notslot1, h, id);
Caml_array.caml_array_set(notslot2, h, f);
return f;
}
}
}
function and2(n1, n2) {
if (typeof n1 === "number") {
if (n1 !== 0) {
return /* Zero */1;
} else {
return n2;
}
} else {
var r1 = n1[3];
var i1 = n1[2];
var v1 = n1[1];
var l1 = n1[0];
if (typeof n2 === "number") {
if (n2 !== 0) {
return /* Zero */1;
} else {
return n1;
}
} else {
var r2 = n2[3];
var i2 = n2[2];
var v2 = n2[1];
var l2 = n2[0];
var h = hash(i1, i2);
if (i1 === Caml_array.caml_array_get(andslot1, h) && i2 === Caml_array.caml_array_get(andslot2, h)) {
return Caml_array.caml_array_get(andslot3, h);
} else {
var match = cmpVar(v1, v2);
var f;
switch (match) {
case 0 :
f = mkNode(and2(l1, n2), v1, and2(r1, n2));
break;
case 1 :
f = mkNode(and2(l1, l2), v1, and2(r1, r2));
break;
case 2 :
f = mkNode(and2(n1, l2), v2, and2(n1, r2));
break;
}
Caml_array.caml_array_set(andslot1, h, i1);
Caml_array.caml_array_set(andslot2, h, i2);
Caml_array.caml_array_set(andslot3, h, f);
return f;
}
}
}
}
function xor(n1, n2) {
if (typeof n1 === "number") {
if (n1 !== 0) {
return n2;
} else {
return not(n2);
}
} else {
var r1 = n1[3];
var i1 = n1[2];
var v1 = n1[1];
var l1 = n1[0];
if (typeof n2 === "number") {
if (n2 !== 0) {
return n1;
} else {
return not(n1);
}
} else {
var r2 = n2[3];
var i2 = n2[2];
var v2 = n2[1];
var l2 = n2[0];
var h = hash(i1, i2);
if (i1 === Caml_array.caml_array_get(andslot1, h) && i2 === Caml_array.caml_array_get(andslot2, h)) {
return Caml_array.caml_array_get(andslot3, h);
} else {
var match = cmpVar(v1, v2);
var f;
switch (match) {
case 0 :
f = mkNode(xor(l1, n2), v1, xor(r1, n2));
break;
case 1 :
f = mkNode(xor(l1, l2), v1, xor(r1, r2));
break;
case 2 :
f = mkNode(xor(n1, l2), v2, xor(n1, r2));
break;
}
Caml_array.caml_array_set(andslot1, h, i1);
Caml_array.caml_array_set(andslot2, h, i2);
Caml_array.caml_array_set(andslot3, h, f);
return f;
}
}
}
}
function hwb(n) {
var h = function (i, j) {
if (i === j) {
return mkNode(/* Zero */1, i, /* One */0);
} else {
return xor(and2(not(mkNode(/* Zero */1, j, /* One */0)), h(i, j - 1 | 0)), and2(mkNode(/* Zero */1, j, /* One */0), g(i, j - 1 | 0)));
}
};
var g = function (i, j) {
if (i === j) {
return mkNode(/* Zero */1, i, /* One */0);
} else {
return xor(and2(not(mkNode(/* Zero */1, i, /* One */0)), h(i + 1 | 0, j)), and2(mkNode(/* Zero */1, i, /* One */0), g(i + 1 | 0, j)));
}
};
return h(0, n - 1 | 0);
}
var seed = [0];
function random() {
seed[0] = Caml_int32.imul(seed[0], 25173) + 17431 | 0;
return (seed[0] & 1) > 0;
}
function random_vars(n) {
var vars = Caml_array.caml_make_vect(n, false);
for(var i = 0 ,i_finish = n - 1 | 0; i <= i_finish; ++i){
Caml_array.caml_array_set(vars, i, random(/* () */0));
}
return vars;
}
function bool_equal(a, b) {
if (a) {
if (b) {
return true;
} else {
return false;
}
} else if (b) {
return false;
} else {
return true;
}
}
function test_hwb(bdd, vars) {
var ntrue = 0;
for(var i = 0 ,i_finish = vars.length - 1 | 0; i <= i_finish; ++i){
if (Caml_array.caml_array_get(vars, i)) {
ntrue = ntrue + 1 | 0;
}
}
return bool_equal($$eval(bdd, vars), ntrue > 0 ? Caml_array.caml_array_get(vars, ntrue - 1 | 0) : false);
}
function main() {
var bdd = hwb(22);
var succeeded = true;
for(var i = 1; i <= 100; ++i){
succeeded = succeeded && test_hwb(bdd, random_vars(22));
}
if (succeeded) {
return 0;
} else {
throw [
Caml_builtin_exceptions.assert_failure,
[
"bdd.ml",
233,
2
]
];
}
}
main(/* () */0);
var initSize_1 = 8191;
var zero = /* Zero */1;
var one = /* One */0;
var cacheSize = 1999;
exports.$$eval = $$eval;
exports.getId = getId;
exports.initSize_1 = initSize_1;
exports.nodeC = nodeC;
exports.sz_1 = sz_1;
exports.htab = htab;
exports.n_items = n_items;
exports.hashVal = hashVal;
exports.resize = resize;
exports.insert = insert;
exports.resetUnique = resetUnique;
exports.mkNode = mkNode;
exports.cmpVar = cmpVar;
exports.zero = zero;
exports.one = one;
exports.mkVar = mkVar;
exports.cacheSize = cacheSize;
exports.andslot1 = andslot1;
exports.andslot2 = andslot2;
exports.andslot3 = andslot3;
exports.xorslot1 = xorslot1;
exports.xorslot2 = xorslot2;
exports.xorslot3 = xorslot3;
exports.notslot1 = notslot1;
exports.notslot2 = notslot2;
exports.hash = hash;
exports.not = not;
exports.and2 = and2;
exports.xor = xor;
exports.hwb = hwb;
exports.seed = seed;
exports.random = random;
exports.random_vars = random_vars;
exports.bool_equal = bool_equal;
exports.test_hwb = test_hwb;
exports.main = main;
/* Not a pure module */