-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtext2dimacs.js
More file actions
76 lines (67 loc) · 1.67 KB
/
text2dimacs.js
File metadata and controls
76 lines (67 loc) · 1.67 KB
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
function VariableMap() {
this.o = {};
this.maxVar = 0;
}
VariableMap.prototype.get = function(v) {
if (!(v in this.o)) {
this.o[v] = ++this.maxVar;
}
return this.o[v];
}
VariableMap.prototype.list = function() {
var o = this.o;
return Object.keys(o)
.map(function(k) {
return [o[k], k];
})
.sort()
.map(function(kv) {
return 'c ' + kv[0].toString() + ' ' + kv[1];
})
}
function variableNumberer(varMap) {
return function(v) {
if (v[0] === '¬' || v[0] === '-') {
return -varMap.get(v.slice(1));
} else {
return varMap.get(v);
}
};
}
function lineConverter(varMap) {
return function(line) {
var items = line.trim().split(/\s+/).filter((e) => !(e==='∨' || e==='v'));
if (line.trim() === '') {
return [''];
}
else if (items[0] == 'c') {
return [line];
}
else {
return ['c ' + line, items.map(variableNumberer(varMap)).join(' ') + ' 0' ];
}
};
}
function flatten(arrays) { return [].concat.apply([], arrays); }
function convertLines(lines, varMap) { return flatten(lines.map(lineConverter(varMap))); }
function doConvert()
{
var lines = document.getElementById('in').value.split('\n');
var varMap = new VariableMap();
var converted =
convertLines(
document.getElementById('in').value.split('\n'),
varMap
)
var map = varMap.list();
document.getElementById('out').value =
[].concat(map, [''], converted).join('\n')
;
}
function onLoad()
{
inArea = document.getElementById('in');
outArea = document.getElementById('out');
document.getElementById('convert').addEventListener('click', doConvert);
}
window.addEventListener('load', onLoad);