-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuin_McCluskey.js
More file actions
216 lines (176 loc) · 5.89 KB
/
Quin_McCluskey.js
File metadata and controls
216 lines (176 loc) · 5.89 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
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
function BooleanMinimize(numVarsArg, minTermsArg, dontCaresArg = []){
var __result;
Object.defineProperties(
this,
{
'minTerms': {
value: minTermsArg,
enumerable: false,
writable: false,
configurable: true
},
'dontCares': {
value: dontCaresArg,
enumerable: false,
writable: false,
configurable: true
},
'numVars': {
value: numVarsArg,
enumerable: false,
writable: false,
configurable: true
},
'result': {
enumerable: true,
configurable: true,
get: function(){
if(__result === undefined){
__result = BooleanMinimize.prototype.solve.call(this);
}
return __result;
},
set: function(){
throw new Error("result cannot be assigned a value");
}
}
}
)
}
BooleanMinimize.prototype.solve = function(){
function dec_to_binary_string(n){
var str = n.toString(2);
while(str.length != this.numVars){
str = '0' + str;
}
return str;
};
function num_set_bits(s){
var ans = 0;
for(let i = 0; i < s.length; ++i) if(s[i] === '1') ans++;
return ans;
};
function get_prime_implicants(allTerms){
var table = [];
var primeImplicants = new Set();
var reduced;
while(1){
for(let i = 0; i <= this.numVars; ++i) table[i] = new Set();
for(let i = 0; i < allTerms.length; ++i) table[num_set_bits(allTerms[i])].add(allTerms[i]);
allTerms = [];
reduced = new Set();
for(let i = 0; i < table.length - 1; ++i){
for(let str1 of table[i]){
for(let str2 of table[i+1]){
let diff = -1;
for(let j = 0; j < this.numVars; ++j){
if(str1[j] != str2[j]){
if(diff === -1){
diff = j;
}
else{
diff = -1;
break;
}
}
}
if(diff !== -1){
allTerms.push(str1.slice(0, diff) + '-' + str1.slice(diff + 1));
reduced.add(str1);
reduced.add(str2);
}
}
}
}
for(let t of table){
for(let str of t){
if(!(reduced.has(str))) primeImplicants.add(str);
}
}
if(!reduced.size) break;
}
return primeImplicants;
};
function get_essential_prime_implicants(primeImplicants, minTerms){
var table = [], column;
function check_if_similar(minTerm, primeImplicant){
for(let i = 0; i < primeImplicant.length; ++i){
if(primeImplicant[i] !== '-' && (minTerm[i] !== primeImplicant[i])) return false;
}
return true;
}
function get_complexity(terms){
var complexity = terms.length;
for(let t of terms){
for(let i = 0; i < t.length; ++i){
if(t[i] !== '-'){
complexity++;
if(t[i] === '0') complexity++;
}
}
}
return complexity;
}
function isSubset(sub, sup){
for(let i of sub){
if(!(sup.has(i))) return false;
}
return true;
}
for(let m of minTerms){
column = [];
for(let i = 0; i < primeImplicants.length; ++i){
if(check_if_similar(m, primeImplicants[i])){
column.push(i);
}
}
table.push(column);
}
var possibleSets = [], tempSets;
for(let i of table[0]){
possibleSets.push(new Set([i]));
}
for(let i = 1; i < table.length; ++i){
tempSets = [];
for(let s of possibleSets){
for(let p of table[i]){
let x = new Set(s);
x.add(p);
let append = true;
for(let j = tempSets.length - 1; j >= 0; --j){
if(isSubset(x, tempSets[j])){
tempSets.splice(j, 1);
}
else{
append = false;
}
}
if(append){
tempSets.push(x);
}
}
possibleSets = tempSets;
}
}
var essentialImplicants, minComplexity = 1e9;
for(let s of possibleSets){
let p = [];
for(let i of s){
p.push(primeImplicants[i]);
}
let comp = get_complexity(p);
if(comp < minComplexity){
essentialImplicants = p;
minComplexity = comp;
}
}
return essentialImplicants;
};
var minTerms = this.minTerms.map(dec_to_binary_string.bind(this));
var dontCares = this.dontCares.map(dec_to_binary_string.bind(this));
return get_essential_prime_implicants.call(
this,
Array.from(get_prime_implicants.call(this, minTerms.concat(dontCares))),
minTerms
);
};